Class MySql

All Implemented Interfaces:
Backend
Direct Known Subclasses:
MariaDb

@Service(Backend.class) public class MySql extends AbstractSql92Backend
Backend for MySQL.
Author:
harald
  • Field Details

  • Constructor Details

    • MySql

      public MySql()
      Creates the MySql backend.
  • Method Details

    • isMatchingUrl

      public boolean isMatchingUrl(String url)
      Description copied from interface: Backend
      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
    • getName

      public String getName()
      Description copied from interface: Backend
      Gets the name of the backend.
      Returns:
      the name
    • getDriverClassName

      public String getDriverClassName()
      Description copied from interface: Backend
      Gets the JDBC driver class name.
      Returns:
      the class name
    • getBackendId

      public String getBackendId(Connection connection)
      Description copied from interface: Backend
      Determines the unique id or name used by the backend for a given connection.

      Useful to figure out the corresponding ManagedConnection from the backend's logfiles.

      Parameters:
      connection - the jdbc connection
      Returns:
      the backend id, null if none
    • isSchemaSupported

      public boolean isSchemaSupported()
      Description copied from interface: Backend
      Returns whether the backend supports schemas.
      Specified by:
      isSchemaSupported in interface Backend
      Overrides:
      isSchemaSupported in class AbstractBackend
      Returns:
      true if supported
      See Also:
    • 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
      Overrides:
      isReservedSchemaName in class AbstractBackend
      Parameters:
      name - the schema name
      Returns:
      true if reserved by backend
    • 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
      Overrides:
      isReleaseSavepointSupported in class AbstractBackend
      Returns:
      true if release is supported, else false
    • isFunctionBasedIndexSupported

      public boolean isFunctionBasedIndexSupported()
      Description copied from interface: Backend
      Returns whether function based indexes are supported.
      Specified by:
      isFunctionBasedIndexSupported in interface Backend
      Overrides:
      isFunctionBasedIndexSupported in class AbstractBackend
      Returns:
      true if supported
    • 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
      Overrides:
      isPosixEscapeSyntaxSupported in class AbstractBackend
      Returns:
      true if supported
    • 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
      Overrides:
      isExpressionReferringToTableBeingUpdatedSupported in class AbstractBackend
      Returns:
      true if allowed
    • buildSelectSql

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

      sqlBuilder is anything as [SELECT] FROM... WHERE... ORDER BY....
      It is decorated with a leading SELECT, if missing, plus optional clauses like for the given parameters.

      Specified by:
      buildSelectSql in interface Backend
      Overrides:
      buildSelectSql in class AbstractSql92Backend
      Parameters:
      sqlBuilder - the sql builder, initially containing the inner 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
    • setLeadingSelectParameters

      public int setLeadingSelectParameters(BackendPreparedStatement stmt, int limit, int offset)
      Description copied from interface: Backend
      Sets optional parameters before applying the original parameters.
      Specified by:
      setLeadingSelectParameters in interface Backend
      Overrides:
      setLeadingSelectParameters in class AbstractSql92Backend
      Parameters:
      stmt - the prepared statement
      limit - the limit value, ≤ 0 if no limit clause
      offset - the offset value, ≤ 0 if no offset clause
      Returns:
      the next parameter index
    • setTrailingSelectParameters

      public int setTrailingSelectParameters(BackendPreparedStatement stmt, int index, int limit, int offset)
      Description copied from interface: Backend
      Sets optional parameters after applying the original parameters.
      Specified by:
      setTrailingSelectParameters in interface Backend
      Overrides:
      setTrailingSelectParameters in class AbstractSql92Backend
      Parameters:
      stmt - the prepared statement
      index - the first parameter index
      limit - the limit value, ≤ 0 if no limit clause
      offset - the offset value, ≤ 0 if no offset clause
      Returns:
      the next parameter index
    • 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
      Overrides:
      getMaxSize in class AbstractBackend
      Parameters:
      sqlType - the SQL-type
      Returns:
      the maximum size, 0 if unlimited, -1 if type without size
    • 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
      Overrides:
      sqlTypeToString in class AbstractBackend
      Parameters:
      sqlType - the java sql type
      size - the column's size
      Returns:
      the corresponding type string
    • 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
      Overrides:
      jdbcTypeToSqlType in class AbstractBackend
      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:
    • 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
      Overrides:
      sqlCreateTableClosing in class AbstractBackend
      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
    • 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
      Overrides:
      getMetaData in class AbstractBackend
      Parameters:
      backendInfo - the backend info
      Returns:
      the metadata, may be more than one if schemas set in backendInfo
      Throws:
      SQLException - if failed
    • createColumnMetaData

      public ColumnMetaData createColumnMetaData(TableMetaData tableMetaData)
      Description copied from interface: Backend
      Creates a column meta data instance.
      Specified by:
      createColumnMetaData in interface Backend
      Overrides:
      createColumnMetaData in class AbstractBackend
      Parameters:
      tableMetaData - the table metadata this column belongs to
      Returns:
      the meta data
    • 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
      Overrides:
      sqlCreateColumn in class AbstractBackend
      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
    • 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
      Overrides:
      sqlAddColumn in class AbstractBackend
      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
    • 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
      Overrides:
      sqlRenameColumn in class AbstractBackend
      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
      Overrides:
      sqlRenameIndex in class AbstractBackend
      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
      Overrides:
      sqlRenameAndAlterColumnType in class AbstractBackend
      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
    • 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
      Overrides:
      sqlAlterColumnType in class AbstractBackend
      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
    • 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
      Overrides:
      sqlAlterColumnComment in class AbstractBackend
      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
    • 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
      Overrides:
      sqlDropIndex in class AbstractBackend
      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
    • 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
      Overrides:
      getMigrationStrategy in class AbstractBackend
      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
    • extractWhereClause

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