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
Modifier and TypeFieldDescriptionstatic final boolean
static final boolean
static final int
static final String
Fields 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
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
configureConnection
(Connection connection) Configure a newly createdConnection
.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
).protected SQLKVTransaction
createSQLKVTransaction
(Connection connection) Create a newSQLKVTransaction
for a new transaction given the specifiedConnection
.int
Get the lock timeout with which to configure new transactions.Get schema name.protected void
initializeDatabaseIfNecessary
(Connection connection) Perform any required database initialization.boolean
Get whether support for large keys (greater than 8,000 bytes) are enabled.boolean
Get 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 String
quoteString
(String value) Encloses the givenname
in single quotes and doubles up any single quotes therein.void
setLargeKeys
(boolean largeKeys) void
setLargeValues
(boolean largeValues) void
setLockTimeout
(int lockTimeout) void
Wrap the givenSQLException
in 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:SQLKVDatabase
Perform any required database initialization.The implementation in
SQLKVDatabase
does nothing. Subclasses will typically invokeCREATE TABLE ... IF NOT EXISTS
here, etc.- Overrides:
initializeDatabaseIfNecessary
in 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:SQLKVDatabase
Configure a newly createdConnection
.The implementation in
SQLKVDatabase
does nothing.- Overrides:
configureConnection
in classSQLKVDatabase
- Parameters:
connection
- newly createdConnection
- Throws:
SQLException
- if an error occurs
-
createSQLKVTransaction
Description copied from class:SQLKVDatabase
Create a newSQLKVTransaction
for a new transaction given the specifiedConnection
. There will already be an SQL transaction open onconnection
.The implementation in
SQLKVDatabase
just invokesnew SQLKVTransaction(this, connection)
.- Overrides:
createSQLKVTransaction
in classSQLKVDatabase
- Parameters:
connection
- theConnection
for a new transaction- Returns:
- newly created transaction
- Throws:
SQLException
- if an error occurs
-
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 classSQLKVDatabase
- Returns:
- SQL insertion statement
-
quoteString
Encloses the givenname
in single quotes and doubles up any single quotes therein.- Parameters:
value
- string to quote- Returns:
- quoted string
-
limitSingleRow
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 classSQLKVDatabase
- Parameters:
sql
- SQL statement- Returns:
- SQL statement
-
wrapException
Description copied from class:SQLKVDatabase
Wrap the givenSQLException
in the appropriateKVTransactionException
.- Overrides:
wrapException
in classSQLKVDatabase
- Parameters:
tx
- theSQLKVTransaction
in which the exception occurede
- SQL exception- Returns:
- appropriate
KVTransactionException
with chained exceptione
-