Class AbstractDatabase.InitScripts

java.lang.Object
is.codion.common.db.database.AbstractDatabase.InitScripts
Enclosing class:
AbstractDatabase

protected abstract static class AbstractDatabase.InitScripts extends Object
The init scripts of a database, each run once in the database's life, as Database.INIT_SCRIPTS describes, for a dialect running them to provide the record of the scripts run, table(), and how a script runs.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected abstract void
    create(Statement statement)
    Creates the table recording the scripts run, see table(), in case it does not exist.
    protected abstract boolean
    exists(Connection connection)
     
    protected abstract InputStream
    open(String scriptPath)
    Opens a script for reading, its path as listed.
    final void
    run(Connection connection, List<String> scriptPaths, boolean existing)
    Runs the scripts not yet run, in the order listed, each recorded with a checksum of its content once run, after checking that each one run is unchanged and still listed, and that none not yet run is listed before one that has, so that a database brought up to date one list at a time ends up as one created from the last.
    protected abstract void
    run(Statement statement, String scriptPath, String script)
    Runs a script, within the transaction recording it.
    protected static List<String>
    sanitize(List<String> scriptPaths)
    Sanitizes script paths, a dialect possibly running a script by its path in a statement of its own, for a dialect to refuse them before opening the database, run(Connection, List, boolean) and validate(Connection, List) sanitizing them as well.
    final String
     
    final void
    validate(Connection connection, List<String> scriptPaths)
    Validates a database against the init scripts it would be opened with, refusing it as run(Connection, List, boolean) would, changing nothing, so the connection may be read only.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • InitScripts

      protected InitScripts(String table)
      Parameters:
      table - the table recording the scripts run, with the columns path, a varchar primary key, checksum, 32 bytes, and run_at, a timestamp defaulting to the current one
  • Method Details

    • table

      public final String table()
      Returns:
      the table recording the scripts run
    • run

      public final void run(Connection connection, List<String> scriptPaths, boolean existing) throws SQLException
      Runs the scripts not yet run, in the order listed, each recorded with a checksum of its content once run, after checking that each one run is unchanged and still listed, and that none not yet run is listed before one that has, so that a database brought up to date one list at a time ends up as one created from the last. A database refused is left as it was, a moved script's record included.
      Parameters:
      connection - the connection, its auto-commit disabled by this method
      scriptPaths - the scripts, in the order to run
      existing - true in case the database existed before, false in case it is being created
      Throws:
      DatabaseException - in case the database is refused, a script can not be read or fails
      SQLException - in case of an exception
    • validate

      public final void validate(Connection connection, List<String> scriptPaths) throws SQLException
      Validates a database against the init scripts it would be opened with, refusing it as run(Connection, List, boolean) would, 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
      scriptPaths - 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
    • create

      protected abstract void create(Statement statement) throws SQLException
      Creates the table recording the scripts run, see table(), in case it does not exist.
      Parameters:
      statement - the statement to use
      Throws:
      SQLException - in case of an exception
    • exists

      protected abstract boolean exists(Connection connection) throws SQLException
      Parameters:
      connection - the connection
      Returns:
      true if the table recording the scripts run exists
      Throws:
      SQLException - in case of an exception
    • open

      protected abstract InputStream open(String scriptPath) throws IOException
      Opens a script for reading, its path as listed.
      Parameters:
      scriptPath - the script path
      Returns:
      a stream reading the script
      Throws:
      IOException - in case the script can not be read
    • run

      protected abstract void run(Statement statement, String scriptPath, String script) throws SQLException
      Runs a script, within the transaction recording it.
      Parameters:
      statement - the statement to use
      scriptPath - the script path, as listed
      script - the script, as open(String) reads it
      Throws:
      SQLException - in case the script fails
    • sanitize

      protected static List<String> sanitize(List<String> scriptPaths)
      Sanitizes script paths, a dialect possibly running a script by its path in a statement of its own, for a dialect to refuse them before opening the database, run(Connection, List, boolean) and validate(Connection, List) sanitizing them as well.
      Parameters:
      scriptPaths - the script paths
      Returns:
      the paths, trimmed, their separators forward slashes
      Throws:
      SecurityException - if a script path is empty or contains potential injection attempts