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
- SQLException
void createIndex(String indexName, String tableName, String... columnNames) throws SQLException
indexName
- tableName
- columnNames
- SQLException
void 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
- SQLException
void addFKConstraint(String tableName, String constraintName, String keyColumn, String referenceTableName, String referenceColumn, boolean shouldCascadeOnDelete, boolean ignoreFailure) throws SQLException
tableName
- constraintName
- keyColumn
- referenceTableName
- referenceColumn
- shouldCascadeOnDelete
- ignoreFailure
- SQLException
void addFKConstraint(String tableName, String constraintName, String[] keyColumns, String referenceTableName, String[] referenceColumns, boolean shouldCascadeOnDelete, boolean ignoreFailure) throws SQLException
tableName
- constraintName
- keyColumns
- referenceTableName
- referenceColumns
- shouldCascadeOnDelete
- ignoreFailure
- SQLException
void addFKConstraint(String tableName, String constraintName, String[] keyColumns, String referenceTableName, String[] referenceColumns, boolean ignoreFailure) throws SQLException
tableName
- constraintName
- keyColumns
- referenceTableName
- referenceColumns
- ignoreFailure
- SQLException
void 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
- SQLException
void addUniqueConstraint(String tableName, String constraintName, String... columnNames) throws SQLException
constraintName
- name of the constrainttableName
- name of the tablecolumnNames
- list of columnsSQLException
void updateUniqueConstraint(String tableName, String constraintName, String... columnNames) throws SQLException
constraintName
- name of the constrainttableName
- name of the tablecolumnNames
- list of columnsSQLException
void 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 errorsSQLException
void addPKConstraint(String tableName, String constraintName, String... columnName) throws SQLException
tableName
- name of the tableconstraintName
- name of the constraintcolumnName
- name of the columnSQLException
void renameColumn(String tableName, String oldColumnName, DBAccessor.DBColumnInfo columnInfo) throws SQLException
tableName
- oldColumnName
- columnInfo
- SQLException
void 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
- SQLException
boolean insertRow(String tableName, String[] columnNames, String[] values, boolean ignoreFailure) throws SQLException
tableName
- columnNames
- values
- ignoreFailure
- SQLException
boolean insertRowIfMissing(String tableName, String[] columnNames, String[] values, boolean ignoreFailure) throws SQLException
tableName
- columnNames
- values
- ignoreFailure
- SQLException
int updateTable(String tableName, String columnName, Object value, String whereClause) throws SQLException
tableName
- columnName
- value
- whereClause
- SQLException
void updateTable(String tableName, DBAccessor.DBColumnInfo columnNameSrc, DBAccessor.DBColumnInfo columnNameTgt) throws SQLException
tableName
- columnNameSrc
- columnNameTgt
- SQLException
void executeScript(String filePath) throws SQLException, IOException
filePath
- SQLException
IOException
int executeUpdate(String query) throws SQLException
query
- update queryjava.sql.Statement
SQLException
int executeUpdate(String query, boolean ignoreErrors) throws SQLException
query
- update queryignoreErrors
- true to ignore errorsjava.sql.Statement
SQLException
void executeQuery(String query, String tableName, String hasColumnName) throws SQLException
query
- tableName
- hasColumnName
- SQLException
void executeQuery(String query) throws SQLException
query
- SQLException
void executeQuery(String query, boolean ignoreFailure) throws SQLException
query
- ignoreFailure
- SQLException
void executePreparedQuery(String query, Object... arguments) throws SQLException
query
- query to executearguments
- list of arguments for prepared statementSQLException
void 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 statementSQLException
void executePreparedUpdate(String query, Object... arguments) throws SQLException
query
- query to executearguments
- list of arguments for prepared statementSQLException
void executePreparedUpdate(String query, boolean ignoreFailure, Object... arguments) throws SQLException
query
- ignoreFailure
- arguments
- SQLException
List<Integer> getIntColumnValues(String tableName, String columnName, String[] conditionColumnNames, String[] conditionValues, boolean ignoreFailure) throws SQLException
columnName
from tableName
where columnNames
values = values
tableName
- 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)SQLException
Map<Long,String> getKeyToStringColumnMap(String tableName, String keyColumnName, String valueColumnName, String[] conditionColumnNames, String[] conditionValues, boolean ignoreFailure) throws SQLException
keyColumnName
, valueColumnName
from tableName
where columnNames
values = values
tableName
- 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)SQLException
void dropTable(String tableName) throws SQLException
tableName
- SQLException
void truncateTable(String tableName) throws SQLException
tableName
- SQLException
void dropColumn(String tableName, String columnName) throws SQLException
tableName
- columnName
- SQLException
void dropSequence(String sequenceName) throws SQLException
sequenceName
- SQLException
void 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 constraintSQLException
void dropPKConstraint(String tableName, String constraintName, boolean ignoreFailure, boolean cascade) throws SQLException
tableName
- constraintName
- name of the constraintignoreFailure
- cascade
- cascade deleteSQLException
void dropPKConstraint(String tableName, String constraintName, boolean cascade) throws SQLException
tableName
- name of the tableconstraintName
- name of the constraintcascade
- cascade deleteSQLException
void 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 deleteSQLException
void dropFKConstraint(String tableName, String constraintName, boolean ignoreFailure) throws SQLException
tableName
- name of the tableconstraintName
- name of the constraintSQLException
void dropUniqueConstraint(String tableName, String constraintName, boolean ignoreFailure) throws SQLException
tableName
- name of the tableconstraintName
- name of the constraintignoreFailure
- SQLException
void dropUniqueConstraint(String tableName, String constraintName) throws SQLException
tableName
- name of the tableconstraintName
- name of the constraintSQLException
boolean tableExists(String tableName) throws SQLException
tableName
- name of the tableSQLException
boolean tableHasData(String tableName) throws SQLException
tableName
- SQLException
boolean tableHasColumn(String tableName, String columnName) throws SQLException
tableName
- columnName
- SQLException
boolean tableHasColumn(String tableName, String... columnName) throws SQLException
tableName
- name of the tablecolumnName
- name of the column to checkSQLException
boolean tableHasForeignKey(String tableName, String fkName) throws SQLException
tableName
- fkName
- SQLException
boolean tableHasForeignKey(String tableName, String refTableName, String columnName, String refColumnName) throws SQLException
tableName
- refTableName
- columnName
- refColumnName
- SQLException
boolean tableHasForeignKey(String tableName, String referenceTableName, String[] keyColumns, String[] referenceColumns) throws SQLException
tableName
- referenceTableName
- keyColumns
- referenceColumns
- SQLException
org.eclipse.persistence.sessions.DatabaseSession getNewDatabaseSession()
boolean tableHasPrimaryKey(String tableName, String columnName) throws SQLException
tableName
- name of the tablecolumnName
- name of the constraint, could be null
SQLException
List<String> getIndexesList(String tableName, boolean unique) throws SQLException
tableName
- the name of the table (not null
).unique
- list only unique indexes (not null
).SQLException
boolean 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 checkSQLException
int 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
).SQLException
Class getColumnClass(String tableName, String columnName) throws SQLException, ClassNotFoundException
tableName
- name of the tablecolumnName
- name of the columnSQLException
ClassNotFoundException
boolean isColumnNullable(String tableName, String columnName) throws SQLException
tableName
- name of the tablecolumnName
- name of the columnSQLException
void 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.SQLException
void setColumnNullable(String tableName, String columnName, boolean nullable) throws SQLException
SQLException
void changeColumnType(String tableName, String columnName, Class fromType, Class toType) throws SQLException
tableName
- name of the tablecolumnName
- name of the columnfromType
- previous typetoType
- new desired typeSQLException
DBAccessor.DBColumnInfo getColumnInfo(String tableName, String columnName) throws SQLException
tableName
- name of the tablecolumnName
- name of the columnSQLException
String 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.SQLException
void 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.SQLException
void 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
).SQLException
void 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 targetIDFieldName
targetTableName
- the target table nametargetColumn
- the target column nametargetIDFieldName
- the target id key name matched with sourceIDFieldName
initialValue
- initial value for null-contained cellsSQLException
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) throws SQLException
targetTable
by matching
table keys sourceIDColumnName
and targetIDColumnName
and condition sourceConditionFieldName
= condition
sourceTableName
- the source table namesourceColumn
- the source column namesourceIDFieldName1
- the source id key filed name matched with targetIDFieldName1
sourceIDFieldName2
- the source id key filed name matched with targetIDFieldName2
sourceIDFieldName3
- the source id key filed name matched with targetIDFieldName3
targetTableName
- the target table nametargetColumn
- the target column nametargetIDFieldName1
- the target id key name matched with sourceIDFieldName1
targetIDFieldName2
- the target id key name matched with sourceIDFieldName2
targetIDFieldName3
- the target id key name matched with sourceIDFieldName3
sourceConditionFieldName
- source key column name which should match condition
condition
- value which should match sourceConditionFieldName
initialValue
- initial value for null-contained cellsSQLException
void clearTable(String tableName) throws SQLException
tableName
- name of the tableSQLException
void 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 updateSQLException
DBAccessor.DbType getDbType()
String getDbSchema()
Copyright © 2022 Apache Software Foundation. All rights reserved.