- All Implemented Interfaces:
DatabaseProvider
- Direct Known Subclasses:
H2DatabaseProvider,OracleDatabaseProvider
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordA binding value and its associated SQL data type.protected static final recordA prepared row with associated value specifiers and bound values.static final recordA prepared SQL statement along with its associated bind values. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidappendLimitClause(Limit limit, StringBuilder sql) protected StringBuilderappendTable(StringBuilder sql, String schema, String table) protected StringBuilderappendTable(StringBuilder sql, Table table) protected StringBuilderappendTable(StringBuilder sql, TableMetaData table) protected StringcreateAlias(String alias) protected StringcreateColumnIdentifier(Column column, boolean includeColumnAlias, @Nullable Select select) protected StringcreateCondition(Condition condition, @Nullable Select select) Generate a SQL condition string based on the givenCondition.protected StringcreateJoin(Join join) Create a SQL JOIN clause based on the providedJoinobject.protected StringcreateMathOperation(ColumnMetaData column, MathOperation mathOperation) protected PreparedStatementcreatePreparedStatementUsingConnection(AbstractDatabaseProvider.PreparedSql preparedSql, boolean returnGeneratedKeys, TableMetaData tableMetaData, ManagedConnection connection) protected StringcreateSequenceNextValueForDirectInsert(String sequence) Generate a SQL fragment to retrieve the next value from a sequence for direct use in an INSERT or UPDATE statement, e.g. to generate "INSERT INTO LB.ACCOUNT(ACCOUNT_ID, ACCOUNT_NAME) VALUES (NEXT VALUE FOR sequence_name, ?)"delete(Delete delete, ConnectionProvider connectionProvider) protected ColumnMetaDataensureColumnMetaData(Column column, ConnectionProvider connectionProvider) protected InsertResultexecuteSqlInsert(AbstractDatabaseProvider.PreparedSql preparedSql, TableMetaData tableMetaData, boolean returnGeneratedKeys, ConnectionProvider connectionProvider) Execute a SQL INSERT operation using the provided prepared SQL statement and table metadata.protected UpdateResultexecuteSqlUpdate(AbstractDatabaseProvider.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 columns.protected List<ColumnMetaData> getColumnNames(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) Retrieve theTypeConverterinstance associated with the database provider.insert(Insert insert, ConnectionProvider connectionProvider) Execute an INSERT operation in the database using the providedInsertstatement.protected StringmapOperator(Operator operator) Map anOperatorenum to its corresponding string representation used in logical or database operations.protected AbstractDatabaseProvider.PreparedRowprepareRow(RowValue rowValue, ConnectionProvider connectionProvider) Prepare a row for insertion based on the provided row value.protected AbstractDatabaseProvider.PreparedSqlprepareSql(Delete delete, ConnectionProvider connectionProvider) protected AbstractDatabaseProvider.PreparedSqlprepareSql(Insert insert, ConnectionProvider connectionProvider) Prepare a SQL INSERT statement along with its bind values for execution.protected AbstractDatabaseProvider.PreparedSqlprepareSql(Update update, ConnectionProvider connectionProvider) Prepare a SQL UPDATE statement along with its bind values for execution.protected PreparedStatementprepareStatement(AbstractDatabaseProvider.PreparedSql preparedSql, boolean returnGeneratedKeys, TableMetaData tableMetaData, ConnectionProvider connectionProvider) Prepare aPreparedStatementobject based on the provided SQL and bind values.protected @Nullable StringquoteIdentifier(@Nullable String identifier) 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.protected @Nullable StringtransformAlias(@Nullable String dbAlias) update(Update update, ConnectionProvider connectionProvider) Execute an UPDATE operation in the database using the providedUpdatestatement.protected static voidverifySchemaAndTableExists(Table table, DatabaseMetaData databaseMetaData)
-
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, columns, 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 - Specified by:
deletein interfaceDatabaseProvider- Throws:
SQLException
-
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
-
toSql
- Specified by:
toSqlin interfaceDatabaseProvider
-
createSequenceNextValueForDirectInsert
Generate a SQL fragment to retrieve the next value from a sequence for direct use in an INSERT or UPDATE statement, e.g. to generate "INSERT INTO LB.ACCOUNT(ACCOUNT_ID, ACCOUNT_NAME) VALUES (NEXT VALUE FOR sequence_name, ?)", this method returns "NEXT VALUE FOR sequence_name".- Parameters:
sequence- the name of the database sequence to generate the next value from- Returns:
- a formatted SQL string representing the next sequence value for direct insertion
-
prepareSql
protected AbstractDatabaseProvider.PreparedSql prepareSql(Insert insert, ConnectionProvider connectionProvider) throws SQLException Prepare a SQL INSERT statement along with its bind values for execution.This method constructs the SQL query string based on the provided
Insertobject, which contains the table's metadata, columns, and rows to be inserted. The bind values are derived from the rows and included in the returnedAbstractDatabaseProvider.PreparedSql.- Parameters:
insert- theInsertobject containing the table metadata, columns, and rows for the SQL INSERT operation- Returns:
- a
AbstractDatabaseProvider.PreparedSqlobject containing the generated SQL query string and the list of bind values - Throws:
SQLException
-
prepareSql
protected AbstractDatabaseProvider.PreparedSql prepareSql(Update update, ConnectionProvider connectionProvider) throws SQLException Prepare a SQL UPDATE statement along with its bind values for execution.This method constructs the SQL query string based on the provided
Updateobject, which contains the table's metadata, column-value pairs, and conditions for the WHERE clause. It ensures proper formatting of the SQL query and converts values as needed using a type converter. The resulting SQL query and its associated bind values are encapsulated in aAbstractDatabaseProvider.PreparedSqlobject.- Parameters:
update- theUpdateobject containing table metadata, column-value pairs for the SET clause, and conditions for the WHERE clause to specify target rows.- Returns:
- a
AbstractDatabaseProvider.PreparedSqlobject containing the generated SQL query string and the list of bind values. - Throws:
SQLException
-
createMathOperation
-
prepareSql
protected AbstractDatabaseProvider.PreparedSql prepareSql(Delete delete, ConnectionProvider connectionProvider) throws SQLException - Throws:
SQLException
-
createJoin
Create a SQL JOIN clause based on the providedJoinobject.The join clause is constructed by specifying the target table, optional schema, and any associated conditions for the join operation. Conditional logic is applied to determine the join type (e.g., ON or USING) and format the resulting SQL string.
- Parameters:
join- theJoinobject containing the target table information and the list of conditions defining the join relationship- Returns:
- a
Stringrepresenting the constructed SQL join clause
-
createCondition
Generate a SQL condition string based on the givenCondition. This method constructs the SQL fragment by combining the column, operator, and value (if applicable) for the provided condition.- Parameters:
condition- theConditionobject specifying the column, operator, and value for the SQL condition- Returns:
- a
Stringrepresenting the constructed SQL condition fragment
-
appendTable
-
appendTable
-
appendTable
-
createColumnIdentifier
-
mapOperator
Map anOperatorenum to its corresponding string representation used in logical or database operations.- Parameters:
operator- the Operator enum to be mapped- Returns:
- the string representation of the provided Operator
-
executeSqlInsert
protected InsertResult executeSqlInsert(AbstractDatabaseProvider.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- theAbstractDatabaseProvider.PreparedSqlobject 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(AbstractDatabaseProvider.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- theAbstractDatabaseProvider.PreparedSqlobject 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
-
transformAlias
-
getColumnNames
protected List<ColumnMetaData> getColumnNames(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(AbstractDatabaseProvider.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- theAbstractDatabaseProvider.PreparedSqlobject 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(AbstractDatabaseProvider.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
-
prepareRow
protected AbstractDatabaseProvider.PreparedRow prepareRow(RowValue rowValue, ConnectionProvider connectionProvider) throws SQLException Prepare a row for insertion based on the provided row value. This includes processing column values, converting them to a suitable format, and generating value specifiers and bind values for the prepared row. Handles nullable columns, auto-increment columns, and sequence-based value generation as necessary.- Parameters:
rowValue- the row value object containing the column definitions and their values- Returns:
- a PreparedRow instance containing processed value specifiers and bind values
- Throws:
IllegalArgumentException- if a non-nullable column without an auto-increment or sequence value is attempted to be set to NULLSQLException
-
fetchTableMetaData
protected TableMetaData fetchTableMetaData(Table table, ConnectionProvider connectionProvider) throws SQLException Retrieve metadata for the specified table, including its primary keys and columns.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
-
quoteIdentifier
-
createAlias
-
appendLimitClause
-
ensureColumnMetaData
protected ColumnMetaData ensureColumnMetaData(Column column, ConnectionProvider connectionProvider) throws SQLException - Throws:
SQLException
-