- All Implemented Interfaces:
DatabaseProvider
- Direct Known Subclasses:
H2DatabaseProvider,OracleDatabaseProvider,PostgresDatabaseProvider,SQLiteDatabaseProvider
DatabaseProvider interface that provides a framework for interacting
with a database by managing SQL queries, metadata retrieval, and type conversions. This class serves as a base
for specific database implementations, handling common functionality while leaving database-specific details
to subclasses.
This class includes utility methods for preparing and executing SQL statements, fetching table metadata, and
performing insert, update, and select operations. It uses a caching mechanism for table metadata to improve
efficiency and ensures type conversion using a pluggable TypeConverter.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final ConcurrentLazy<ColumnIdentifierGenerator> protected final ConcurrentLazy<DeleteSqlGenerator> protected final ConcurrentLazy<InsertSqlGenerator> protected final ConcurrentLazy<SelectSqlGenerator> protected final TypeConverterprotected final ConcurrentLazy<UpdateSqlGenerator> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected AliasTransformerprotected ColumnIdentifierGeneratorprotected DeleteSqlGeneratorprotected InsertSqlGeneratorprotected PreparedStatementcreatePreparedStatementUsingConnection(PreparedSql preparedSql, boolean returnGeneratedKeys, TableMetaData tableMetaData, ManagedConnection connection) protected SelectSqlGeneratorprotected SqlFunctionRegistryFactoryCreate aSqlFunctionRegistryFactoryinstance for the generation of a SQL function registry.protected UpdateSqlGeneratordelete(Delete delete, ConnectionProvider connectionProvider) Execute a DELETE operation in the database using the providedDeletestatement.protected TableMetaDataensureTableMetaData(Table table, ConnectionProvider connectionProvider) protected InsertResultexecuteSqlInsert(PreparedSql preparedSql, TableMetaData tableMetaData, boolean returnGeneratedKeys, ConnectionProvider connectionProvider) Execute a SQL INSERT operation using the provided prepared SQL statement and table metadata.protected UpdateResultexecuteSqlUpdate(PreparedSql preparedSql, TableMetaData tableMetaData, ConnectionProvider connectionProvider) Execute a SQL UPDATE operation using the provided prepared SQL statement and table metadata.protected Map<ColumnMetaData, Object> extractGeneratedKeys(TableMetaData tableMetaData, PreparedStatement preparedStatement) protected TableMetaDatafetchTableMetaData(Table table, ConnectionProvider connectionProvider) Retrieve metadata for the specified table, including its primary keys and expressions.protected List<ColumnMetaData> getColumnMetaData(Table table, DatabaseMetaData databaseMetaData) protected List<ColumnMetaData> getGeneratedPrimaryKeyColumns(TableMetaData tableMetaData) protected org.slf4j.LoggerReturn the logger instance for this database provider.getPrimaryKeyColumnNames(Table table, DatabaseMetaData databaseMetaData) getSequenceColumnValueGenerator(String sequence) Retrieves aSequenceColumnValueGeneratorinstance for generating SQL fragments that fetch the next value from a specified database sequence.Retrieve theSqlFunctionRegistryinstance associated with the database provider.Retrieve theTypeConverterinstance associated with the database provider.insert(Insert insert, ConnectionProvider connectionProvider) Execute an INSERT operation in the database using the providedInsertstatement.protected PreparedStatementprepareStatement(PreparedSql preparedSql, boolean returnGeneratedKeys, TableMetaData tableMetaData, ConnectionProvider connectionProvider) Prepare aPreparedStatementobject based on the provided SQL and bind values.select(Select select, ConnectionProvider connectionProvider) Execute a SELECT operation in the database using the providedSelectstatement.tableMetaData(Table table, ConnectionProvider connectionProvider) Retrieve metadata for the specified table.toSql(Operation operation, ConnectionProvider connectionProvider) Converts the givenOperationinto its corresponding SQL representation.update(Update update, ConnectionProvider connectionProvider) Execute an UPDATE operation in the database using the providedUpdatestatement.protected static voidverifySchemaAndTableExists(Table table, DatabaseMetaData databaseMetaData)
-
Field Details
-
typeConverter
-
columnIdentifierGenerator
-
selectSqlGenerator
-
insertSqlGenerator
-
updateSqlGenerator
-
deleteSqlGenerator
-
-
Constructor Details
-
AbstractDatabaseProvider
-
-
Method Details
-
tableMetaData
public TableMetaData tableMetaData(Table table, ConnectionProvider connectionProvider) throws SQLException Description copied from interface:DatabaseProviderRetrieve metadata for the specified table.- Specified by:
tableMetaDatain interfaceDatabaseProvider- Parameters:
table- theTableobject representing the table for which metadata is to be retrieved.connectionProvider- theConnectionProviderused to get a database connection.- Returns:
- a
TableMetaDataobject containing metadata information about the specified table, including its columns, primary key, and other details. - Throws:
SQLException- if any SQL error occurs while retrieving the metadata.
-
insert
public InsertResult insert(Insert insert, ConnectionProvider connectionProvider) throws SQLException Description copied from interface:DatabaseProviderExecute an INSERT operation in the database using the providedInsertstatement.- Specified by:
insertin interfaceDatabaseProvider- Parameters:
insert- theInsertstatement containing the table, columns, and rows to insert.connectionProvider- theConnectionProviderused to get a database connection.- Returns:
- an
InsertResultcontaining the number of rows affected and any generated keys. - Throws:
SQLException- if any SQL error occurs during the execution of the insert operation.
-
update
public UpdateResult update(Update update, ConnectionProvider connectionProvider) throws SQLException Description copied from interface:DatabaseProviderExecute an UPDATE operation in the database using the providedUpdatestatement.- Specified by:
updatein interfaceDatabaseProvider- Parameters:
update- theUpdatestatement containing the table, columns, and rows to update.connectionProvider- theConnectionProviderused to get a database connection.- Returns:
- an
UpdateResultcontaining the number of rows affected. - Throws:
SQLException- if any SQL error occurs during the execution of the update operation.
-
select
Description copied from interface:DatabaseProviderExecute a SELECT operation in the database using the providedSelectstatement.- Specified by:
selectin interfaceDatabaseProvider- Parameters:
select- theSelectstatement containing information about the table, expressions, joins, conditions, ordering, and optional limits for the query.connectionProvider- theConnectionProviderused to get a database connection.- Returns:
- a
ListofRowobjects representing the results of the SELECT operation. - Throws:
SQLException- if any SQL error occurs during the execution of the SELECT operation.
-
delete
public UpdateResult delete(Delete delete, ConnectionProvider connectionProvider) throws SQLException Description copied from interface:DatabaseProviderExecute a DELETE operation in the database using the providedDeletestatement.- Specified by:
deletein interfaceDatabaseProvider- Parameters:
delete- theDeletestatement containing the table and rows to delete.connectionProvider- theConnectionProviderused to get a database connection.- Returns:
- an
UpdateResultcontaining the number of rows affected. - Throws:
SQLException- if any SQL error occurs during the execution of the delete operation.
-
getTypeConverter
Description copied from interface:DatabaseProviderRetrieve theTypeConverterinstance associated with the database provider.The
TypeConverteris used for converting objects between different types, typically for database data type conversions and mapping domain-specific representations.- Specified by:
getTypeConverterin interfaceDatabaseProvider- Returns:
- the
TypeConverterinstance for handling data type conversions
-
getSequenceColumnValueGenerator
public SequenceColumnValueGenerator getSequenceColumnValueGenerator(String sequence) throws UnsupportedOperationException Description copied from interface:DatabaseProviderRetrieves aSequenceColumnValueGeneratorinstance for generating SQL fragments that fetch the next value from a specified database sequence. This is typically used in SQL statements such as INSERT or UPDATE to generate unique values from a sequence.- Specified by:
getSequenceColumnValueGeneratorin interfaceDatabaseProvider- Parameters:
sequence- the name of the database sequence from which the values will be generated. It is used to create the SQL fragment to retrieve the next value in the sequence.- Returns:
- a
SequenceColumnValueGeneratorinstance that generates SQL fragments for retrieving sequence values. - Throws:
UnsupportedOperationException- if the database provider does not support sequence-based value generation.
-
executeSqlInsert
protected InsertResult executeSqlInsert(PreparedSql preparedSql, TableMetaData tableMetaData, boolean returnGeneratedKeys, ConnectionProvider connectionProvider) throws SQLException Execute a SQL INSERT operation using the provided prepared SQL statement and table metadata.This method executes the prepared statement, retrieves any generated primary key values, and wraps the results in an
InsertResultobject.- Parameters:
preparedSql- thePreparedSqlobject containing the SQL query string and bind values to be executedtableMetaData- theTableMetaDataobject containing the metadata of the target table, including primary key informationreturnGeneratedKeys- a boolean indicating whether the statement should return generated keys. Passtrueto configure the statement to return generated keys, orfalseotherwise.- Returns:
- an
InsertResultobject encapsulating the number of affected rows and a list of generated keys (if any) - Throws:
SQLException- if an error occurs while executing the SQL insert or retrieving the generated keys
-
getGeneratedPrimaryKeyColumns
-
extractGeneratedKeys
protected Map<ColumnMetaData,Object> extractGeneratedKeys(TableMetaData tableMetaData, PreparedStatement preparedStatement) throws SQLException - Throws:
SQLException
-
executeSqlUpdate
protected UpdateResult executeSqlUpdate(PreparedSql preparedSql, TableMetaData tableMetaData, ConnectionProvider connectionProvider) throws SQLException Execute a SQL UPDATE operation using the provided prepared SQL statement and table metadata.This method performs the execution of a prepared update statement and wraps the number of affected rows in an
UpdateResultobject.- Parameters:
preparedSql- thePreparedSqlobject containing the SQL query string and bind values to be executedconnectionProvider- theConnectionProviderused to obtain a database connection.- Returns:
- an
UpdateResultobject encapsulating the number of rows affected by the update operation - Throws:
SQLException- if an error occurs while executing the SQL update
-
toSql
Description copied from interface:DatabaseProviderConverts the givenOperationinto its corresponding SQL representation.This does not execute the operation, it only generates the SQL string.
- Specified by:
toSqlin interfaceDatabaseProvider- Parameters:
operation- theOperationobject representing the database operation (e.g., SELECT, INSERT, UPDATE, DELETE) to be converted to a SQL string.connectionProvider- theConnectionProviderused to obtain database-specific context or information required for SQL generation, such as metadata or dialect.- Returns:
- a
Stringcontaining the SQL representation of the givenOperation.
-
getSqlFunctionRegistry
Description copied from interface:DatabaseProviderRetrieve theSqlFunctionRegistryinstance associated with the database provider.The
SqlFunctionRegistryis used for registering and managing SQL functions that can be used in SQL queries executed by the database provider.- Specified by:
getSqlFunctionRegistryin interfaceDatabaseProvider- Returns:
- the
SqlFunctionRegistryinstance for managing SQL functions
-
getAliasTransformer
- Specified by:
getAliasTransformerin interfaceDatabaseProvider
-
getColumnMetaData
protected List<ColumnMetaData> getColumnMetaData(Table table, DatabaseMetaData databaseMetaData) throws SQLException - Throws:
SQLException
-
getPrimaryKeyColumnNames
protected List<String> getPrimaryKeyColumnNames(Table table, DatabaseMetaData databaseMetaData) throws SQLException - Throws:
SQLException
-
verifySchemaAndTableExists
protected static void verifySchemaAndTableExists(Table table, DatabaseMetaData databaseMetaData) throws SQLException - Throws:
SQLException
-
prepareStatement
protected PreparedStatement prepareStatement(PreparedSql preparedSql, boolean returnGeneratedKeys, TableMetaData tableMetaData, ConnectionProvider connectionProvider) throws SQLException Prepare aPreparedStatementobject based on the provided SQL and bind values.Optionally, the statement can be configured to return generated keys.
- Parameters:
preparedSql- thePreparedSqlobject containing the SQL query and associated bind values.returnGeneratedKeys- a boolean indicating whether the statement should return generated keys. Passtrueto configure the statement to return generated keys, orfalseotherwise.tableMetaData- Meta-data for the current tableconnectionProvider- theConnectionProviderused to obtain a database connection.- Returns:
- a
PreparedStatementthat is ready to be executed based on the provided SQL and bind values. - Throws:
SQLException- if a database access error occurs or the preparation of the SQL statement fails.
-
createPreparedStatementUsingConnection
protected PreparedStatement createPreparedStatementUsingConnection(PreparedSql preparedSql, boolean returnGeneratedKeys, TableMetaData tableMetaData, ManagedConnection connection) throws SQLException - Throws:
SQLException
-
getLogger
protected org.slf4j.Logger getLogger()Return the logger instance for this database provider.- Returns:
- the logger instance
-
ensureTableMetaData
-
fetchTableMetaData
protected TableMetaData fetchTableMetaData(Table table, ConnectionProvider connectionProvider) throws SQLException Retrieve metadata for the specified table, including its primary keys and expressions.This executes a database query to fetch database metadata.
- Parameters:
table- the table for which metadata is being fetched, containing schema, catalog, and table name details- Returns:
- a
TableMetaDataobject containing details about the table's structure, primary keys, and column metadata - Throws:
SQLException- if an error occurs while fetching database metadata
-
createSqlFunctionRegistryFactory
Create aSqlFunctionRegistryFactoryinstance for the generation of a SQL function registry.This method may be overridden to provide a custom
SqlFunctionRegistryFactoryinstance, allowing overriding/disabling of specific SQL functions.- Returns:
- a
SqlFunctionRegistryFactoryimplementation instance
-
createAliasTransformer
-
createColumnIdentifierGenerator
-
createSelectSqlGenerator
-
createInsertSqlGenerator
-
createUpdateSqlGenerator
-
createDeleteSqlGenerator
-