Class SQLiteDatabaseFactory

java.lang.Object
is.codion.dbms.sqlite.SQLiteDatabaseFactory
All Implemented Interfaces:
DatabaseFactory

public final class SQLiteDatabaseFactory extends Object implements DatabaseFactory
Provides sqlite database implementations.

A database in a file runs its init scripts, Database.INIT_SCRIPTS, each once in its life, when an instance is created for it, once per JVM until the database is closed, its schemas attached, see SCHEMAS. They are recorded in the table codion_init_script of the main database. validateInitScripts(Connection, String...) tells whether a database would be refused, without opening it, for a copy of a database, say. A database whose files hold nothing, neither the main one nor a schema's, is a new one, running every script, not an existing one without the record: a first start killed before it committed anything leaves such files behind.

A script runs in the transaction recording it, SQLite's DDL being transactional, so a script that fails leaves nothing behind. Its statements, the driver running only the first of several, are split at the semicolons ending them, those within quotes, comments and a trigger's body left alone, a trigger ending with END and a semicolon. A CASE expression in a trigger's body, its END followed by a semicolon, ends the trigger early. A statement that can not run in a transaction, ATTACH or VACUUM, or has no effect in one, PRAGMA foreign_keys, does not belong in a script. A script holding a statement beginning or ending a transaction, BEGIN, COMMIT, END or ROLLBACK, as one written for another database may end with, is refused, none of it run; a savepoint, and a rollback to one, are within the script's transaction.

The scripts of a database whose connections enforce foreign keys, foreign_keys=true in its URL, run with the foreign keys not enforced, so that a script can replace a table other tables reference, as SQLite documents a change ALTER TABLE can not make: a new table created and filled, the old one dropped and the new one renamed, which is refused while they are enforced, and they can not be turned off within a transaction. Each script's foreign keys are checked before it is committed, PRAGMA foreign_key_check, and a script leaving a row referencing none is refused, nothing of it left. A delete in a script therefore neither cascades nor sets null, an action of a foreign key being part of its enforcing.

A transaction takes the database's write lock as it starts, BEGIN IMMEDIATE, as does a write outside of one, waiting for another writer within the connection's busy timeout, so that one reading before it writes can not fail for another having written meanwhile; a transaction that only reads waits for a writer as well. A transaction's changes are kept in memory until it commits, PRAGMA cache_spill = off on every connection, so that other connections read while it writes, however much, a large one taking that much memory, where SQLite would otherwise write the database file once its page cache, 2 MB by default, is full, locking readers out until the transaction ends.

Text is compared as on other databases: upper() and lower() are Java's, registered on every connection in place of SQLite's own, which change the letters of ASCII alone, so that a case-insensitive condition finds Þingvellir by þing%, and LIKE is case-sensitive, PRAGMA case_sensitive_like = on, where SQLite's ignores the case of an ASCII letter. The functions are registered by the driver's own classes, which this module reads with the driver a module as well, org.xerial.sqlitejdbc: with this module on the module path and the driver on the class path a connection is refused, an IllegalStateException saying so, rather than made with SQLite's own functions. A connection made past this dialect, another tool's or the driver's own, has SQLite's, so neither upper(), lower() nor LIKE belongs in the database's schema, an index on an expression, a check constraint or a generated column: an index on upper(name) written by one definition is damaged to the other, PRAGMA integrity_check reporting its rows missing, and a row written by the other is not found by it.

A key the database generates, one read back by Column.Generator.identity(), is declared exactly INTEGER PRIMARY KEY AUTOINCREMENT: SQLite's row id, which the driver returns for an insert, never handed out twice. A key of another type, INT or BIGINT PRIMARY KEY, is no row id, the driver then returning a row id the row's own key, left null, does not hold. Without AUTOINCREMENT the next id is the one after the highest there is, so the id of the newest row, once that row is deleted, goes to the next row inserted, and an entity held from before the delete, or a reference to it about to be saved, then names that row, where a database never handing an id out twice refuses it. An init script replacing a table with such a key, as above, carries over how far its ids have come, the old table's entry in sqlite_sequence to the new table's, which otherwise starts after the highest id it was given.

A date or time is stored as text, a column of SQLite's having no such type: LocalDate, LocalTime and LocalDateTime values in SQLite's own format, as its date and time functions write them, yyyy-MM-dd, HH:mm:ss and yyyy-MM-dd HH:mm:ss, a fraction of a second following where there is one, which a script writes its literals in, for conditions to compare them, and OffsetDateTime and OffsetTime values as their ISO text. A time is written to the microsecond, what is past it left out, where a column without a precision would keep every digit a value has, nine from a clock counting nanoseconds, a database with a precision, a server the database is synced with say, keeping six by default, the two then differing. A value written to a database keeping fewer digits still, a time without a fraction for example, is the application's to shorten.

A decimal is stored as SQLite's NUMERIC affinity has it, in a column declared DECIMAL or NUMERIC for example: a whole number as an integer, exactly within the range of a long, and a number with a fraction as a double, which keeps 15 significant digits, a value with more possibly read back changed. A column declared TEXT keeps a decimal exactly, but compares and orders it as text in SQL, 10 before 9.

  • Field Details

    • SCHEMAS

      public static final PropertyValue<String> SCHEMAS
      The schemas attached to every connection, a comma separated list of names, each a database file of its own beside the main one, named after it and the schema, fieldbook-sync.db for the schema sync of fieldbook.db, created where it does not exist. Tables are then named by schema, sync.row_state for example, as on a database with schemas.

      A schema is attached with the main database's journal mode and synchronous setting. A transaction writing to several files is atomic across them in a rollback journal mode only, not in WAL mode, in which a crash commits it in some of the files and not in others, so a main database in WAL mode is refused its schemas, creating a connection throwing an IllegalStateException.

      • Value type: String
      • Default value: null, no schemas attached
  • Constructor Details

    • SQLiteDatabaseFactory

      public SQLiteDatabaseFactory()
  • Method Details

    • supports

      public boolean supports(String driverClassName)
      Specified by:
      supports in interface DatabaseFactory
      Parameters:
      driverClassName - the driver class name
      Returns:
      true if this database factory supports the given driver
    • create

      public Database create(String url)
      Specified by:
      create in interface DatabaseFactory
      Parameters:
      url - the jdbc url
      Returns:
      a new Database implementation based on the given jdbc url.
    • database

      public static Database database(String url, @Nullable String schemas, String... initScripts)
      Creates a SQLite Database instance with the given schemas and init scripts, rather than those of SCHEMAS and Database.INIT_SCRIPTS, which create(String) takes.
      Parameters:
      url - the jdbc url
      schemas - the schemas attached to every connection, as SCHEMAS takes them, null for none
      initScripts - the init scripts, each run once in the database's life, see Database.INIT_SCRIPTS
      Returns:
      a SQLite Database instance
      Throws:
      IllegalArgumentException - in case a schema is not a valid name, or schemas are given for a database without a file, in memory or temporary
    • validateInitScripts

      public static void validateInitScripts(Connection connection, String... initScripts) throws SQLException
      Validates a database against the init scripts it would be opened with, refusing it as opening it would, see Database.INIT_SCRIPTS, changing nothing, so the connection may be read only. A database without the record of its scripts is never refused, nor one opened with none listed.
      Parameters:
      connection - a connection to the database
      initScripts - the init scripts it would be opened with
      Throws:
      DatabaseException - in case the database would be refused, or a script listed can not be read
      SQLException - in case the record of the scripts run can not be read