-
- All Known Implementing Classes:
AbstractBackend,AbstractSql2003Backend,AbstractSql2008Backend,AbstractSql92Backend,Db2,Informix,Ingres,MariaDb,MsSql,MySql,Oracle,Postgres
public interface BackendA database backend.
Defines the backend specifics.
There is only one instance per database type.
Implementations must not maintain any state except for the whole of all connections to the specific kind of database.- Author:
- harald
-
-
Field Summary
Fields Modifier and Type Field Description static StringSQL_ALLSTAR'*' all string.static StringSQL_ALTER_COLUMNSQL ALTER COLUMN fragment.static StringSQL_ALTER_INDEXSQL ALTER INDEX intro.static StringSQL_ALTER_TABLESQL ALTER TABLE intro.static StringSQL_ANDAND string.static StringSQL_ANDNOTAND NOT string.static StringSQL_ARRAY_ALLALL array operator string.static StringSQL_ARRAY_ALL_PARParameter ALL array operator string.static StringSQL_ARRAY_ANYANY array operator string.static StringSQL_ARRAY_ANY_PARParameter ANY array operator string.static StringSQL_ARRAY_ININ array operator string.static StringSQL_ARRAY_IN_PARParameter IN array operator string.static StringSQL_ARRAY_NOT_INNOT IN array operator string.static StringSQL_ARRAY_NOT_IN_PARParameter NOT IN array operator string.static StringSQL_ARRAY_PARArray parameter (?).static StringSQL_COMMAcomma separator.static StringSQL_COMMA_PARParameter ,? string.static StringSQL_DELETEDELETE string.static StringSQL_EQUAL= operator string.static StringSQL_EQUAL_PARParameter =? string.static StringSQL_EQUAL_PAR_COMMAParameter =?, string.static StringSQL_EQUAL_ZERO=0 string.static StringSQL_EXISTSSQL EXISTS clause with SELECT 1 and opening left parenthesis.static StringSQL_FROMFROM string.static StringSQL_GREATER> operator string.static StringSQL_GREATER_PARParameter >? string.static StringSQL_GREATEROREQUAL>= operator string.static StringSQL_GREATEROREQUAL_PARParameter ≥? string.static StringSQL_GROUPBYGROUP BY string.static StringSQL_IF_EXISTSSQL IF EXISTS fragment.static StringSQL_INSERT_INTOINSERT INTO string.static StringSQL_INSERT_VALUES(VALUES) string for insert.static StringSQL_ISNOTNULLIS NOT NULL string.static StringSQL_ISNULLIS NULL string.static StringSQL_LEFT_PARENTHESISopening bracket.static StringSQL_LESS< operator string.static StringSQL_LESS_PARParameter <? string.static StringSQL_LESSOREQUAL<= operator string.static StringSQL_LESSOREQUAL_PARParameter <=? string.static StringSQL_LIKELIKE operator string.static StringSQL_LIKE_PARParameter LIKE ? string.static StringSQL_MAXMAX function name.static StringSQL_MINMAX function name.static StringSQL_NOTNOT string.static StringSQL_NOTEQUAL<> operator string.static StringSQL_NOTEQUAL_PARParameter <>? string.static StringSQL_NOTEQUAL_ZERO<>0 string.static StringSQL_NOTLIKENOT LIKE operator string.static StringSQL_NOTLIKE_PARParameter NOT LIKE ? string.static StringSQL_OROR string.static StringSQL_ORDERBYORDER BY string.static StringSQL_ORNOTOR NOT string.static StringSQL_PAR? string.static StringSQL_PAR_COMMAParameter ?, string.static StringSQL_PLUS_ONE+1 string.static StringSQL_RIGHT_PARENTHESISclosing bracket.static StringSQL_SELECTSELECT string.static StringSQL_SELECT_ALL_FROMSELECT * FROM string.static StringSQL_SETSET string.static StringSQL_SORTASCsort ASC string.static StringSQL_SORTDESCsort DESC string.static StringSQL_UPDATEUPDATE string.static StringSQL_WHEREWHERE string.static StringSQL_WHEREALL"WHERE 1=1" allows AND to be added.static StringSQL_WHEREANDSQL_WHEREALL + AND will be replaced by...static StringSQL_WHERENOTHING"WHERE 1=0" to select nothing.static StringSQL_WHEREORSQL_WHEREALL + OR will be replaced by...
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description booleanallowsExpressionsReferringToTablesBeingUpdated()Determines whether backend allows expressions referring to tables being updated.voidassertValidName(SqlNameType nameType, String name)Asserts that given string is a valid name and usage.voidbuildSelectSql(StringBuilder sqlBuilder, boolean writeLock, int limit, int offset)Creates a select statement string from an inner sql string.StringbuildSelectSql(String sql, boolean writeLock, int limit, int offset)Creates a select statement string from an inner sql string.StringcolumnTypeNullDefaultToString(String columnName, SqlType sqlType, int size, int scale, boolean nullable, Object defaultValue)Converts an sqltype, size, scale, nullable and default value to a database type declaration.StringcolumnTypeToString(SqlType sqlType, int size, int scale)Converts an sqltype, size and scale to a database type declaration.ColumnMetaDatacreateColumnMetaData(TableMetaData tableMetaData)Creates a column meta data instance.ConnectioncreateConnection(String url, String username, char[] password)Creates a jdbc connection.IndexColumnMetaDatacreateIndexColumnMetaData(IndexMetaData indexMetaData)Creates an index column meta data instance.IndexMetaDatacreateIndexMetaData(TableMetaData tableMetaData)Creates an index meta data instance.TableMetaDatacreateTableMetaData(ModelMetaData metaData, String tableName)Creates a meta data instance.StringgetBackendId(Connection connection)Determines the unique id or name used by the backend for a given connection.StringgetCoalesceKeyword()Gets the keyword for the COALESCE function.StringgetDefaultSchema()Gets the default schema name.
Some backends store the objects in a default schema, if no explicit schema is given.intgetDefaultSize(SqlType sqlType)Gets the default size if no size given in model.StringgetDriverClassName()Gets the JDBC driver class name.StringgetEmptyString()Gets the empty string.intgetMaxScale(SqlType sqlType, int size)Returns the maximum scale for a given SQL-type and size.intgetMaxSize(SqlType sqlType)Returns the maximum size for a given SQL-type.DatabaseMetaData[]getMetaData(BackendInfo backendInfo)Gets the metadata from the backend.MigrationStrategy[]getMigrationStrategy(ColumnMetaData column, String columnName, String comment, SqlType sqlType, int size, int scale, boolean nullable, Object defaultValue)Determines the best migration strategy.ModelMetaDatagetModelMetaData(DatabaseMetaData[] metaData, String[] schemas, String... tableNames)Retrieves the metadata.StringgetName()Gets the name of the backend.Set<String>getReservedWords()Gets the list of reserved words for this backend.List<DatabaseMetaDataTableHeader>getTableHeaders(DatabaseMetaData metaData)Gets all table headers for a given metadata.TableMetaDatagetTableMetaData(ModelMetaData modelMetaData, String tableName)Gets the table meta data for a given table name.booleanisArrayOperatorSupported(String operator)Returns whether the backend supports given array operator.booleanisClobSupported()Returns whether the backend support CLOB types.
If not, it will be emulated as VARCHAR with the maximum possible size.booleanisCommunicationLinkException(SQLException ex)Determines whether exception is a communication error.booleanisConstraintException(SQLException ex)Determines whether exception is a constraint violation.booleanisDefaultEqual(ColumnMetaData column, SqlType sqlType, Object defaultValue)Checks whether the column's default corresponds to the model's default value.booleanisDropIfExistsEnabled()Returns whether backend should add IF EXISTS for generated DROPs.booleanisFilteredIndexSupported()Returns whether backend supports filtered indexes.booleanisFunctionBasedIndexSupported()Returns whether function based indexes are supported.booleanisMatchingName(String name)Checks whether the backend belongs to the given name.booleanisMatchingUrl(String url)Checks whether the backend belongs to the given jdbc url.
If multiple backend implementations are provided for the same database type, only one backend should match the URL.booleanisReleaseSavepointSupported()Returns whether the backend supports releasing savepoints explicitly.booleanisReservedSchemaName(String name)Checks whether this is a reserved schema name for this backend.booleanisReservedTableName(String name)Checks whether this a reserved table name for this backend.
Applies only to tables.booleanisTemporaryName(String name)Checks whether this is a temporary name.
Returns whether an entity-, attribute- or index-name is temporary and should not be taken into account for migration or even used in the model.
Depending on the backend, temporary names start with an underscore or some other lead string.booleanisTransientTransactionException(SQLException ex)Determines whether exception is transient and retrying the transaction may succeed.
Typical transient exceptions are thrown when a deadlock is detected or when serialized transaction isolation is used.SqlType[]jdbcTypeToSqlType(int jdbcType, int size, int scale)Converts the JDBC data type integer to a tentackle backend SqlTypes.booleanneedAliasForSubselect()Returns whether backend needs an alias for a subselect.booleanneedSetLongWorkaround()Checks whether we need a workaround for the (Ingres) setLong-Bug.booleanneedTxForFetchsize()Checks for the (postgres) bug that fetchsize is ignored if not within a new transaction.StringoptimizeSql(String sql)Optimize SQL code.
Replace WHERE 1=1 AND/OR to WHERE.voidsetArray(PreparedStatement statement, int pos, SqlType type, Collection<?> elements, String operator)Sets an array parameter in a prepared statement.voidsetDropIfExistsEnabled(boolean dropIfExists)Some database support drop index/constraint/column IF EXISTS.
This may be useful if migration scripts are generated against non-production databases that contain test indexes etc...
Notice, that those temporary DDL changes should better use the temporary prefix (seeisTemporaryName(String)in order to be ignored by the migrator.intsetLeadingSelectParameters(BackendPreparedStatement stmt, int limit, int offset)Sets optional parameters before applying the original parameters.intsetTrailingSelectParameters(BackendPreparedStatement stmt, int index, int limit, int offset)Sets optional parameters after applying the original parameters.StringsqlAddColumn(String tableName, String columnName, String comment, SqlType sqlType, int size, int scale, boolean nullable, Object defaultValue)Generates sql code to add a column.StringsqlAlterColumnComment(String tableName, String columnName, String comment)Generates SQL code to alter the comment for an attribute.StringsqlAlterColumnDefault(String tableName, String columnName, SqlType sqlType, Object defaultValue)Generates sql code to change the default value a column.StringsqlAlterColumnNullConstraint(String tableName, String columnName, boolean nullable)Generates sql code to change the null-constraint of a column.StringsqlAlterColumnType(String tableName, String columnName, String comment, SqlType sqlType, int size, int scale, boolean nullable, Object defaultValue)Generates sql code to change the datatype of a column.StringsqlAlterTableComment(String tableName, String comment)Generates SQL code to alter the comment for a table.StringsqlAsBeforeTableAlias()Gets the string before the table alias.
In most databases the " AS " is optional and some don't accept it all.StringsqlComment(String text)Create comment sql code that is interpreted as a comment by the backend.StringsqlCreateColumn(String columnName, String comment, SqlType sqlType, int size, int scale, boolean nullable, Object defaultValue, boolean primaryKey, boolean withTrailingComma)Generates the attribute definition of a CREATE TABLE statement.StringsqlCreateColumnComment(String tableName, String columnName, String comment)Generates SQL code to create the comment for an attribute.StringsqlCreateForeignKey(String referencingTableName, String referencingColumnName, String referencedTableName, String referencedColumnName, String foreignKeyName, boolean composite)Generates the ALTER TABLE statement to add a foreign key constraint.StringsqlCreateIndex(String tableName, String indexName, boolean unique, String filterCondition, String... columnNames)Generates the CREATE INDEX statement.StringsqlCreateTableClosing(String tableName, String comment)Generates the last line of a CREATE TABLE statement.StringsqlCreateTableComment(String tableName, String comment)Generates SQL code to create the comment for a table.StringsqlCreateTableIntro(String tableName, String comment)Generates the first line of a CREATE TABLE statement.StringsqlDropColumn(String tableName, String columnName)Generates sql code to drop a column.StringsqlDropForeignKey(String referencingTableName, String foreignKeyName)Generates the sql statement to drop a foreign key constraint.StringsqlDropIndex(String schemaName, String tableNameWithoutSchema, String indexName)Generates the DROP INDEX statement.StringsqlDropTable(String schemaName, String tableNameWithoutSchema)Generates the DROP TABLE statement.StringsqlFunction(String functionName, String expression)Creates an sql function expression.StringsqlJoin(JoinType type, String joinedTableName, String joinedTableAlias, String join)Creates a join clause.voidsqlJoinSelects(JoinType type, boolean addColumns, StringBuilder select, String joinSelect, String joinSelectIdAlias, String joinAlias, String join)Generate SQL code for joining a table to an existing select.StringsqlJoinSelects(JoinType type, boolean addColumns, String select, String joinSelect, String joinSelectIdAlias, String joinAlias, String join)Generate SQL code for joining a table to an existing select.StringsqlNextFromSequene(String name)Creates the SQL string to retrieve the next id from a sequence.StringsqlRenameAndAlterColumnType(String tableName, String oldColumnName, String newColumnName, String comment, SqlType sqlType, int size, int scale, boolean nullable, Object defaultValue)Generates sql code to rename a column.StringsqlRenameColumn(String tableName, String oldColumnName, String newColumnName)Generates sql code to rename a column.StringsqlRenameIndex(String tableName, String oldIndexName, String newIndexName)Generates sql code to rename an index.StringsqlRenameTable(String tableName, String newTableName)Generate sql code to rename a table.booleansqlRequiresExtraCommit()According to the JDBC-specsConnection.setAutoCommit(boolean)(true) should commit, but some backends require an extraConnection.commit().booleansqlResultSetIsClosedSupported()Determines whether the jdbc driver supportsResultSet.isClosed().StringsqlTypeToString(SqlType sqlType, int size)Converts the java SQL-type to the database type name.StringsqlUpdateToNotNull(String tableName, String columnName, SqlType sqlType, Object defaultValue)Generates code to update a column to a non-null value.booleansupportsSequences()True if backend provides transactionless sequences.StringtoInternalType(String sqlTypeName)Converts a backend specific type name to an internal name used by driver specific methods.StringtoQuotedString(String str)Converts a string to a string enclosed in single quotes.StringvalueToLiteral(SqlType sqlType, Object value)Conberts a type and value to a literal string.
-
-
-
Field Detail
-
SQL_WHERE
static final String SQL_WHERE
WHERE string.- See Also:
- Constant Field Values
-
SQL_SELECT
static final String SQL_SELECT
SELECT string.- See Also:
- Constant Field Values
-
SQL_SELECT_ALL_FROM
static final String SQL_SELECT_ALL_FROM
SELECT * FROM string.- See Also:
- Constant Field Values
-
SQL_DELETE
static final String SQL_DELETE
DELETE string.- See Also:
- Constant Field Values
-
SQL_UPDATE
static final String SQL_UPDATE
UPDATE string.- See Also:
- Constant Field Values
-
SQL_INSERT_INTO
static final String SQL_INSERT_INTO
INSERT INTO string.- See Also:
- Constant Field Values
-
SQL_LEFT_PARENTHESIS
static final String SQL_LEFT_PARENTHESIS
opening bracket.- See Also:
- Constant Field Values
-
SQL_RIGHT_PARENTHESIS
static final String SQL_RIGHT_PARENTHESIS
closing bracket.- See Also:
- Constant Field Values
-
SQL_INSERT_VALUES
static final String SQL_INSERT_VALUES
(VALUES) string for insert.- See Also:
- Constant Field Values
-
SQL_SET
static final String SQL_SET
SET string.- See Also:
- Constant Field Values
-
SQL_FROM
static final String SQL_FROM
FROM string.- See Also:
- Constant Field Values
-
SQL_AND
static final String SQL_AND
AND string.- See Also:
- Constant Field Values
-
SQL_OR
static final String SQL_OR
OR string.- See Also:
- Constant Field Values
-
SQL_NOT
static final String SQL_NOT
NOT string.- See Also:
- Constant Field Values
-
SQL_ANDNOT
static final String SQL_ANDNOT
AND NOT string.- See Also:
- Constant Field Values
-
SQL_ORNOT
static final String SQL_ORNOT
OR NOT string.- See Also:
- Constant Field Values
-
SQL_COMMA
static final String SQL_COMMA
comma separator.- See Also:
- Constant Field Values
-
SQL_PAR
static final String SQL_PAR
? string.- See Also:
- Constant Field Values
-
SQL_ARRAY_PAR
static final String SQL_ARRAY_PAR
Array parameter (?).- See Also:
- Constant Field Values
-
SQL_EQUAL
static final String SQL_EQUAL
= operator string.- See Also:
- Constant Field Values
-
SQL_NOTEQUAL
static final String SQL_NOTEQUAL
<> operator string.- See Also:
- Constant Field Values
-
SQL_LESS
static final String SQL_LESS
< operator string.- See Also:
- Constant Field Values
-
SQL_GREATER
static final String SQL_GREATER
> operator string.- See Also:
- Constant Field Values
-
SQL_LESSOREQUAL
static final String SQL_LESSOREQUAL
<= operator string.- See Also:
- Constant Field Values
-
SQL_GREATEROREQUAL
static final String SQL_GREATEROREQUAL
>= operator string.- See Also:
- Constant Field Values
-
SQL_LIKE
static final String SQL_LIKE
LIKE operator string.- See Also:
- Constant Field Values
-
SQL_NOTLIKE
static final String SQL_NOTLIKE
NOT LIKE operator string.- See Also:
- Constant Field Values
-
SQL_ARRAY_IN
static final String SQL_ARRAY_IN
IN array operator string.- See Also:
- Constant Field Values
-
SQL_ARRAY_NOT_IN
static final String SQL_ARRAY_NOT_IN
NOT IN array operator string.- See Also:
- Constant Field Values
-
SQL_ARRAY_ANY
static final String SQL_ARRAY_ANY
ANY array operator string.- See Also:
- Constant Field Values
-
SQL_ARRAY_ALL
static final String SQL_ARRAY_ALL
ALL array operator string.- See Also:
- Constant Field Values
-
SQL_EQUAL_ZERO
static final String SQL_EQUAL_ZERO
=0 string.- See Also:
- Constant Field Values
-
SQL_NOTEQUAL_ZERO
static final String SQL_NOTEQUAL_ZERO
<>0 string.- See Also:
- Constant Field Values
-
SQL_COMMA_PAR
static final String SQL_COMMA_PAR
Parameter ,? string.- See Also:
- Constant Field Values
-
SQL_PAR_COMMA
static final String SQL_PAR_COMMA
Parameter ?, string.- See Also:
- Constant Field Values
-
SQL_EQUAL_PAR
static final String SQL_EQUAL_PAR
Parameter =? string.- See Also:
- Constant Field Values
-
SQL_EQUAL_PAR_COMMA
static final String SQL_EQUAL_PAR_COMMA
Parameter =?, string.- See Also:
- Constant Field Values
-
SQL_NOTEQUAL_PAR
static final String SQL_NOTEQUAL_PAR
Parameter <>? string.- See Also:
- Constant Field Values
-
SQL_GREATER_PAR
static final String SQL_GREATER_PAR
Parameter >? string.- See Also:
- Constant Field Values
-
SQL_GREATEROREQUAL_PAR
static final String SQL_GREATEROREQUAL_PAR
Parameter ≥? string.- See Also:
- Constant Field Values
-
SQL_LESS_PAR
static final String SQL_LESS_PAR
Parameter <? string.- See Also:
- Constant Field Values
-
SQL_LESSOREQUAL_PAR
static final String SQL_LESSOREQUAL_PAR
Parameter <=? string.- See Also:
- Constant Field Values
-
SQL_LIKE_PAR
static final String SQL_LIKE_PAR
Parameter LIKE ? string.- See Also:
- Constant Field Values
-
SQL_NOTLIKE_PAR
static final String SQL_NOTLIKE_PAR
Parameter NOT LIKE ? string.- See Also:
- Constant Field Values
-
SQL_ARRAY_IN_PAR
static final String SQL_ARRAY_IN_PAR
Parameter IN array operator string.- See Also:
- Constant Field Values
-
SQL_ARRAY_NOT_IN_PAR
static final String SQL_ARRAY_NOT_IN_PAR
Parameter NOT IN array operator string.- See Also:
- Constant Field Values
-
SQL_ARRAY_ANY_PAR
static final String SQL_ARRAY_ANY_PAR
Parameter ANY array operator string.- See Also:
- Constant Field Values
-
SQL_ARRAY_ALL_PAR
static final String SQL_ARRAY_ALL_PAR
Parameter ALL array operator string.- See Also:
- Constant Field Values
-
SQL_ALLSTAR
static final String SQL_ALLSTAR
'*' all string.- See Also:
- Constant Field Values
-
SQL_PLUS_ONE
static final String SQL_PLUS_ONE
+1 string.- See Also:
- Constant Field Values
-
SQL_WHEREALL
static final String SQL_WHEREALL
"WHERE 1=1" allows AND to be added.- See Also:
- Constant Field Values
-
SQL_WHERENOTHING
static final String SQL_WHERENOTHING
"WHERE 1=0" to select nothing.- See Also:
- Constant Field Values
-
SQL_WHEREAND
static final String SQL_WHEREAND
SQL_WHEREALL + AND will be replaced by... SQL_WHERE.- See Also:
- Constant Field Values
-
SQL_WHEREOR
static final String SQL_WHEREOR
SQL_WHEREALL + OR will be replaced by... SQL_WHERE.- See Also:
- Constant Field Values
-
SQL_ISNULL
static final String SQL_ISNULL
IS NULL string.- See Also:
- Constant Field Values
-
SQL_ISNOTNULL
static final String SQL_ISNOTNULL
IS NOT NULL string.- See Also:
- Constant Field Values
-
SQL_ORDERBY
static final String SQL_ORDERBY
ORDER BY string.- See Also:
- Constant Field Values
-
SQL_SORTASC
static final String SQL_SORTASC
sort ASC string.- See Also:
- Constant Field Values
-
SQL_SORTDESC
static final String SQL_SORTDESC
sort DESC string.- See Also:
- Constant Field Values
-
SQL_GROUPBY
static final String SQL_GROUPBY
GROUP BY string.- See Also:
- Constant Field Values
-
SQL_MAX
static final String SQL_MAX
MAX function name.- See Also:
- Constant Field Values
-
SQL_MIN
static final String SQL_MIN
MAX function name.- See Also:
- Constant Field Values
-
SQL_EXISTS
static final String SQL_EXISTS
SQL EXISTS clause with SELECT 1 and opening left parenthesis.- See Also:
- Constant Field Values
-
SQL_ALTER_TABLE
static final String SQL_ALTER_TABLE
SQL ALTER TABLE intro.- See Also:
- Constant Field Values
-
SQL_ALTER_INDEX
static final String SQL_ALTER_INDEX
SQL ALTER INDEX intro.- See Also:
- Constant Field Values
-
SQL_ALTER_COLUMN
static final String SQL_ALTER_COLUMN
SQL ALTER COLUMN fragment.- See Also:
- Constant Field Values
-
SQL_IF_EXISTS
static final String SQL_IF_EXISTS
SQL IF EXISTS fragment.- See Also:
- Constant Field Values
-
-
Method Detail
-
isMatchingUrl
boolean isMatchingUrl(String url)
Checks whether the backend belongs to the given jdbc url.
If multiple backend implementations are provided for the same database type, only one backend should match the URL. The others must be selected via name.- Parameters:
url- the jdbc url- Returns:
- true if matches
-
isMatchingName
boolean isMatchingName(String name)
Checks whether the backend belongs to the given name.- Parameters:
name- the backend's name- Returns:
- true if matches
-
getName
String getName()
Gets the name of the backend.- Returns:
- the name
-
getDriverClassName
String getDriverClassName()
Gets the JDBC driver class name.- Returns:
- the class name
-
createConnection
Connection createConnection(String url, String username, char[] password) throws SQLException
Creates a jdbc connection.- Parameters:
url- the jdbc urlusername- the usernamepassword- the password- Returns:
- the created connection
- Throws:
SQLException- if connection could not be established
-
getMetaData
DatabaseMetaData[] getMetaData(BackendInfo backendInfo) throws SQLException
Gets the metadata from the backend.Important: the connection of the metadata is open!
- Parameters:
backendInfo- the backend info- Returns:
- the metadata, may be more than one if schemas set in backendInfo
- Throws:
SQLException- if failed
-
getTableHeaders
List<DatabaseMetaDataTableHeader> getTableHeaders(DatabaseMetaData metaData) throws SQLException
Gets all table headers for a given metadata.- Parameters:
metaData- the database metadata- Returns:
- the list of table headers
- Throws:
SQLException- if failed
-
isTemporaryName
boolean isTemporaryName(String name)
Checks whether this is a temporary name.
Returns whether an entity-, attribute- or index-name is temporary and should not be taken into account for migration or even used in the model.
Depending on the backend, temporary names start with an underscore or some other lead string.- Parameters:
name- the name- Returns:
- true temporary (invalid) name
-
isReservedTableName
boolean isReservedTableName(String name)
Checks whether this a reserved table name for this backend.
Applies only to tables. Not to be mixed up withgetReservedWords().
Useful to avoid conflicts with naming conventions for backend specific things, such as snapshots.- Parameters:
name- the table name- Returns:
- true if reserved by backend
-
isReservedSchemaName
boolean isReservedSchemaName(String name)
Checks whether this is a reserved schema name for this backend.- Parameters:
name- the schema name- Returns:
- true if reserved by backend
-
buildSelectSql
void buildSelectSql(StringBuilder sqlBuilder, boolean writeLock, int limit, int offset)
Creates a select statement string from an inner sql string.sqlBuilderis anything asFROM... WHERE... ORDER BY....
It is decorated with a leadingSELECTplus optional clauses like for the given parameters.- Parameters:
sqlBuilder- the sql builder, initially containing the inner sql without leadingSELECT.writeLock- true select should write locklimit- the limit value, ≤ 0 if no limit clauseoffset- the offset value, ≤ 0 if no offset clause
-
buildSelectSql
String buildSelectSql(String sql, boolean writeLock, int limit, int offset)
Creates a select statement string from an inner sql string.sqlBuilderis anything asFROM... WHERE... ORDER BY....
It is decorated with a leadingSELECTplus optional clauses like for the given parameters.- Parameters:
sql- the sql without leadingSELECT.writeLock- true select should write locklimit- the limit value, ≤ 0 if no limit clauseoffset- the offset value, ≤ 0 if no offset clause- Returns:
- the select statement
-
setLeadingSelectParameters
int setLeadingSelectParameters(BackendPreparedStatement stmt, int limit, int offset)
Sets optional parameters before applying the original parameters.- Parameters:
stmt- the prepared statementlimit- the limit value, ≤ 0 if no limit clauseoffset- the offset value, ≤ 0 if no offset clause- Returns:
- the next parameter index
-
setTrailingSelectParameters
int setTrailingSelectParameters(BackendPreparedStatement stmt, int index, int limit, int offset)
Sets optional parameters after applying the original parameters.- Parameters:
stmt- the prepared statementindex- the first parameter indexlimit- the limit value, ≤ 0 if no limit clauseoffset- the offset value, ≤ 0 if no offset clause- Returns:
- the next parameter index
-
getEmptyString
String getEmptyString()
Gets the empty string.Some dbms (most famous: Oracle) handle empty strings as null. In such cases the empty string (e.g. Oracle) may consist of a single blank or whatever.
- Returns:
- the empty string (never null)
-
sqlRequiresExtraCommit
boolean sqlRequiresExtraCommit()
According to the JDBC-specsConnection.setAutoCommit(boolean)(true) should commit, but some backends require an extraConnection.commit().- Returns:
- true if the database needs an extra commit
-
sqlResultSetIsClosedSupported
boolean sqlResultSetIsClosedSupported()
Determines whether the jdbc driver supportsResultSet.isClosed().- Returns:
- true if the driver supports it
-
getCoalesceKeyword
String getCoalesceKeyword()
Gets the keyword for the COALESCE function.- Returns:
- the keyword
-
allowsExpressionsReferringToTablesBeingUpdated
boolean allowsExpressionsReferringToTablesBeingUpdated()
Determines whether backend allows expressions referring to tables being updated.- Returns:
- true if allowed
-
supportsSequences
boolean supportsSequences()
True if backend provides transactionless sequences.- Returns:
- true if database supports sequences
-
sqlAsBeforeTableAlias
String sqlAsBeforeTableAlias()
Gets the string before the table alias.
In most databases the " AS " is optional and some don't accept it all.- Returns:
- the as-string
-
needAliasForSubselect
boolean needAliasForSubselect()
Returns whether backend needs an alias for a subselect.- Returns:
- true if subselect needs an alias.
-
sqlNextFromSequene
String sqlNextFromSequene(String name)
Creates the SQL string to retrieve the next id from a sequence.- Parameters:
name- the name of the sequence- Returns:
- the SQL code
-
sqlComment
String sqlComment(String text)
Create comment sql code that is interpreted as a comment by the backend.- Parameters:
text- any text, single or multiline- Returns:
- the comment, null if text was null
-
sqlJoin
String sqlJoin(JoinType type, String joinedTableName, String joinedTableAlias, String join)
Creates a join clause.- Parameters:
type- the join typejoinedTableName- the joined tablenamejoinedTableAlias- the joined table alias, null if nonejoin- the join expression- Returns:
- the sql code
-
sqlFunction
String sqlFunction(String functionName, String expression)
Creates an sql function expression.Example:
sqlFunction("max", CN_ID) --> "MAX(id)"- Parameters:
functionName- the function name, will be translated to uppercaseexpression- the expression, may be null- Returns:
- the sql code
-
needSetLongWorkaround
boolean needSetLongWorkaround()
Checks whether we need a workaround for the (Ingres) setLong-Bug.- Returns:
- true if workaround needed
-
needTxForFetchsize
boolean needTxForFetchsize()
Checks for the (postgres) bug that fetchsize is ignored if not within a new transaction.- Returns:
- true if start tx
-
isConstraintException
boolean isConstraintException(SQLException ex)
Determines whether exception is a constraint violation.- Parameters:
ex- the exception- Returns:
- true if constraint violation
-
isCommunicationLinkException
boolean isCommunicationLinkException(SQLException ex)
Determines whether exception is a communication error.- Parameters:
ex- the exception- Returns:
- true if comlink down or alike
-
isTransientTransactionException
boolean isTransientTransactionException(SQLException ex)
Determines whether exception is transient and retrying the transaction may succeed.
Typical transient exceptions are thrown when a deadlock is detected or when serialized transaction isolation is used.Other transient errors are not covered by this method (e.g. connection timeouts, etc...).
- Parameters:
ex- the exception- Returns:
- true if retrying the transaction may succeed
-
getMaxSize
int getMaxSize(SqlType sqlType)
Returns the maximum size for a given SQL-type.- Parameters:
sqlType- the SQL-type- Returns:
- the maximum size, 0 if unlimited, -1 if type without size
-
getMaxScale
int getMaxScale(SqlType sqlType, int size)
Returns the maximum scale for a given SQL-type and size.- Parameters:
sqlType- the SQL-typesize- the size, 0 if unlimited- Returns:
- the maximum scale, 0 if unlimited
-
getDefaultSize
int getDefaultSize(SqlType sqlType)
Gets the default size if no size given in model.- Parameters:
sqlType- the SQL-type- Returns:
- the default size, 0 if no default size
-
getDefaultSchema
String getDefaultSchema()
Gets the default schema name.
Some backends store the objects in a default schema, if no explicit schema is given.- Returns:
- the defailt schema, null if none
-
getReservedWords
Set<String> getReservedWords()
Gets the list of reserved words for this backend.- Returns:
- the list of reserved words
-
assertValidName
void assertValidName(SqlNameType nameType, String name)
Asserts that given string is a valid name and usage.- Parameters:
nameType- the type/usage of the namename- the name- Throws:
BackendException- if invalid name
-
getBackendId
String getBackendId(Connection connection)
Determines the unique id or name used by the backend for a given connection.Useful to figure out the corresponding
ManagedConnectionfrom the backend's logfiles.- Parameters:
connection- the jdbc connection- Returns:
- the backend id, null if none
-
getModelMetaData
ModelMetaData getModelMetaData(DatabaseMetaData[] metaData, String[] schemas, String... tableNames)
Retrieves the metadata.- Parameters:
metaData- the meta data for the whole databaseschemas- the optional schemas to filter tables, null if no filtertableNames- the tables of the model- Returns:
- the meta data, null if no such table
-
createColumnMetaData
ColumnMetaData createColumnMetaData(TableMetaData tableMetaData)
Creates a column meta data instance.- Parameters:
tableMetaData- the table meta data this column belongs to- Returns:
- the meta data
-
createTableMetaData
TableMetaData createTableMetaData(ModelMetaData metaData, String tableName)
Creates a meta data instance.- Parameters:
metaData- the whole meta datatableName- the model's table name- Returns:
- the meta data
-
createIndexMetaData
IndexMetaData createIndexMetaData(TableMetaData tableMetaData)
Creates an index meta data instance.- Parameters:
tableMetaData- the table meta data this index belongs to- Returns:
- the meta data
-
createIndexColumnMetaData
IndexColumnMetaData createIndexColumnMetaData(IndexMetaData indexMetaData)
Creates an index column meta data instance.- Parameters:
indexMetaData- the meta meta data this column belongs to- Returns:
- the meta data
-
sqlTypeToString
String sqlTypeToString(SqlType sqlType, int size)
Converts the java SQL-type to the database type name.- Parameters:
sqlType- the java sql typesize- the column's size- Returns:
- the corresponding type string
-
columnTypeToString
String columnTypeToString(SqlType sqlType, int size, int scale)
Converts an sqltype, size and scale to a database type declaration.- Parameters:
sqlType- the SQL typesize- the sizescale- the scale- Returns:
- the type definition
-
columnTypeNullDefaultToString
String columnTypeNullDefaultToString(String columnName, SqlType sqlType, int size, int scale, boolean nullable, Object defaultValue)
Converts an sqltype, size, scale, nullable and default value to a database type declaration.- Parameters:
columnName- the database column namesqlType- the JDBC sql typesize- the optional sizescale- the optional scalenullable- true if NULL, else NOT NULLdefaultValue- the optional default value- Returns:
- the SQL code
-
valueToLiteral
String valueToLiteral(SqlType sqlType, Object value)
Conberts a type and value to a literal string.- Parameters:
sqlType- the sql typevalue- the value- Returns:
- the constant
-
sqlCreateTableIntro
String sqlCreateTableIntro(String tableName, String comment)
Generates the first line of a CREATE TABLE statement.- Parameters:
tableName- the tablename with optional schema separated by a dotcomment- optional comment, null if none- Returns:
- the SQL code including the opening bracket
-
sqlCreateTableClosing
String sqlCreateTableClosing(String tableName, String comment)
Generates the last line of a CREATE TABLE statement.- Parameters:
tableName- the tablename with optional schema separated by a dotcomment- optional comment, null if none- Returns:
- the SQL code including the closing bracket
-
sqlCreateTableComment
String sqlCreateTableComment(String tableName, String comment)
Generates SQL code to create the comment for a table.- Parameters:
tableName- the table namecomment- optional comment, null if none- Returns:
- the SQL code, empty string if comment is created via
sqlCreateTableIntro(java.lang.String, java.lang.String)orsqlCreateTableClosing(java.lang.String, java.lang.String)
-
sqlAlterTableComment
String sqlAlterTableComment(String tableName, String comment)
Generates SQL code to alter the comment for a table.- Parameters:
tableName- the table namecomment- optional comment, null to clear- Returns:
- the SQL code
-
sqlCreateColumn
String sqlCreateColumn(String columnName, String comment, SqlType sqlType, int size, int scale, boolean nullable, Object defaultValue, boolean primaryKey, boolean withTrailingComma)
Generates the attribute definition of a CREATE TABLE statement.- Parameters:
columnName- the database column namecomment- optional comment, null if nonesqlType- the JDBC sql typesize- the optional sizescale- the optional scalenullable- true if NULL, else NOT NULLdefaultValue- the optional default valueprimaryKey- true if this is a primary keywithTrailingComma- true if append a comma- Returns:
- the SQL code
-
isDefaultEqual
boolean isDefaultEqual(ColumnMetaData column, SqlType sqlType, Object defaultValue)
Checks whether the column's default corresponds to the model's default value.- Parameters:
column- the column to inspectsqlType- the sql typedefaultValue- the model's default value- Returns:
- true if same
-
getMigrationStrategy
MigrationStrategy[] getMigrationStrategy(ColumnMetaData column, String columnName, String comment, SqlType sqlType, int size, int scale, boolean nullable, Object defaultValue)
Determines the best migration strategy.- Parameters:
column- old column meta datacolumnName- new column namecomment- new commentsqlType- new sql typesize- new sizescale- new scalenullable- new nullabledefaultValue- new default- Returns:
- the strategies in order to achieve the migration
-
sqlRenameTable
String sqlRenameTable(String tableName, String newTableName)
Generate sql code to rename a table.- Parameters:
tableName- the old tablename (with leading schema, if any)newTableName- the new tablename (without schema)- Returns:
- the SQL code
-
sqlRenameColumn
String sqlRenameColumn(String tableName, String oldColumnName, String newColumnName)
Generates sql code to rename a column.- Parameters:
tableName- the tablenameoldColumnName- the old column namenewColumnName- the new column name- Returns:
- the SQL code, null if need full spec sqlRenameAndAlterColumnType below
-
sqlRenameIndex
String sqlRenameIndex(String tableName, String oldIndexName, String newIndexName)
Generates sql code to rename an index.- Parameters:
tableName- the tablenameoldIndexName- the old column namenewIndexName- the new column name- Returns:
- the SQL code, null if not supported by the backend and we need to drop and create
-
sqlRenameAndAlterColumnType
String sqlRenameAndAlterColumnType(String tableName, String oldColumnName, String newColumnName, String comment, SqlType sqlType, int size, int scale, boolean nullable, Object defaultValue)
Generates sql code to rename a column.- Parameters:
tableName- the tablenameoldColumnName- the old column namenewColumnName- the new column namecomment- optional comment, null if nonesqlType- the JDBC sql typesize- the optional sizescale- the optional scalenullable- true if NULL, else NOT NULLdefaultValue- the optional default value- Returns:
- the SQL code
-
sqlAddColumn
String sqlAddColumn(String tableName, String columnName, String comment, SqlType sqlType, int size, int scale, boolean nullable, Object defaultValue)
Generates sql code to add a column.- Parameters:
tableName- the tablenamecolumnName- the new column namecomment- optional comment, null if nonesqlType- the JDBC sql typesize- the optional sizescale- the optional scalenullable- true if NULL, else NOT NULLdefaultValue- the optional default value- Returns:
- the SQL code
-
sqlDropColumn
String sqlDropColumn(String tableName, String columnName)
Generates sql code to drop a column.- Parameters:
tableName- the tablenamecolumnName- the new column name- Returns:
- the SQL code
-
sqlAlterColumnType
String sqlAlterColumnType(String tableName, String columnName, String comment, SqlType sqlType, int size, int scale, boolean nullable, Object defaultValue)
Generates sql code to change the datatype of a column.- Parameters:
tableName- the tablenamecolumnName- the new column namecomment- optional comment, null if nonesqlType- the JDBC sql typesize- the optional sizescale- the optional scalenullable- true if NULL, else NOT NULLdefaultValue- the optional default value- Returns:
- the SQL code
-
sqlUpdateToNotNull
String sqlUpdateToNotNull(String tableName, String columnName, SqlType sqlType, Object defaultValue)
Generates code to update a column to a non-null value.- Parameters:
tableName- the tablenamecolumnName- the column namesqlType- the sql typedefaultValue- the optional default value if defined in the model- Returns:
- the SQL code
-
sqlAlterColumnNullConstraint
String sqlAlterColumnNullConstraint(String tableName, String columnName, boolean nullable)
Generates sql code to change the null-constraint of a column.- Parameters:
tableName- the tablenamecolumnName- the new column namenullable- true if NULL, else NOT NULL- Returns:
- the SQL code, null if need
sqlAlterColumnType(java.lang.String, java.lang.String, java.lang.String, org.tentackle.sql.SqlType, int, int, boolean, java.lang.Object)
-
sqlAlterColumnDefault
String sqlAlterColumnDefault(String tableName, String columnName, SqlType sqlType, Object defaultValue)
Generates sql code to change the default value a column.- Parameters:
tableName- the tablenamecolumnName- the new column namesqlType- the JDBC sql typedefaultValue- the optional default value- Returns:
- the SQL code, null if need
sqlAlterColumnType(java.lang.String, java.lang.String, java.lang.String, org.tentackle.sql.SqlType, int, int, boolean, java.lang.Object)
-
sqlCreateColumnComment
String sqlCreateColumnComment(String tableName, String columnName, String comment)
Generates SQL code to create the comment for an attribute.- Parameters:
tableName- the table namecolumnName- the column namecomment- optional comment, null if none- Returns:
- the SQL code, empty string if comment is created via
sqlCreateColumn(java.lang.String, java.lang.String, org.tentackle.sql.SqlType, int, int, boolean, java.lang.Object, boolean, boolean)
-
sqlCreateIndex
String sqlCreateIndex(String tableName, String indexName, boolean unique, String filterCondition, String... columnNames)
Generates the CREATE INDEX statement.- Parameters:
tableName- the table nameindexName- the name of the indexunique- true if index is uniquefilterCondition- the filter condition (null if none)columnNames- the column names (with a leading '-' if descending). Simple function-based indexes are also supported.- Returns:
- the SQL code
-
sqlDropIndex
String sqlDropIndex(String schemaName, String tableNameWithoutSchema, String indexName)
Generates the DROP INDEX statement.- Parameters:
schemaName- the optional schema, null if nonetableNameWithoutSchema- the table name without the schemaindexName- the name of the index- Returns:
- the SQL code
-
sqlDropTable
String sqlDropTable(String schemaName, String tableNameWithoutSchema)
Generates the DROP TABLE statement.- Parameters:
schemaName- the optional schema, null if nonetableNameWithoutSchema- the table name without the schema- Returns:
- the SQL code
-
sqlCreateForeignKey
String sqlCreateForeignKey(String referencingTableName, String referencingColumnName, String referencedTableName, String referencedColumnName, String foreignKeyName, boolean composite)
Generates the ALTER TABLE statement to add a foreign key constraint.- Parameters:
referencingTableName- the referencing foreign table namereferencingColumnName- the referencing foreign column namereferencedTableName- the referenced primary table namereferencedColumnName- the referenced primary column nameforeignKeyName- the name of the foreign keycomposite- true if referencing table is a component of the referenced table- Returns:
- the SQL code
-
sqlDropForeignKey
String sqlDropForeignKey(String referencingTableName, String foreignKeyName)
Generates the sql statement to drop a foreign key constraint.- Parameters:
referencingTableName- the referencing foreign table nameforeignKeyName- the name of the foreign key- Returns:
- the SQL code
-
sqlAlterColumnComment
String sqlAlterColumnComment(String tableName, String columnName, String comment)
Generates SQL code to alter the comment for an attribute.- Parameters:
tableName- the table namecolumnName- the column namecomment- optional comment, null to clear- Returns:
- the SQL code, null if use
sqlRenameAndAlterColumnType(java.lang.String, java.lang.String, java.lang.String, java.lang.String, org.tentackle.sql.SqlType, int, int, boolean, java.lang.Object)instead
-
sqlJoinSelects
void sqlJoinSelects(JoinType type, boolean addColumns, StringBuilder select, String joinSelect, String joinSelectIdAlias, String joinAlias, String join)
Generate SQL code for joining a table to an existing select.- Parameters:
type- the join typeaddColumns- true if columns of joined table should be added to the result setselect- the original select statementjoinSelect- the select to join with the original sql or just a tablenamejoinSelectIdAlias- extra id alias to add to the joined select, null if nonejoinAlias- the alias of the joined selectjoin- the join clause
-
sqlJoinSelects
String sqlJoinSelects(JoinType type, boolean addColumns, String select, String joinSelect, String joinSelectIdAlias, String joinAlias, String join)
Generate SQL code for joining a table to an existing select.- Parameters:
type- the join typeaddColumns- true if columns of joined table should be added to the result setselect- the original select statementjoinSelect- the select to join with the original sql or just a tablenamejoinSelectIdAlias- extra id alias to add to the joined select, null if nonejoinAlias- the alias of the joined selectjoin- the join clause- Returns:
- the select statement
-
jdbcTypeToSqlType
SqlType[] jdbcTypeToSqlType(int jdbcType, int size, int scale)
Converts the JDBC data type integer to a tentackle backend SqlTypes.There may be more than one sqltype returned!
- Parameters:
jdbcType- the jdbc data typesize- the column sizescale- the column's scale- Returns:
- the SqlTypes, empty array if no mapping available or no exact match (not generated by TT)
- See Also:
Types
-
getTableMetaData
TableMetaData getTableMetaData(ModelMetaData modelMetaData, String tableName)
Gets the table meta data for a given table name.- Parameters:
modelMetaData- the model the table belongs totableName- the model's table name- Returns:
- the table data
-
toQuotedString
String toQuotedString(String str)
Converts a string to a string enclosed in single quotes.- Parameters:
str- the string- Returns:
- the quoted string
-
optimizeSql
String optimizeSql(String sql)
Optimize SQL code.
Replace WHERE 1=1 AND/OR to WHERE. Any remaining WHERE 1=1 will be removed too. If you don't want your statements getting optimized, use lowercase for those keywords.- Parameters:
sql- the original sql code- Returns:
- the optimized sql code
-
isReleaseSavepointSupported
boolean isReleaseSavepointSupported()
Returns whether the backend supports releasing savepoints explicitly. If not, the savepoints are released when the transaction finishs.- Returns:
- true if release is supported, else false
-
isClobSupported
boolean isClobSupported()
Returns whether the backend support CLOB types.
If not, it will be emulated as VARCHAR with the maximum possible size.- Returns:
- true if supported, false if treat as varchar
-
isFunctionBasedIndexSupported
boolean isFunctionBasedIndexSupported()
Returns whether function based indexes are supported.- Returns:
- true if supported
-
isFilteredIndexSupported
boolean isFilteredIndexSupported()
Returns whether backend supports filtered indexes.- Returns:
- true if supported, false if model will raise an error if filtered index is used
-
setDropIfExistsEnabled
void setDropIfExistsEnabled(boolean dropIfExists)
Some database support drop index/constraint/column IF EXISTS.
This may be useful if migration scripts are generated against non-production databases that contain test indexes etc...
Notice, that those temporary DDL changes should better use the temporary prefix (seeisTemporaryName(String)in order to be ignored by the migrator.- Parameters:
dropIfExists- true if use IF EXISTS if the backend supports it
-
isDropIfExistsEnabled
boolean isDropIfExistsEnabled()
Returns whether backend should add IF EXISTS for generated DROPs.- Returns:
- true if IF EXISTS is turned on and supported, false if not supported or turned off
-
toInternalType
String toInternalType(String sqlTypeName)
Converts a backend specific type name to an internal name used by driver specific methods.- Parameters:
sqlTypeName- the original type used in SQL scripts- Returns:
- the JDBC internal name
-
isArrayOperatorSupported
boolean isArrayOperatorSupported(String operator)
Returns whether the backend supports given array operator.- Parameters:
operator- the array operator- Returns:
- true if supported
-
setArray
void setArray(PreparedStatement statement, int pos, SqlType type, Collection<?> elements, String operator) throws SQLException
Sets an array parameter in a prepared statement.- Parameters:
statement- the prepared statementpos- the parameter position (starting at 1)type- the element's SQL typeelements- the elements to be converted to an array parameteroperator- the array operator- Throws:
SQLException
-
-