Class MSSQLKVDatabase
- All Implemented Interfaces:
KVDatabase
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final booleanstatic final booleanstatic final intstatic final StringFields inherited from class io.permazen.kv.sql.SQLKVDatabase
dataSource, DEFAULT_KEY_COLUMN_NAME, DEFAULT_TABLE_NAME, DEFAULT_VALUE_COLUMN_NAME, isolationLevel, keyColumnName, log, OPTION_ISOLATION, tableName, valueColumnName -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidconfigureConnection(Connection connection) Configure a newly createdConnection.Create an SQL statement that inserts the key/value pair with key?1and value?2A row with key?1may already exist; if so, the value should be updated to?2(if syntax requires it, the value may be updated to?3instead;?3, if it exists, will be set to the same value as?2).protected SQLKVTransactioncreateSQLKVTransaction(Connection connection) Create a newSQLKVTransactionfor a new transaction given the specifiedConnection.intGet the lock timeout with which to configure new transactions.Get schema name.protected voidinitializeDatabaseIfNecessary(Connection connection) Perform any required database initialization.booleanGet whether support for large keys (greater than 8,000 bytes) are enabled.booleanGet whether support for large values (greater than 8,000 bytes) are enabled.limitSingleRow(String sql) Modify the given SQL statement so that only one row is returned.protected StringquoteString(String value) Encloses the givennamein single quotes and doubles up any single quotes therein.voidsetLargeKeys(boolean largeKeys) voidsetLargeValues(boolean largeValues) voidsetLockTimeout(int lockTimeout) voidWrap the givenSQLExceptionin the appropriateKVTransactionException.Methods inherited from class io.permazen.kv.sql.SQLKVDatabase
beginTransaction, createGetAllStatement, createGetAtLeastStatement, createGetAtMostStatement, createGetRangeStatement, createGetStatement, createRemoveAllStatement, createRemoveAtLeastStatement, createRemoveAtMostStatement, createRemoveRangeStatement, createRemoveStatement, createTransaction, createTransaction, createTransactionConnection, getDataSource, getIsolationLevel, getIsolationLevel, getKeyColumnName, getTableName, getValueColumnName, isRollbackForReadOnly, postBeginTransaction, preBeginTransaction, quote, setDataSource, setIsolationLevel, setKeyColumnName, setRollbackForReadOnly, setTableName, setValueColumnName, start, stop
-
Field Details
-
DEFAULT_SCHEMA
- See Also:
-
DEFAULT_LARGE_KEYS
public static final boolean DEFAULT_LARGE_KEYS- See Also:
-
DEFAULT_LARGE_VALUES
public static final boolean DEFAULT_LARGE_VALUES- See Also:
-
DEFAULT_LOCK_TIMEOUT
public static final int DEFAULT_LOCK_TIMEOUT- See Also:
-
-
Constructor Details
-
MSSQLKVDatabase
public MSSQLKVDatabase()
-
-
Method Details
-
getSchema
Get schema name.Default is "dbo".
- Returns:
- schema name
-
setSchema
-
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
Description copied from class:SQLKVDatabasePerform any required database initialization.The implementation in
SQLKVDatabasedoes nothing. Subclasses will typically invokeCREATE TABLE ... IF NOT EXISTShere, etc.- Overrides:
initializeDatabaseIfNecessaryin classSQLKVDatabase- Parameters:
connection- open database connection (will be closed by the caller of this method)- Throws:
SQLException- if an error occurs
-
configureConnection
Description copied from class:SQLKVDatabaseConfigure a newly createdConnection.The implementation in
SQLKVDatabasedoes nothing.- Overrides:
configureConnectionin classSQLKVDatabase- Parameters:
connection- newly createdConnection- Throws:
SQLException- if an error occurs
-
createSQLKVTransaction
Description copied from class:SQLKVDatabaseCreate a newSQLKVTransactionfor a new transaction given the specifiedConnection. There will already be an SQL transaction open onconnection.The implementation in
SQLKVDatabasejust invokesnew SQLKVTransaction(this, connection).- Overrides:
createSQLKVTransactionin classSQLKVDatabase- Parameters:
connection- theConnectionfor a new transaction- Returns:
- newly created transaction
- Throws:
SQLException- if an error occurs
-
createPutStatement
Description copied from class:SQLKVDatabaseCreate an SQL statement that inserts the key/value pair with key?1and value?2A row with key?1may already exist; if so, the value should be updated to?2(if syntax requires it, the value may be updated to?3instead;?3, if it exists, will be set to the same value as?2).- Overrides:
createPutStatementin classSQLKVDatabase- Returns:
- SQL insertion statement
-
quoteString
Encloses the givennamein single quotes and doubles up any single quotes therein.- Parameters:
value- string to quote- Returns:
- quoted string
-
limitSingleRow
Description copied from class:SQLKVDatabaseModify the given SQL statement so that only one row is returned.This is an optional method; returning
statementunmodified 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
SQLKVDatabasereturns its parameter unchanged.- Overrides:
limitSingleRowin classSQLKVDatabase- Parameters:
sql- SQL statement- Returns:
- SQL statement
-
wrapException
Description copied from class:SQLKVDatabaseWrap the givenSQLExceptionin the appropriateKVTransactionException.- Overrides:
wrapExceptionin classSQLKVDatabase- Parameters:
tx- theSQLKVTransactionin which the exception occurede- SQL exception- Returns:
- appropriate
KVTransactionExceptionwith chained exceptione
-