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
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
actuallyChangeColumnToNotNull
(Connection c, String tableName, String columnName) void
addColumn
(Connection c, String tableName, String columnName, int columnType, Integer columnSize) Add a column to a table.protected void
changeColumnDefinition
(Connection c, String table, String column, String sqlType, boolean isNullable) void
changeColumnToNotNull
(Connection c, String tableName, String columnName) Changes column to not null, unless it's already not null.void
changeColumnType
(Connection c, String table, String column, int type, @Nullable Integer columnSize, boolean isNullable) Changes column type to the new type (seeTypes
.void
changeTableNameToUpperCase
(Connection connection, String oldName) Rename table from lower case to upper case.@NotNull String
clobEquals
(@NotNull String columnName) Comparison string for CLOB column.void
createColumn
(@NotNull Connection connection, @NotNull String tableName, @NotNull String columnName, @org.jetbrains.annotations.NotNull int columnType) void
createIndex
(Connection c, String index, String table, String column) protected void
createPrimaryKey
(Connection c, String table, String pkName, Collection<String> columns) void
createUniqueConstraint
(Connection c, String uniqueKey, String table, String... columns) Create unique keyboolean
dropColumn
(@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.void
dropConstraint
(Connection c, String tableName, String constraint) Drop constraintprotected void
dropConstraints
(Connection connection, String tableName, String columnName) void
dropForeignKeyConstraint
(Connection c, String tableName, String foreignKey) void
dropIndex
(Connection c, String table, String index) boolean
dropIndexIfExists
(Connection c, String table, @NotNull String indexName) Tries to find index by name ignoring case and removes it if found.void
dropTable
(Connection c, String tableName) Drop table.protected @Nullable DbmsBean.ColumnDefinition
getColumnDefinition
(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 String
Schema name, retrieved from connection or cacheprotected @Nullable String
protected @NotNull String
getSqlTypeName
(int type) getTables
(@NotNull Connection c) Retrieves the list of table names in the current schema.boolean
isColumnPresent
(@NotNull Connection connection, @NotNull String tableName, @NotNull String columnName) Checks whether given table contains the specified column.boolean
isColumnPresent
(@NotNull Statement statement, @NotNull String tableName, @NotNull String columnName) Checks whether given table contains the specified column.protected boolean
isColumnPresent
(Statement statement, String tableName, Predicate<String> columnNamePredicate) boolean
isH2()
boolean
isHsqldb()
boolean
boolean
isMySql()
boolean
isOracle()
boolean
protected boolean
boolean
isTablePresent
(@NotNull Connection c, @NotNull String tableName) Checks whether given table is present in the databasevoid
migrateColumnType
(Connection c, String table, String column, int columnType, @Nullable Integer columnSize) Changes column type to the new type (seeTypes
.protected DbmsBean.ColumnDefinition
newColumnDefinition
(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 String
quoteIfNeeded
(String name) Some tables in db can be stored in a case sensitive manner.abstract void
renameColumn
(Connection c, String tableName, String oldName, String newName) Rename columns.toLowerCase
(String columnName) Wraps column name with DB-specific toLowerCase functionMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.atlassian.bamboo.persistence3.BambooDatabaseTypeProvider
getDatabaseType
Methods 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:DbmsBean
Checks whether given table contains the specified column.- Specified by:
isColumnPresent
in 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:DbmsBean
Checks whether given table contains the specified column.- Specified by:
isColumnPresent
in 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:DbmsBean
Changes column type to the new type (seeTypes
. Uses alter table, so won't cope with all type conversions.- Specified by:
changeColumnType
in 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:DbmsBean
Changes column type to the new type (seeTypes
. Uses rename + add + copy + drop for maximum compatibility.- Specified by:
migrateColumnType
in 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:DbmsBean
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. The method will automatically detect constraint names on DBMS that supports it or use provided names on DBMS that don't.- Specified by:
dropColumn
in 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:
createColumn
in interfaceDbmsBean
- Throws:
SQLException
-
dropConstraints
-
dropIndex
- Specified by:
dropIndex
in interfaceDbmsBean
- Throws:
SQLException
-
dropIndexIfExists
public boolean dropIndexIfExists(Connection c, String table, @NotNull @NotNull String indexName) throws SQLException Description copied from interface:DbmsBean
Tries to find index by name ignoring case and removes it if found.- Specified by:
dropIndexIfExists
in 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:
createIndex
in interfaceDbmsBean
- Throws:
SQLException
-
createUniqueConstraint
public void createUniqueConstraint(Connection c, String uniqueKey, String table, String... columns) throws SQLException Description copied from interface:DbmsBean
Create unique key- Specified by:
createUniqueConstraint
in 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:DbmsBean
Checks whether given table is present in the database- Specified by:
isTablePresent
in 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:DbmsBean
Retrieves the list of table names in the current schema.- Specified by:
getTables
in interfaceDbmsBean
- Throws:
SQLException
-
isTable
-
changeTableNameToUpperCase
Description copied from interface:DbmsBean
Rename table from lower case to upper case. No-op if database is table names are case insensitive.- Specified by:
changeTableNameToUpperCase
in interfaceDbmsBean
- Throws:
SQLException
-
renameColumn
public abstract void renameColumn(Connection c, String tableName, String oldName, String newName) throws SQLException Description copied from interface:DbmsBean
Rename columns.- Specified by:
renameColumn
in interfaceDbmsBean
- Throws:
SQLException
-
addColumn
public void addColumn(Connection c, String tableName, String columnName, int columnType, Integer columnSize) throws SQLException Description copied from interface:DbmsBean
Add a column to a table.- Specified by:
addColumn
in interfaceDbmsBean
- Throws:
SQLException
-
dropConstraint
Description copied from interface:DbmsBean
Drop constraint- Specified by:
dropConstraint
in interfaceDbmsBean
- Throws:
SQLException
- if constraint does not exist
-
dropForeignKeyConstraint
public void dropForeignKeyConstraint(Connection c, String tableName, String foreignKey) throws SQLException - Specified by:
dropForeignKeyConstraint
in interfaceDbmsBean
- Throws:
SQLException
-
dropTable
Description copied from interface:DbmsBean
Drop table.- Specified by:
dropTable
in interfaceDbmsBean
- Throws:
SQLException
-
changeColumnToNotNull
public void changeColumnToNotNull(Connection c, String tableName, String columnName) throws SQLException Description copied from interface:DbmsBean
Changes column to not null, unless it's already not null.- Specified by:
changeColumnToNotNull
in interfaceDbmsBean
- Throws:
SQLException
-
actuallyChangeColumnToNotNull
protected void actuallyChangeColumnToNotNull(Connection c, String tableName, String columnName) throws SQLException - Throws:
SQLException
-
quote
Description copied from interface:DbmsBean
Quotes the supplied name according to the quoting rules of the underlying DBMS. -
clobEquals
Description copied from interface:DbmsBean
Comparison string for CLOB column.- Specified by:
clobEquals
in 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. -
getIndexNames
public Set<String> getIndexNames(@NotNull @NotNull Connection c, @NotNull @NotNull String table, @Nullable @Nullable String column) throws SQLException Description copied from interface:DbmsBean
Returns 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:
getIndexNames
in interfaceDbmsBean
- Throws:
SQLException
-
getIndices
public Collection<DbmsBean.IndexDefinition> getIndices(@NotNull @NotNull Connection c, @NotNull @NotNull String table) throws SQLException - Specified by:
getIndices
in interfaceDbmsBean
- Throws:
SQLException
-
getColumns
public List<DbmsBean.ColumnDefinition> getColumns(Connection c, @NotNull @NotNull String table, @Nullable @Nullable String column) throws SQLException - Specified by:
getColumns
in 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:DbmsBean
Schema name, retrieved from connection or cache- Specified by:
getSchema
in interfaceDbmsBean
- Returns:
- schema name
- Throws:
SQLException
-
getSchemaUncached
- Throws:
SQLException
-
getLargeTextTypeName
- Specified by:
getLargeTextTypeName
in interfaceDbmsBean
-
getColumnDefinitionString
- Specified by:
getColumnDefinitionString
in interfaceDbmsBean
-
toLowerCase
Wraps column name with DB-specific toLowerCase function- Specified by:
toLowerCase
in interfaceDbmsBean
-
isHsqldb
public boolean isHsqldb() -
isH2
public boolean isH2() -
isMsSqlServer
public boolean isMsSqlServer()- Specified by:
isMsSqlServer
in 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:
isPostgreSql
in interfaceDbmsBean
- Returns:
- true if db configured in Hibernate is PostgreSQL
-