Class DBAdaptor

java.lang.Object
ru.curs.celesta.dbutils.adaptors.DBAdaptor
All Implemented Interfaces:
StaticDataAdaptor, QueryBuildingHelper
Direct Known Subclasses:
FirebirdAdaptor, MSSQLAdaptor, OpenSourceDbAdaptor, OraAdaptor

public abstract class DBAdaptor extends Object implements QueryBuildingHelper, StaticDataAdaptor
Adaptor for connection to the database.
  • Method Details

    • dropTable

      public final void dropTable(Connection conn, TableElement t)
      Deletes table from RDBMS.
      Parameters:
      conn - Connection to use.
      t - TableElement metadata of deleting table provided by Celesta.
    • userTablesExist

      public final boolean userTablesExist()
      Returns true in that and only that case if DB contains user tables (i.e. DB is not empty).
    • createSchemaIfNotExists

      public final void createSchemaIfNotExists(String name)
      Creates DB schema with the specified name if such didn't exist before.
      Parameters:
      name - schema name.
    • createColumn

      public final void createColumn(Connection conn, Column<?> c)
      Adds a new column to the table.
      Parameters:
      conn - DB connection
      c - column
    • getUpdateRecordStatement

      public final PreparedStatement getUpdateRecordStatement(Connection conn, BasicTable t, boolean[] equalsMask, boolean[] nullsMask, List<ParameterSetter> program, String where)
      Builds prepared statement for records UPDATE.

      equalsMask[columnIndex] should contain true for the column with index equal to columnIndex to take part in the evaluation.
      If nullsMask[columnIndex] contains true IS NULL check has a priority above program[columnIndex] check - column = ?.
      Parameters:
      conn - DB connection
      t - updatable table
      equalsMask - equals mask
      nullsMask - nulls mask
      program - collects parameter predicates
      where - WHERE clause
    • getRenameRecordStatement

      public final PreparedStatement getRenameRecordStatement(Connection conn, Table t, List<ParameterSetter> program, String where)
      Returns a statement that changes the values of the primary key fields of a record (renames the record).

      The program is filled with the setters for the SET clause. The new values of the key fields are expected to be placed in the parameters array right after the current values of all the fields of the record.

      Parameters:
      conn - DB connection
      t - table
      program - program of parameter setters
      where - WHERE clause identifying the record to rename
      Returns:
    • createIndex

      public final void createIndex(Connection conn, Index index)
      Creates a table index in the DB.
      Parameters:
      conn - DB connection
      index - table index
    • createFK

      public final void createFK(Connection conn, ForeignKey fk)
      Creates a foreign key in the DB.
      Parameters:
      conn - DB connection
      fk - foreign key
    • dropIndex

      public final void dropIndex(Grain g, DbIndexInfo dBIndexInfo)
      Removes table index in the grain.
      Parameters:
      g - Grain
      dBIndexInfo - Information on index
    • getRecordSetStatement

      public final PreparedStatement getRecordSetStatement(Connection conn, FromClause from, String whereClause, String orderBy, long offset, long rowCount, Set<String> fields)
      Returns PreparedStatement containing a filtered set of entries.
      Parameters:
      conn - Connection
      from - Object for forming FROM part of the query
      whereClause - Where clause
      orderBy - Sort order
      offset - Number of entries to skip
      rowCount - Number of entries to return (limit filter)
      fields - Requested columns. If none are provided all columns are requested
    • getSetCountStatement

      public final PreparedStatement getSetCountStatement(Connection conn, FromClause from, String whereClause)
      Builds a SELECT COUNT statement.
      Parameters:
      conn - Connection
      from - From clause
      whereClause - Where clause
    • dropTrigger

      public final void dropTrigger(Connection conn, TriggerQuery query)
      Drops a trigger from DB.
      Parameters:
      conn - Connection
      query - Trigger query
    • updateVersioningTrigger

      public final void updateVersioningTrigger(Connection conn, TableElement t)
    • createPK

      public final void createPK(Connection conn, TableElement t)
      Creates primary key on a table.
      Parameters:
      conn - DB connection
      t - table
    • getViewSQLGenerator

      public final SQLGenerator getViewSQLGenerator()
      Description copied from interface: QueryBuildingHelper
      Returns SQL generator for Celesta views/complex filters.
      Specified by:
      getViewSQLGenerator in interface QueryBuildingHelper
    • createView

      public final void createView(Connection conn, View v)
      Creates a view in the DB based on metadata.
      Parameters:
      conn - DB connection
      v - View
    • createParameterizedView

      public final void createParameterizedView(Connection conn, ParameterizedView pv)
    • dropTableTriggersForMaterializedViews

      public final void dropTableTriggersForMaterializedViews(Connection conn, BasicTable t)
    • createTableTriggersForMaterializedViews

      public final void createTableTriggersForMaterializedViews(Connection conn, BasicTable t)
    • executeNative

      public final void executeNative(Connection conn, String sql)
    • isValidConnection

      public boolean isValidConnection(Connection conn, int timeout)
      Checking for connection validity.
      Parameters:
      conn - connection
      timeout - time-out
      Returns:
      true if connection is valid, otherwise false
    • tableString

      public String tableString(String schemaName, String tableName)
      Returns template by table name.
      Parameters:
      schemaName - schema name
      tableName - table name
    • sequenceString

      public String sequenceString(String schemaName, String sequenceName)
      Returns template by sequence name.
      Parameters:
      schemaName - schema name
      sequenceName - sequence name
    • pkConstraintString

      public String pkConstraintString(TableElement tableElement)
      Returns DB specific PK constraint name for a table element.
      Parameters:
      tableElement - table element
    • createTable

      public void createTable(Connection conn, TableElement te)
      Creates a table "from scratch" in the database.
      Parameters:
      conn - Connection
      te - Table for creation. tables also in case if such table exists.
    • getColumns

      public Set<String> getColumns(Connection conn, TableElement t)
      Returns a set of column names for a specific table.
      Parameters:
      conn - DB connection
      t - Table to look the columns in.
    • dropFK

      public void dropFK(Connection conn, String schemaName, String tableName, String fkName)
      Drops a foreign key from the database.
      Parameters:
      conn - DB connection
      schemaName - schema name
      tableName - table possessing the foreign key
      fkName - name of foreign key
    • dropParameterizedView

      public void dropParameterizedView(Connection conn, String schemaName, String viewName)
      Drops parameterized view from the database.
      Parameters:
      conn - DB connection
      schemaName - schema name
      viewName - view name
    • getViewList

      public List<String> getViewList(Connection conn, Grain g)
      Returns list of view names in the grain.
      Parameters:
      conn - DB connection
      g - Grain for which the list of view names have to be returned.
    • getCallFunctionSql

      public String getCallFunctionSql(ParameterizedView pv)
      Prepares a function call expression.
      Parameters:
      pv - current parameterized view
    • createSequence

      public void createSequence(Connection conn, SequenceElement s)
      Creates a sequence in the database.
      Parameters:
      conn - DB connection
      s - sequence element
    • alterSequence

      public void alterSequence(Connection conn, SequenceElement s)
      Alters sequence in the database.
      Parameters:
      conn - DB connection
      s - sequence element
    • dropSequence

      public void dropSequence(Connection conn, SequenceElement s)
      Drops sequence from the database.
      Parameters:
      conn - DB connection
      s - sequence element
    • dropView

      public void dropView(Connection conn, String schemaName, String viewName)
      Drops view.
      Parameters:
      conn - DB connection
      schemaName - grain name
      viewName - view name
    • createSysObjects

      public void createSysObjects(Connection conn, String sysSchemaName)
      Creates or recreates other system objects (stored procedures, functions) needed for Celesta functioning on current RDBMS.
      Parameters:
      conn - DB connection
      sysSchemaName - system schema name
    • translateDate

      public String translateDate(String date)
      Translates Celesta date literal to the one from specific database.
      Specified by:
      translateDate in interface QueryBuildingHelper
      Parameters:
      date - Date literal
    • getTriggerBody

      public Optional<String> getTriggerBody(Connection conn, TriggerQuery query)
      Returns body of the trigger existing in the database.
      Parameters:
      conn - Connection
      query - Trigger query
      Returns:
      Trigger body or empty optional if not exists.
    • initDataForMaterializedView

      public void initDataForMaterializedView(Connection conn, MaterializedView mv)
      Initializes data for a newly crated materialized view.
      Parameters:
      conn - connection
      mv - current materialized view
    • selectStaticStrings

      public List<String> selectStaticStrings(List<String> data, String columnName, String orderByDirection)
      Returned strings ordered by the database according to the current collation. This method is required for operations that depend on collation rules.
      Specified by:
      selectStaticStrings in interface StaticDataAdaptor
      Parameters:
      data - List of String to select
      columnName - name of result column
      orderByDirection - expression to concatenate after "ORDER BY" (ASC or DESC)
      Returns:
      list of strings sorted according to the current collation rule.
    • compareStrings

      public int compareStrings(String left, String right)
      Compare strings using database according to current collation rules.
      Specified by:
      compareStrings in interface StaticDataAdaptor
      Parameters:
      left - left string
      right - right string
      Returns:
      result of comparison
    • supportsCortegeComparing

      public boolean supportsCortegeComparing()
      Whether DB supports cortege comparing.
      Specified by:
      supportsCortegeComparing in interface QueryBuildingHelper
      Returns:
    • dropPk

      public void dropPk(Connection conn, TableElement t, String pkName)
      Drops primary key from the table by using known name of the primary key.
      Parameters:
      conn - DB connection
      t - Table
      pkName - name of the primary key
    • updateColumn

      public void updateColumn(Connection conn, Column<?> c, DbColumnInfo actual)
      Updates a table column.
      Parameters:
      conn - DB connection
      c - Column to update
      actual - Actual column info
    • prepareZonedDateTimeForParameterSetter

      public ZonedDateTime prepareZonedDateTimeForParameterSetter(Connection conn, ZonedDateTime z)
      Specified by:
      prepareZonedDateTimeForParameterSetter in interface QueryBuildingHelper
    • getNavigationStatement

      public abstract PreparedStatement getNavigationStatement(Connection conn, FromClause from, String orderBy, String navigationWhereClause, Set<String> fields, long offset)
      Returns navigable PreparedStatement by a filtered set of records.
      Parameters:
      conn - Connection
      from - From clause
      orderBy - Sorting order (ascending or descending)
      navigationWhereClause - Navigable set condition (from current record)
      fields - Fields of selection
      offset - First record offset
    • tableExists

      public abstract boolean tableExists(Connection conn, String schema, String name)
      Checks if table exists in the DB.
      Parameters:
      conn - DB connection
      schema - schema name
      name - table name
    • triggerExists

      public abstract boolean triggerExists(Connection conn, TriggerQuery query) throws SQLException
      Checks if trigger exists in the DB.
      Parameters:
      conn - DB connection.
      query - trigger query parameters
      Throws:
      SQLException - thrown if resulting query fails
    • getOneRecordStatement

      public abstract PreparedStatement getOneRecordStatement(Connection conn, TableElement t, String where, Set<String> fields)
      Creates a PreparedStatement object for a SELECT statement containing at most one record.
      Parameters:
      conn - DB connection
      t - table
      where - WHERE condition
      fields - fields of selection
    • getOneFieldStatement

      public abstract PreparedStatement getOneFieldStatement(Connection conn, Column<?> c, String where)
      Creates a PreparedStatement object for a SELECT statement of a single column containing at most one record.
      Parameters:
      conn - DB connection
      c - column to select
      where - WHERE condition
    • deleteRecordSetStatement

      public abstract PreparedStatement deleteRecordSetStatement(Connection conn, TableElement t, String where)
      Creates a PreparedStatement object for a DELETE statement for deleting a set of records that satisfy a condition.
      Parameters:
      conn - DB connection
      t - table
      where - condition
    • getInsertRecordStatement

      public abstract PreparedStatement getInsertRecordStatement(Connection conn, BasicTable t, boolean[] nullsMask, List<ParameterSetter> program)
      Creates a PreparedStatement object for an INSERT statement to insert a record into a table.
      Parameters:
      conn - DB connection
      t - table
      nullsMask - null-flags (if set the corresponding field at n-th position becomes null)
      program - collects parameters that can be set with the query
    • getCurrentIdent

      public abstract int getCurrentIdent(Connection conn, BasicTable t)
      Returns current identity value for the table.
      Parameters:
      conn - DB connection
      t - table
    • getDeleteRecordStatement

      public abstract PreparedStatement getDeleteRecordStatement(Connection conn, TableElement t, String where)
      Creates a PreparedStatement object for a DELETE statement for deleting a set of records that satisfy a condition.
      Parameters:
      conn - DB connection
      t - table
      where - condition (can be null)
    • getColumnInfo

      public abstract DbColumnInfo getColumnInfo(Connection conn, Column<?> c)
      Returns information on a column.
      Parameters:
      conn - DB connection
      c - column
    • getPKInfo

      public abstract DbPkInfo getPKInfo(Connection conn, TableElement t)
      Returns information on the primary key of a table.
      Parameters:
      conn - DB connection
      t - Table that the information on the primary key has to be returned from
    • getFKInfo

      public abstract List<DbFkInfo> getFKInfo(Connection conn, Grain g)
      Returns information on the foreign keys from grain.
      Parameters:
      conn - DB connection
      g - grain name
      Returns:
      list where each item contain information on a separate foreign key
    • getIndices

      public abstract Map<String,DbIndexInfo> getIndices(Connection conn, Grain g)
      Returns a set of indices referring to tables specified in the indicated grain.
      Parameters:
      conn - DB connection
      g - Grain the tables of which have to be traversed for the indices.
    • getParameterizedViewList

      public abstract List<String> getParameterizedViewList(Connection conn, Grain g)
      Get names of existing parameterized views.
      Parameters:
      conn - connection
      g - current grain
    • getDBPid

      public abstract int getDBPid(Connection conn)
      Returns process id of current database connection.
      Parameters:
      conn - DB connection
    • getType

      public abstract DBType getType()
      Returns current database type. E.g. H2, POSTGRESQL etc.
    • nextSequenceValue

      public abstract long nextSequenceValue(Connection conn, SequenceElement s)
      Retrieves next value from the sequence.
      Parameters:
      conn - DB connection
      s - sequence
    • sequenceExists

      public abstract boolean sequenceExists(Connection conn, String schema, String name)
      Checks if sequence exists in the DB.
      Parameters:
      conn - DB connection
      schema - schema name
      name - sequence name
    • getSequenceInfo

      public abstract DbSequenceInfo getSequenceInfo(Connection conn, SequenceElement s)
      Returns information on a sequence.
      Parameters:
      conn - DB connection
      s - sequence