Class AbstractDatabaseProvider

java.lang.Object
org.litebridge.db.spi.impl.AbstractDatabaseProvider
All Implemented Interfaces:
DatabaseProvider
Direct Known Subclasses:
H2DatabaseProvider, OracleDatabaseProvider, PostgresDatabaseProvider, SQLiteDatabaseProvider

public abstract class AbstractDatabaseProvider extends Object implements DatabaseProvider
An abstract implementation of the 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 Details

  • Constructor Details

    • AbstractDatabaseProvider

      public AbstractDatabaseProvider(TypeConverter typeConverter)
      Constructs a new AbstractDatabaseProvider.
      Parameters:
      typeConverter - The type converter to use.
  • Method Details

    • tableMetaData

      public TableMetaData tableMetaData(Table table, ConnectionProvider connectionProvider) throws SQLException
      Description copied from interface: DatabaseProvider
      Retrieve metadata for the specified table.
      Specified by:
      tableMetaData in interface DatabaseProvider
      Parameters:
      table - the Table object representing the table for which metadata is to be retrieved.
      connectionProvider - the ConnectionProvider used to get a database connection.
      Returns:
      a TableMetaData object 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: DatabaseProvider
      Execute an INSERT operation in the database using the provided Insert statement.
      Specified by:
      insert in interface DatabaseProvider
      Parameters:
      insert - the Insert statement containing the table, columns, and rows to insert.
      connectionProvider - the ConnectionProvider used to get a database connection.
      Returns:
      an InsertResult containing 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: DatabaseProvider
      Execute an UPDATE operation in the database using the provided Update statement.
      Specified by:
      update in interface DatabaseProvider
      Parameters:
      update - the Update statement containing the table, columns, and rows to update.
      connectionProvider - the ConnectionProvider used to get a database connection.
      Returns:
      an UpdateResult containing the number of rows affected.
      Throws:
      SQLException - if any SQL error occurs during the execution of the update operation.
    • select

      public List<Row> select(Select select, ConnectionProvider connectionProvider) throws SQLException
      Description copied from interface: DatabaseProvider
      Execute a SELECT operation in the database using the provided Select statement.
      Specified by:
      select in interface DatabaseProvider
      Parameters:
      select - the Select statement containing information about the table, expressions, joins, conditions, ordering, and optional limits for the query.
      connectionProvider - the ConnectionProvider used to get a database connection.
      Returns:
      a List of Row objects 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: DatabaseProvider
      Execute a DELETE operation in the database using the provided Delete statement.
      Specified by:
      delete in interface DatabaseProvider
      Parameters:
      delete - the Delete statement containing the table and rows to delete.
      connectionProvider - the ConnectionProvider used to get a database connection.
      Returns:
      an UpdateResult containing the number of rows affected.
      Throws:
      SQLException - if any SQL error occurs during the execution of the delete operation.
    • getTypeConverter

      public TypeConverter getTypeConverter()
      Description copied from interface: DatabaseProvider
      Retrieve the TypeConverter instance associated with the database provider.

      The TypeConverter is used for converting objects between different types, typically for database data type conversions and mapping domain-specific representations.

      Specified by:
      getTypeConverter in interface DatabaseProvider
      Returns:
      the TypeConverter instance for handling data type conversions
    • getSequenceColumnValueGenerator

      public SequenceColumnValueGenerator getSequenceColumnValueGenerator(String sequence) throws UnsupportedOperationException
      Description copied from interface: DatabaseProvider
      Retrieves a SequenceColumnValueGenerator instance 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:
      getSequenceColumnValueGenerator in interface DatabaseProvider
      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 SequenceColumnValueGenerator instance that generates SQL fragments for retrieving sequence values.
      Throws:
      UnsupportedOperationException - if the database provider does not support sequence-based value generation.
    • toSql

      public String toSql(Operation operation, ConnectionProvider connectionProvider)
      Description copied from interface: DatabaseProvider
      Converts the given Operation into its corresponding SQL representation.

      This does not execute the operation, it only generates the SQL string.

      Specified by:
      toSql in interface DatabaseProvider
      Parameters:
      operation - the Operation object representing the database operation (e.g., SELECT, INSERT, UPDATE, DELETE) to be converted to a SQL string.
      connectionProvider - the ConnectionProvider used to obtain database-specific context or information required for SQL generation, such as metadata or dialect.
      Returns:
      a String containing the SQL representation of the given Operation.
    • nativeSqlQuery

      public List<Row> nativeSqlQuery(String sql, List<@Nullable Object> bindParameters, ConnectionProvider connectionProvider) throws SQLException
      Description copied from interface: DatabaseProvider
      Executes a SQL query with the given SQL string and a list of positional bind parameters.
      Specified by:
      nativeSqlQuery in interface DatabaseProvider
      Parameters:
      sql - the SQL query string to be executed; must not be null
      bindParameters - the list of parameters to bind to the query
      connectionProvider - the ConnectionProvider used to get a database connection.
      Returns:
      a list of Row objects 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: DatabaseProvider
      Executes 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:
      nativeSqlUpdate in interface DatabaseProvider
      Parameters:
      sql - the SQL update statement to execute; must not be null
      bindParameters - the list of named parameters to bind to the statement
      connectionProvider - the ConnectionProvider used to get a database connection.
      Returns:
      an UpdateResult object that encapsulates the outcome of the update operation
      Throws:
      SQLException - if an error occurs while executing the update statement
    • getSqlFunctionRegistry

      public SqlFunctionRegistry getSqlFunctionRegistry()
      Description copied from interface: DatabaseProvider
      Retrieve the SqlFunctionRegistry instance associated with the database provider.

      The SqlFunctionRegistry is used for registering and managing SQL functions that can be used in SQL queries executed by the database provider.

      Specified by:
      getSqlFunctionRegistry in interface DatabaseProvider
      Returns:
      the SqlFunctionRegistry instance for managing SQL functions
    • getAliasTransformer

      public AliasTransformer getAliasTransformer()
      Description copied from interface: DatabaseProvider
      Retrieve the AliasTransformer instance associated with the database provider.
      Specified by:
      getAliasTransformer in interface DatabaseProvider
      Returns:
      the AliasTransformer instance 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 InsertResult object.

      Parameters:
      preparedSql - the PreparedSql object containing the SQL query string and bind values to be executed
      tableMetaData - the TableMetaData object containing the metadata of the target table, including primary key information
      returnGeneratedKeys - a boolean indicating whether the statement should return generated keys. Pass true to configure the statement to return generated keys, or false otherwise.
      connectionProvider - the ConnectionProvider used to obtain a database connection.
      Returns:
      an InsertResult object 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

      protected List<ColumnMetaData> getGeneratedPrimaryKeyColumns(TableMetaData tableMetaData)
      Get the primary key columns for which the database generates values.
      Parameters:
      tableMetaData - the TableMetaData object containing the metadata of the target table
      Returns:
      a list of ColumnMetaData objects 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 - the TableMetaData object containing the metadata of the target table
      preparedStatement - the executed PreparedStatement containing any generated keys
      Returns:
      a map of ColumnMetaData to 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 UpdateResult object.

      Parameters:
      preparedSql - the PreparedSql object containing the SQL query string and bind values to be executed
      tableMetaData - the TableMetaData object containing the metadata of the target table
      connectionProvider - the ConnectionProvider used to obtain a database connection.
      Returns:
      an UpdateResult object 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 - the Table object representing the table
      databaseMetaData - the DatabaseMetaData object used to retrieve column information
      Returns:
      a list of ColumnMetaData objects 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 - the Table object representing the table
      databaseMetaData - the DatabaseMetaData object 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 - the Table object representing the table to verify
      databaseMetaData - the DatabaseMetaData object used to perform the verification
      Throws:
      SQLException - if an error occurs while performing the verification
      IllegalArgumentException - if the schema or table is not found
    • prepareStatement

      protected PreparedStatement prepareStatement(PreparedSql preparedSql, boolean returnGeneratedKeys, @Nullable TableMetaData tableMetaData, ConnectionProvider connectionProvider) throws SQLException
      Prepare a PreparedStatement object based on the provided SQL and bind values.

      Optionally, the statement can be configured to return generated keys.

      Parameters:
      preparedSql - the PreparedSql object containing the SQL query and associated bind values.
      returnGeneratedKeys - a boolean indicating whether the statement should return generated keys. Pass true to configure the statement to return generated keys, or false otherwise.
      tableMetaData - Meta-data for the current table
      connectionProvider - the ConnectionProvider used to obtain a database connection.
      Returns:
      a PreparedStatement that 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 prepare
      bindParameters - the list of parameters to bind to the query
      returnGeneratedKeys - whether to return generated keys
      connectionProvider - 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 a PreparedStatement using the provided connection and prepared SQL.
      Parameters:
      preparedSql - the SQL and bind values to use
      returnGeneratedKeys - whether to return generated keys
      tableMetaData - the metadata for the table, required if returnGeneratedKeys is true
      connection - 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

      protected TableMetaData ensureTableMetaData(Table table, ConnectionProvider connectionProvider)
      Ensure that table metadata is available for the specified table, fetching it if not already cached.
      Parameters:
      table - the Table object representing the table
      connectionProvider - the ConnectionProvider used to fetch metadata if needed
      Returns:
      the TableMetaData for 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 details
      connectionProvider - the ConnectionProvider used to obtain a database connection.
      Returns:
      a TableMetaData object containing details about the table's structure, primary keys, and column metadata
      Throws:
      SQLException - if an error occurs while fetching database metadata
    • createSqlFunctionRegistryFactory

      protected SqlFunctionRegistryFactory createSqlFunctionRegistryFactory()
      Create a SqlFunctionRegistryFactory instance for the generation of a SQL function registry.

      This method may be overridden to provide a custom SqlFunctionRegistryFactory instance, allowing overriding/disabling of specific SQL functions.

      Returns:
      a SqlFunctionRegistryFactory implementation instance
    • createAliasTransformer

      protected AliasTransformer createAliasTransformer()
      Create an AliasTransformer instance for the database provider.
      Returns:
      an AliasTransformer instance
    • createColumnIdentifierGenerator

      protected ColumnIdentifierGenerator createColumnIdentifierGenerator()
      Create a ColumnIdentifierGenerator instance for the database provider.
      Returns:
      a ColumnIdentifierGenerator instance
    • createSelectSqlGenerator

      protected SelectSqlGenerator createSelectSqlGenerator()
      Create a SelectSqlGenerator instance for the database provider.
      Returns:
      a SelectSqlGenerator instance
    • createInsertSqlGenerator

      protected InsertSqlGenerator createInsertSqlGenerator()
      Create an InsertSqlGenerator instance for the database provider.
      Returns:
      an InsertSqlGenerator instance
    • createUpdateSqlGenerator

      protected UpdateSqlGenerator createUpdateSqlGenerator()
      Create an UpdateSqlGenerator instance for the database provider.
      Returns:
      an UpdateSqlGenerator instance
    • createDeleteSqlGenerator

      protected DeleteSqlGenerator createDeleteSqlGenerator()
      Create a DeleteSqlGenerator instance for the database provider.
      Returns:
      a DeleteSqlGenerator instance