Class CommonDatabaseAdapter
- java.lang.Object
-
- com.ibm.fhir.database.utils.common.CommonDatabaseAdapter
-
- All Implemented Interfaces:
IDatabaseAdapter,IDatabaseTypeAdapter
- Direct Known Subclasses:
Db2Adapter,DerbyAdapter,PostgresAdapter
public abstract class CommonDatabaseAdapter extends Object implements IDatabaseAdapter, IDatabaseTypeAdapter
Provides schema control functions common to our supported databases (DB2 and Derby)
-
-
Field Summary
Fields Modifier and Type Field Description protected IConnectionProviderconnectionProviderprotected IDatabaseTargettarget
-
Constructor Summary
Constructors Modifier Constructor Description protectedCommonDatabaseAdapter()Constructor used by AddColumn only for getting DB type specific column name.protectedCommonDatabaseAdapter(IConnectionProvider cp, IDatabaseTranslator dt)Public constructor for when we're using a connection providerprotectedCommonDatabaseAdapter(IDatabaseTarget tgt, IDatabaseTranslator dt)Protected constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddNewTenantPartitions(Collection<Table> tables, String schemaName, int newTenantId)Add a new tenant partition to each of the tables in the collection.intallocateTenant(String adminSchemaName, String schemaName, String tenantName, String tenantKey, String tenantSalt, String idSequenceName)Allocate a new tenantvoidalterSequenceRestartWith(String schemaName, String sequenceName, long restartWith, int cache, int incrementBy)Sets/resets the sequence to start with the given value.voidalterTableAddColumn(String schemaName, String tableName, ColumnBase column)Add a new column to an existing tablevoidalterTableColumnIdentityCache(String schemaName, String tableName, String columnName, int cache)Change the CACHE value of the named identity generated always columnprotected StringbuildColumns(List<ColumnBase> columns, IdentityDef identity)Build the list of columns in the create table statementprotected StringbuildCreateTableStatement(String schema, String name, List<ColumnBase> columns, PrimaryKeyDef pkDef, IdentityDef identity, String tablespaceName, List<With> withs, List<CheckConstraint> checkConstraints)Generate a create table statement suitable for DerbyvoidcreateForeignKeyConstraint(String constraintName, String schemaName, String name, String targetSchema, String targetTable, String targetColumnName, String tenantColumnName, List<String> columns, boolean enforced)voidcreateIndex(String schemaName, String tableName, String indexName, String tenantColumnName, List<OrderedColumnDef> indexColumns)voidcreateOrReplaceFunction(String schemaName, String functionName, Supplier<String> supplier)creates or replaces the SQL functionvoidcreateOrReplaceProcedure(String schemaName, String procedureName, Supplier<String> supplier)Create the stored procedure using the DDL text provided by the suppliervoidcreateOrReplaceView(String schemaName, String viewName, String selectClause)Create or replace the viewvoidcreateSequence(String schemaName, String sequenceName, long startWith, int cache, int incrementBy)voidcreateUniqueConstraint(String constraintName, List<String> columns, String schemaName, String name)create a unique constraint on a table.voidcreateUniqueIndex(String schemaName, String tableName, String indexName, String tenantColumnName, List<OrderedColumnDef> indexColumns)voidcreateUniqueIndex(String schemaName, String tableName, String indexName, String tenantColumnName, List<OrderedColumnDef> indexColumns, List<String> includeColumns)voidcreateView(String schemaName, String viewName, String selectClause)Create the view as defined by the selectClausevoiddeleteTenantMeta(String adminSchemaName, int tenantId)Delete all the metadata associated with the given tenant identifier, as long as the tenant status is DROPPED.voiddropForeignKey(String schemaName, String tableName, String constraintName)Drop the FK on the table with the given constraint namevoiddropFunction(String schemaName, String functionName)drops a given functionvoiddropIndex(String schemaName, String indexName)Drop the named indexvoiddropPermission(String schemaName, String permissionName)Drop permission object from the schemavoiddropProcedure(String schemaName, String procedureName)Drop the given procedurevoiddropSequence(String schemaName, String sequenceName)voiddropTable(String schemaName, String tableName)Drop table from the schemavoiddropVariable(String schemaName, String variableName)voiddropView(String schemaName, String viewName)Drop the view from the databaseintfindTenantId(String adminSchemaName, String tenantName)Get the tenant id for the given schema and tenant nameStringgetQualifiedName(String schemaName, String objectName)Return the fully qualified name in the form "SCHEMA.OBJECT" Validates that both schema and object names are validIDatabaseTranslatorgetTranslator()Get theIDatabaseTranslatorassociated with this adaptervoidgrantFunctionPrivileges(String schemaName, String functionName, Collection<Privilege> privileges, String toUser)grants permissions on a given functionvoidgrantObjectPrivileges(String schemaName, String tableName, Collection<Privilege> privileges, String toUser)Grant the list of privileges on the named object to the user.voidgrantProcedurePrivileges(String schemaName, String procedureName, Collection<Privilege> privileges, String toUser)Grant the collection of privileges on the named procedure to the uservoidgrantSequencePrivileges(String schemaName, String variableName, Collection<Privilege> privileges, String toUser)Grant the collection of privileges on the named variable to the uservoidgrantVariablePrivileges(String schemaName, String variableName, Collection<Privilege> privileges, String toUser)Grant the collection of privileges on the named variable to the userprotected List<OrderedColumnDef>prefixTenantColumn(String tenantColumnName, List<OrderedColumnDef> columns)Prefix the tenantColumnName to the list of columns, or do nothing if tenantColumnName is nullvoidreorgTable(String schemaName, String tableName)Reorg the table if the underlying database supports it.voidrunStatement(IDatabaseStatement stmt)Run the statement using the connectionProvider to obtain a new connection.<T> TrunStatement(IDatabaseSupplier<T> supplier)Run the given supplier statement against the database represented by this adapterprotected voidrunStatement(String ddl)Execute the statement on a connection managed by our connection providervoidupdateTenantStatus(String adminSchemaName, int tenantId, TenantStatus status)Update the tenant status-
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.ibm.fhir.database.utils.api.IDatabaseAdapter
activateRowAccessControl, checkCompatibility, createArrType, createIntVariable, createOrReplacePermission, createRowType, createSchema, createTable, createTablespace, createTablespace, createTenantPartitions, deactivateRowAccessControl, detachPartition, disableForeignKey, doesTableExist, dropDetachedPartitions, dropTablespace, dropTenantTablespace, dropType, enableForeignKey, listSchemaObjects, removeTenantPartitions, setIntegrityOff, setIntegrityUnchecked, setIntVariable, useSessionVariable
-
Methods inherited from interface com.ibm.fhir.database.utils.api.IDatabaseTypeAdapter
blobClause, clobClause, doubleClause, timestampClause, varbinaryClause, varcharClause
-
-
-
-
Field Detail
-
target
protected final IDatabaseTarget target
-
connectionProvider
protected final IConnectionProvider connectionProvider
-
-
Constructor Detail
-
CommonDatabaseAdapter
protected CommonDatabaseAdapter(IDatabaseTarget tgt, IDatabaseTranslator dt)
Protected constructor- Parameters:
tgt- database targeteddt- the translator for this type of database
-
CommonDatabaseAdapter
protected CommonDatabaseAdapter(IConnectionProvider cp, IDatabaseTranslator dt)
Public constructor for when we're using a connection provider- Parameters:
cp-dt-
-
CommonDatabaseAdapter
protected CommonDatabaseAdapter()
Constructor used by AddColumn only for getting DB type specific column name.
-
-
Method Detail
-
getTranslator
public IDatabaseTranslator getTranslator()
Description copied from interface:IDatabaseAdapterGet theIDatabaseTranslatorassociated with this adapter- Specified by:
getTranslatorin interfaceIDatabaseAdapter- Returns:
-
buildColumns
protected String buildColumns(List<ColumnBase> columns, IdentityDef identity)
Build the list of columns in the create table statement
-
buildCreateTableStatement
protected String buildCreateTableStatement(String schema, String name, List<ColumnBase> columns, PrimaryKeyDef pkDef, IdentityDef identity, String tablespaceName, List<With> withs, List<CheckConstraint> checkConstraints)
Generate a create table statement suitable for Derby- Parameters:
schema-name-columns-pkDef-tablespaceName-withs- the list of table metadata parameters- Returns:
-
createUniqueIndex
public void createUniqueIndex(String schemaName, String tableName, String indexName, String tenantColumnName, List<OrderedColumnDef> indexColumns, List<String> includeColumns)
- Specified by:
createUniqueIndexin interfaceIDatabaseAdapter
-
createUniqueIndex
public void createUniqueIndex(String schemaName, String tableName, String indexName, String tenantColumnName, List<OrderedColumnDef> indexColumns)
- Specified by:
createUniqueIndexin interfaceIDatabaseAdapter
-
createIndex
public void createIndex(String schemaName, String tableName, String indexName, String tenantColumnName, List<OrderedColumnDef> indexColumns)
- Specified by:
createIndexin interfaceIDatabaseAdapter
-
prefixTenantColumn
protected List<OrderedColumnDef> prefixTenantColumn(String tenantColumnName, List<OrderedColumnDef> columns)
Prefix the tenantColumnName to the list of columns, or do nothing if tenantColumnName is null- Parameters:
tenantColumnName-columns-- Returns:
-
runStatement
protected void runStatement(String ddl)
Execute the statement on a connection managed by our connection provider- Parameters:
ddl-
-
getQualifiedName
public String getQualifiedName(String schemaName, String objectName)
Return the fully qualified name in the form "SCHEMA.OBJECT" Validates that both schema and object names are valid- Parameters:
schemaName-objectName-- Returns:
- the fully qualified name
- Throws:
IllegalArgumentException- if either name is not a valid database object name
-
dropTable
public void dropTable(String schemaName, String tableName)
Description copied from interface:IDatabaseAdapterDrop table from the schema- Specified by:
dropTablein interfaceIDatabaseAdapter
-
dropProcedure
public void dropProcedure(String schemaName, String procedureName)
Description copied from interface:IDatabaseAdapterDrop the given procedure- Specified by:
dropProcedurein interfaceIDatabaseAdapter
-
dropFunction
public void dropFunction(String schemaName, String functionName)
Description copied from interface:IDatabaseAdapterdrops a given function- Specified by:
dropFunctionin interfaceIDatabaseAdapter
-
dropPermission
public void dropPermission(String schemaName, String permissionName)
Description copied from interface:IDatabaseAdapterDrop permission object from the schema- Specified by:
dropPermissionin interfaceIDatabaseAdapter
-
dropVariable
public void dropVariable(String schemaName, String variableName)
- Specified by:
dropVariablein interfaceIDatabaseAdapter
-
createForeignKeyConstraint
public void createForeignKeyConstraint(String constraintName, String schemaName, String name, String targetSchema, String targetTable, String targetColumnName, String tenantColumnName, List<String> columns, boolean enforced)
- Specified by:
createForeignKeyConstraintin interfaceIDatabaseAdapter
-
createUniqueConstraint
public void createUniqueConstraint(String constraintName, List<String> columns, String schemaName, String name)
Description copied from interface:IDatabaseAdaptercreate a unique constraint on a table.- Specified by:
createUniqueConstraintin interfaceIDatabaseAdapter
-
allocateTenant
public int allocateTenant(String adminSchemaName, String schemaName, String tenantName, String tenantKey, String tenantSalt, String idSequenceName)
Description copied from interface:IDatabaseAdapterAllocate a new tenant- Specified by:
allocateTenantin interfaceIDatabaseAdapter- Returns:
-
updateTenantStatus
public void updateTenantStatus(String adminSchemaName, int tenantId, TenantStatus status)
Update the tenant status- Specified by:
updateTenantStatusin interfaceIDatabaseAdapter- Parameters:
adminSchemaName-tenantId-status-
-
addNewTenantPartitions
public void addNewTenantPartitions(Collection<Table> tables, String schemaName, int newTenantId)
Add a new tenant partition to each of the tables in the collection. Idempotent, so can be run to add partitions for existing tenants to new tables- Specified by:
addNewTenantPartitionsin interfaceIDatabaseAdapter- Parameters:
tables-schemaName-newTenantId-
-
runStatement
public void runStatement(IDatabaseStatement stmt)
Run the statement using the connectionProvider to obtain a new connection. Also, there should be a transaction open on the current thread at this time- Specified by:
runStatementin interfaceIDatabaseAdapter- Parameters:
stmt-
-
runStatement
public <T> T runStatement(IDatabaseSupplier<T> supplier)
Description copied from interface:IDatabaseAdapterRun the given supplier statement against the database represented by this adapter- Specified by:
runStatementin interfaceIDatabaseAdapter- Returns:
-
createOrReplaceProcedure
public void createOrReplaceProcedure(String schemaName, String procedureName, Supplier<String> supplier)
Description copied from interface:IDatabaseAdapterCreate the stored procedure using the DDL text provided by the supplier- Specified by:
createOrReplaceProcedurein interfaceIDatabaseAdapter
-
createOrReplaceFunction
public void createOrReplaceFunction(String schemaName, String functionName, Supplier<String> supplier)
Description copied from interface:IDatabaseAdaptercreates or replaces the SQL function- Specified by:
createOrReplaceFunctionin interfaceIDatabaseAdapter
-
createSequence
public void createSequence(String schemaName, String sequenceName, long startWith, int cache, int incrementBy)
- Specified by:
createSequencein interfaceIDatabaseAdapterstartWith- the START WITH value for the sequencecache- the sequence CACHE value
-
dropSequence
public void dropSequence(String schemaName, String sequenceName)
- Specified by:
dropSequencein interfaceIDatabaseAdapter
-
alterSequenceRestartWith
public void alterSequenceRestartWith(String schemaName, String sequenceName, long restartWith, int cache, int incrementBy)
Description copied from interface:IDatabaseAdapterSets/resets the sequence to start with the given value.- Specified by:
alterSequenceRestartWithin interfaceIDatabaseAdapter
-
alterTableColumnIdentityCache
public void alterTableColumnIdentityCache(String schemaName, String tableName, String columnName, int cache)
Description copied from interface:IDatabaseAdapterChange the CACHE value of the named identity generated always column- Specified by:
alterTableColumnIdentityCachein interfaceIDatabaseAdapter
-
dropForeignKey
public void dropForeignKey(String schemaName, String tableName, String constraintName)
Description copied from interface:IDatabaseAdapterDrop the FK on the table with the given constraint name- Specified by:
dropForeignKeyin interfaceIDatabaseAdapter
-
findTenantId
public int findTenantId(String adminSchemaName, String tenantName)
Description copied from interface:IDatabaseAdapterGet the tenant id for the given schema and tenant name- Specified by:
findTenantIdin interfaceIDatabaseAdapter- Returns:
-
grantObjectPrivileges
public void grantObjectPrivileges(String schemaName, String tableName, Collection<Privilege> privileges, String toUser)
Description copied from interface:IDatabaseAdapterGrant the list of privileges on the named object to the user. This is a general purpose method which can be used to specify privileges for any object type which doesn't need the object type to be specified in the grant DDL.- Specified by:
grantObjectPrivilegesin interfaceIDatabaseAdapter
-
grantProcedurePrivileges
public void grantProcedurePrivileges(String schemaName, String procedureName, Collection<Privilege> privileges, String toUser)
Description copied from interface:IDatabaseAdapterGrant the collection of privileges on the named procedure to the user- Specified by:
grantProcedurePrivilegesin interfaceIDatabaseAdapter
-
grantFunctionPrivileges
public void grantFunctionPrivileges(String schemaName, String functionName, Collection<Privilege> privileges, String toUser)
Description copied from interface:IDatabaseAdaptergrants permissions on a given function- Specified by:
grantFunctionPrivilegesin interfaceIDatabaseAdapter
-
grantVariablePrivileges
public void grantVariablePrivileges(String schemaName, String variableName, Collection<Privilege> privileges, String toUser)
Description copied from interface:IDatabaseAdapterGrant the collection of privileges on the named variable to the user- Specified by:
grantVariablePrivilegesin interfaceIDatabaseAdapter
-
grantSequencePrivileges
public void grantSequencePrivileges(String schemaName, String variableName, Collection<Privilege> privileges, String toUser)
Description copied from interface:IDatabaseAdapterGrant the collection of privileges on the named variable to the user- Specified by:
grantSequencePrivilegesin interfaceIDatabaseAdapter
-
deleteTenantMeta
public void deleteTenantMeta(String adminSchemaName, int tenantId)
Description copied from interface:IDatabaseAdapterDelete all the metadata associated with the given tenant identifier, as long as the tenant status is DROPPED.- Specified by:
deleteTenantMetain interfaceIDatabaseAdapter
-
dropIndex
public void dropIndex(String schemaName, String indexName)
Description copied from interface:IDatabaseAdapterDrop the named index- Specified by:
dropIndexin interfaceIDatabaseAdapter
-
dropView
public void dropView(String schemaName, String viewName)
Description copied from interface:IDatabaseAdapterDrop the view from the database- Specified by:
dropViewin interfaceIDatabaseAdapter
-
createOrReplaceView
public void createOrReplaceView(String schemaName, String viewName, String selectClause)
Description copied from interface:IDatabaseAdapterCreate or replace the view- Specified by:
createOrReplaceViewin interfaceIDatabaseAdapter
-
createView
public void createView(String schemaName, String viewName, String selectClause)
Description copied from interface:IDatabaseAdapterCreate the view as defined by the selectClause- Specified by:
createViewin interfaceIDatabaseAdapter
-
alterTableAddColumn
public void alterTableAddColumn(String schemaName, String tableName, ColumnBase column)
Description copied from interface:IDatabaseAdapterAdd a new column to an existing table- Specified by:
alterTableAddColumnin interfaceIDatabaseAdapter
-
reorgTable
public void reorgTable(String schemaName, String tableName)
Description copied from interface:IDatabaseAdapterReorg the table if the underlying database supports it. Required after columns are added/removed from a table.- Specified by:
reorgTablein interfaceIDatabaseAdapter
-
-