- java.lang.Object
-
- org.tentackle.sql.backends.AbstractBackend
-
- org.tentackle.sql.backends.AbstractSql92Backend
-
- org.tentackle.sql.backends.MySql
-
- All Implemented Interfaces:
Backend
- Direct Known Subclasses:
MariaDb
@Service(Backend.class) public class MySql extends AbstractSql92Backend
Backend for MySQL.- Author:
- harald
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringSQL_LIMITLIMIT string.static java.lang.StringSQL_LIMIT_PARLIMIT ? string.static java.lang.StringSQL_OFFSETOFFSET string.static java.lang.StringSQL_OFFSET_PAROFFSET ? string.-
Fields inherited from class org.tentackle.sql.backends.AbstractSql92Backend
RESERVED_WORDS_SQL92, SQL_COALESCE, SQL_FOR_UPDATE
-
Fields inherited from class org.tentackle.sql.backends.AbstractBackend
TYPE_BIGINT, TYPE_BIT, TYPE_BLOB, TYPE_BOOL, TYPE_BOOLEAN, TYPE_BYTE, TYPE_BYTEA, TYPE_CHAR_1, TYPE_CLOB, TYPE_DATE, TYPE_DATETIME, TYPE_DATETIME_YEAR_TO_SECOND, TYPE_DECIMAL, TYPE_DECIMAL_19, TYPE_DOUBLE, TYPE_FLOAT, TYPE_FLOAT4, TYPE_FLOAT8, TYPE_INT, TYPE_INT2, TYPE_INT4, TYPE_INT8, TYPE_INTEGER, TYPE_NCHAR_1, TYPE_NUMBER, TYPE_NUMBER_1, TYPE_NUMBER_10, TYPE_NUMBER_19, TYPE_NUMBER_5, TYPE_NVARCHAR, TYPE_NVARCHAR_MAX, TYPE_REAL, TYPE_SMALLFLOAT, TYPE_SMALLINT, TYPE_TEXT, TYPE_TIME, TYPE_TIMESTAMP, TYPE_TINYINT, TYPE_VARBINARY_MAX, TYPE_VARCHAR
-
Fields inherited from interface org.tentackle.sql.Backend
SQL_ALLSTAR, SQL_ALTER_COLUMN, SQL_ALTER_INDEX, SQL_ALTER_TABLE, SQL_AND, SQL_ANDNOT, SQL_ARRAY_ALL, SQL_ARRAY_ALL_PAR, SQL_ARRAY_ANY, SQL_ARRAY_ANY_PAR, SQL_ARRAY_IN, SQL_ARRAY_IN_PAR, SQL_ARRAY_NOT_IN, SQL_ARRAY_NOT_IN_PAR, SQL_ARRAY_PAR, SQL_COMMA, SQL_COMMA_PAR, SQL_DELETE, SQL_EQUAL, SQL_EQUAL_PAR, SQL_EQUAL_PAR_COMMA, SQL_EQUAL_ZERO, SQL_EXISTS, SQL_FROM, SQL_GREATER, SQL_GREATER_PAR, SQL_GREATEROREQUAL, SQL_GREATEROREQUAL_PAR, SQL_GROUPBY, SQL_IF_EXISTS, SQL_INSERT_INTO, SQL_INSERT_VALUES, SQL_ISNOTNULL, SQL_ISNULL, SQL_LEFT_PARENTHESIS, SQL_LESS, SQL_LESS_PAR, SQL_LESSOREQUAL, SQL_LESSOREQUAL_PAR, SQL_LIKE, SQL_LIKE_PAR, SQL_MAX, SQL_MIN, SQL_NOT, SQL_NOTEQUAL, SQL_NOTEQUAL_PAR, SQL_NOTEQUAL_ZERO, SQL_NOTLIKE, SQL_NOTLIKE_PAR, SQL_OR, SQL_ORDERBY, SQL_ORNOT, SQL_PAR, SQL_PAR_COMMA, SQL_PLUS_ONE, SQL_RIGHT_PARENTHESIS, SQL_SELECT, SQL_SELECT_ALL_FROM, SQL_SET, SQL_SORTASC, SQL_SORTDESC, SQL_UPDATE, SQL_WHERE, SQL_WHEREALL, SQL_WHEREAND, SQL_WHERENOTHING, SQL_WHEREOR
-
-
Constructor Summary
Constructors Constructor Description MySql()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanallowsExpressionsReferringToTablesBeingUpdated()Determines whether backend allows expressions referring to tables being updated.voidbuildSelectSql(java.lang.StringBuilder sqlBuilder, boolean writeLock, int limit, int offset)Creates a select statement string from an inner sql string.ColumnMetaDatacreateColumnMetaData(TableMetaData tableMetaData)Creates a column meta data instance.protected java.lang.StringextractWhereClause(java.lang.String sql, int whereOffset)Extracts the where clause from a given sql.java.lang.StringgetBackendId(java.sql.Connection connection)Determines the unique id or name used by the backend for a given connection.java.lang.StringgetDriverClassName()Gets the JDBC driver class name.intgetMaxSize(SqlType sqlType)Returns the maximum size for a given SQL-type.java.sql.DatabaseMetaData[]getMetaData(BackendInfo backendInfo)Gets the metadata from the backend.MigrationStrategy[]getMigrationStrategy(ColumnMetaData column, java.lang.String columnName, java.lang.String comment, SqlType sqlType, int size, int scale, boolean nullable, java.lang.Object defaultValue)Determines the best migration strategy.java.lang.StringgetName()Gets the name of the backend.booleanisFunctionBasedIndexSupported()Returns whether function based indexes are supported.booleanisMatchingUrl(java.lang.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(java.lang.String name)Checks whether this is a reserved schema name for this backend.SqlType[]jdbcTypeToSqlType(int jdbcType, int size, int scale)Converts the JDBC data type integer to a tentackle backend SqlTypes.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.java.lang.StringsqlAddColumn(java.lang.String tableName, java.lang.String columnName, java.lang.String comment, SqlType sqlType, int size, int scale, boolean nullable, java.lang.Object defaultValue)Generates sql code to add a column.java.lang.StringsqlAlterColumnComment(java.lang.String tableName, java.lang.String columnName, java.lang.String comment)Generates SQL code to alter the comment for an attribute.java.lang.StringsqlAlterColumnType(java.lang.String tableName, java.lang.String columnName, java.lang.String comment, SqlType sqlType, int size, int scale, boolean nullable, java.lang.Object defaultValue)Generates sql code to change the datatype of a column.java.lang.StringsqlCreateColumn(java.lang.String columnName, java.lang.String comment, SqlType sqlType, int size, int scale, boolean nullable, java.lang.Object defaultValue, boolean primaryKey, boolean withTrailingComma)Generates the attribute definition of a CREATE TABLE statement.java.lang.StringsqlCreateTableClosing(java.lang.String tableName, java.lang.String comment)Generates the last line of a CREATE TABLE statement.java.lang.StringsqlDropIndex(java.lang.String schemaName, java.lang.String tableNameWithoutSchema, java.lang.String indexName)Generates the DROP INDEX statement.java.lang.StringsqlRenameAndAlterColumnType(java.lang.String tableName, java.lang.String oldColumnName, java.lang.String newColumnName, java.lang.String comment, SqlType sqlType, int size, int scale, boolean nullable, java.lang.Object defaultValue)Generates sql code to rename a column.java.lang.StringsqlRenameColumn(java.lang.String tableName, java.lang.String oldColumnName, java.lang.String newColumnName)Generates sql code to rename a column.java.lang.StringsqlRenameIndex(java.lang.String tableName, java.lang.String oldIndexName, java.lang.String newIndexName)Generates sql code to rename an index.java.lang.StringsqlTypeToString(SqlType sqlType, int size)Converts the java SQL-type to the database type name.-
Methods inherited from class org.tentackle.sql.backends.AbstractSql92Backend
getCoalesceKeyword, getReservedWords, sqlCreateForeignKey, sqlDropForeignKey, sqlNextFromSequene, supportsSequences
-
Methods inherited from class org.tentackle.sql.backends.AbstractBackend
assertValidName, buildSelectSql, columnTypeNullDefaultToString, columnTypeToString, createConnection, createIndexColumnMetaData, createIndexMetaData, createPassword, createTableMetaData, getDefaultSchema, getDefaultSize, getEmptyString, getMaxScale, getModelMetaData, getTableHeaders, getTableMetaData, isArrayOperatorSupported, isClobSupported, isCommunicationLinkException, isConstraintException, isDefaultEqual, isDropIfExistsEnabled, isDropIfExistsSupported, isFilteredIndexSupported, isMatchingName, isReservedTableName, isTemporaryName, needAliasForSubselect, needSetLongWorkaround, needTxForFetchsize, normalizeDefault, optimizeSql, setArray, setDropIfExistsEnabled, sqlAlterColumnDefault, sqlAlterColumnNullConstraint, sqlAlterTableComment, sqlAsBeforeTableAlias, sqlComment, sqlCreateColumnComment, sqlCreateIndex, sqlCreateTableAttributeWithoutComment, sqlCreateTableComment, sqlCreateTableIntro, sqlCreateTableIntroWithoutComment, sqlDropColumn, sqlDropTable, sqlFunction, sqlJoin, sqlJoinSelects, sqlJoinSelects, sqlRenameTable, sqlRequiresExtraCommit, sqlResultSetIsClosedSupported, sqlUpdateToNotNull, toInternalType, toQuotedString, toString, valueToLiteral
-
-
-
-
Field Detail
-
SQL_LIMIT
public static final java.lang.String SQL_LIMIT
LIMIT string.- See Also:
- Constant Field Values
-
SQL_OFFSET
public static final java.lang.String SQL_OFFSET
OFFSET string.- See Also:
- Constant Field Values
-
SQL_LIMIT_PAR
public static final java.lang.String SQL_LIMIT_PAR
LIMIT ? string.- See Also:
- Constant Field Values
-
SQL_OFFSET_PAR
public static final java.lang.String SQL_OFFSET_PAR
OFFSET ? string.- See Also:
- Constant Field Values
-
-
Method Detail
-
isMatchingUrl
public boolean isMatchingUrl(java.lang.String url)
Description copied from interface:BackendChecks 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 java.lang.String getName()
Description copied from interface:BackendGets the name of the backend.- Returns:
- the name
-
getDriverClassName
public java.lang.String getDriverClassName()
Description copied from interface:BackendGets the JDBC driver class name.- Returns:
- the class name
-
getBackendId
public java.lang.String getBackendId(java.sql.Connection connection)
Description copied from interface:BackendDetermines 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
-
isReservedSchemaName
public boolean isReservedSchemaName(java.lang.String name)
Description copied from interface:BackendChecks whether this is a reserved schema name for this backend.- Specified by:
isReservedSchemaNamein interfaceBackend- Overrides:
isReservedSchemaNamein classAbstractBackend- Parameters:
name- the schema name- Returns:
- true if reserved by backend
-
isReleaseSavepointSupported
public boolean isReleaseSavepointSupported()
Description copied from interface:BackendReturns whether the backend supports releasing savepoints explicitly. If not, the savepoints are released when the transaction finishs.- Specified by:
isReleaseSavepointSupportedin interfaceBackend- Overrides:
isReleaseSavepointSupportedin classAbstractBackend- Returns:
- true if release is supported, else false
-
isFunctionBasedIndexSupported
public boolean isFunctionBasedIndexSupported()
Description copied from interface:BackendReturns whether function based indexes are supported.- Specified by:
isFunctionBasedIndexSupportedin interfaceBackend- Overrides:
isFunctionBasedIndexSupportedin classAbstractBackend- Returns:
- true if supported
-
allowsExpressionsReferringToTablesBeingUpdated
public boolean allowsExpressionsReferringToTablesBeingUpdated()
Description copied from interface:BackendDetermines whether backend allows expressions referring to tables being updated.- Specified by:
allowsExpressionsReferringToTablesBeingUpdatedin interfaceBackend- Overrides:
allowsExpressionsReferringToTablesBeingUpdatedin classAbstractBackend- Returns:
- true if allowed
-
buildSelectSql
public void buildSelectSql(java.lang.StringBuilder sqlBuilder, boolean writeLock, int limit, int offset)Description copied from interface:BackendCreates 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.- Specified by:
buildSelectSqlin interfaceBackend- Overrides:
buildSelectSqlin classAbstractSql92Backend- 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
-
setLeadingSelectParameters
public int setLeadingSelectParameters(BackendPreparedStatement stmt, int limit, int offset)
Description copied from interface:BackendSets optional parameters before applying the original parameters.- Specified by:
setLeadingSelectParametersin interfaceBackend- Overrides:
setLeadingSelectParametersin classAbstractSql92Backend- 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
public int setTrailingSelectParameters(BackendPreparedStatement stmt, int index, int limit, int offset)
Description copied from interface:BackendSets optional parameters after applying the original parameters.- Specified by:
setTrailingSelectParametersin interfaceBackend- Overrides:
setTrailingSelectParametersin classAbstractSql92Backend- 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
-
getMaxSize
public int getMaxSize(SqlType sqlType)
Description copied from interface:BackendReturns the maximum size for a given SQL-type.- Specified by:
getMaxSizein interfaceBackend- Overrides:
getMaxSizein classAbstractBackend- Parameters:
sqlType- the SQL-type- Returns:
- the maximum size, 0 if unlimited, -1 if type without size
-
sqlTypeToString
public java.lang.String sqlTypeToString(SqlType sqlType, int size)
Description copied from interface:BackendConverts the java SQL-type to the database type name.- Specified by:
sqlTypeToStringin interfaceBackend- Overrides:
sqlTypeToStringin classAbstractBackend- Parameters:
sqlType- the java sql typesize- the column's size- Returns:
- the corresponding type string
-
jdbcTypeToSqlType
public SqlType[] jdbcTypeToSqlType(int jdbcType, int size, int scale)
Description copied from interface:BackendConverts the JDBC data type integer to a tentackle backend SqlTypes.There may be more than one sqltype returned!
- Specified by:
jdbcTypeToSqlTypein interfaceBackend- Overrides:
jdbcTypeToSqlTypein classAbstractBackend- 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
-
sqlCreateTableClosing
public java.lang.String sqlCreateTableClosing(java.lang.String tableName, java.lang.String comment)Description copied from interface:BackendGenerates the last line of a CREATE TABLE statement.- Specified by:
sqlCreateTableClosingin interfaceBackend- Overrides:
sqlCreateTableClosingin classAbstractBackend- Parameters:
tableName- the tablename with optional schema separated by a dotcomment- optional comment, null if none- Returns:
- the SQL code including the closing bracket
-
getMetaData
public java.sql.DatabaseMetaData[] getMetaData(BackendInfo backendInfo) throws java.sql.SQLException
Description copied from interface:BackendGets the metadata from the backend.Important: the connection of the metadata is open!
- Specified by:
getMetaDatain interfaceBackend- Overrides:
getMetaDatain classAbstractBackend- Parameters:
backendInfo- the backend info- Returns:
- the metadata, may be more than one if schemas set in backendInfo
- Throws:
java.sql.SQLException- if failed
-
createColumnMetaData
public ColumnMetaData createColumnMetaData(TableMetaData tableMetaData)
Description copied from interface:BackendCreates a column meta data instance.- Specified by:
createColumnMetaDatain interfaceBackend- Overrides:
createColumnMetaDatain classAbstractBackend- Parameters:
tableMetaData- the table meta data this column belongs to- Returns:
- the meta data
-
sqlCreateColumn
public java.lang.String sqlCreateColumn(java.lang.String columnName, java.lang.String comment, SqlType sqlType, int size, int scale, boolean nullable, java.lang.Object defaultValue, boolean primaryKey, boolean withTrailingComma)Description copied from interface:BackendGenerates the attribute definition of a CREATE TABLE statement.- Specified by:
sqlCreateColumnin interfaceBackend- Overrides:
sqlCreateColumnin classAbstractBackend- 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
-
sqlAddColumn
public java.lang.String sqlAddColumn(java.lang.String tableName, java.lang.String columnName, java.lang.String comment, SqlType sqlType, int size, int scale, boolean nullable, java.lang.Object defaultValue)Description copied from interface:BackendGenerates sql code to add a column.- Specified by:
sqlAddColumnin interfaceBackend- Overrides:
sqlAddColumnin classAbstractBackend- 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
-
sqlRenameColumn
public java.lang.String sqlRenameColumn(java.lang.String tableName, java.lang.String oldColumnName, java.lang.String newColumnName)Description copied from interface:BackendGenerates sql code to rename a column.- Specified by:
sqlRenameColumnin interfaceBackend- Overrides:
sqlRenameColumnin classAbstractBackend- Parameters:
tableName- the tablenameoldColumnName- the old column namenewColumnName- the new column name- Returns:
- the SQL code, null if need full spec sqlRenameAndAlterColumnType below
-
sqlRenameIndex
public java.lang.String sqlRenameIndex(java.lang.String tableName, java.lang.String oldIndexName, java.lang.String newIndexName)Description copied from interface:BackendGenerates sql code to rename an index.- Specified by:
sqlRenameIndexin interfaceBackend- Overrides:
sqlRenameIndexin classAbstractBackend- 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
public java.lang.String sqlRenameAndAlterColumnType(java.lang.String tableName, java.lang.String oldColumnName, java.lang.String newColumnName, java.lang.String comment, SqlType sqlType, int size, int scale, boolean nullable, java.lang.Object defaultValue)Description copied from interface:BackendGenerates sql code to rename a column.- Specified by:
sqlRenameAndAlterColumnTypein interfaceBackend- Overrides:
sqlRenameAndAlterColumnTypein classAbstractBackend- 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
-
sqlAlterColumnType
public java.lang.String sqlAlterColumnType(java.lang.String tableName, java.lang.String columnName, java.lang.String comment, SqlType sqlType, int size, int scale, boolean nullable, java.lang.Object defaultValue)Description copied from interface:BackendGenerates sql code to change the datatype of a column.- Specified by:
sqlAlterColumnTypein interfaceBackend- Overrides:
sqlAlterColumnTypein classAbstractBackend- 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
-
sqlAlterColumnComment
public java.lang.String sqlAlterColumnComment(java.lang.String tableName, java.lang.String columnName, java.lang.String comment)Description copied from interface:BackendGenerates SQL code to alter the comment for an attribute.- Specified by:
sqlAlterColumnCommentin interfaceBackend- Overrides:
sqlAlterColumnCommentin classAbstractBackend- Parameters:
tableName- the table namecolumnName- the column namecomment- optional comment, null to clear- Returns:
- the SQL code, null if use
Backend.sqlRenameAndAlterColumnType(java.lang.String, java.lang.String, java.lang.String, java.lang.String, org.tentackle.sql.SqlType, int, int, boolean, java.lang.Object)instead
-
sqlDropIndex
public java.lang.String sqlDropIndex(java.lang.String schemaName, java.lang.String tableNameWithoutSchema, java.lang.String indexName)Description copied from interface:BackendGenerates the DROP INDEX statement.- Specified by:
sqlDropIndexin interfaceBackend- Overrides:
sqlDropIndexin classAbstractBackend- Parameters:
schemaName- the optional schema, null if nonetableNameWithoutSchema- the table name without the schemaindexName- the name of the index- Returns:
- the SQL code
-
getMigrationStrategy
public MigrationStrategy[] getMigrationStrategy(ColumnMetaData column, java.lang.String columnName, java.lang.String comment, SqlType sqlType, int size, int scale, boolean nullable, java.lang.Object defaultValue)
Description copied from interface:BackendDetermines the best migration strategy.- Specified by:
getMigrationStrategyin interfaceBackend- Overrides:
getMigrationStrategyin classAbstractBackend- 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
-
extractWhereClause
protected java.lang.String extractWhereClause(java.lang.String sql, int whereOffset)Description copied from class:AbstractBackendExtracts the where clause from a given sql.- Overrides:
extractWhereClausein classAbstractBackend- Parameters:
sql- the sql statementwhereOffset- the location of WHERE in sql- Returns:
- the where clause without the keyword WHERE
-
-