Class CommonDatabaseAdapter
- java.lang.Object
-
- org.linuxforhealth.fhir.database.utils.common.CommonDatabaseAdapter
-
- All Implemented Interfaces:
IDatabaseAdapter
,IDatabaseTypeAdapter
- Direct Known Subclasses:
DerbyAdapter
,PostgresAdapter
public abstract class CommonDatabaseAdapter extends java.lang.Object implements IDatabaseAdapter, IDatabaseTypeAdapter
Provides schema control functions common to our supported databases: PostgreSQL Derby (for unit tests, not production)
-
-
Field Summary
Fields Modifier and Type Field Description protected IConnectionProvider
connectionProvider
protected IDatabaseTarget
target
-
Constructor Summary
Constructors Modifier Constructor Description protected
CommonDatabaseAdapter()
Constructor used by AddColumn only for getting DB type specific column name.protected
CommonDatabaseAdapter(IConnectionProvider cp, IDatabaseTranslator dt)
Public constructor for when we're using a connection providerprotected
CommonDatabaseAdapter(IDatabaseTarget tgt, IDatabaseTranslator dt)
Protected constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
alterSequenceRestartWith(java.lang.String schemaName, java.lang.String sequenceName, long restartWith, int cache, int incrementBy)
Sets/resets the sequence to start with the given value.void
alterTableAddColumn(java.lang.String schemaName, java.lang.String tableName, ColumnBase column)
Add a new column to an existing tablevoid
alterTableColumnIdentityCache(java.lang.String schemaName, java.lang.String tableName, java.lang.String columnName, int cache)
Change the CACHE value of the named identity generated always columnvoid
applyDistributionRules(java.lang.String schemaName, java.lang.String tableName, DistributionContext distributionContext)
Apply any distribution rules configured for the named tableprotected java.lang.String
buildColumns(java.util.List<ColumnBase> columns, IdentityDef identity)
Build the list of columns in the create table statementprotected java.lang.String
buildCreateTableStatement(java.lang.String schema, java.lang.String name, java.util.List<ColumnBase> columns, PrimaryKeyDef pkDef, IdentityDef identity, java.lang.String tablespaceName, java.util.List<With> withs, java.util.List<CheckConstraint> checkConstraints)
Generate a create table statement suitable for Derbyvoid
createForeignKeyConstraint(java.lang.String constraintName, java.lang.String schemaName, java.lang.String name, java.lang.String targetSchema, java.lang.String targetTable, java.lang.String targetColumnName, java.util.List<java.lang.String> columns, boolean enforced)
void
createIndex(java.lang.String schemaName, java.lang.String tableName, java.lang.String indexName, java.util.List<OrderedColumnDef> indexColumns)
void
createOrReplaceFunction(java.lang.String schemaName, java.lang.String functionName, java.util.function.Supplier<java.lang.String> supplier)
creates or replaces the SQL functionvoid
createOrReplaceProcedure(java.lang.String schemaName, java.lang.String procedureName, java.util.function.Supplier<java.lang.String> supplier)
Create the stored procedure using the DDL text provided by the suppliervoid
createOrReplaceView(java.lang.String schemaName, java.lang.String viewName, java.lang.String selectClause)
Create or replace the viewvoid
createSequence(java.lang.String schemaName, java.lang.String sequenceName, long startWith, int cache, int incrementBy)
void
createUniqueConstraint(java.lang.String constraintName, java.util.List<java.lang.String> columns, java.lang.String schemaName, java.lang.String name)
create a unique constraint on a table.void
createUniqueIndex(java.lang.String schemaName, java.lang.String tableName, java.lang.String indexName, java.util.List<OrderedColumnDef> indexColumns, java.util.List<java.lang.String> includeColumns, DistributionContext distributionContext)
Create a unique indexvoid
createUniqueIndex(java.lang.String schemaName, java.lang.String tableName, java.lang.String indexName, java.util.List<OrderedColumnDef> indexColumns, DistributionContext distributionContext)
Create a unique indexvoid
createView(java.lang.String schemaName, java.lang.String viewName, java.lang.String selectClause)
Create the view as defined by the selectClausevoid
distributeFunction(java.lang.String schemaName, java.lang.String functionName, int distributeByParamNumber)
For Citus, functions can be distributed by one of their parameters (typically the first)void
dropForeignKey(java.lang.String schemaName, java.lang.String tableName, java.lang.String constraintName)
Drop the FK on the table with the given constraint namevoid
dropFunction(java.lang.String schemaName, java.lang.String functionName)
drops a given functionvoid
dropIndex(java.lang.String schemaName, java.lang.String indexName)
Drop the named indexvoid
dropPermission(java.lang.String schemaName, java.lang.String permissionName)
Drop permission object from the schemavoid
dropProcedure(java.lang.String schemaName, java.lang.String procedureName)
Drop the given procedurevoid
dropSequence(java.lang.String schemaName, java.lang.String sequenceName)
void
dropTable(java.lang.String schemaName, java.lang.String tableName)
Drop table from the schemavoid
dropVariable(java.lang.String schemaName, java.lang.String variableName)
void
dropView(java.lang.String schemaName, java.lang.String viewName)
Drop the view from the databasejava.lang.String
getQualifiedName(java.lang.String schemaName, java.lang.String objectName)
Return the fully qualified name in the form "SCHEMA.OBJECT" Validates that both schema and object names are validIDatabaseTranslator
getTranslator()
Get theIDatabaseTranslator
associated with this adaptervoid
grantAllSequenceUsage(java.lang.String schemaName, java.lang.String grantToUser)
Grant access to all sequences in the named schemavoid
grantFunctionPrivileges(java.lang.String schemaName, java.lang.String functionName, java.util.Collection<Privilege> privileges, java.lang.String toUser)
grants permissions on a given functionvoid
grantObjectPrivileges(java.lang.String schemaName, java.lang.String tableName, java.util.Collection<Privilege> privileges, java.lang.String toUser)
Grant the list of privileges on the named object to the user.void
grantProcedurePrivileges(java.lang.String schemaName, java.lang.String procedureName, java.util.Collection<Privilege> privileges, java.lang.String toUser)
Grant the collection of privileges on the named procedure to the uservoid
grantSchemaUsage(java.lang.String schemaName, java.lang.String grantToUser)
Grants USAGE on the given schemaName to the given uservoid
grantSequencePrivileges(java.lang.String schemaName, java.lang.String variableName, java.util.Collection<Privilege> privileges, java.lang.String toUser)
Grant the collection of privileges on the named variable to the uservoid
grantVariablePrivileges(java.lang.String schemaName, java.lang.String variableName, java.util.Collection<Privilege> privileges, java.lang.String toUser)
Grant the collection of privileges on the named variable to the userprotected java.util.List<OrderedColumnDef>
prefixTenantColumn(java.lang.String tenantColumnName, java.util.List<OrderedColumnDef> columns)
Prefix the tenantColumnName to the list of columns, or do nothing if tenantColumnName is nullprotected void
runStatement(java.lang.String ddl)
Execute the statement on a connection managed by our connection providervoid
runStatement(IDatabaseStatement stmt)
Run the statement using the connectionProvider to obtain a new connection.<T> T
runStatement(IDatabaseSupplier<T> supplier)
Run the given supplier statement against the database represented by this adapter-
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.linuxforhealth.fhir.database.utils.api.IDatabaseAdapter
checkCompatibility, createArrType, createRowType, createSchema, createTable, createTablespace, createTablespace, disableForeignKey, doesForeignKeyConstraintExist, doesTableExist, dropTablespace, dropType, enableForeignKey, listSchemaObjects, setIntegrityOff, setIntegrityUnchecked, useSessionVariable
-
Methods inherited from interface org.linuxforhealth.fhir.database.utils.api.IDatabaseTypeAdapter
blobClause, clobClause, doubleClause, smallintClause, 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:IDatabaseAdapter
Get theIDatabaseTranslator
associated with this adapter- Specified by:
getTranslator
in interfaceIDatabaseAdapter
- Returns:
-
buildColumns
protected java.lang.String buildColumns(java.util.List<ColumnBase> columns, IdentityDef identity)
Build the list of columns in the create table statement
-
buildCreateTableStatement
protected java.lang.String buildCreateTableStatement(java.lang.String schema, java.lang.String name, java.util.List<ColumnBase> columns, PrimaryKeyDef pkDef, IdentityDef identity, java.lang.String tablespaceName, java.util.List<With> withs, java.util.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(java.lang.String schemaName, java.lang.String tableName, java.lang.String indexName, java.util.List<OrderedColumnDef> indexColumns, java.util.List<java.lang.String> includeColumns, DistributionContext distributionContext)
Description copied from interface:IDatabaseAdapter
Create a unique index- Specified by:
createUniqueIndex
in interfaceIDatabaseAdapter
-
createUniqueIndex
public void createUniqueIndex(java.lang.String schemaName, java.lang.String tableName, java.lang.String indexName, java.util.List<OrderedColumnDef> indexColumns, DistributionContext distributionContext)
Description copied from interface:IDatabaseAdapter
Create a unique index- Specified by:
createUniqueIndex
in interfaceIDatabaseAdapter
-
createIndex
public void createIndex(java.lang.String schemaName, java.lang.String tableName, java.lang.String indexName, java.util.List<OrderedColumnDef> indexColumns)
- Specified by:
createIndex
in interfaceIDatabaseAdapter
-
prefixTenantColumn
protected java.util.List<OrderedColumnDef> prefixTenantColumn(java.lang.String tenantColumnName, java.util.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(java.lang.String ddl)
Execute the statement on a connection managed by our connection provider- Parameters:
ddl
-
-
getQualifiedName
public java.lang.String getQualifiedName(java.lang.String schemaName, java.lang.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:
java.lang.IllegalArgumentException
- if either name is not a valid database object name
-
dropTable
public void dropTable(java.lang.String schemaName, java.lang.String tableName)
Description copied from interface:IDatabaseAdapter
Drop table from the schema- Specified by:
dropTable
in interfaceIDatabaseAdapter
-
dropProcedure
public void dropProcedure(java.lang.String schemaName, java.lang.String procedureName)
Description copied from interface:IDatabaseAdapter
Drop the given procedure- Specified by:
dropProcedure
in interfaceIDatabaseAdapter
-
dropFunction
public void dropFunction(java.lang.String schemaName, java.lang.String functionName)
Description copied from interface:IDatabaseAdapter
drops a given function- Specified by:
dropFunction
in interfaceIDatabaseAdapter
-
dropPermission
public void dropPermission(java.lang.String schemaName, java.lang.String permissionName)
Description copied from interface:IDatabaseAdapter
Drop permission object from the schema- Specified by:
dropPermission
in interfaceIDatabaseAdapter
-
dropVariable
public void dropVariable(java.lang.String schemaName, java.lang.String variableName)
- Specified by:
dropVariable
in interfaceIDatabaseAdapter
-
createForeignKeyConstraint
public void createForeignKeyConstraint(java.lang.String constraintName, java.lang.String schemaName, java.lang.String name, java.lang.String targetSchema, java.lang.String targetTable, java.lang.String targetColumnName, java.util.List<java.lang.String> columns, boolean enforced)
- Specified by:
createForeignKeyConstraint
in interfaceIDatabaseAdapter
-
createUniqueConstraint
public void createUniqueConstraint(java.lang.String constraintName, java.util.List<java.lang.String> columns, java.lang.String schemaName, java.lang.String name)
Description copied from interface:IDatabaseAdapter
create a unique constraint on a table.- Specified by:
createUniqueConstraint
in interfaceIDatabaseAdapter
-
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:
runStatement
in interfaceIDatabaseAdapter
- Parameters:
stmt
-
-
runStatement
public <T> T runStatement(IDatabaseSupplier<T> supplier)
Description copied from interface:IDatabaseAdapter
Run the given supplier statement against the database represented by this adapter- Specified by:
runStatement
in interfaceIDatabaseAdapter
- Returns:
-
createOrReplaceProcedure
public void createOrReplaceProcedure(java.lang.String schemaName, java.lang.String procedureName, java.util.function.Supplier<java.lang.String> supplier)
Description copied from interface:IDatabaseAdapter
Create the stored procedure using the DDL text provided by the supplier- Specified by:
createOrReplaceProcedure
in interfaceIDatabaseAdapter
-
createOrReplaceFunction
public void createOrReplaceFunction(java.lang.String schemaName, java.lang.String functionName, java.util.function.Supplier<java.lang.String> supplier)
Description copied from interface:IDatabaseAdapter
creates or replaces the SQL function- Specified by:
createOrReplaceFunction
in interfaceIDatabaseAdapter
-
createSequence
public void createSequence(java.lang.String schemaName, java.lang.String sequenceName, long startWith, int cache, int incrementBy)
- Specified by:
createSequence
in interfaceIDatabaseAdapter
startWith
- the START WITH value for the sequencecache
- the sequence CACHE value
-
dropSequence
public void dropSequence(java.lang.String schemaName, java.lang.String sequenceName)
- Specified by:
dropSequence
in interfaceIDatabaseAdapter
-
alterSequenceRestartWith
public void alterSequenceRestartWith(java.lang.String schemaName, java.lang.String sequenceName, long restartWith, int cache, int incrementBy)
Description copied from interface:IDatabaseAdapter
Sets/resets the sequence to start with the given value.- Specified by:
alterSequenceRestartWith
in interfaceIDatabaseAdapter
-
alterTableColumnIdentityCache
public void alterTableColumnIdentityCache(java.lang.String schemaName, java.lang.String tableName, java.lang.String columnName, int cache)
Description copied from interface:IDatabaseAdapter
Change the CACHE value of the named identity generated always column- Specified by:
alterTableColumnIdentityCache
in interfaceIDatabaseAdapter
-
dropForeignKey
public void dropForeignKey(java.lang.String schemaName, java.lang.String tableName, java.lang.String constraintName)
Description copied from interface:IDatabaseAdapter
Drop the FK on the table with the given constraint name- Specified by:
dropForeignKey
in interfaceIDatabaseAdapter
-
grantObjectPrivileges
public void grantObjectPrivileges(java.lang.String schemaName, java.lang.String tableName, java.util.Collection<Privilege> privileges, java.lang.String toUser)
Description copied from interface:IDatabaseAdapter
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.- Specified by:
grantObjectPrivileges
in interfaceIDatabaseAdapter
-
grantProcedurePrivileges
public void grantProcedurePrivileges(java.lang.String schemaName, java.lang.String procedureName, java.util.Collection<Privilege> privileges, java.lang.String toUser)
Description copied from interface:IDatabaseAdapter
Grant the collection of privileges on the named procedure to the user- Specified by:
grantProcedurePrivileges
in interfaceIDatabaseAdapter
-
grantFunctionPrivileges
public void grantFunctionPrivileges(java.lang.String schemaName, java.lang.String functionName, java.util.Collection<Privilege> privileges, java.lang.String toUser)
Description copied from interface:IDatabaseAdapter
grants permissions on a given function- Specified by:
grantFunctionPrivileges
in interfaceIDatabaseAdapter
-
grantVariablePrivileges
public void grantVariablePrivileges(java.lang.String schemaName, java.lang.String variableName, java.util.Collection<Privilege> privileges, java.lang.String toUser)
Description copied from interface:IDatabaseAdapter
Grant the collection of privileges on the named variable to the user- Specified by:
grantVariablePrivileges
in interfaceIDatabaseAdapter
-
grantSequencePrivileges
public void grantSequencePrivileges(java.lang.String schemaName, java.lang.String variableName, java.util.Collection<Privilege> privileges, java.lang.String toUser)
Description copied from interface:IDatabaseAdapter
Grant the collection of privileges on the named variable to the user- Specified by:
grantSequencePrivileges
in interfaceIDatabaseAdapter
-
dropIndex
public void dropIndex(java.lang.String schemaName, java.lang.String indexName)
Description copied from interface:IDatabaseAdapter
Drop the named index- Specified by:
dropIndex
in interfaceIDatabaseAdapter
-
dropView
public void dropView(java.lang.String schemaName, java.lang.String viewName)
Description copied from interface:IDatabaseAdapter
Drop the view from the database- Specified by:
dropView
in interfaceIDatabaseAdapter
-
createOrReplaceView
public void createOrReplaceView(java.lang.String schemaName, java.lang.String viewName, java.lang.String selectClause)
Description copied from interface:IDatabaseAdapter
Create or replace the view- Specified by:
createOrReplaceView
in interfaceIDatabaseAdapter
-
createView
public void createView(java.lang.String schemaName, java.lang.String viewName, java.lang.String selectClause)
Description copied from interface:IDatabaseAdapter
Create the view as defined by the selectClause- Specified by:
createView
in interfaceIDatabaseAdapter
-
alterTableAddColumn
public void alterTableAddColumn(java.lang.String schemaName, java.lang.String tableName, ColumnBase column)
Description copied from interface:IDatabaseAdapter
Add a new column to an existing table- Specified by:
alterTableAddColumn
in interfaceIDatabaseAdapter
-
applyDistributionRules
public void applyDistributionRules(java.lang.String schemaName, java.lang.String tableName, DistributionContext distributionContext)
Description copied from interface:IDatabaseAdapter
Apply any distribution rules configured for the named table- Specified by:
applyDistributionRules
in interfaceIDatabaseAdapter
-
distributeFunction
public void distributeFunction(java.lang.String schemaName, java.lang.String functionName, int distributeByParamNumber)
Description copied from interface:IDatabaseAdapter
For Citus, functions can be distributed by one of their parameters (typically the first)- Specified by:
distributeFunction
in interfaceIDatabaseAdapter
-
grantSchemaUsage
public void grantSchemaUsage(java.lang.String schemaName, java.lang.String grantToUser)
Description copied from interface:IDatabaseAdapter
Grants USAGE on the given schemaName to the given user- Specified by:
grantSchemaUsage
in interfaceIDatabaseAdapter
-
grantAllSequenceUsage
public void grantAllSequenceUsage(java.lang.String schemaName, java.lang.String grantToUser)
Description copied from interface:IDatabaseAdapter
Grant access to all sequences in the named schema- Specified by:
grantAllSequenceUsage
in interfaceIDatabaseAdapter
-
-