Package com.ibm.fhir.database.utils.api
Interface IDatabaseAdapter
-
- All Known Implementing Classes:
CommonDatabaseAdapter
,Db2Adapter
,DerbyAdapter
,PostgresAdapter
public interface IDatabaseAdapter
Abstraction of the SQL to use for a given database. This allows us to hide as much as possible the differences in syntax and support between DB2 and Derby (which is used for unit-testing). Derby is pretty close to DB2 in most cases, but notably does not support partitioning, variables or SPL stored procedures.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description void
activateRowAccessControl(String schemaName, String tableName)
ALTER TABLEACTIVATE ROW ACCESS CONTROL void
addNewTenantPartitions(Collection<Table> tables, String schemaName, int newTenantId)
Add a new tenant partition to each of the tables in the collection.int
allocateTenant(String adminSchemaName, String schemaName, String tenantName, String tenantKey, String tenantSalt, String idSequenceName)
Allocate a new tenantvoid
alterSequenceRestartWith(String schemaName, String sequenceName, long restartWith, int cache, int incrementBy)
Sets/resets the sequence to start with the given value.void
alterTableAddColumn(String schemaName, String tableName, ColumnBase column)
Add a new column to an existing tablevoid
alterTableColumnIdentityCache(String schemaName, String objectName, String columnName, int cache)
Change the CACHE value of the named identity generated always columnboolean
checkCompatibility(String adminSchema)
checks connectivity to the database and that it is compatiblevoid
createArrType(String schemaName, String typeName, String valueType, int arraySize)
Create ARRAY type used for passing values to stored procedures e.g.: CREATE OR REPLACE TYPE.t_str_values_arr AS .t_str_values ARRAY[256] void
createForeignKeyConstraint(String constraintName, String schemaName, String name, String targetSchema, String targetTable, String targetColumnName, String tenantColumnName, List<String> columns, boolean enforced)
void
createIndex(String schemaName, String tableName, String indexName, String tenantColumnName, List<OrderedColumnDef> indexColumns)
void
createIntVariable(String schemaName, String variableName)
CREATE VARIABLE ptng.session_tenant INT DEFAULT NULL;void
createOrReplaceFunction(String schemaName, String objectName, Supplier<String> supplier)
creates or replaces the SQL functionvoid
createOrReplacePermission(String schemaName, String permissionName, String tableName, String predicate)
CREATE OR REPLACE PERMISSION ROW_ACCESS ON ptng.patients FOR ROWS WHERE patients.mt_id = ptng.session_tenant ENFORCED FOR ALL ACCESS ENABLE;void
createOrReplaceProcedure(String schemaName, String procedureName, Supplier<String> supplier)
Create the stored procedure using the DDL text provided by the suppliervoid
createOrReplaceView(String schemaName, String objectName, String selectClause)
Create or replace the viewvoid
createRowType(String schemaName, String typeName, List<ColumnBase> columns)
Create ROW type used for passing values to stored procedures e.g.:void
createSchema(String schemaName)
Create a database schemavoid
createSequence(String schemaName, String sequenceName, long startWith, int cache, int incrementBy)
void
createTable(String schemaName, String name, String tenantColumnName, List<ColumnBase> columns, PrimaryKeyDef primaryKey, IdentityDef identity, String tablespaceName)
Build the create table DDLvoid
createTablespace(String tablespaceName)
Create a new tablespace with the given namevoid
createTablespace(String tablespaceName, int extentSizeKB)
Create a new tablespace using the given extent sizevoid
createTenantPartitions(Collection<Table> tables, String schemaName, int newTenantId, int extentSizeKB)
Create the partitions on each of these tablesvoid
createUniqueConstraint(String constraintName, List<String> columns, String schemaName, String name)
create a unique constraint on a table.void
createUniqueIndex(String schemaName, String tableName, String indexName, String tenantColumnName, List<OrderedColumnDef> indexColumns)
void
createUniqueIndex(String schemaName, String tableName, String indexName, String tenantColumnName, List<OrderedColumnDef> indexColumns, List<String> includeColumns)
void
createView(String schemaName, String objectName, String selectClause)
Create the view as defined by the selectClausevoid
deactivateRowAccessControl(String schemaName, String tableName)
Deactivate row access control on a table ALTER TABLEDEACTIVATE ROW ACCESS CONTROL void
deleteTenantMeta(String adminSchemaName, int tenantId)
Delete all the metadata associated with the given tenant identifier, as long as the tenant status is DROPPED.void
detachPartition(String schemaName, String tableName, String partitionName, String newTableName)
Detach the partitionvoid
disableForeignKey(String schemaName, String tableName, String constraintName)
Disable the FK with the given constraint nameboolean
doesTableExist(String schemaName, String objectName)
Check if the table currently existsvoid
dropDetachedPartitions(Collection<Table> tables, String schemaName, int tenantId)
Drop the tables which were created by the detach partition operation (as part of tenant deprovisioning).void
dropForeignKey(String schemaName, String tableName, String constraintName)
Drop the FK on the table with the given constraint namevoid
dropFunction(String schemaName, String functionName)
drops a given functionvoid
dropIndex(String schemaName, String indexName)
Drop the named indexvoid
dropPermission(String schemaName, String permissionName)
Drop permission object from the schemavoid
dropProcedure(String schemaName, String procedureName)
Drop the given procedurevoid
dropSequence(String schemaName, String sequenceName)
void
dropTable(String schemaName, String name)
Drop table from the schemavoid
dropTablespace(String tablespaceName)
Drop an existing tablespace, including all of its contentsvoid
dropTenantTablespace(int tenantId)
Drop the tablespace associated with the given tenantIdvoid
dropType(String schemaName, String typeName)
Drop the type object from the schemavoid
dropVariable(String schemaName, String variableName)
void
dropView(String schemaName, String objectName)
Drop the view from the databasevoid
enableForeignKey(String schemaName, String tableName, String constraintName)
Enable the FK with the given constraint nameint
findTenantId(String adminSchemaName, String tenantName)
Get the tenant id for the given schema and tenant nameIDatabaseTranslator
getTranslator()
Get theIDatabaseTranslator
associated with this adaptervoid
grantFunctionPrivileges(String schemaName, String functionName, Collection<Privilege> privileges, String toUser)
grants permissions on a given functionvoid
grantObjectPrivileges(String schemaName, String tableName, Collection<Privilege> privileges, String toUser)
Grant the list of privileges on the named object to the user.void
grantProcedurePrivileges(String schemaName, String procedureName, Collection<Privilege> privileges, String toUser)
Grant the collection of privileges on the named procedure to the uservoid
grantSequencePrivileges(String schemaName, String objectName, Collection<Privilege> group, String toUser)
Grant the collection of privileges on the named variable to the uservoid
grantVariablePrivileges(String schemaName, String variableName, Collection<Privilege> privileges, String toUser)
Grant the collection of privileges on the named variable to the uservoid
removeTenantPartitions(Collection<Table> tables, String schemaName, int tenantId)
Detach the partition associated with the tenantId from each of the given tablesvoid
reorgTable(String schemaName, String tableName)
Reorg the table if the underlying database supports it.void
runStatement(IDatabaseStatement statement)
Run the given statement against the database represented by this adapter<T> T
runStatement(IDatabaseSupplier<T> supplier)
Run the given supplier statement against the database represented by this adaptervoid
setIntegrityOff(String schemaName, String tableName)
void
setIntegrityUnchecked(String schemaName, String tableName)
void
setIntVariable(String schemaName, String variableName, int value)
Build the DML statement for setting a session variablevoid
updateTenantStatus(String adminSchemaName, int tenantId, TenantStatus status)
Update the tenant statusdefault boolean
useSessionVariable()
-
-
-
Method Detail
-
getTranslator
IDatabaseTranslator getTranslator()
Get theIDatabaseTranslator
associated with this adapter- Returns:
-
createTablespace
void createTablespace(String tablespaceName)
Create a new tablespace with the given name- Parameters:
tablespaceName
-
-
createTablespace
void createTablespace(String tablespaceName, int extentSizeKB)
Create a new tablespace using the given extent size- Parameters:
tablespaceName
-extentSizeKB
-
-
dropTablespace
void dropTablespace(String tablespaceName)
Drop an existing tablespace, including all of its contents- Parameters:
tablespaceName
-
-
detachPartition
void detachPartition(String schemaName, String tableName, String partitionName, String newTableName)
Detach the partition- Parameters:
schemaName
-tableName
-partitionName
-newTableName
-
-
createTable
void createTable(String schemaName, String name, String tenantColumnName, List<ColumnBase> columns, PrimaryKeyDef primaryKey, IdentityDef identity, String tablespaceName)
Build the create table DDL- Parameters:
schemaName
-name
-tenantColumnName
- optional column name to enable multi-tenancycolumns
-primaryKey
-identity
-tablespaceName
-
-
alterTableAddColumn
void alterTableAddColumn(String schemaName, String tableName, ColumnBase column)
Add a new column to an existing table- Parameters:
schemaName
-tableName
-column
-
-
reorgTable
void reorgTable(String schemaName, String tableName)
Reorg the table if the underlying database supports it. Required after columns are added/removed from a table.- Parameters:
schemaName
-tableName
-
-
createRowType
void createRowType(String schemaName, String typeName, List<ColumnBase> columns)
Create ROW type used for passing values to stored procedures e.g.:CREATE OR REPLACE TYPE
.t_str_values AS ROW (parameter_name_id INTEGER, str_value VARCHAR(511 OCTETS), str_value_lcase VARCHAR(511 OCTETS)) - Parameters:
schemaName
-typeName
-columns
-
-
createArrType
void createArrType(String schemaName, String typeName, String valueType, int arraySize)
Create ARRAY type used for passing values to stored procedures e.g.: CREATE OR REPLACE TYPE.t_str_values_arr AS .t_str_values ARRAY[256] - Parameters:
schemaName
-typeName
-valueType
-arraySize
-
-
dropType
void dropType(String schemaName, String typeName)
Drop the type object from the schema- Parameters:
schemaName
-typeName
-
-
createOrReplaceProcedure
void createOrReplaceProcedure(String schemaName, String procedureName, Supplier<String> supplier)
Create the stored procedure using the DDL text provided by the supplier- Parameters:
schemaName
-procedureName
-supplier
-
-
dropProcedure
void dropProcedure(String schemaName, String procedureName)
Drop the given procedure- Parameters:
schemaName
-procedureName
-
-
createUniqueIndex
void createUniqueIndex(String schemaName, String tableName, String indexName, String tenantColumnName, List<OrderedColumnDef> indexColumns, List<String> includeColumns)
- Parameters:
schemaName
-tableName
-indexName
-tenantColumnName
-indexColumns
-includeColumns
-
-
createUniqueIndex
void createUniqueIndex(String schemaName, String tableName, String indexName, String tenantColumnName, List<OrderedColumnDef> indexColumns)
- Parameters:
schemaName
-tableName
-indexName
-tenantColumnName
-indexColumns
-
-
createIndex
void createIndex(String schemaName, String tableName, String indexName, String tenantColumnName, List<OrderedColumnDef> indexColumns)
- Parameters:
schemaName
-tableName
-indexName
-tenantColumnName
-indexColumns
-
-
createIntVariable
void createIntVariable(String schemaName, String variableName)
CREATE VARIABLE ptng.session_tenant INT DEFAULT NULL;
- Parameters:
schemaName
-variableName
-
-
createOrReplacePermission
void createOrReplacePermission(String schemaName, String permissionName, String tableName, String predicate)
CREATE OR REPLACE PERMISSION ROW_ACCESS ON ptng.patients FOR ROWS WHERE patients.mt_id = ptng.session_tenant ENFORCED FOR ALL ACCESS ENABLE;
- Parameters:
schemaName
-permissionName
-tableName
-predicate
-
-
activateRowAccessControl
void activateRowAccessControl(String schemaName, String tableName)
ALTER TABLE
ACTIVATE ROW ACCESS CONTROL - Parameters:
schemaName
-tableName
-
-
deactivateRowAccessControl
void deactivateRowAccessControl(String schemaName, String tableName)
Deactivate row access control on a table ALTER TABLEDEACTIVATE ROW ACCESS CONTROL - Parameters:
schemaName
-tableName
-
-
setIntVariable
void setIntVariable(String schemaName, String variableName, int value)
Build the DML statement for setting a session variable- Parameters:
schemaName
-variableName
-value
-
-
dropTable
void dropTable(String schemaName, String name)
Drop table from the schema- Parameters:
schemaName
-name
-
-
dropPermission
void dropPermission(String schemaName, String permissionName)
Drop permission object from the schema- Parameters:
schemaName
-permissionName
-
-
dropVariable
void dropVariable(String schemaName, String variableName)
- Parameters:
schemaName
-variableName
-
-
createForeignKeyConstraint
void createForeignKeyConstraint(String constraintName, String schemaName, String name, String targetSchema, String targetTable, String targetColumnName, String tenantColumnName, List<String> columns, boolean enforced)
- Parameters:
constraintName
-schemaName
-name
-targetSchema
-targetTable
-targetColumnName
-tenantColumnName
-columns
-enforced
-
-
allocateTenant
int allocateTenant(String adminSchemaName, String schemaName, String tenantName, String tenantKey, String tenantSalt, String idSequenceName)
Allocate a new tenant- Parameters:
adminSchemaName
-schemaName
-tenantName
-tenantKey
-tenantSalt
-idSequenceName
-- Returns:
-
deleteTenantMeta
void deleteTenantMeta(String adminSchemaName, int tenantId)
Delete all the metadata associated with the given tenant identifier, as long as the tenant status is DROPPED.- Parameters:
tenantId
-
-
findTenantId
int findTenantId(String adminSchemaName, String tenantName)
Get the tenant id for the given schema and tenant name- Parameters:
adminSchemaName
-tenantName
-- Returns:
-
createTenantPartitions
void createTenantPartitions(Collection<Table> tables, String schemaName, int newTenantId, int extentSizeKB)
Create the partitions on each of these tables- Parameters:
tables
-schemaName
-newTenantId
-extentSizeKB
-
-
addNewTenantPartitions
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- Parameters:
tables
-schemaName
-newTenantId
-
-
removeTenantPartitions
void removeTenantPartitions(Collection<Table> tables, String schemaName, int tenantId)
Detach the partition associated with the tenantId from each of the given tables- Parameters:
tables
-schemaName
-tenantId
-tenantStagingTable
-
-
dropDetachedPartitions
void dropDetachedPartitions(Collection<Table> tables, String schemaName, int tenantId)
Drop the tables which were created by the detach partition operation (as part of tenant deprovisioning).- Parameters:
tables
-schemaName
-tenantId
-
-
updateTenantStatus
void updateTenantStatus(String adminSchemaName, int tenantId, TenantStatus status)
Update the tenant status- Parameters:
adminSchemaName
-tenantId
-status
-
-
createSequence
void createSequence(String schemaName, String sequenceName, long startWith, int cache, int incrementBy)
- Parameters:
schemaName
-sequenceName
-startWith
- the START WITH value for the sequencecache
- the sequence CACHE value
-
dropSequence
void dropSequence(String schemaName, String sequenceName)
- Parameters:
schemaName
-sequenceName
-
-
alterSequenceRestartWith
void alterSequenceRestartWith(String schemaName, String sequenceName, long restartWith, int cache, int incrementBy)
Sets/resets the sequence to start with the given value.- Parameters:
schemaName
-sequenceName
-restartWith
-cache
-
-
grantObjectPrivileges
void grantObjectPrivileges(String schemaName, String tableName, Collection<Privilege> privileges, String toUser)
Grant 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.- Parameters:
schemaName
-tableName
-privileges
-toUser
-
-
grantProcedurePrivileges
void grantProcedurePrivileges(String schemaName, String procedureName, Collection<Privilege> privileges, String toUser)
Grant the collection of privileges on the named procedure to the user- Parameters:
schemaName
-procedureName
-privileges
-toUser
-
-
grantVariablePrivileges
void grantVariablePrivileges(String schemaName, String variableName, Collection<Privilege> privileges, String toUser)
Grant the collection of privileges on the named variable to the user- Parameters:
schemaName
-variableName
-privileges
-toUser
-
-
grantSequencePrivileges
void grantSequencePrivileges(String schemaName, String objectName, Collection<Privilege> group, String toUser)
Grant the collection of privileges on the named variable to the user- Parameters:
schemaName
-objectName
-group
-toUser
-
-
runStatement
<T> T runStatement(IDatabaseSupplier<T> supplier)
Run the given supplier statement against the database represented by this adapter- Type Parameters:
T
-- Parameters:
supplier
-- Returns:
-
runStatement
void runStatement(IDatabaseStatement statement)
Run the given statement against the database represented by this adapter- Parameters:
statement
-
-
doesTableExist
boolean doesTableExist(String schemaName, String objectName)
Check if the table currently exists- Parameters:
schemaName
-objectName
-- Returns:
-
createSchema
void createSchema(String schemaName)
Create a database schema- Parameters:
schemaName
-
-
createUniqueConstraint
void createUniqueConstraint(String constraintName, List<String> columns, String schemaName, String name)
create a unique constraint on a table.- Parameters:
constraintName
-columns
-schemaName
-name
-
-
checkCompatibility
boolean checkCompatibility(String adminSchema)
checks connectivity to the database and that it is compatible- Parameters:
adminSchema
-- Returns:
-
useSessionVariable
default boolean useSessionVariable()
- Returns:
- a false, if not used, or true if used with the persistence layer.
-
createOrReplaceFunction
void createOrReplaceFunction(String schemaName, String objectName, Supplier<String> supplier)
creates or replaces the SQL function- Parameters:
schemaName
-objectName
-supplier
-
-
dropFunction
void dropFunction(String schemaName, String functionName)
drops a given function- Parameters:
schemaName
-functionName
-
-
grantFunctionPrivileges
void grantFunctionPrivileges(String schemaName, String functionName, Collection<Privilege> privileges, String toUser)
grants permissions on a given function- Parameters:
schemaName
-functionName
-privileges
-toUser
-
-
dropTenantTablespace
void dropTenantTablespace(int tenantId)
Drop the tablespace associated with the given tenantId- Parameters:
tenantId
-
-
disableForeignKey
void disableForeignKey(String schemaName, String tableName, String constraintName)
Disable the FK with the given constraint name- Parameters:
tableName
-constraintName
-
-
dropForeignKey
void dropForeignKey(String schemaName, String tableName, String constraintName)
Drop the FK on the table with the given constraint name- Parameters:
schemaName
-tableName
-constraintName
-
-
enableForeignKey
void enableForeignKey(String schemaName, String tableName, String constraintName)
Enable the FK with the given constraint name- Parameters:
schemaName
-tableName
-constraintName
-
-
setIntegrityOff
void setIntegrityOff(String schemaName, String tableName)
- Parameters:
schemaName
-tableName
-
-
setIntegrityUnchecked
void setIntegrityUnchecked(String schemaName, String tableName)
- Parameters:
schemaName
-tableName
-
-
alterTableColumnIdentityCache
void alterTableColumnIdentityCache(String schemaName, String objectName, String columnName, int cache)
Change the CACHE value of the named identity generated always column- Parameters:
schemaName
-objectName
-columnName
-cache
-
-
dropIndex
void dropIndex(String schemaName, String indexName)
Drop the named index- Parameters:
schemaName
-indexName
-
-
createView
void createView(String schemaName, String objectName, String selectClause)
Create the view as defined by the selectClause- Parameters:
schemaName
-objectName
-selectClause
-
-
dropView
void dropView(String schemaName, String objectName)
Drop the view from the database- Parameters:
schemaName
-objectName
-
-
-