Class AbstractBackend

java.lang.Object
org.tentackle.sql.backends.AbstractBackend
All Implemented Interfaces:
Backend
Direct Known Subclasses:
AbstractSql92Backend

public abstract class AbstractBackend extends Object implements Backend
Abstract parent class for backends.
Author:
harald
  • Field Details

  • Constructor Details

    • AbstractBackend

      public AbstractBackend()
      Parent constructor.
  • Method Details

    • isMatchingName

      public boolean isMatchingName(String name)
      Description copied from interface: Backend
      Checks whether the backend belongs to the given name.
      Specified by:
      isMatchingName in interface Backend
      Parameters:
      name - the backend's name
      Returns:
      true if matches
    • validateVersion

      public void validateVersion(int databaseMajorVersion, int databaseMinorVersion)
      Description copied from interface: Backend
      Verifies that the major and minor version of the connection's metadata is compatible with this backend.
      If the latest backend is not compatible, the URL may be post-fixed with a backend type for an older version, for example:
         jdbc:oracle:thin:@//localhost:1521/xe|Oracle8
       

      Throws BackendException if incompatible.

      Specified by:
      validateVersion in interface Backend
      Parameters:
      databaseMajorVersion - the major version
      databaseMinorVersion - the minor version
      See Also:
    • isDeprecated

      public boolean isDeprecated()
      Description copied from interface: Backend
      Returns whether this backend is deprecated and superseded by a newer implementation.
      Deprecated backends are omitted from BackendFactory.getAllBackends().
      Specified by:
      isDeprecated in interface Backend
      Returns:
      true if deprecated
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • createConnection

      public Connection createConnection(String url, String username, char[] password) throws SQLException
      Description copied from interface: Backend
      Creates a jdbc connection.
      Specified by:
      createConnection in interface Backend
      Parameters:
      url - the jdbc url
      username - the username
      password - the password
      Returns:
      the created connection
      Throws:
      SQLException - if connection could not be established
    • getMetaData

      public DatabaseMetaData[] getMetaData(BackendInfo backendInfo) throws SQLException
      Description copied from interface: Backend
      Gets the metadata from the backend.

      Important: the connection of the metadata is open!

      Specified by:
      getMetaData in interface Backend
      Parameters:
      backendInfo - the backend info
      Returns:
      the metadata, may be more than one if schemas set in backendInfo
      Throws:
      SQLException - if failed
    • getTableHeaders

      public List<DatabaseMetaDataTableHeader> getTableHeaders(DatabaseMetaData metaData) throws SQLException
      Description copied from interface: Backend
      Gets all table headers for a given metadata.
      Specified by:
      getTableHeaders in interface Backend
      Parameters:
      metaData - the database metadata
      Returns:
      the list of table headers
      Throws:
      SQLException - if failed
    • isTemporaryName

      public boolean isTemporaryName(String name)
      Description copied from interface: Backend
      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.
      Specified by:
      isTemporaryName in interface Backend
      Parameters:
      name - the name
      Returns:
      true temporary (invalid) name
    • isReservedWord

      public boolean isReservedWord(String word)
      Checks whether given string is a reserved word.
      Parameters:
      word - the word
      Returns:
      true if reserved word
    • isReservedTableName

      public boolean isReservedTableName(String name)
      Description copied from interface: Backend
      Checks whether this a reserved table name for this backend.
      Applies only to tables. Not to be mixed up with Backend.getReservedWords().
      Useful to avoid conflicts with naming conventions for backend specific things, such as snapshots.
      Specified by:
      isReservedTableName in interface Backend
      Parameters:
      name - the table name
      Returns:
      true if reserved by backend
    • isReservedSchemaName

      public boolean isReservedSchemaName(String name)
      Description copied from interface: Backend
      Checks whether this is a reserved schema name for this backend.
      Specified by:
      isReservedSchemaName in interface Backend
      Parameters:
      name - the schema name
      Returns:
      true if reserved by backend
    • getEmptyString

      public String getEmptyString()
      Description copied from interface: Backend
      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.
      Specified by:
      getEmptyString in interface Backend
      Returns:
      the empty string (never null)
    • getDummySelect

      public String getDummySelect()
      Description copied from interface: Backend
      Gets the dummy select to keep/test connections alive.
      Most databases just require a "SELECT 1".
      Specified by:
      getDummySelect in interface Backend
      Returns:
      the select string
    • sqlAsBeforeTableAlias

      public String sqlAsBeforeTableAlias()
      Description copied from interface: Backend
      Gets the string before the table alias.
      In most databases the " AS " is optional and some don't accept it all.
      Specified by:
      sqlAsBeforeTableAlias in interface Backend
      Returns:
      the as-string
    • isAliasRequiredForSubSelect

      public boolean isAliasRequiredForSubSelect()
      Description copied from interface: Backend
      Returns whether backend requires an alias for a sub-select.
      Specified by:
      isAliasRequiredForSubSelect in interface Backend
      Returns:
      true if sub-select needs an alias.
    • isExtraCommitRequired

      public boolean isExtraCommitRequired()
      Description copied from interface: Backend
      According to the JDBC-specs Connection.setAutoCommit(boolean)(true) should commit, but some backends require an extra Connection.commit().
      Specified by:
      isExtraCommitRequired in interface Backend
      Returns:
      true if the database needs an extra commit
    • sqlComment

      public String sqlComment(String text)
      Description copied from interface: Backend
      Create comment sql code that is interpreted as a comment by the backend.
      Specified by:
      sqlComment in interface Backend
      Parameters:
      text - any text, single or multiline
      Returns:
      the comment, null if text was null
    • sqlJoin

      public String sqlJoin(JoinType type, String joinedTableName, String joinedTableAlias, String join)
      Description copied from interface: Backend
      Creates a join clause.
      Specified by:
      sqlJoin in interface Backend
      Parameters:
      type - the join type
      joinedTableName - the joined tablename
      joinedTableAlias - the joined table alias, null if none
      join - the join expression
      Returns:
      the sql code
    • sqlFunction

      public String sqlFunction(String functionName, String expression)
      Description copied from interface: Backend
      Creates an sql function expression.

      Example:

        sqlFunction("max", CN_ID) --> "MAX(id)"
       
      Specified by:
      sqlFunction in interface Backend
      Parameters:
      functionName - the function name, will be translated to uppercase
      expression - the expression, may be null
      Returns:
      the sql code
    • getCoalesceKeyword

      public String getCoalesceKeyword()
      Description copied from interface: Backend
      Gets the keyword for the COALESCE function.
      Specified by:
      getCoalesceKeyword in interface Backend
      Returns:
      the keyword
    • isExpressionReferringToTableBeingUpdatedSupported

      public boolean isExpressionReferringToTableBeingUpdatedSupported()
      Description copied from interface: Backend
      Determines whether backend allows expressions referring to tables being updated.
      Specified by:
      isExpressionReferringToTableBeingUpdatedSupported in interface Backend
      Returns:
      true if allowed
    • isTxRequiredForFetchsize

      public boolean isTxRequiredForFetchsize()
      Description copied from interface: Backend
      Checks for the (postgres) bug that fetch-size is ignored if not within a new transaction.
      Specified by:
      isTxRequiredForFetchsize in interface Backend
      Returns:
      true if start tx
    • isConstraintException

      public boolean isConstraintException(SQLException ex)
      Description copied from interface: Backend
      Determines whether exception is a constraint violation.
      Specified by:
      isConstraintException in interface Backend
      Parameters:
      ex - the exception
      Returns:
      true if constraint violation
    • isCommunicationLinkException

      public boolean isCommunicationLinkException(SQLException ex)
      Description copied from interface: Backend
      Determines whether exception is a communication error.
      Specified by:
      isCommunicationLinkException in interface Backend
      Parameters:
      ex - the exception
      Returns:
      true if comlink down or alike
    • isTransientTransactionException

      public boolean isTransientTransactionException(SQLException ex)
      Description copied from interface: Backend
      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...).

      Specified by:
      isTransientTransactionException in interface Backend
      Parameters:
      ex - the exception
      Returns:
      true if retrying the transaction may succeed
    • getMaxSize

      public int getMaxSize(SqlType sqlType)
      Description copied from interface: Backend
      Returns the maximum size for a given SQL-type.
      Specified by:
      getMaxSize in interface Backend
      Parameters:
      sqlType - the SQL-type
      Returns:
      the maximum size, 0 if unlimited, -1 if type without size
    • getMaxScale

      public int getMaxScale(SqlType sqlType, int size)
      Description copied from interface: Backend
      Returns the maximum scale for a given SQL-type and size.
      Specified by:
      getMaxScale in interface Backend
      Parameters:
      sqlType - the SQL-type
      size - the size, 0 if unlimited
      Returns:
      the maximum scale, 0 if unlimited
    • getDefaultSize

      public int getDefaultSize(SqlType sqlType)
      Description copied from interface: Backend
      Gets the default size if no size given in model.
      Specified by:
      getDefaultSize in interface Backend
      Parameters:
      sqlType - the SQL-type
      Returns:
      the default size, 0 if no default size
    • getDefaultSchema

      public String getDefaultSchema()
      Description copied from interface: Backend
      Gets the default schema name.
      Some backends store the objects in a default schema, if no explicit schema is given.
      Specified by:
      getDefaultSchema in interface Backend
      Returns:
      the default schema, null if none
    • getModelMetaData

      public ModelMetaData getModelMetaData(DatabaseMetaData[] metaData, String[] schemas, String... tableNames)
      Description copied from interface: Backend
      Retrieves the metadata.
      Specified by:
      getModelMetaData in interface Backend
      Parameters:
      metaData - the metadata for the whole database
      schemas - the optional schemas to filter tables, null if no filter
      tableNames - the tables of the model
      Returns:
      the metadata, null if no such table
    • getTableMetaData

      public TableMetaData getTableMetaData(ModelMetaData modelMetaData, String tableName)
      Description copied from interface: Backend
      Gets the table metadata for a given table name.
      Specified by:
      getTableMetaData in interface Backend
      Parameters:
      modelMetaData - the model the table belongs to
      tableName - the model's table name
      Returns:
      the table data
    • createTableMetaData

      public TableMetaData createTableMetaData(ModelMetaData modelMetaData, String tableName)
      Description copied from interface: Backend
      Creates a meta data instance.
      Specified by:
      createTableMetaData in interface Backend
      Parameters:
      modelMetaData - the whole metadata
      tableName - the model's table name
      Returns:
      the meta data
    • createColumnMetaData

      public ColumnMetaData createColumnMetaData(TableMetaData tableMetaData)
      Description copied from interface: Backend
      Creates a column meta data instance.
      Specified by:
      createColumnMetaData in interface Backend
      Parameters:
      tableMetaData - the table metadata this column belongs to
      Returns:
      the meta data
    • createIndexMetaData

      public IndexMetaData createIndexMetaData(TableMetaData tableMetaData)
      Description copied from interface: Backend
      Creates an index meta data instance.
      Specified by:
      createIndexMetaData in interface Backend
      Parameters:
      tableMetaData - the table metadata this index belongs to
      Returns:
      the meta data
    • createIndexColumnMetaData

      public IndexColumnMetaData createIndexColumnMetaData(IndexMetaData indexMetaData)
      Description copied from interface: Backend
      Creates an index column meta data instance.
      Specified by:
      createIndexColumnMetaData in interface Backend
      Parameters:
      indexMetaData - the meta meta data this column belongs to
      Returns:
      the meta data
    • sqlCreateTableIntro

      public String sqlCreateTableIntro(String tableName, String comment)
      Description copied from interface: Backend
      Generates the first line of a CREATE TABLE statement.
      Specified by:
      sqlCreateTableIntro in interface Backend
      Parameters:
      tableName - the tablename with optional schema separated by a dot
      comment - optional comment, null if none
      Returns:
      the SQL code including the opening bracket
    • sqlCreateTableClosing

      public String sqlCreateTableClosing(String tableName, String comment)
      Description copied from interface: Backend
      Generates the last line of a CREATE TABLE statement.
      Specified by:
      sqlCreateTableClosing in interface Backend
      Parameters:
      tableName - the tablename with optional schema separated by a dot
      comment - optional comment, null if none
      Returns:
      the SQL code including the closing bracket
    • sqlCreateTableComment

      public String sqlCreateTableComment(String tableName, String comment)
      Description copied from interface: Backend
      Generates SQL code to create the comment for a table.
      Specified by:
      sqlCreateTableComment in interface Backend
      Parameters:
      tableName - the table name
      comment - optional comment, null if none
      Returns:
      the SQL code, empty string if comment is created via Backend.sqlCreateTableIntro(java.lang.String, java.lang.String) or Backend.sqlCreateTableClosing(java.lang.String, java.lang.String)
    • sqlAlterTableComment

      public String sqlAlterTableComment(String tableName, String comment)
      Description copied from interface: Backend
      Generates SQL code to alter the comment for a table.
      Specified by:
      sqlAlterTableComment in interface Backend
      Parameters:
      tableName - the table name
      comment - optional comment, null to clear
      Returns:
      the SQL code
    • columnTypeToString

      public String columnTypeToString(SqlType sqlType, int size, int scale)
      Description copied from interface: Backend
      Converts an sqltype, size and scale to a database type declaration.
      Specified by:
      columnTypeToString in interface Backend
      Parameters:
      sqlType - the SQL type
      size - the size
      scale - the scale
      Returns:
      the type definition
    • columnTypeNullDefaultToString

      public String columnTypeNullDefaultToString(String columnName, SqlType sqlType, int size, int scale, boolean nullable, Object defaultValue)
      Description copied from interface: Backend
      Converts an sqltype, size, scale, nullable and default value to a database type declaration.
      Specified by:
      columnTypeNullDefaultToString in interface Backend
      Parameters:
      columnName - the database column name
      sqlType - the JDBC sql type
      size - the optional size
      scale - the optional scale
      nullable - true if NULL, else NOT NULL
      defaultValue - the optional default value
      Returns:
      the SQL code
    • valueToLiteral

      public String valueToLiteral(SqlType sqlType, Object value)
      Description copied from interface: Backend
      Converts a type and value to a literal string.
      Specified by:
      valueToLiteral in interface Backend
      Parameters:
      sqlType - the sql type
      value - the value
      Returns:
      the constant
    • getStatementSeparators

      public String[] getStatementSeparators()
      Description copied from interface: Backend
      The all SQL statement separators.
      Specified by:
      getStatementSeparators in interface Backend
      Returns:
      the separators, usually ";"
    • getStatementSeparator

      public String getStatementSeparator()
      Description copied from interface: Backend
      The SQL statement separator used in generated code.
      Specified by:
      getStatementSeparator in interface Backend
      Returns:
      the separator, usually ";"
    • getSingleLineComments

      public String[] getSingleLineComments()
      Description copied from interface: Backend
      Gets all strings that begin a single line comment.
      Specified by:
      getSingleLineComments in interface Backend
      Returns:
      the single line comment strings, usually "--"
    • getSingleLineComment

      public String getSingleLineComment()
      Description copied from interface: Backend
      Gets the string to begin a single line comment in generated code.
      Specified by:
      getSingleLineComment in interface Backend
      Returns:
      the single line comment string, usually "--"
    • getBlockCommentBegins

      public String[] getBlockCommentBegins()
      Description copied from interface: Backend
      Gets all strings that begin a block comment.
      Specified by:
      getBlockCommentBegins in interface Backend
      Returns:
      the strings to begin a block comment, usually "/*"
    • getBlockCommentBegin

      public String getBlockCommentBegin()
      Description copied from interface: Backend
      Gets the string to begin a block comment in generated code.
      Specified by:
      getBlockCommentBegin in interface Backend
      Returns:
      the string to begin a block comment, usually "/*"
    • getBlockCommentEnds

      public String[] getBlockCommentEnds()
      Description copied from interface: Backend
      Gets all strings that end a block comment.
      Specified by:
      getBlockCommentEnds in interface Backend
      Returns:
      the strings to end a block comment, usually "*&#47;"
    • getBlockCommentEnd

      public String getBlockCommentEnd()
      Description copied from interface: Backend
      Gets the string to end a block comment in generated code.
      Specified by:
      getBlockCommentEnd in interface Backend
      Returns:
      the string to end a block comment, usually "*&#47;"
    • sqlCreateColumn

      public String sqlCreateColumn(String columnName, String comment, SqlType sqlType, int size, int scale, boolean nullable, Object defaultValue, boolean primaryKey, boolean withTrailingComma)
      Description copied from interface: Backend
      Generates the attribute definition of a CREATE TABLE statement.
      Specified by:
      sqlCreateColumn in interface Backend
      Parameters:
      columnName - the database column name
      comment - optional comment, null if none
      sqlType - the JDBC sql type
      size - the optional size
      scale - the optional scale
      nullable - true if NULL, else NOT NULL
      defaultValue - the optional default value
      primaryKey - true if this is a primary key
      withTrailingComma - true if append a comma
      Returns:
      the SQL code
    • isDefaultEqual

      public boolean isDefaultEqual(ColumnMetaData column, SqlType sqlType, Object defaultValue)
      Description copied from interface: Backend
      Checks whether the column's default corresponds to the model's default value.
      Specified by:
      isDefaultEqual in interface Backend
      Parameters:
      column - the column to inspect
      sqlType - the sql type
      defaultValue - the model's default value
      Returns:
      true if same
    • getMigrationStrategy

      public MigrationStrategy[] getMigrationStrategy(ColumnMetaData column, String columnName, String comment, SqlType sqlType, int size, int scale, boolean nullable, Object defaultValue)
      Description copied from interface: Backend
      Determines the best migration strategy.
      Specified by:
      getMigrationStrategy in interface Backend
      Parameters:
      column - old column meta data
      columnName - new column name
      comment - new comment
      sqlType - new sql type
      size - new size
      scale - new scale
      nullable - new nullable
      defaultValue - new default
      Returns:
      the strategies in order to achieve the migration
    • sqlRenameTable

      public String sqlRenameTable(String tableName, String newTableName)
      Description copied from interface: Backend
      Generate sql code to rename a table.
      Specified by:
      sqlRenameTable in interface Backend
      Parameters:
      tableName - the old tablename (with leading schema, if any)
      newTableName - the new tablename (without schema)
      Returns:
      the SQL code
    • sqlRenameColumn

      public String sqlRenameColumn(String tableName, String oldColumnName, String newColumnName)
      Description copied from interface: Backend
      Generates sql code to rename a column.
      Specified by:
      sqlRenameColumn in interface Backend
      Parameters:
      tableName - the tablename
      oldColumnName - the old column name
      newColumnName - the new column name
      Returns:
      the SQL code, null if full spec sqlRenameAndAlterColumnType below is necessary
    • sqlRenameIndex

      public String sqlRenameIndex(String tableName, String oldIndexName, String newIndexName)
      Description copied from interface: Backend
      Generates sql code to rename an index.
      Specified by:
      sqlRenameIndex in interface Backend
      Parameters:
      tableName - the tablename
      oldIndexName - the old column name
      newIndexName - the new column name
      Returns:
      the SQL code, null if not supported by the backend, and we need to drop and create
    • sqlRenameAndAlterColumnType

      public String sqlRenameAndAlterColumnType(String tableName, String oldColumnName, String newColumnName, String comment, SqlType sqlType, int size, int scale, boolean nullable, Object defaultValue)
      Description copied from interface: Backend
      Generates sql code to rename a column.
      Specified by:
      sqlRenameAndAlterColumnType in interface Backend
      Parameters:
      tableName - the tablename
      oldColumnName - the old column name
      newColumnName - the new column name
      comment - optional comment, null if none
      sqlType - the JDBC sql type
      size - the optional size
      scale - the optional scale
      nullable - true if NULL, else NOT NULL
      defaultValue - the optional default value
      Returns:
      the SQL code
    • sqlAddColumn

      public String sqlAddColumn(String tableName, String columnName, String comment, SqlType sqlType, int size, int scale, boolean nullable, Object defaultValue)
      Description copied from interface: Backend
      Generates sql code to add a column.
      Specified by:
      sqlAddColumn in interface Backend
      Parameters:
      tableName - the tablename
      columnName - the new column name
      comment - optional comment, null if none
      sqlType - the JDBC sql type
      size - the optional size
      scale - the optional scale
      nullable - true if NULL, else NOT NULL
      defaultValue - the optional default value
      Returns:
      the SQL code
    • sqlDropColumn

      public String sqlDropColumn(String tableName, String columnName)
      Description copied from interface: Backend
      Generates sql code to drop a column.
      Specified by:
      sqlDropColumn in interface Backend
      Parameters:
      tableName - the tablename
      columnName - the new column name
      Returns:
      the SQL code
    • sqlAlterColumnType

      public String sqlAlterColumnType(String tableName, String columnName, String comment, SqlType sqlType, int size, int scale, boolean nullable, Object defaultValue)
      Description copied from interface: Backend
      Generates sql code to change the datatype of a column.
      Specified by:
      sqlAlterColumnType in interface Backend
      Parameters:
      tableName - the tablename
      columnName - the new column name
      comment - optional comment, null if none
      sqlType - the JDBC sql type
      size - the optional size
      scale - the optional scale
      nullable - true if NULL, else NOT NULL
      defaultValue - the optional default value
      Returns:
      the SQL code
    • sqlAlterColumnNullConstraint

      public String sqlAlterColumnNullConstraint(String tableName, String columnName, boolean nullable)
      Description copied from interface: Backend
      Generates sql code to change the null-constraint of a column.
      Specified by:
      sqlAlterColumnNullConstraint in interface Backend
      Parameters:
      tableName - the tablename
      columnName - the new column name
      nullable - true if NULL, else NOT NULL
      Returns:
      the SQL code, null if need Backend.sqlAlterColumnType(java.lang.String, java.lang.String, java.lang.String, org.tentackle.sql.SqlType, int, int, boolean, java.lang.Object)
    • sqlUpdateToNotNull

      public String sqlUpdateToNotNull(String tableName, String columnName, SqlType sqlType, Object defaultValue, boolean migrated)
      Description copied from interface: Backend
      Generates code to update a column to a non-null value.
      Specified by:
      sqlUpdateToNotNull in interface Backend
      Parameters:
      tableName - the tablename
      columnName - the column name
      sqlType - the sql type
      defaultValue - the optional default value if defined in the model
      migrated - true if column is migrated, false if added
      Returns:
      the SQL code
    • sqlAlterColumnDefault

      public String sqlAlterColumnDefault(String tableName, String columnName, SqlType sqlType, Object defaultValue)
      Description copied from interface: Backend
      Generates sql code to change the default value a column.
      Specified by:
      sqlAlterColumnDefault in interface Backend
      Parameters:
      tableName - the tablename
      columnName - the new column name
      sqlType - the JDBC sql type
      defaultValue - the optional default value
      Returns:
      the SQL code, null if need Backend.sqlAlterColumnType(java.lang.String, java.lang.String, java.lang.String, org.tentackle.sql.SqlType, int, int, boolean, java.lang.Object)
    • sqlCreateColumnComment

      public String sqlCreateColumnComment(String tableName, String columnName, String comment)
      Description copied from interface: Backend
      Generates SQL code to create the comment for an attribute.
      Specified by:
      sqlCreateColumnComment in interface Backend
      Parameters:
      tableName - the table name
      columnName - the column name
      comment - optional comment, null if none
      Returns:
      the SQL code, empty string if comment is created via Backend.sqlCreateColumn(java.lang.String, java.lang.String, org.tentackle.sql.SqlType, int, int, boolean, java.lang.Object, boolean, boolean)
    • sqlAlterColumnComment

      public String sqlAlterColumnComment(String tableName, String columnName, String comment)
      Description copied from interface: Backend
      Generates SQL code to alter the comment for an attribute.
      Specified by:
      sqlAlterColumnComment in interface Backend
      Parameters:
      tableName - the table name
      columnName - the column name
      comment - optional comment, null to clear
      Returns:
      the SQL code, null if Backend.sqlRenameAndAlterColumnType(java.lang.String, java.lang.String, java.lang.String, java.lang.String, org.tentackle.sql.SqlType, int, int, boolean, java.lang.Object) must be used instead
    • sqlTypeToString

      public String sqlTypeToString(SqlType sqlType, int size)
      Description copied from interface: Backend
      Converts the java SQL-type to the database type name.
      Specified by:
      sqlTypeToString in interface Backend
      Parameters:
      sqlType - the java sql type
      size - the column's size
      Returns:
      the corresponding type string
    • sqlCreateIndex

      public String sqlCreateIndex(String tableName, String indexName, boolean unique, String filterCondition, String... columnNames)
      Description copied from interface: Backend
      Generates the CREATE INDEX statement.
      Specified by:
      sqlCreateIndex in interface Backend
      Parameters:
      tableName - the table name
      indexName - the name of the index
      unique - true if index is unique
      filterCondition - 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

      public String sqlDropIndex(String schemaName, String tableNameWithoutSchema, String indexName)
      Description copied from interface: Backend
      Generates the DROP INDEX statement.
      Specified by:
      sqlDropIndex in interface Backend
      Parameters:
      schemaName - the optional schema, null if none
      tableNameWithoutSchema - the table name without the schema
      indexName - the name of the index
      Returns:
      the SQL code
    • sqlDropTable

      public String sqlDropTable(String schemaName, String tableNameWithoutSchema)
      Description copied from interface: Backend
      Generates the DROP TABLE statement.
      Specified by:
      sqlDropTable in interface Backend
      Parameters:
      schemaName - the optional schema, null if none
      tableNameWithoutSchema - the table name without the schema
      Returns:
      the SQL code
    • sqlJoinSelects

      public void sqlJoinSelects(JoinType type, boolean addColumns, StringBuilder select, String joinSelect, String joinSelectIdAlias, String joinAlias, String join)
      Description copied from interface: Backend
      Generate SQL-code for joining a table to an existing select.
      Specified by:
      sqlJoinSelects in interface Backend
      Parameters:
      type - the join type
      addColumns - true if columns of joined table should be added to the result set
      select - the original select statement
      joinSelect - the select to join with the original sql or just a tablename
      joinSelectIdAlias - extra id alias to add to the joined select, null if none
      joinAlias - the alias of the joined select
      join - the join clause
    • assertValidName

      public void assertValidName(SqlNameType nameType, String name)
      Description copied from interface: Backend
      Asserts that given string is a valid name and usage.
      Specified by:
      assertValidName in interface Backend
      Parameters:
      nameType - the type/usage of the name
      name - the name
    • jdbcTypeToSqlType

      public SqlType[] jdbcTypeToSqlType(int jdbcType, int size, int scale)
      Description copied from interface: Backend
      Converts the JDBC data type integer to a tentackle backend SqlTypes.

      There may be more than one sqltype returned!

      Specified by:
      jdbcTypeToSqlType in interface Backend
      Parameters:
      jdbcType - the jdbc data type
      size - the column size
      scale - the column's scale
      Returns:
      the SqlTypes, empty array if no mapping available or no exact match (not generated by TT)
      See Also:
    • toQuotedString

      public String toQuotedString(String str)
      Description copied from interface: Backend
      Converts a string to a string enclosed in single quotes.
      Specified by:
      toQuotedString in interface Backend
      Parameters:
      str - the string
      Returns:
      the quoted string
    • optimizeSql

      public String optimizeSql(String sql)
      Description copied from interface: Backend
      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.
      Specified by:
      optimizeSql in interface Backend
      Parameters:
      sql - the original sql code
      Returns:
      the optimized sql code
    • buildSelectSql

      public String buildSelectSql(String sql, boolean writeLock, int limit, int offset)
      Description copied from interface: Backend
      Creates a select statement string from an inner sql string.

      Same as Backend.buildSelectSql(StringBuilder, boolean, int, int), but for strings.

      Specified by:
      buildSelectSql in interface Backend
      Parameters:
      sql - the sql without leading SELECT.
      writeLock - true select should write lock
      limit - the limit value, ≤ 0 if no limit clause
      offset - the offset value, ≤ 0 if no offset clause
      Returns:
      the select statement
    • sqlJoinSelects

      public String sqlJoinSelects(JoinType type, boolean addColumns, String select, String joinSelect, String joinSelectIdAlias, String joinAlias, String join)
      Description copied from interface: Backend
      Generate SQL-code for joining a table to an existing select.
      Specified by:
      sqlJoinSelects in interface Backend
      Parameters:
      type - the join type
      addColumns - true if columns of joined table should be added to the result set
      select - the original select statement
      joinSelect - the select to join with the original sql or just a tablename
      joinSelectIdAlias - extra id alias to add to the joined select, null if none
      joinAlias - the alias of the joined select
      join - the join clause
      Returns:
      the select statement
    • isReleaseSavepointSupported

      public boolean isReleaseSavepointSupported()
      Description copied from interface: Backend
      Returns whether the backend supports releasing savepoints explicitly. If not, the savepoints are released when the transaction finishs.
      Specified by:
      isReleaseSavepointSupported in interface Backend
      Returns:
      true if release is supported, else false
    • isClobSupported

      public boolean isClobSupported()
      Description copied from interface: Backend
      Returns whether the backend support CLOB types.
      If not, it will be emulated as VARCHAR with the maximum possible size.
      Specified by:
      isClobSupported in interface Backend
      Returns:
      true if supported, false if treat as varchar
    • isUUIDSupported

      public boolean isUUIDSupported()
      Description copied from interface: Backend
      Returns whether the backend supports the UUID natively via JDBC setObject/getObject.
      Specified by:
      isUUIDSupported in interface Backend
      Returns:
      true if supported, false if mapped to VARCHAR(36)
    • isFunctionBasedIndexSupported

      public boolean isFunctionBasedIndexSupported()
      Description copied from interface: Backend
      Returns whether function based indexes are supported.
      Specified by:
      isFunctionBasedIndexSupported in interface Backend
      Returns:
      true if supported
    • isFilteredIndexSupported

      public boolean isFilteredIndexSupported()
      Description copied from interface: Backend
      Returns whether backend supports filtered indexes.
      Specified by:
      isFilteredIndexSupported in interface Backend
      Returns:
      true if supported, false if model will raise an error if filtered index is used
    • setDropIfExistsEnabled

      public void setDropIfExistsEnabled(boolean dropIfExistsEnabled)
      Description copied from interface: Backend
      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 (see Backend.isTemporaryName(String)) in order to be ignored by the migrator.
      Specified by:
      setDropIfExistsEnabled in interface Backend
      Parameters:
      dropIfExistsEnabled - true if IF EXISTS should be used
    • isDropIfExistsEnabled

      public boolean isDropIfExistsEnabled()
      Description copied from interface: Backend
      Returns whether backend should add IF EXISTS for generated DROPs.
      Specified by:
      isDropIfExistsEnabled in interface Backend
      Returns:
      true if IF EXISTS is turned on and supported, false if not supported or turned off
    • toInternalType

      public String toInternalType(String sqlTypeName)
      Description copied from interface: Backend
      Converts a backend specific type name to an internal name used by driver specific methods.
      Specified by:
      toInternalType in interface Backend
      Parameters:
      sqlTypeName - the original type used in SQL scripts
      Returns:
      the JDBC internal name
    • isArrayOperatorSupported

      public boolean isArrayOperatorSupported(String operator)
      Description copied from interface: Backend
      Returns whether the backend supports given array operator.
      Specified by:
      isArrayOperatorSupported in interface Backend
      Parameters:
      operator - the array operator
      Returns:
      true if supported
    • setArray

      public void setArray(PreparedStatement statement, int pos, SqlType type, Collection<?> elements, String operator) throws SQLException
      Description copied from interface: Backend
      Sets an array parameter in a prepared statement.
      Specified by:
      setArray in interface Backend
      Parameters:
      statement - the prepared statement
      pos - the parameter position (starting at 1)
      type - the element's SQL type
      elements - the elements to be converted to an array parameter
      operator - the array operator
      Throws:
      SQLException
    • isSchemaSupported

      public boolean isSchemaSupported()
      Description copied from interface: Backend
      Returns whether the backend supports schemas.
      Specified by:
      isSchemaSupported in interface Backend
      Returns:
      true if supported
      See Also:
    • sqlCreateSchema

      public String sqlCreateSchema(String name)
      Description copied from interface: Backend
      Creates the SQL string to create a schema.
      Specified by:
      sqlCreateSchema in interface Backend
      Parameters:
      name - the schema name
      Returns:
      the SQL code
      See Also:
    • isPosixEscapeSyntaxSupported

      public boolean isPosixEscapeSyntaxSupported()
      Description copied from interface: Backend
      Returns whether the backend supports posix-style escapes via backslash.
      Specified by:
      isPosixEscapeSyntaxSupported in interface Backend
      Returns:
      true if supported
    • createScriptRunner

      public ScriptRunner createScriptRunner(Connection connection)
      Description copied from interface: Backend
      Creates a script runner instance.
      Specified by:
      createScriptRunner in interface Backend
      Parameters:
      connection - the SQL-connection
    • isDatabaseInMemory

      public boolean isDatabaseInMemory(String url)
      Description copied from interface: Backend
      Returns whether the URL denotes an in-memory database.
      Used by test suites to decide whether to populate the database before running the tests.
      Specified by:
      isDatabaseInMemory in interface Backend
      Parameters:
      url - the JDBC url
      Returns:
      true if in-memory
    • sqlCreateTableIntroWithoutComment

      protected String sqlCreateTableIntroWithoutComment(String tableName)
      Generates the first line of a CREATE TABLE statement.
      Parameters:
      tableName - the tablename with optional schema separated by a dot
      Returns:
      the SQL code including the opening bracket
    • createPassword

      protected String createPassword(char[] password)
      Creates a password string from a password char array.
      Unfortunately, DriverManager.getConnection(String, String, String) requires the password as a string. This method creates a string from a char array and optionally decrypts it. Encrypted passwords are detected by a leading ~. If an unencrypted password must begin with a ~, it must be escaped by a leading backslash \~.
      Parameters:
      password - the probably encrypted password
      Returns:
      the cleartext password
    • normalizeDefault

      protected String normalizeDefault(String str)
      Normalize the default string to compare.
      Parameters:
      str - the default value as a string, may be null
      Returns:
      the normalized value, never null
    • sqlCreateTableAttributeWithoutComment

      protected String sqlCreateTableAttributeWithoutComment(String columnName, 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 name
      sqlType - the JDBC sql type
      size - the optional size
      scale - the optional scale
      nullable - true if NULL, else NOT NULL
      defaultValue - the optional default value
      primaryKey - true if this is a primary key
      withTrailingComma - true if append a comma
      Returns:
      the SQL code
    • extractWhereClause

      protected String extractWhereClause(String sql, int whereOffset)
      Extracts the where clause from a given sql.
      Parameters:
      sql - the sql statement
      whereOffset - the location of WHERE in sql
      Returns:
      the where clause without the keyword WHERE
    • isDropIfExistsSupported

      protected boolean isDropIfExistsSupported()
      Adds "IF EXISTS" to drop clauses, if supported by the backend.
      Makes migration a little more robust in case the same SQL scripts are applied to different databases than those used to generate the migration scripts for.
      Returns:
      true if supported
    • isExceptionStateStartingWith

      protected boolean isExceptionStateStartingWith(SQLException ex, String... prefixes)
      Checks if the exception's state starts with a given string(s).
      Parameters:
      ex - the exception
      prefixes - the prefixes the state starts with
      Returns:
      true if so
    • isExceptionStateMatching

      protected boolean isExceptionStateMatching(SQLException ex, String... states)
      Checks if the exception's state equals given string(s).
      Parameters:
      ex - the exception
      states - the states to check for
      Returns:
      true if matches
    • isExceptionErrorCodeMatching

      protected boolean isExceptionErrorCodeMatching(SQLException ex, int... codes)
      Checks if the exception's error code equals given number(s).
      Parameters:
      ex - the exception
      codes - the error codes to check for
      Returns:
      true if matches