Class MSSQLKVDatabase

java.lang.Object
io.permazen.kv.sql.SQLKVDatabase
io.permazen.kv.mssql.MSSQLKVDatabase
All Implemented Interfaces:
KVDatabase

public class MSSQLKVDatabase extends SQLKVDatabase
Microsoft SQL Server variant of SQLKVDatabase.

Automatically creates the key/value table on startup if it doesn't already exist.

Note: Because SQL Server ignores trailing zero bytes when comparing VARBINARY values, we are forced to use "byte stuffing" to encode keys, whereby 0x00 and 0x01 are replaced by 0x0101 and 0x0102, respectively.

  • Field Details

  • Constructor Details

    • MSSQLKVDatabase

      public MSSQLKVDatabase()
  • Method Details

    • getSchema

      public String getSchema()
      Get schema name.

      Default is "dbo".

      Returns:
      schema name
    • setSchema

      public void setSchema(String schema)
    • isLargeKeys

      public boolean isLargeKeys()
      Get whether support for large keys (greater than 8,000 bytes) are enabled.

      This only affects initial table creation; it will have no effect on an already-initialized database.

      Note that if large values are enabled, and any object field containing values greater than 8,000 bytes is indexed, then large keys must also be enabled.

      Default is false.

      Returns:
      true if large key support is enabled
    • setLargeKeys

      public void setLargeKeys(boolean largeKeys)
    • isLargeValues

      public boolean isLargeValues()
      Get whether support for large values (greater than 8,000 bytes) are enabled.

      This only affects initial table creation; it will have no effect on an already-initialized database.

      Note that if large values are enabled, and any object field containing values greater than 8,000 bytes is indexed, then large keys must also be enabled.

      Default is true.

      Returns:
      true if large value support is enabled
    • setLargeValues

      public void setLargeValues(boolean largeValues)
    • getLockTimeout

      public int getLockTimeout()
      Get the lock timeout with which to configure new transactions.

      Default is 3000.

      Returns:
      lock timeout for new transactions
    • setLockTimeout

      public void setLockTimeout(int lockTimeout)
    • initializeDatabaseIfNecessary

      protected void initializeDatabaseIfNecessary(Connection connection) throws SQLException
      Description copied from class: SQLKVDatabase
      Perform any required database initialization.

      The implementation in SQLKVDatabase does nothing. Subclasses will typically invoke CREATE TABLE ... IF NOT EXISTS here, etc.

      Overrides:
      initializeDatabaseIfNecessary in class SQLKVDatabase
      Parameters:
      connection - open database connection (will be closed by the caller of this method)
      Throws:
      SQLException - if an error occurs
    • configureConnection

      protected void configureConnection(Connection connection) throws SQLException
      Description copied from class: SQLKVDatabase
      Configure a newly created Connection.

      The implementation in SQLKVDatabase does nothing.

      Overrides:
      configureConnection in class SQLKVDatabase
      Parameters:
      connection - newly created Connection
      Throws:
      SQLException - if an error occurs
    • createSQLKVTransaction

      protected SQLKVTransaction createSQLKVTransaction(Connection connection) throws SQLException
      Description copied from class: SQLKVDatabase
      Create a new SQLKVTransaction for a new transaction given the specified Connection. There will already be an SQL transaction open on connection.

      The implementation in SQLKVDatabase just invokes new SQLKVTransaction(this, connection).

      Overrides:
      createSQLKVTransaction in class SQLKVDatabase
      Parameters:
      connection - the Connection for a new transaction
      Returns:
      newly created transaction
      Throws:
      SQLException - if an error occurs
    • createPutStatement

      public String createPutStatement()
      Description copied from class: SQLKVDatabase
      Create an SQL statement that inserts the key/value pair with key ?1 and value ?2 A row with key ?1 may already exist; if so, the value should be updated to ?2 (if syntax requires it, the value may be updated to ?3 instead; ?3, if it exists, will be set to the same value as ?2).
      Overrides:
      createPutStatement in class SQLKVDatabase
      Returns:
      SQL insertion statement
    • quoteString

      protected String quoteString(String value)
      Encloses the given name in single quotes and doubles up any single quotes therein.
      Parameters:
      value - string to quote
      Returns:
      quoted string
    • limitSingleRow

      public String limitSingleRow(String sql)
      Description copied from class: SQLKVDatabase
      Modify the given SQL statement so that only one row is returned.

      This is an optional method; returning statement unmodified is acceptable, but subclasses may be able to improve efficiency by modifying the SQL statement in a vendor-specific manner to only return one row.

      The implementation in SQLKVDatabase returns its parameter unchanged.

      Overrides:
      limitSingleRow in class SQLKVDatabase
      Parameters:
      sql - SQL statement
      Returns:
      SQL statement
    • wrapException

      public KVTransactionException wrapException(SQLKVTransaction tx, SQLException e)
      Description copied from class: SQLKVDatabase
      Wrap the given SQLException in the appropriate KVTransactionException.
      Overrides:
      wrapException in class SQLKVDatabase
      Parameters:
      tx - the SQLKVTransaction in which the exception occured
      e - SQL exception
      Returns:
      appropriate KVTransactionException with chained exception e