- 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> Lazy-loaded column identifier generator.protected final ConcurrentLazy<DeleteSqlGenerator> Lazy-loaded DELETE SQL generator.protected final ConcurrentLazy<InsertSqlGenerator> Lazy-loaded INSERT SQL generator.protected final ConcurrentLazy<SelectSqlGenerator> Lazy-loaded SELECT SQL generator.protected final TypeConverterThe type converter used for mapping between database and Java types.protected final ConcurrentLazy<UpdateSqlGenerator> Lazy-loaded UPDATE SQL generator. -
Constructor Summary
ConstructorsConstructorDescriptionAbstractDatabaseProvider(TypeConverter typeConverter) Constructs a newAbstractDatabaseProvider. -
Method Summary
Modifier and TypeMethodDescriptionprotected AliasTransformerCreate anAliasTransformerinstance for the database provider.protected ColumnIdentifierGeneratorCreate aColumnIdentifierGeneratorinstance for the database provider.protected DeleteSqlGeneratorCreate aDeleteSqlGeneratorinstance for the database provider.protected InsertSqlGeneratorCreate anInsertSqlGeneratorinstance for the database provider.protected PreparedStatementcreatePreparedStatementUsingConnection(PreparedSql preparedSql, boolean returnGeneratedKeys, @Nullable TableMetaData tableMetaData, ManagedConnection connection) Creates aPreparedStatementusing the provided connection and prepared SQL.protected SelectSqlGeneratorCreate aSelectSqlGeneratorinstance for the database provider.protected SqlFunctionRegistryFactoryCreate aSqlFunctionRegistryFactoryinstance for the generation of a SQL function registry.protected UpdateSqlGeneratorCreate anUpdateSqlGeneratorinstance for the database provider.delete(Delete delete, ConnectionProvider connectionProvider) Execute a DELETE operation in the database using the providedDeletestatement.protected TableMetaDataensureTableMetaData(Table table, ConnectionProvider connectionProvider) Ensure that table metadata is available for the specified table, fetching it if not already cached.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) Extract the generated primary key values from the provided prepared statement.protected TableMetaDatafetchTableMetaData(Table table, ConnectionProvider connectionProvider) Retrieve metadata for the specified table, including its primary keys and expressions.Retrieve theAliasTransformerinstance associated with the database provider.protected List<ColumnMetaData> getColumnMetaData(Table table, DatabaseMetaData databaseMetaData) Retrieve column metadata for the specified table.protected List<ColumnMetaData> getGeneratedPrimaryKeyColumns(TableMetaData tableMetaData) Get the primary key columns for which the database generates values.protected org.slf4j.LoggerReturn the logger instance for this database provider.getPrimaryKeyColumnNames(Table table, DatabaseMetaData databaseMetaData) Retrieve the names of the primary key columns for the specified table.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.nativeSqlQuery(String sql, List<@Nullable Object> bindParameters, ConnectionProvider connectionProvider) Executes a SQL query with the given SQL string and a list of positional bind parameters.nativeSqlUpdate(String sql, List<@Nullable Object> bindParameters, ConnectionProvider connectionProvider) Executes a SQL update statement with the given SQL string and a list of positional bind parameters.protected PreparedStatementprepareNativeStatement(String sql, List<@Nullable Object> bindParameters, boolean returnGeneratedKeys, ConnectionProvider connectionProvider) Prepares a native SQL statement with the provided bind parameters.protected PreparedStatementprepareStatement(PreparedSql preparedSql, boolean returnGeneratedKeys, @Nullable 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) Verify that the specified schema and table exist in the database.
-
Field Details
-
typeConverter
The type converter used for mapping between database and Java types. -
columnIdentifierGenerator
Lazy-loaded column identifier generator. -
selectSqlGenerator
Lazy-loaded SELECT SQL generator. -
insertSqlGenerator
Lazy-loaded INSERT SQL generator. -
updateSqlGenerator
Lazy-loaded UPDATE SQL generator. -
deleteSqlGenerator
Lazy-loaded DELETE SQL generator.
-
-
Constructor Details
-
AbstractDatabaseProvider
Constructs a newAbstractDatabaseProvider.- Parameters:
typeConverter- The type converter to use.
-
-
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.
-
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.
-
nativeSqlQuery
public List<Row> nativeSqlQuery(String sql, List<@Nullable Object> bindParameters, ConnectionProvider connectionProvider) throws SQLException Description copied from interface:DatabaseProviderExecutes a SQL query with the given SQL string and a list of positional bind parameters.- Specified by:
nativeSqlQueryin interfaceDatabaseProvider- Parameters:
sql- the SQL query string to be executed; must not benullbindParameters- the list of parameters to bind to the queryconnectionProvider- theConnectionProviderused to get a database connection.- Returns:
- a list of
Rowobjects representing the result set of the query - Throws:
SQLException- if an error occurs during query execution
-
nativeSqlUpdate
public UpdateResult nativeSqlUpdate(String sql, List<@Nullable Object> bindParameters, ConnectionProvider connectionProvider) throws SQLException Description copied from interface:DatabaseProviderExecutes a SQL update statement with the given SQL string and a list of positional bind parameters.This method delegates to the overloaded execute method that accepts a list of bind parameters.
- Specified by:
nativeSqlUpdatein interfaceDatabaseProvider- Parameters:
sql- the SQL update statement to execute; must not benullbindParameters- the list of named parameters to bind to the statementconnectionProvider- theConnectionProviderused to get a database connection.- Returns:
- an
UpdateResultobject that encapsulates the outcome of the update operation - Throws:
SQLException- if an error occurs while executing the update statement
-
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
Description copied from interface:DatabaseProviderRetrieve theAliasTransformerinstance associated with the database provider.- Specified by:
getAliasTransformerin interfaceDatabaseProvider- Returns:
- the
AliasTransformerinstance for transforming aliases
-
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.connectionProvider- theConnectionProviderused to obtain a database connection.- 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
Get the primary key columns for which the database generates values.- Parameters:
tableMetaData- theTableMetaDataobject containing the metadata of the target table- Returns:
- a list of
ColumnMetaDataobjects representing the generated primary key columns
-
extractGeneratedKeys
protected Map<ColumnMetaData,Object> extractGeneratedKeys(TableMetaData tableMetaData, PreparedStatement preparedStatement) throws SQLException Extract the generated primary key values from the provided prepared statement.- Parameters:
tableMetaData- theTableMetaDataobject containing the metadata of the target tablepreparedStatement- the executedPreparedStatementcontaining any generated keys- Returns:
- a map of
ColumnMetaDatato the generated key value - Throws:
SQLException- if an error occurs while retrieving the generated keys
-
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 executedtableMetaData- theTableMetaDataobject containing the metadata of the target tableconnectionProvider- 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
-
getColumnMetaData
protected List<ColumnMetaData> getColumnMetaData(Table table, DatabaseMetaData databaseMetaData) throws SQLException Retrieve column metadata for the specified table.- Parameters:
table- theTableobject representing the tabledatabaseMetaData- theDatabaseMetaDataobject used to retrieve column information- Returns:
- a list of
ColumnMetaDataobjects representing the table's columns - Throws:
SQLException- if an error occurs while retrieving column metadata
-
getPrimaryKeyColumnNames
protected List<String> getPrimaryKeyColumnNames(Table table, DatabaseMetaData databaseMetaData) throws SQLException Retrieve the names of the primary key columns for the specified table.- Parameters:
table- theTableobject representing the tabledatabaseMetaData- theDatabaseMetaDataobject used to retrieve primary key information- Returns:
- a list of primary key column names
- Throws:
SQLException- if an error occurs while retrieving primary key information
-
verifySchemaAndTableExists
protected static void verifySchemaAndTableExists(Table table, DatabaseMetaData databaseMetaData) throws SQLException Verify that the specified schema and table exist in the database.- Parameters:
table- theTableobject representing the table to verifydatabaseMetaData- theDatabaseMetaDataobject used to perform the verification- Throws:
SQLException- if an error occurs while performing the verificationIllegalArgumentException- if the schema or table is not found
-
prepareStatement
protected PreparedStatement prepareStatement(PreparedSql preparedSql, boolean returnGeneratedKeys, @Nullable 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.
-
prepareNativeStatement
protected PreparedStatement prepareNativeStatement(String sql, List<@Nullable Object> bindParameters, boolean returnGeneratedKeys, ConnectionProvider connectionProvider) throws SQLException Prepares a native SQL statement with the provided bind parameters.- Parameters:
sql- the SQL query to preparebindParameters- the list of parameters to bind to the queryreturnGeneratedKeys- whether to return generated keysconnectionProvider- the provider to use for obtaining a connection- Returns:
- the prepared statement
- Throws:
SQLException- if a database access error occurs
-
createPreparedStatementUsingConnection
protected PreparedStatement createPreparedStatementUsingConnection(PreparedSql preparedSql, boolean returnGeneratedKeys, @Nullable TableMetaData tableMetaData, ManagedConnection connection) throws SQLException Creates aPreparedStatementusing the provided connection and prepared SQL.- Parameters:
preparedSql- the SQL and bind values to usereturnGeneratedKeys- whether to return generated keystableMetaData- the metadata for the table, required if returnGeneratedKeys is trueconnection- the connection to use for preparing the statement- Returns:
- the created prepared statement
- Throws:
SQLException- if a database access error occurs
-
getLogger
protected org.slf4j.Logger getLogger()Return the logger instance for this database provider.- Returns:
- the logger instance
-
ensureTableMetaData
Ensure that table metadata is available for the specified table, fetching it if not already cached.- Parameters:
table- theTableobject representing the tableconnectionProvider- theConnectionProviderused to fetch metadata if needed- Returns:
- the
TableMetaDatafor the table
-
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 detailsconnectionProvider- theConnectionProviderused to obtain a database connection.- 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
Create anAliasTransformerinstance for the database provider.- Returns:
- an
AliasTransformerinstance
-
createColumnIdentifierGenerator
Create aColumnIdentifierGeneratorinstance for the database provider.- Returns:
- a
ColumnIdentifierGeneratorinstance
-
createSelectSqlGenerator
Create aSelectSqlGeneratorinstance for the database provider.- Returns:
- a
SelectSqlGeneratorinstance
-
createInsertSqlGenerator
Create anInsertSqlGeneratorinstance for the database provider.- Returns:
- an
InsertSqlGeneratorinstance
-
createUpdateSqlGenerator
Create anUpdateSqlGeneratorinstance for the database provider.- Returns:
- an
UpdateSqlGeneratorinstance
-
createDeleteSqlGenerator
Create aDeleteSqlGeneratorinstance for the database provider.- Returns:
- a
DeleteSqlGeneratorinstance
-