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
Modifier and TypeFieldDescriptionstatic final File
The specialFile
that, when configured viasetDatabaseFile()
, causes SQLite to use an in-memory database.static final String
The 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
-
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
).Get the configured database file.protected void
initializeDatabaseIfNecessary
(Connection connection) Perform any required database initialization.limitSingleRow
(String sql) AppendsLIMIT 1
to the statement.Encloses the givenname
in double quotes.void
setDatabaseFile
(File file) Set the configured database file.void
setExclusiveLocking
(boolean exclusiveLocking) Configure whether the database connection locking mode uses exclusive locking.void
setPragmas
(List<String> pragmas) Configure arbitraryPRAGMA
statements to be executed on newly createdConnection
s.void
setSQLiteConfig
(org.sqlite.SQLiteConfig config) Set anSQLiteConfig
with which to configure the auto-createdSQLiteDataSource
.void
start()
Start this instance.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, 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 specialFile
that, 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 anSQLiteConfig
with 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 arbitraryPRAGMA
statements to be executed on newly createdConnection
s.- Parameters:
pragmas
- zero or more pragma statements (without thePRAGMA
keyword) to execute on newConnection
s- See Also:
-
start
public void start()Description copied from interface:KVDatabase
Start 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:
start
in interfaceKVDatabase
- Overrides:
start
in classSQLKVDatabase
-
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
-
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
-
quote
Encloses the givenname
in double quotes.- Overrides:
quote
in classSQLKVDatabase
- Parameters:
name
- table or column name- Returns:
name
enquoted as necessary for this database type
-
limitSingleRow
AppendsLIMIT 1
to the statement.- 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
-