public interface DBAccessor
| Modifier and Type | Interface and Description |
|---|---|
static class |
DBAccessor.DBColumnInfo
Capture column type
|
static class |
DBAccessor.DbType |
| Modifier and Type | Method and Description |
|---|---|
void |
addColumn(String tableName,
DBAccessor.DBColumnInfo columnInfo)
Adds a column to an existing table.
|
void |
addDefaultConstraint(String tableName,
DBAccessor.DBColumnInfo column)
Adds a default constraint to an existing column.
|
void |
addFKConstraint(String tableName,
String constraintName,
String[] keyColumns,
String referenceTableName,
String[] referenceColumns,
boolean ignoreFailure)
Add foreign key for a relation
|
void |
addFKConstraint(String tableName,
String constraintName,
String[] keyColumns,
String referenceTableName,
String[] referenceColumns,
boolean shouldCascadeOnDelete,
boolean ignoreFailure)
Add foreign key for a relation
|
void |
addFKConstraint(String tableName,
String constraintName,
String keyColumn,
String referenceTableName,
String referenceColumn,
boolean ignoreFailure)
Add foreign key for a relation
|
void |
addFKConstraint(String tableName,
String constraintName,
String keyColumn,
String referenceTableName,
String referenceColumn,
boolean shouldCascadeOnDelete,
boolean ignoreFailure) |
void |
addPKConstraint(String tableName,
String constraintName,
boolean ignoreErrors,
String... columnName) |
void |
addPKConstraint(String tableName,
String constraintName,
String... columnName) |
void |
addUniqueConstraint(String tableName,
String constraintName,
String... columnNames)
Add unique table constraint
|
void |
alterColumn(String tableName,
DBAccessor.DBColumnInfo columnInfo)
Alter column from existing table, only supports varchar extension
Use following sequence for more complex stuff: addColumn(String, org.apache.ambari.server.orm.DBAccessor.DBColumnInfo)
updateTable(String, String, Object, String)
dropColumn(String, String)
renameColumn(String, String, org.apache.ambari.server.orm.DBAccessor.DBColumnInfo) |
void |
changeColumnType(String tableName,
String columnName,
Class fromType,
Class toType)
Alter column wrapper, which handle DB specific type conversion
|
void |
clearTable(String tableName)
Remove all rows from the table
|
void |
clearTableColumn(String tableName,
String columnName,
Object value)
Reset all rows with
value for columnName column |
void |
copyColumnToAnotherTable(String sourceTableName,
DBAccessor.DBColumnInfo sourceColumn,
String sourceIDFieldName1,
String sourceIDFieldName2,
String sourceIDFieldName3,
String targetTableName,
DBAccessor.DBColumnInfo targetColumn,
String targetIDFieldName1,
String targetIDFieldName2,
String targetIDFieldName3,
String sourceConditionFieldName,
String condition,
Object initialValue)
Copy column from
targetTable by matching
table keys sourceIDColumnName and targetIDColumnName
and condition sourceConditionFieldName = condition |
void |
createIndex(String indexName,
String tableName,
boolean isUnique,
String... columnNames)
Create new index
|
void |
createIndex(String indexName,
String tableName,
String... columnNames)
Create new index
|
void |
createTable(String tableName,
List<DBAccessor.DBColumnInfo> columnInfo,
String... primaryKeyColumns)
Create new table
|
void |
dropColumn(String tableName,
String columnName)
Drop a column from table
|
void |
dropFKConstraint(String tableName,
String constraintName)
Drops a FK constraint from a table.
|
void |
dropFKConstraint(String tableName,
String constraintName,
boolean ignoreFailure)
Drop a FK constraint from table
|
void |
dropPKConstraint(String tableName,
String defaultConstraintName)
Attempts to drop the discovered PRIMARY KEY constraint on the specified
table, defaulting to the specified default if not found.
|
void |
dropPKConstraint(String tableName,
String constraintName,
boolean cascade)
Drop a PK constraint from table
|
void |
dropPKConstraint(String tableName,
String constraintName,
boolean ignoreFailure,
boolean cascade)
Drop a PK constraint from table
|
void |
dropPKConstraint(String tableName,
String constraintName,
String columnName,
boolean cascade)
Drop a PK constraint from table
|
void |
dropSequence(String sequenceName)
Drop sequence
|
void |
dropTable(String tableName)
Drop table from schema
|
void |
dropUniqueConstraint(String tableName,
String constraintName)
Drop a unique constraint from table
|
void |
dropUniqueConstraint(String tableName,
String constraintName,
boolean ignoreFailure)
Drop a unique constraint from table
|
void |
executePreparedQuery(String query,
boolean ignoreFailure,
Object... arguments)
Execute prepared update statements
|
void |
executePreparedQuery(String query,
Object... arguments)
Execute prepared statements
|
void |
executePreparedUpdate(String query,
boolean ignoreFailure,
Object... arguments)
Execute prepared statements which will not ignore failures
|
void |
executePreparedUpdate(String query,
Object... arguments)
Execute prepared update statements
|
void |
executeQuery(String query)
Execute ad-hoc query on DB.
|
void |
executeQuery(String query,
boolean ignoreFailure)
Execute query on DB
|
void |
executeQuery(String query,
String tableName,
String hasColumnName)
Conditional ad-hoc query on DB
|
void |
executeScript(String filePath)
Helper method to run third party scripts like Quartz DDL
|
int |
executeUpdate(String query) |
int |
executeUpdate(String query,
boolean ignoreErrors) |
Class |
getColumnClass(String tableName,
String columnName)
Get type class of the column
|
DBAccessor.DBColumnInfo |
getColumnInfo(String tableName,
String columnName)
Obtain column metadata information by given table and column name
|
int |
getColumnType(String tableName,
String columnName)
Gets the column's SQL type
|
Connection |
getConnection() |
String |
getDbSchema()
Get database schema name
|
DBAccessor.DbType |
getDbType()
Get type of database platform
|
List<String> |
getIndexesList(String tableName,
boolean unique)
Gets list of index names from database metadata
|
List<Integer> |
getIntColumnValues(String tableName,
String columnName,
String[] conditionColumnNames,
String[] conditionValues,
boolean ignoreFailure)
Execute select
columnName from tableName
where columnNames values = values |
Map<Long,String> |
getKeyToStringColumnMap(String tableName,
String keyColumnName,
String valueColumnName,
String[] conditionColumnNames,
String[] conditionValues,
boolean ignoreFailure)
Execute select
keyColumnName, valueColumnName from tableName
where columnNames values = values |
Connection |
getNewConnection() |
org.eclipse.persistence.sessions.DatabaseSession |
getNewDatabaseSession()
Get a new DB session
|
String |
getPrimaryKeyConstraintName(String tableName)
Queries the database to determine the name of the primary key constraint on
the specified table.
|
boolean |
insertRow(String tableName,
String[] columnNames,
String[] values,
boolean ignoreFailure)
Insert row into table
|
boolean |
insertRowIfMissing(String tableName,
String[] columnNames,
String[] values,
boolean ignoreFailure)
Conditionally insert row into table if it does not already exist
|
boolean |
isColumnNullable(String tableName,
String columnName)
Check if column could be nullable
|
void |
moveColumnToAnotherTable(String sourceTableName,
DBAccessor.DBColumnInfo sourceColumn,
String sourceIDFieldName,
String targetTableName,
DBAccessor.DBColumnInfo targetColumn,
String targetIDFieldName,
Object initialValue)
Move column data from
sourceTableName to targetTableName using sourceIDFieldName and
targetIDFieldName keys to match right rows |
String |
quoteObjectName(String name)
Wraps object name with dbms-specific quotes
|
void |
renameColumn(String tableName,
String oldColumnName,
DBAccessor.DBColumnInfo columnInfo)
Rename existing column
|
void |
setColumnNullable(String tableName,
DBAccessor.DBColumnInfo columnInfo,
boolean nullable)
Sets the specified column to either allow or prohibit
NULL. |
void |
setColumnNullable(String tableName,
String columnName,
boolean nullable) |
boolean |
tableExists(String tableName)
Verify if table exists by looking at metadata.
|
boolean |
tableHasColumn(String tableName,
String... columnName)
Verify if table already has a column defined.
|
boolean |
tableHasColumn(String tableName,
String columnName)
Verify if table already has a column defined.
|
boolean |
tableHasData(String tableName)
Verify if table has any data
|
boolean |
tableHasForeignKey(String tableName,
String fkName)
Verify if table has a FK constraint.
|
boolean |
tableHasForeignKey(String tableName,
String referenceTableName,
String[] keyColumns,
String[] referenceColumns)
Verify if table already has a FK constraint.
|
boolean |
tableHasForeignKey(String tableName,
String refTableName,
String columnName,
String refColumnName)
Verify if table already has a FK constraint.
|
boolean |
tableHasIndex(String tableName,
boolean unique,
String indexName)
Check if index is already in scheme
|
boolean |
tableHasPrimaryKey(String tableName,
String columnName)
Table has primary key
|
void |
truncateTable(String tableName)
Delete all table data
|
void |
updateTable(String tableName,
DBAccessor.DBColumnInfo columnNameSrc,
DBAccessor.DBColumnInfo columnNameTgt)
Simple update operation on table
|
int |
updateTable(String tableName,
String columnName,
Object value,
String whereClause)
Simple update operation on table
|
void |
updateUniqueConstraint(String tableName,
String constraintName,
String... columnNames)
Add unique table constraint
|
Connection getConnection()
Connection getNewConnection()
String quoteObjectName(String name)
name - object name without quotesvoid createTable(String tableName, List<DBAccessor.DBColumnInfo> columnInfo, String... primaryKeyColumns) throws SQLException
tableName - columnInfo - primaryKeyColumns - SQLExceptionvoid createIndex(String indexName, String tableName, String... columnNames) throws SQLException
indexName - tableName - columnNames - SQLExceptionvoid createIndex(String indexName, String tableName, boolean isUnique, String... columnNames) throws SQLException
indexName - The name of the index to be createdtableName - The database table the index to be created oncolumnNames - The columns included into the indexisUnique - Specifies whether unique index is to be created.SQLException - Exception in case the index creation fails.void addFKConstraint(String tableName, String constraintName, String keyColumn, String referenceTableName, String referenceColumn, boolean ignoreFailure) throws SQLException
tableName - constraintName - keyColumn - referenceColumn - SQLExceptionvoid addFKConstraint(String tableName, String constraintName, String keyColumn, String referenceTableName, String referenceColumn, boolean shouldCascadeOnDelete, boolean ignoreFailure) throws SQLException
tableName - constraintName - keyColumn - referenceTableName - referenceColumn - shouldCascadeOnDelete - ignoreFailure - SQLExceptionvoid addFKConstraint(String tableName, String constraintName, String[] keyColumns, String referenceTableName, String[] referenceColumns, boolean shouldCascadeOnDelete, boolean ignoreFailure) throws SQLException
tableName - constraintName - keyColumns - referenceTableName - referenceColumns - shouldCascadeOnDelete - ignoreFailure - SQLExceptionvoid addFKConstraint(String tableName, String constraintName, String[] keyColumns, String referenceTableName, String[] referenceColumns, boolean ignoreFailure) throws SQLException
tableName - constraintName - keyColumns - referenceTableName - referenceColumns - ignoreFailure - SQLExceptionvoid addColumn(String tableName, DBAccessor.DBColumnInfo columnInfo) throws SQLException
DBAccessor.DBColumnInfo.isNullable and DBAccessor.DBColumnInfo.getDefaultValue()
methods and create a new column that has a DEFAULT constraint.
Oracle is, of course, the exception here since their syntax doesn't conform
to widely accepted SQL standards. Because they switch the order of the
NULL constraint and the DEFAULT constraint, we need to
create the table in a non-atomic fashion. This is because the EclipseLink
FieldDeclaration class hard codes the order of the constraints. In
the case of Oracle, we alter the nullability of the table after its
creation.
No work is performed if the column already exists.tableName - columnInfo - SQLExceptionvoid addUniqueConstraint(String tableName, String constraintName, String... columnNames) throws SQLException
constraintName - name of the constrainttableName - name of the tablecolumnNames - list of columnsSQLExceptionvoid updateUniqueConstraint(String tableName, String constraintName, String... columnNames) throws SQLException
constraintName - name of the constrainttableName - name of the tablecolumnNames - list of columnsSQLExceptionvoid addPKConstraint(String tableName, String constraintName, boolean ignoreErrors, String... columnName) throws SQLException
tableName - name of the tableconstraintName - name of the constraintcolumnName - name of the columnignoreErrors - true to ignore database errorsSQLExceptionvoid addPKConstraint(String tableName, String constraintName, String... columnName) throws SQLException
tableName - name of the tableconstraintName - name of the constraintcolumnName - name of the columnSQLExceptionvoid renameColumn(String tableName, String oldColumnName, DBAccessor.DBColumnInfo columnInfo) throws SQLException
tableName - oldColumnName - columnInfo - SQLExceptionvoid alterColumn(String tableName, DBAccessor.DBColumnInfo columnInfo) throws SQLException
addColumn(String, org.apache.ambari.server.orm.DBAccessor.DBColumnInfo)
updateTable(String, String, Object, String)
dropColumn(String, String)
renameColumn(String, String, org.apache.ambari.server.orm.DBAccessor.DBColumnInfo)tableName - columnInfo - SQLExceptionboolean insertRow(String tableName, String[] columnNames, String[] values, boolean ignoreFailure) throws SQLException
tableName - columnNames - values - ignoreFailure - SQLExceptionboolean insertRowIfMissing(String tableName, String[] columnNames, String[] values, boolean ignoreFailure) throws SQLException
tableName - columnNames - values - ignoreFailure - SQLExceptionint updateTable(String tableName, String columnName, Object value, String whereClause) throws SQLException
tableName - columnName - value - whereClause - SQLExceptionvoid updateTable(String tableName, DBAccessor.DBColumnInfo columnNameSrc, DBAccessor.DBColumnInfo columnNameTgt) throws SQLException
tableName - columnNameSrc - columnNameTgt - SQLExceptionvoid executeScript(String filePath) throws SQLException, IOException
filePath - SQLExceptionIOExceptionint executeUpdate(String query) throws SQLException
query - update queryjava.sql.StatementSQLExceptionint executeUpdate(String query, boolean ignoreErrors) throws SQLException
query - update queryignoreErrors - true to ignore errorsjava.sql.StatementSQLExceptionvoid executeQuery(String query, String tableName, String hasColumnName) throws SQLException
query - tableName - hasColumnName - SQLExceptionvoid executeQuery(String query) throws SQLException
query - SQLExceptionvoid executeQuery(String query, boolean ignoreFailure) throws SQLException
query - ignoreFailure - SQLExceptionvoid executePreparedQuery(String query, Object... arguments) throws SQLException
query - query to executearguments - list of arguments for prepared statementSQLExceptionvoid executePreparedQuery(String query, boolean ignoreFailure, Object... arguments) throws SQLException
query - query to executeignoreFailure - determines if exceptions during query execution should be ignoredarguments - list of arguments for prepared statementSQLExceptionvoid executePreparedUpdate(String query, Object... arguments) throws SQLException
query - query to executearguments - list of arguments for prepared statementSQLExceptionvoid executePreparedUpdate(String query, boolean ignoreFailure, Object... arguments) throws SQLException
query - ignoreFailure - arguments - SQLExceptionList<Integer> getIntColumnValues(String tableName, String columnName, String[] conditionColumnNames, String[] conditionValues, boolean ignoreFailure) throws SQLException
columnName from tableName
where columnNames values = valuestableName - the table namecolumnName - the name of the column with the data to selectconditionColumnNames - an array of column names to use in the where clauseconditionValues - an array of value to pair with the column names in conditionColumnNamesignoreFailure - true to ignore failures executing the query; false otherwise (errors building the query will be thrown, however)SQLExceptionMap<Long,String> getKeyToStringColumnMap(String tableName, String keyColumnName, String valueColumnName, String[] conditionColumnNames, String[] conditionValues, boolean ignoreFailure) throws SQLException
keyColumnName, valueColumnName from tableName
where columnNames values = valuestableName - the table namekeyColumnName - the name of the column with the key data to selectvalueColumnName - the name of the column with the value data to selectconditionColumnNames - an array of column names to use in the where clauseconditionValues - an array of value to pair with the column names in conditionColumnNamesignoreFailure - true to ignore failures executing the query; false otherwise (errors building the query will be thrown, however)SQLExceptionvoid dropTable(String tableName) throws SQLException
tableName - SQLExceptionvoid truncateTable(String tableName) throws SQLException
tableName - SQLExceptionvoid dropColumn(String tableName, String columnName) throws SQLException
tableName - columnName - SQLExceptionvoid dropSequence(String sequenceName) throws SQLException
sequenceName - SQLExceptionvoid dropFKConstraint(String tableName, String constraintName) throws SQLException
Configuration.DatabaseType.MYSQL, this will also ensure that any associated
indexes are also dropped.tableName - name of the tableconstraintName - name of the constraintSQLExceptionvoid dropPKConstraint(String tableName, String constraintName, boolean ignoreFailure, boolean cascade) throws SQLException
tableName - constraintName - name of the constraintignoreFailure - cascade - cascade deleteSQLExceptionvoid dropPKConstraint(String tableName, String constraintName, boolean cascade) throws SQLException
tableName - name of the tableconstraintName - name of the constraintcascade - cascade deleteSQLExceptionvoid dropPKConstraint(String tableName, String constraintName, String columnName, boolean cascade) throws SQLException
tableName - name of the tableconstraintName - name of the constraintcolumnName - name of the column from the pk constraintcascade - cascade deleteSQLExceptionvoid dropFKConstraint(String tableName, String constraintName, boolean ignoreFailure) throws SQLException
tableName - name of the tableconstraintName - name of the constraintSQLExceptionvoid dropUniqueConstraint(String tableName, String constraintName, boolean ignoreFailure) throws SQLException
tableName - name of the tableconstraintName - name of the constraintignoreFailure - SQLExceptionvoid dropUniqueConstraint(String tableName, String constraintName) throws SQLException
tableName - name of the tableconstraintName - name of the constraintSQLExceptionboolean tableExists(String tableName) throws SQLException
tableName - name of the tableSQLExceptionboolean tableHasData(String tableName) throws SQLException
tableName - SQLExceptionboolean tableHasColumn(String tableName, String columnName) throws SQLException
tableName - columnName - SQLExceptionboolean tableHasColumn(String tableName, String... columnName) throws SQLException
tableName - name of the tablecolumnName - name of the column to checkSQLExceptionboolean tableHasForeignKey(String tableName, String fkName) throws SQLException
tableName - fkName - SQLExceptionboolean tableHasForeignKey(String tableName, String refTableName, String columnName, String refColumnName) throws SQLException
tableName - refTableName - columnName - refColumnName - SQLExceptionboolean tableHasForeignKey(String tableName, String referenceTableName, String[] keyColumns, String[] referenceColumns) throws SQLException
tableName - referenceTableName - keyColumns - referenceColumns - SQLExceptionorg.eclipse.persistence.sessions.DatabaseSession getNewDatabaseSession()
boolean tableHasPrimaryKey(String tableName, String columnName) throws SQLException
tableName - name of the tablecolumnName - name of the constraint, could be nullSQLExceptionList<String> getIndexesList(String tableName, boolean unique) throws SQLException
tableName - the name of the table (not null).unique - list only unique indexes (not null).SQLExceptionboolean tableHasIndex(String tableName, boolean unique, String indexName) throws SQLException
tableName - the name of the table (not null).unique - list only unique indexes (not null).indexName - name of the index to checkSQLExceptionint getColumnType(String tableName, String columnName) throws SQLException
tableName - the name of the table (not null).columnName - the name of the column to retrieve type for (not null).SQLExceptionClass getColumnClass(String tableName, String columnName) throws SQLException, ClassNotFoundException
tableName - name of the tablecolumnName - name of the columnSQLExceptionClassNotFoundExceptionboolean isColumnNullable(String tableName, String columnName) throws SQLException
tableName - name of the tablecolumnName - name of the columnSQLExceptionvoid setColumnNullable(String tableName, DBAccessor.DBColumnInfo columnInfo, boolean nullable) throws SQLException
NULL.tableName - the name of the table (not null).columnInfo - the column object to get name and type of column (not null).nullable - true to indicate that the column allows NULL
values, false otherwise.SQLExceptionvoid setColumnNullable(String tableName, String columnName, boolean nullable) throws SQLException
SQLExceptionvoid changeColumnType(String tableName, String columnName, Class fromType, Class toType) throws SQLException
tableName - name of the tablecolumnName - name of the columnfromType - previous typetoType - new desired typeSQLExceptionDBAccessor.DBColumnInfo getColumnInfo(String tableName, String columnName) throws SQLException
tableName - name of the tablecolumnName - name of the columnSQLExceptionString getPrimaryKeyConstraintName(String tableName) throws SQLException
Configuration.DatabaseType.POSTGRES, Configuration.DatabaseType.ORACLE and
Configuration.DatabaseType.SQL_SERVER. Configuration.DatabaseType.MYSQL does not need
this since PKs can be dropped without referencing their name.tableName - the name of the table to lookup the PK constraint.null if none.SQLExceptionvoid dropPKConstraint(String tableName, String defaultConstraintName) throws SQLException
tableName - the table to drop the PK from (not null).defaultConstraintName - the default name of the PK constraint if none is found.SQLExceptionvoid addDefaultConstraint(String tableName, DBAccessor.DBColumnInfo column) throws SQLException
tableName - the table where the column is defined (not null).column - the column information which contains the default value (not
null).SQLExceptionvoid moveColumnToAnotherTable(String sourceTableName, DBAccessor.DBColumnInfo sourceColumn, String sourceIDFieldName, String targetTableName, DBAccessor.DBColumnInfo targetColumn, String targetIDFieldName, Object initialValue) throws SQLException
sourceTableName to targetTableName using sourceIDFieldName and
targetIDFieldName keys to match right rowssourceTableName - the source table namesourceColumn - the source column namesourceIDFieldName - the source id key filed name matched with targetIDFieldNametargetTableName - the target table nametargetColumn - the target column nametargetIDFieldName - the target id key name matched with sourceIDFieldNameinitialValue - initial value for null-contained cellsSQLExceptionvoid copyColumnToAnotherTable(String sourceTableName, DBAccessor.DBColumnInfo sourceColumn, String sourceIDFieldName1, String sourceIDFieldName2, String sourceIDFieldName3, String targetTableName, DBAccessor.DBColumnInfo targetColumn, String targetIDFieldName1, String targetIDFieldName2, String targetIDFieldName3, String sourceConditionFieldName, String condition, Object initialValue) throws SQLException
targetTable by matching
table keys sourceIDColumnName and targetIDColumnName
and condition sourceConditionFieldName = conditionsourceTableName - the source table namesourceColumn - the source column namesourceIDFieldName1 - the source id key filed name matched with targetIDFieldName1sourceIDFieldName2 - the source id key filed name matched with targetIDFieldName2sourceIDFieldName3 - the source id key filed name matched with targetIDFieldName3targetTableName - the target table nametargetColumn - the target column nametargetIDFieldName1 - the target id key name matched with sourceIDFieldName1targetIDFieldName2 - the target id key name matched with sourceIDFieldName2targetIDFieldName3 - the target id key name matched with sourceIDFieldName3sourceConditionFieldName - source key column name which should match conditioncondition - value which should match sourceConditionFieldNameinitialValue - initial value for null-contained cellsSQLExceptionvoid clearTable(String tableName) throws SQLException
tableName - name of the tableSQLExceptionvoid clearTableColumn(String tableName, String columnName, Object value) throws SQLException
value for columnName columntableName - name of the tablecolumnName - name of the column name to be updatevalue - data to use for updateSQLExceptionDBAccessor.DbType getDbType()
String getDbSchema()
Copyright © 2022 Apache Software Foundation. All rights reserved.