Interface JdbcStoreDialect
- All Known Implementing Classes:
H2JdbcStoreDialect,MysqlJdbcStoreDialect,PostgresJdbcStoreDialect,SqliteJdbcStoreDialect
JdbcStore.
Each dialect renders the four SQL statements that vary between vendors:
getCreateTableSql()— schema bootstrap, used whenJdbcStore.Builder#initializeSchema(true)is set.getUpsertSql()— unconditional write that incrementsversionon update.getInsertSql()— create-only insert, used by theBaseStore.putIfVersion(java.util.List<java.lang.String>, java.lang.String, java.util.Map<java.lang.String, java.lang.Object>, long)CAS path whenexpectedVersion == 0; primary-key conflicts indicate the row already exists.getCasUpdateSql()— conditional update used by theBaseStore.putIfVersion(java.util.List<java.lang.String>, java.lang.String, java.util.Map<java.lang.String, java.lang.Object>, long)CAS path whenexpectedVersion > 0; relies onWHERE version = ?returning zero rows on mismatch.
The fixed-shape SQL statements (getSelectSql(), getDeleteSql(),
getSearchSql()) have safe defaults in this interface.
All SQL templates use the %s placeholder for the table name so that callers can
customise it via JdbcStore.Builder#tableName.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic JdbcStoreDialectfrom(DataSource dataSource) Detects the dialect from theDataSource's database product name.Returns the SQL to conditionally update an item only when the stored version matches.Returns the SQL to create the store table if it does not yet exist.default StringReturns the SQL to delete a single item.Returns the SQL to insert a new row withversion=1.default charThe escape character paired withgetSearchSql()'sESCAPEclause.default StringReturns the SQL to list items whose namespace_path matches aLIKEpattern.default StringReturns the SQL to fetch a single item.Returns the SQL to insert-or-update an item, incrementingversionon update.
-
Field Details
-
LOG
static final org.slf4j.Logger LOG
-
-
Method Details
-
getCreateTableSql
String getCreateTableSql()Returns the SQL to create the store table if it does not yet exist.The table must have columns:
namespace_path VARCHAR(2048),item_key VARCHAR(255),value_json(TEXT-like),version BIGINT,updated_at BIGINT, plus a primary key on(namespace_path, item_key).- Returns:
- CREATE TABLE statement; must use
%sfor the table name
-
getUpsertSql
String getUpsertSql()Returns the SQL to insert-or-update an item, incrementingversionon update.Bind parameters in order:
(namespace_path, item_key, value_json, updated_at)— the dialect is responsible for settingversion=1on insert andversion=version+1on update.- Returns:
- UPSERT statement; must use
%sfor the table name
-
getInsertSql
String getInsertSql()Returns the SQL to insert a new row withversion=1.Bind parameters in order:
(namespace_path, item_key, value_json, updated_at). Used byputIfVersion(... expectedVersion=0): a primary-key violation indicates the row already exists and the CAS attempt has failed.- Returns:
- INSERT statement; must use
%sfor the table name
-
getCasUpdateSql
String getCasUpdateSql()Returns the SQL to conditionally update an item only when the stored version matches.Bind parameters in order:
(value_json, updated_at, namespace_path, item_key, expectedVersion). Implementations must setversion = version + 1. The caller treats an affected-row count of1as success.- Returns:
- UPDATE statement; must use
%sfor the table name
-
getSelectSql
Returns the SQL to fetch a single item.Bind parameters in order:
(namespace_path, item_key). Projection must be(value_json, version)in that column order. -
getDeleteSql
Returns the SQL to delete a single item.Bind parameters in order:
(namespace_path, item_key). -
getSearchSql
Returns the SQL to list items whose namespace_path matches aLIKEpattern.Bind parameters in order:
(namespace_like_pattern, limit, offset). Projection must be(item_key, value_json, version)in that column order.The default uses
ESCAPE '!'(universal across MySQL, PostgreSQL, SQLite, H2 and avoids the backslash-vs-standard_conforming_stringspitfalls) andORDER BY item_keyto matchInMemoryStore's sort behavior. Theitem_keysort approximates the in-memory implementation's compound-key sort closely enough for paging. -
getLikeEscapeChar
default char getLikeEscapeChar()The escape character paired withgetSearchSql()'sESCAPEclause. Callers must use this when building theLIKEpattern to escape literal%,_, and the escape character itself. -
from
Detects the dialect from theDataSource's database product name.Defaults to
PostgresJdbcStoreDialectwhen the product cannot be determined or is not recognised — this keeps unknown engines functional under the SQL-standard subset.- Parameters:
dataSource- the JDBC data source to probe; must not benull- Returns:
- a dialect appropriate for the underlying database
-