Class SQLiteKVDatabase
- All Implemented Interfaces:
KVDatabase
SQLKVDatabase.
Instances need only be configured with the database file, via setDatabaseFile().
In this case, the SQLiteDataSource will then be automatically created and configured. The
configuration can be overridden via setSQLiteConfig() if desired.
Otherwise (i.e., if setDatabaseFile() is not used), then setDataSource()
must be used to explicitly configure a DataSource and any invocation of
setSQLiteConfig() is ignored.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final FileThe specialFilethat, when configured viasetDatabaseFile(), causes SQLite to use an in-memory database.static final StringThe name of the SQLite JDBC driver class ("org.sqlite.JDBC").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
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).Get the configured database file.protected voidinitializeDatabaseIfNecessary(Connection connection) Perform any required database initialization.limitSingleRow(String sql) AppendsLIMIT 1to the statement.Encloses the givennamein double quotes.voidsetDatabaseFile(File file) Set the configured database file.voidsetExclusiveLocking(boolean exclusiveLocking) Configure whether the database connection locking mode uses exclusive locking.voidsetPragmas(List<String> pragmas) Configure arbitraryPRAGMAstatements to be executed on newly createdConnections.voidsetSQLiteConfig(org.sqlite.SQLiteConfig config) Set anSQLiteConfigwith which to configure the auto-createdSQLiteDataSource.voidstart()Start this instance.Wrap the givenSQLExceptionin the appropriateKVTransactionException.Methods inherited from class io.permazen.kv.sql.SQLKVDatabase
beginTransaction, createGetAllStatement, createGetAtLeastStatement, createGetAtMostStatement, createGetRangeStatement, createGetStatement, createRemoveAllStatement, createRemoveAtLeastStatement, createRemoveAtMostStatement, createRemoveRangeStatement, createRemoveStatement, createSQLKVTransaction, createTransaction, createTransaction, createTransactionConnection, getDataSource, getIsolationLevel, getIsolationLevel, getKeyColumnName, getTableName, getValueColumnName, isRollbackForReadOnly, postBeginTransaction, preBeginTransaction, setDataSource, setIsolationLevel, setKeyColumnName, setRollbackForReadOnly, setTableName, setValueColumnName, stop
-
Field Details
-
SQLITE_DRIVER_CLASS_NAME
The name of the SQLite JDBC driver class ("org.sqlite.JDBC").- See Also:
-
MEMORY_FILE
The specialFilethat, when configured viasetDatabaseFile(), causes SQLite to use an in-memory database.
-
-
Constructor Details
-
SQLiteKVDatabase
public SQLiteKVDatabase()Constructor.
-
-
Method Details
-
getDatabaseFile
Get the configured database file.- Returns:
- configured database file, if any, otherwise null
-
setDatabaseFile
Set the configured database file.- Parameters:
file- database file
-
setSQLiteConfig
public void setSQLiteConfig(org.sqlite.SQLiteConfig config) Set anSQLiteConfigwith which to configure the auto-createdSQLiteDataSource.- Parameters:
config- connection config, or null for none
-
setExclusiveLocking
public void setExclusiveLocking(boolean exclusiveLocking) Configure whether the database connection locking mode uses exclusive locking. This provides a performance benefit, but requires that this Java process be the only one accessing the database.Default is normal (non-exclusive) locking.
- Parameters:
exclusiveLocking- true for exclusive locking, false for normal locking- See Also:
-
setPragmas
Configure arbitraryPRAGMAstatements to be executed on newly createdConnections.- Parameters:
pragmas- zero or more pragma statements (without thePRAGMAkeyword) to execute on newConnections- See Also:
-
start
public void start()Description copied from interface:KVDatabaseStart this instance. This method must be called prior to creating any transactions.This method is idempotent: if this instance is already started, nothing happens.
Whether an instance that has been started and stopped can be restarted is implementation-dependent.
- Specified by:
startin interfaceKVDatabase- Overrides:
startin classSQLKVDatabase
-
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
-
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
-
quote
Encloses the givennamein double quotes.- Overrides:
quotein classSQLKVDatabase- Parameters:
name- table or column name- Returns:
nameenquoted as necessary for this database type
-
limitSingleRow
AppendsLIMIT 1to the statement.- 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
-