Class SQLiteKVDatabase

java.lang.Object
io.permazen.kv.sql.SQLKVDatabase
io.permazen.kv.sqlite.SQLiteKVDatabase
All Implemented Interfaces:
KVDatabase

public class SQLiteKVDatabase extends SQLKVDatabase
SQLite variant of 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 Details

    • SQLITE_DRIVER_CLASS_NAME

      public static final String SQLITE_DRIVER_CLASS_NAME
      The name of the SQLite JDBC driver class ("org.sqlite.JDBC").
      See Also:
    • MEMORY_FILE

      public static final File MEMORY_FILE
      The special File that, when configured via setDatabaseFile(), causes SQLite to use an in-memory database.
  • Constructor Details

    • SQLiteKVDatabase

      public SQLiteKVDatabase()
      Constructor.
  • Method Details

    • getDatabaseFile

      public File getDatabaseFile()
      Get the configured database file.
      Returns:
      configured database file, if any, otherwise null
    • setDatabaseFile

      public void setDatabaseFile(File file)
      Set the configured database file.
      Parameters:
      file - database file
    • setSQLiteConfig

      public void setSQLiteConfig(org.sqlite.SQLiteConfig config)
      Set an SQLiteConfig with which to configure the auto-created SQLiteDataSource.
      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

      public void setPragmas(List<String> pragmas)
      Configure arbitrary PRAGMA statements to be executed on newly created Connections.
      Parameters:
      pragmas - zero or more pragma statements (without the PRAGMA keyword) to execute on new Connections
      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 interface KVDatabase
      Overrides:
      start in class SQLKVDatabase
    • 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
    • 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
    • quote

      public String quote(String name)
      Encloses the given name in double quotes.
      Overrides:
      quote in class SQLKVDatabase
      Parameters:
      name - table or column name
      Returns:
      name enquoted as necessary for this database type
    • limitSingleRow

      public String limitSingleRow(String sql)
      Appends LIMIT 1 to the statement.
      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