Interface JdbcStoreDialect

All Known Implementing Classes:
H2JdbcStoreDialect, MysqlJdbcStoreDialect, PostgresJdbcStoreDialect, SqliteJdbcStoreDialect

public interface JdbcStoreDialect
Database-specific SQL for JdbcStore.

Each dialect renders the four SQL statements that vary between vendors:

  • getCreateTableSql() — schema bootstrap, used when JdbcStore.Builder#initializeSchema(true) is set.
  • getUpsertSql() — unconditional write that increments version on update.
  • getInsertSql() — create-only insert, used by the BaseStore.putIfVersion(java.util.List<java.lang.String>, java.lang.String, java.util.Map<java.lang.String, java.lang.Object>, long) CAS path when expectedVersion == 0; primary-key conflicts indicate the row already exists.
  • getCasUpdateSql() — conditional update used by the BaseStore.putIfVersion(java.util.List<java.lang.String>, java.lang.String, java.util.Map<java.lang.String, java.lang.Object>, long) CAS path when expectedVersion > 0; relies on WHERE 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
    Modifier and Type
    Field
    Description
    static final org.slf4j.Logger
     
  • Method Summary

    Modifier and Type
    Method
    Description
    from(DataSource dataSource)
    Detects the dialect from the DataSource'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 String
    Returns the SQL to delete a single item.
    Returns the SQL to insert a new row with version=1.
    default char
    The escape character paired with getSearchSql()'s ESCAPE clause.
    default String
    Returns the SQL to list items whose namespace_path matches a LIKE pattern.
    default String
    Returns the SQL to fetch a single item.
    Returns the SQL to insert-or-update an item, incrementing version on 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 %s for the table name
    • getUpsertSql

      String getUpsertSql()
      Returns the SQL to insert-or-update an item, incrementing version on update.

      Bind parameters in order: (namespace_path, item_key, value_json, updated_at) — the dialect is responsible for setting version=1 on insert and version=version+1 on update.

      Returns:
      UPSERT statement; must use %s for the table name
    • getInsertSql

      String getInsertSql()
      Returns the SQL to insert a new row with version=1.

      Bind parameters in order: (namespace_path, item_key, value_json, updated_at). Used by putIfVersion(... expectedVersion=0): a primary-key violation indicates the row already exists and the CAS attempt has failed.

      Returns:
      INSERT statement; must use %s for 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 set version = version + 1. The caller treats an affected-row count of 1 as success.

      Returns:
      UPDATE statement; must use %s for the table name
    • getSelectSql

      default String 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

      default String getDeleteSql()
      Returns the SQL to delete a single item.

      Bind parameters in order: (namespace_path, item_key).

    • getSearchSql

      default String getSearchSql()
      Returns the SQL to list items whose namespace_path matches a LIKE pattern.

      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_strings pitfalls) and ORDER BY item_key to match InMemoryStore's sort behavior. The item_key sort approximates the in-memory implementation's compound-key sort closely enough for paging.

    • getLikeEscapeChar

      default char getLikeEscapeChar()
      The escape character paired with getSearchSql()'s ESCAPE clause. Callers must use this when building the LIKE pattern to escape literal %, _, and the escape character itself.
    • from

      static JdbcStoreDialect from(DataSource dataSource)
      Detects the dialect from the DataSource's database product name.

      Defaults to PostgresJdbcStoreDialect when 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 be null
      Returns:
      a dialect appropriate for the underlying database