Package ru.curs.celesta.dbutils.adaptors
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 Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidalterSequence(Connection conn, SequenceElement s)Alters sequence in the database.intcompareStrings(String left, String right)Compare strings using database according to current collation rules.voidcreateColumn(Connection conn, Column<?> c)Adds a new column to the table.voidcreateFK(Connection conn, ForeignKey fk)Creates a foreign key in the DB.voidcreateIndex(Connection conn, Index index)Creates a table index in the DB.voidcreateParameterizedView(Connection conn, ParameterizedView pv)voidcreatePK(Connection conn, TableElement t)Creates primary key on a table.voidcreateSchemaIfNotExists(String name)Creates DB schema with the specified name if such didn't exist before.voidcreateSequence(Connection conn, SequenceElement s)Creates a sequence in the database.voidcreateSysObjects(Connection conn, String sysSchemaName)Creates or recreates other system objects (stored procedures, functions) needed for Celesta functioning on current RDBMS.voidcreateTable(Connection conn, TableElement te)Creates a table "from scratch" in the database.voidcreateTableTriggersForMaterializedViews(Connection conn, BasicTable t)voidcreateView(Connection conn, View v)Creates a view in the DB based on metadata.abstract PreparedStatementdeleteRecordSetStatement(Connection conn, TableElement t, String where)Creates a PreparedStatement object for a DELETE statement for deleting a set of records that satisfy a condition.voiddropFK(Connection conn, String schemaName, String tableName, String fkName)Drops a foreign key from the database.voiddropIndex(Grain g, DbIndexInfo dBIndexInfo)Removes table index in the grain.voiddropParameterizedView(Connection conn, String schemaName, String viewName)Drops parameterized view from the database.voiddropPk(Connection conn, TableElement t, String pkName)Drops primary key from the table by using known name of the primary key.voiddropSequence(Connection conn, SequenceElement s)Drops sequence from the database.voiddropTable(Connection conn, TableElement t)Deletes table from RDBMS.voiddropTableTriggersForMaterializedViews(Connection conn, BasicTable t)voiddropTrigger(Connection conn, TriggerQuery query)Drops a trigger from DB.voiddropView(Connection conn, String schemaName, String viewName)Drops view.voidexecuteNative(Connection conn, String sql)StringgetCallFunctionSql(ParameterizedView pv)Prepares a function call expression.abstract DbColumnInfogetColumnInfo(Connection conn, Column<?> c)Returns information on a column.Set<String>getColumns(Connection conn, TableElement t)Returns a set of column names for a specific table.abstract intgetCurrentIdent(Connection conn, BasicTable t)Returns current identity value for the table.abstract intgetDBPid(Connection conn)Returns process id of current database connection.abstract PreparedStatementgetDeleteRecordStatement(Connection conn, TableElement t, String where)Creates a PreparedStatement object for a DELETE statement for deleting a set of records that satisfy a condition.abstract List<DbFkInfo>getFKInfo(Connection conn, Grain g)Returns information on the foreign keys from grain.abstract Map<String,DbIndexInfo>getIndices(Connection conn, Grain g)Returns a set of indices referring to tables specified in the indicated grain.abstract PreparedStatementgetInsertRecordStatement(Connection conn, BasicTable t, boolean[] nullsMask, List<ParameterSetter> program)Creates a PreparedStatement object for an INSERT statement to insert a record into a table.abstract PreparedStatementgetNavigationStatement(Connection conn, FromClause from, String orderBy, String navigationWhereClause, Set<String> fields, long offset)Returns navigable PreparedStatement by a filtered set of records.abstract PreparedStatementgetOneFieldStatement(Connection conn, Column<?> c, String where)Creates a PreparedStatement object for a SELECT statement of a single column containing at most one record.abstract PreparedStatementgetOneRecordStatement(Connection conn, TableElement t, String where, Set<String> fields)Creates a PreparedStatement object for a SELECT statement containing at most one record.abstract List<String>getParameterizedViewList(Connection conn, Grain g)Get names of existing parameterized views.abstract DbPkInfogetPKInfo(Connection conn, TableElement t)Returns information on the primary key of a table.PreparedStatementgetRecordSetStatement(Connection conn, FromClause from, String whereClause, String orderBy, long offset, long rowCount, Set<String> fields)ReturnsPreparedStatementcontaining a filtered set of entries.abstract DbSequenceInfogetSequenceInfo(Connection conn, SequenceElement s)Returns information on a sequence.PreparedStatementgetSetCountStatement(Connection conn, FromClause from, String whereClause)Builds a SELECT COUNT statement.Optional<String>getTriggerBody(Connection conn, TriggerQuery query)Returns body of the trigger existing in the database.abstract DBTypegetType()Returns current database type.PreparedStatementgetUpdateRecordStatement(Connection conn, BasicTable t, boolean[] equalsMask, boolean[] nullsMask, List<ParameterSetter> program, String where)Builds prepared statement for records UPDATE.
equalsMask[columnIndex]should containtruefor the column with index equal tocolumnIndexto take part in the evaluation.
IfnullsMask[columnIndex]containstrueIS NULLcheck has a priority aboveprogram[columnIndex]check -column = ?.List<String>getViewList(Connection conn, Grain g)Returns list of view names in the grain.SQLGeneratorgetViewSQLGenerator()Returns SQL generator for Celesta views/complex filters.voidinitDataForMaterializedView(Connection conn, MaterializedView mv)Initializes data for a newly crated materialized view.booleanisValidConnection(Connection conn, int timeout)Checking for connection validity.abstract longnextSequenceValue(Connection conn, SequenceElement s)Retrieves next value from the sequence.StringpkConstraintString(TableElement tableElement)Returns DB specific PK constraint name for a table element.ZonedDateTimeprepareZonedDateTimeForParameterSetter(Connection conn, ZonedDateTime z)List<String>selectStaticStrings(List<String> data, String columnName, String orderByDirection)Returned strings ordered by the database according to the current collation.abstract booleansequenceExists(Connection conn, String schema, String name)Checks if sequence exists in the DB.StringsequenceString(String schemaName, String sequenceName)Returns template by sequence name.booleansupportsCortegeComparing()Whether DB supports cortege comparing.abstract booleantableExists(Connection conn, String schema, String name)Checks if table exists in the DB.StringtableString(String schemaName, String tableName)Returns template by table name.StringtranslateDate(String date)Translates Celesta date literal to the one from specific database.abstract booleantriggerExists(Connection conn, TriggerQuery query)Checks if trigger exists in the DB.voidupdateColumn(Connection conn, Column<?> c, DbColumnInfo actual)Updates a table column.voidupdateVersioningTrigger(Connection conn, TableElement t)booleanuserTablesExist()Returnstruein that and only that case if DB contains user tables (i.e.-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface ru.curs.celesta.dbutils.QueryBuildingHelper
getInFilterClause, nullsFirst
-
-
-
-
Method Detail
-
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()
Returnstruein 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 connectionc- 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 containtruefor the column with index equal tocolumnIndexto take part in the evaluation.
IfnullsMask[columnIndex]containstrueIS NULLcheck has a priority aboveprogram[columnIndex]check -column = ?.- Parameters:
conn- DB connectiont- updatable tableequalsMask- equals masknullsMask- nulls maskprogram- collects parameter predicateswhere- WHERE clause
-
createIndex
public final void createIndex(Connection conn, Index index)
Creates a table index in the DB.- Parameters:
conn- DB connectionindex- table index
-
createFK
public final void createFK(Connection conn, ForeignKey fk)
Creates a foreign key in the DB.- Parameters:
conn- DB connectionfk- foreign key
-
dropIndex
public final void dropIndex(Grain g, DbIndexInfo dBIndexInfo)
Removes table index in the grain.- Parameters:
g- GraindBIndexInfo- Information on index
-
getRecordSetStatement
public final PreparedStatement getRecordSetStatement(Connection conn, FromClause from, String whereClause, String orderBy, long offset, long rowCount, Set<String> fields)
ReturnsPreparedStatementcontaining a filtered set of entries.- Parameters:
conn- Connectionfrom- Object for forming FROM part of the querywhereClause- Where clauseorderBy- Sort orderoffset- Number of entries to skiprowCount- 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- Connectionfrom- From clausewhereClause- Where clause
-
dropTrigger
public final void dropTrigger(Connection conn, TriggerQuery query)
Drops a trigger from DB.- Parameters:
conn- Connectionquery- 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 connectiont- table
-
getViewSQLGenerator
public final SQLGenerator getViewSQLGenerator()
Description copied from interface:QueryBuildingHelperReturns SQL generator for Celesta views/complex filters.- Specified by:
getViewSQLGeneratorin interfaceQueryBuildingHelper
-
createView
public final void createView(Connection conn, View v)
Creates a view in the DB based on metadata.- Parameters:
conn- DB connectionv- 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- connectiontimeout- time-out- Returns:
trueif connection is valid, otherwisefalse
-
tableString
public String tableString(String schemaName, String tableName)
Returns template by table name.- Parameters:
schemaName- schema nametableName- table name
-
sequenceString
public String sequenceString(String schemaName, String sequenceName)
Returns template by sequence name.- Parameters:
schemaName- schema namesequenceName- 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- Connectionte- 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 connectiont- 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 connectionschemaName- schema nametableName- table possessing the foreign keyfkName- name of foreign key
-
dropParameterizedView
public void dropParameterizedView(Connection conn, String schemaName, String viewName)
Drops parameterized view from the database.- Parameters:
conn- DB connectionschemaName- schema nameviewName- view name
-
getViewList
public List<String> getViewList(Connection conn, Grain g)
Returns list of view names in the grain.- Parameters:
conn- DB connectiong- 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 connections- sequence element
-
alterSequence
public void alterSequence(Connection conn, SequenceElement s)
Alters sequence in the database.- Parameters:
conn- DB connections- sequence element
-
dropSequence
public void dropSequence(Connection conn, SequenceElement s)
Drops sequence from the database.- Parameters:
conn- DB connections- sequence element
-
dropView
public void dropView(Connection conn, String schemaName, String viewName)
Drops view.- Parameters:
conn- DB connectionschemaName- grain nameviewName- 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 connectionsysSchemaName- system schema name
-
translateDate
public String translateDate(String date)
Translates Celesta date literal to the one from specific database.- Specified by:
translateDatein interfaceQueryBuildingHelper- Parameters:
date- Date literal
-
getTriggerBody
public Optional<String> getTriggerBody(Connection conn, TriggerQuery query)
Returns body of the trigger existing in the database.- Parameters:
conn- Connectionquery- 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- connectionmv- 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:
selectStaticStringsin interfaceStaticDataAdaptor- Parameters:
data- List of String to selectcolumnName- name of result columnorderByDirection- 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:
compareStringsin interfaceStaticDataAdaptor- Parameters:
left- left stringright- right string- Returns:
- result of comparison
-
supportsCortegeComparing
public boolean supportsCortegeComparing()
Whether DB supports cortege comparing.- Specified by:
supportsCortegeComparingin interfaceQueryBuildingHelper- 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 connectiont- TablepkName- name of the primary key
-
updateColumn
public void updateColumn(Connection conn, Column<?> c, DbColumnInfo actual)
Updates a table column.- Parameters:
conn- DB connectionc- Column to updateactual- Actual column info
-
prepareZonedDateTimeForParameterSetter
public ZonedDateTime prepareZonedDateTimeForParameterSetter(Connection conn, ZonedDateTime z)
- Specified by:
prepareZonedDateTimeForParameterSetterin interfaceQueryBuildingHelper
-
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- Connectionfrom- From clauseorderBy- Sorting order (ascending or descending)navigationWhereClause- Navigable set condition (from current record)fields- Fields of selectionoffset- First record offset
-
tableExists
public abstract boolean tableExists(Connection conn, String schema, String name)
Checks if table exists in the DB.- Parameters:
conn- DB connectionschema- schema namename- 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 connectiont- tablewhere- WHERE conditionfields- 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 connectionc- column to selectwhere- 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 connectiont- tablewhere- 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 connectiont- tablenullsMask- null-flags (if set the corresponding field at n-th position becomesnull)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 connectiont- 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 connectiont- tablewhere- condition (can benull)
-
getColumnInfo
public abstract DbColumnInfo getColumnInfo(Connection conn, Column<?> c)
Returns information on a column.- Parameters:
conn- DB connectionc- column
-
getPKInfo
public abstract DbPkInfo getPKInfo(Connection conn, TableElement t)
Returns information on the primary key of a table.- Parameters:
conn- DB connectiont- 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 connectiong- 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 connectiong- 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- connectiong- 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 connections- sequence
-
sequenceExists
public abstract boolean sequenceExists(Connection conn, String schema, String name)
Checks if sequence exists in the DB.- Parameters:
conn- DB connectionschema- schema namename- sequence name
-
getSequenceInfo
public abstract DbSequenceInfo getSequenceInfo(Connection conn, SequenceElement s)
Returns information on a sequence.- Parameters:
conn- DB connections- sequence
-
-