Package com.atlassian.bamboo.utils.db
Class AbstractDbmsBean
java.lang.Object
com.atlassian.bamboo.utils.db.AbstractDbmsBean
- All Implemented Interfaces:
BambooDatabaseTypeProvider,DbmsBean
- Direct Known Subclasses:
H2DbmsBean,MsSqlDbmsBean,MySqlDbmsBean,OracleDbmsBean,PostgreSqlDbmsBean
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.atlassian.bamboo.utils.db.DbmsBean
DbmsBean.ColumnDefinition, DbmsBean.ConstraintDefinition, DbmsBean.IndexDefinition -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidactuallyChangeColumnToNotNull(Connection c, String tableName, String columnName) voidaddColumn(Connection c, String tableName, String columnName, int columnType, Integer columnSize) Add a column to a table.protected voidchangeColumnDefinition(Connection c, String table, String column, String sqlType, boolean isNullable) voidchangeColumnToNotNull(Connection c, String tableName, String columnName) Changes column to not null, unless it's already not null.voidchangeColumnType(Connection c, String table, String column, int type, @Nullable Integer columnSize, boolean isNullable) Changes column type to the new type (seeTypes.voidchangeTableNameToUpperCase(Connection connection, String oldName) Rename table from lower case to upper case.@NotNull StringclobEquals(@NotNull String columnName) Comparison string for CLOB column.voidcreateColumn(@NotNull Connection connection, @NotNull String tableName, @NotNull String columnName, @org.jetbrains.annotations.NotNull int columnType) voidcreateIndex(Connection c, String index, String table, String column) protected voidcreatePrimaryKey(Connection c, String table, String pkName, Collection<String> columns) voidcreateUniqueConstraint(Connection c, String uniqueKey, String table, String... columns) Create unique keybooleandropColumn(@NotNull Connection connection, @NotNull String tableName, @NotNull String columnName) Checks whether given table contains the specified column and drops it Checks whether given table contains the specified column and drops it, as well as foreign key constraints and index on the column.voiddropConstraint(Connection c, String tableName, String constraint) Drop constraintprotected voiddropConstraints(Connection connection, String tableName, String columnName) voiddropForeignKeyConstraint(Connection c, String tableName, String foreignKey) voiddropIndex(Connection c, String table, String index) booleandropIndexIfExists(Connection c, String table, @NotNull String indexName) Tries to find index by name ignoring case and removes it if found.voiddropTable(Connection c, String tableName) Drop table.protected @Nullable DbmsBean.ColumnDefinitiongetColumnDefinition(Connection connection, String tableName, @Nullable String columnName) getColumnDefinitionString(int columnType) getColumns(Connection c, @NotNull String table, @Nullable String column) protected List<DbmsBean.ConstraintDefinition>getConstraintDefinitions(Connection connection, String query, @Nullable String column, @Nullable Function<String, String> nullableNameProcessor) Finds all constraints affecting given column.getConstraintType(String typeName) getIndexNames(@NotNull Connection c, @NotNull String table, @Nullable String column) Returns indices set up on a given table, optionally narrowed down to a column.getIndices(@NotNull Connection c, @NotNull String table) @Nullable StringSchema name, retrieved from connection or cacheprotected @Nullable Stringprotected @NotNull StringgetSqlTypeName(int type) getTables(@NotNull Connection c) Retrieves the list of table names in the current schema.booleanhasIndexOnColumns(Connection c, String tableName, List<String> columns) booleanisColumnPresent(@NotNull Connection connection, @NotNull String tableName, @NotNull String columnName) Checks whether given table contains the specified column.booleanisColumnPresent(@NotNull Statement statement, @NotNull String tableName, @NotNull String columnName) Checks whether given table contains the specified column.protected booleanisColumnPresent(Statement statement, String tableName, Predicate<String> columnNamePredicate) booleanisH2()booleanbooleanisMySql()booleanisOracle()booleanprotected booleanbooleanisTablePresent(@NotNull Connection c, @NotNull String tableName) Checks whether given table is present in the databasevoidmigrateColumnType(Connection c, String table, String column, int columnType, @Nullable Integer columnSize) Changes column type to the new type (seeTypes.protected DbmsBean.ColumnDefinitionnewColumnDefinition(String columnName, int dataType, String dataTypeName, OptionalInt columnSize, Optional<Boolean> isNullable) Quotes the supplied name according to the quoting rules of the underlying DBMS.protected StringquoteIfNeeded(String name) Some tables in db can be stored in a case sensitive manner.abstract voidrenameColumn(Connection c, String tableName, String oldName, String newName) Rename columns.toLowerCase(String columnName) Wraps column name with DB-specific toLowerCase functionprotected StringunquoteIfNeeded(String name) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.atlassian.bamboo.persistence3.BambooDatabaseTypeProvider
getDatabaseTypeMethods inherited from interface com.atlassian.bamboo.utils.db.DbmsBean
dropPrimaryKey, getCatalog, getConcat, getConstraints, getQuery, resizeVarcharColumn
-
Field Details
-
LARGE_STRING_TYPE
public static final int LARGE_STRING_TYPE- See Also:
-
-
Constructor Details
-
AbstractDbmsBean
public AbstractDbmsBean()
-
-
Method Details
-
isColumnPresent
public boolean isColumnPresent(@NotNull @NotNull Connection connection, @NotNull @NotNull String tableName, @NotNull @NotNull String columnName) throws SQLException Description copied from interface:DbmsBeanChecks whether given table contains the specified column.- Specified by:
isColumnPresentin interfaceDbmsBean- Returns:
- true if given table contains the specified column or false when either column or table doesn't exist
- Throws:
SQLException
-
isColumnPresent
public boolean isColumnPresent(@NotNull @NotNull Statement statement, @NotNull @NotNull String tableName, @NotNull @NotNull String columnName) throws SQLException Description copied from interface:DbmsBeanChecks whether given table contains the specified column.- Specified by:
isColumnPresentin interfaceDbmsBean- Returns:
- true if given table contains the specified column or false when either column or table doesn't exist
- Throws:
SQLException
-
isColumnPresent
protected boolean isColumnPresent(Statement statement, String tableName, Predicate<String> columnNamePredicate) throws SQLException - Throws:
SQLException
-
changeColumnType
public void changeColumnType(Connection c, String table, String column, int type, @Nullable @Nullable Integer columnSize, boolean isNullable) throws SQLException Description copied from interface:DbmsBeanChanges column type to the new type (seeTypes. Uses alter table, so won't cope with all type conversions.- Specified by:
changeColumnTypein interfaceDbmsBean- Throws:
SQLException
-
getSqlTypeName
-
getConstraintType
-
getConstraintDefinitions
protected List<DbmsBean.ConstraintDefinition> getConstraintDefinitions(Connection connection, String query, @Nullable @Nullable String column, @Nullable @Nullable Function<String, String> nullableNameProcessor) throws SQLExceptionFinds all constraints affecting given column. Constraints affecting more than one column will be included.- Parameters:
connection-query-column-- Returns:
- Throws:
SQLException
-
changeColumnDefinition
protected void changeColumnDefinition(Connection c, String table, String column, String sqlType, boolean isNullable) throws SQLException - Throws:
SQLException
-
migrateColumnType
public void migrateColumnType(Connection c, String table, String column, int columnType, @Nullable @Nullable Integer columnSize) throws SQLException Description copied from interface:DbmsBeanChanges column type to the new type (seeTypes. Uses rename + add + copy + drop for maximum compatibility.- Specified by:
migrateColumnTypein interfaceDbmsBean- Throws:
SQLException
-
dropColumn
public boolean dropColumn(@NotNull @NotNull Connection connection, @NotNull @NotNull String tableName, @NotNull @NotNull String columnName) throws SQLException Description copied from interface:DbmsBeanChecks whether given table contains the specified column and drops it Checks whether given table contains the specified column and drops it, as well as foreign key constraints and index on the column. The method will automatically detect constraint names on DBMS that supports it or use provided names on DBMS that don't.- Specified by:
dropColumnin interfaceDbmsBean- Returns:
- Throws:
SQLException
-
createColumn
public void createColumn(@NotNull @NotNull Connection connection, @NotNull @NotNull String tableName, @NotNull @NotNull String columnName, @NotNull @org.jetbrains.annotations.NotNull int columnType) throws SQLException - Specified by:
createColumnin interfaceDbmsBean- Throws:
SQLException
-
dropConstraints
-
dropIndex
- Specified by:
dropIndexin interfaceDbmsBean- Throws:
SQLException
-
dropIndexIfExists
public boolean dropIndexIfExists(Connection c, String table, @NotNull @NotNull String indexName) throws SQLException Description copied from interface:DbmsBeanTries to find index by name ignoring case and removes it if found.- Specified by:
dropIndexIfExistsin interfaceDbmsBean- Returns:
- true if index was found
- Throws:
SQLException
-
createIndex
public void createIndex(Connection c, String index, String table, String column) throws SQLException - Specified by:
createIndexin interfaceDbmsBean- Throws:
SQLException
-
createUniqueConstraint
public void createUniqueConstraint(Connection c, String uniqueKey, String table, String... columns) throws SQLException Description copied from interface:DbmsBeanCreate unique key- Specified by:
createUniqueConstraintin interfaceDbmsBean- Throws:
SQLException- if the operation fails (ie. key exists or data in the column is not unique)
-
isTablePresent
public boolean isTablePresent(@NotNull @NotNull Connection c, @NotNull @NotNull String tableName) throws SQLException Description copied from interface:DbmsBeanChecks whether given table is present in the database- Specified by:
isTablePresentin interfaceDbmsBean- Parameters:
c- connection used to verify DB table existencetableName- name of DB table- Returns:
- true if database is accessible and table exists, false otherwise
- Throws:
SQLException- if a database error occurs or connection is closed
-
getTables
Description copied from interface:DbmsBeanRetrieves the list of table names in the current schema.- Specified by:
getTablesin interfaceDbmsBean- Throws:
SQLException
-
isTable
-
changeTableNameToUpperCase
Description copied from interface:DbmsBeanRename table from lower case to upper case. No-op if database is table names are case insensitive.- Specified by:
changeTableNameToUpperCasein interfaceDbmsBean- Throws:
SQLException
-
renameColumn
public abstract void renameColumn(Connection c, String tableName, String oldName, String newName) throws SQLException Description copied from interface:DbmsBeanRename columns.- Specified by:
renameColumnin interfaceDbmsBean- Throws:
SQLException
-
addColumn
public void addColumn(Connection c, String tableName, String columnName, int columnType, Integer columnSize) throws SQLException Description copied from interface:DbmsBeanAdd a column to a table.- Specified by:
addColumnin interfaceDbmsBean- Throws:
SQLException
-
dropConstraint
Description copied from interface:DbmsBeanDrop constraint- Specified by:
dropConstraintin interfaceDbmsBean- Throws:
SQLException- if constraint does not exist
-
dropForeignKeyConstraint
public void dropForeignKeyConstraint(Connection c, String tableName, String foreignKey) throws SQLException - Specified by:
dropForeignKeyConstraintin interfaceDbmsBean- Throws:
SQLException
-
dropTable
Description copied from interface:DbmsBeanDrop table.- Specified by:
dropTablein interfaceDbmsBean- Throws:
SQLException
-
changeColumnToNotNull
public void changeColumnToNotNull(Connection c, String tableName, String columnName) throws SQLException Description copied from interface:DbmsBeanChanges column to not null, unless it's already not null.- Specified by:
changeColumnToNotNullin interfaceDbmsBean- Throws:
SQLException
-
actuallyChangeColumnToNotNull
protected void actuallyChangeColumnToNotNull(Connection c, String tableName, String columnName) throws SQLException - Throws:
SQLException
-
quote
Description copied from interface:DbmsBeanQuotes the supplied name according to the quoting rules of the underlying DBMS. -
clobEquals
Description copied from interface:DbmsBeanComparison string for CLOB column.- Specified by:
clobEqualsin interfaceDbmsBean- Parameters:
columnName- column name- Returns:
- string for prepared statement
-
quoteIfNeeded
Some tables in db can be stored in a case sensitive manner. Then ala == ALA != "Ala" != "alA". This method ensures that the names leaving this bean can be used in a non-ambigous manner. -
unquoteIfNeeded
-
getIndexNames
public Set<String> getIndexNames(@NotNull @NotNull Connection c, @NotNull @NotNull String table, @Nullable @Nullable String column) throws SQLException Description copied from interface:DbmsBeanReturns indices set up on a given table, optionally narrowed down to a column. For H2, it will also return implicit indices set up to support FK relationships. For SQL Server, it will return implicit PK indices. Case of returned index names is database specific - all comparisons should be case-independent.- Specified by:
getIndexNamesin interfaceDbmsBean- Throws:
SQLException
-
hasIndexOnColumns
public boolean hasIndexOnColumns(Connection c, String tableName, List<String> columns) throws SQLException - Specified by:
hasIndexOnColumnsin interfaceDbmsBean- Throws:
SQLException
-
getIndices
public Collection<DbmsBean.IndexDefinition> getIndices(@NotNull @NotNull Connection c, @NotNull @NotNull String table) throws SQLException - Specified by:
getIndicesin interfaceDbmsBean- Throws:
SQLException
-
getColumns
public List<DbmsBean.ColumnDefinition> getColumns(Connection c, @NotNull @NotNull String table, @Nullable @Nullable String column) throws SQLException - Specified by:
getColumnsin interfaceDbmsBean- Throws:
SQLException
-
createPrimaryKey
protected void createPrimaryKey(Connection c, String table, String pkName, Collection<String> columns) throws SQLException - Throws:
SQLException
-
newColumnDefinition
protected DbmsBean.ColumnDefinition newColumnDefinition(String columnName, int dataType, String dataTypeName, OptionalInt columnSize, Optional<Boolean> isNullable) -
getColumnDefinition
@Nullable protected @Nullable DbmsBean.ColumnDefinition getColumnDefinition(Connection connection, String tableName, @Nullable @Nullable String columnName) throws SQLException - Throws:
SQLException
-
getSchema
Description copied from interface:DbmsBeanSchema name, retrieved from connection or cache- Specified by:
getSchemain interfaceDbmsBean- Returns:
- schema name
- Throws:
SQLException
-
getSchemaUncached
- Throws:
SQLException
-
getLargeTextTypeName
- Specified by:
getLargeTextTypeNamein interfaceDbmsBean
-
getColumnDefinitionString
- Specified by:
getColumnDefinitionStringin interfaceDbmsBean
-
toLowerCase
Wraps column name with DB-specific toLowerCase function- Specified by:
toLowerCasein interfaceDbmsBean
-
isH2
public boolean isH2() -
isMsSqlServer
public boolean isMsSqlServer()- Specified by:
isMsSqlServerin interfaceDbmsBean- Returns:
- true if db configured in Hibernate is Microsoft SQL Server
-
isMySql
public boolean isMySql() -
isOracle
public boolean isOracle() -
isPostgreSql
public boolean isPostgreSql()- Specified by:
isPostgreSqlin interfaceDbmsBean- Returns:
- true if db configured in Hibernate is PostgreSQL
-