Interface DatabaseSupport

All Known Implementing Classes:
AbstractDatabaseSupport, CockroachDBSupport, DefaultSupport, H2Support, MariaDBSupport, MySQLSupport, PostgresSupport, SQLiteSupport

public interface DatabaseSupport
Database-specific support interface for data generation and SQL handling.

This interface defines the single contract for database-specific implementations: mapping a Column to an appropriate DataGenerator. The cross-database defaults and the per-JDBCType registry live in AbstractDatabaseSupport; database-specific subclasses customize generation by registering or replacing factories rather than implementing this interface directly.

Common implementations include PostgresSupport, MySQLSupport, CockroachDBSupport, and DefaultSupport for generic databases.

Author:
Tim Veil
See Also:
  • Method Details

    • getDataGenerator

      DataGenerator<?> getDataGenerator(Column column, RandomGenerator random)
      Creates an appropriate data generator for the given column based on its JDBCType (and, where relevant, its database-specific type name).
      Parameters:
      column - the column metadata including type and constraints
      random - the random number generator for seeded data generation
      Returns:
      a data generator appropriate for the column type
      Throws:
      UnsupportedOperationException - if the column type is not supported
    • batchRewriteUrlParameter

      default String batchRewriteUrlParameter()
      Returns the JDBC-URL parameter name that enables this driver's batch rewrite optimization — collapsing a JDBC batch into a single multi-row INSERT — or null if the database has no such parameter.

      Bloviate fills through a Connection/DataSource it does not own, so it cannot add this parameter to the URL after the fact. Instead, the fill engine reads it to warn when the parameter is absent (it is often the single biggest fill speedup), and JdbcUrls uses it to help callers build a correctly-parameterized URL.

      Known values: PostgreSQL reWriteBatchedInserts, MySQL rewriteBatchedStatements. CockroachDB ignores batch rewrite, so it returns null; the generic DefaultSupport also returns null.

      Returns:
      the batch-rewrite URL parameter name, or null if not applicable
      Since:
      2.10.0
    • readConstraints

      default Map<String, ColumnConstraint> readConstraints(Connection connection, String schema, String table)
      Reads the value constraints (CHECK constraints and enum/domain allowed values) for a table's columns, so the fill engine can generate values that satisfy them (issue #479). The default returns an empty map (no constraint awareness); database-specific implementations override it with vendor catalog queries.

      Constraint reading is best-effort and must never fail a fill: an implementation that cannot read the catalog should log and return what it has.

      Parameters:
      connection - an open connection to query the catalog with
      schema - the table's schema (may be null for the default schema)
      table - the table name
      Returns:
      constraints keyed by lower-cased column name; empty when none are found or supported
      Since:
      2.14.0
    • supportsBulkLoad

      default boolean supportsBulkLoad()
      Whether this support can disable and re-enable foreign-key enforcement for an UNORDERED_BULK fill (see BulkLoadStrategy). The default is false; callers must check this before invoking disableConstraints(Connection, Database) and fall back to the ordered fill path when it returns false.

      PostgreSQL and MySQL override this to true; CockroachDB leaves it false (no session_replication_role, distributed foreign-key/index handling).

      Returns:
      whether unordered bulk loading with disabled constraints is supported
      Since:
      2.17.0
    • disableConstraints

      default BulkLoadHandle disableConstraints(Connection connection, Database database) throws SQLException
      Disables foreign-key (and, where applicable, unique) enforcement for the duration of a bulk load, on the given connection's session. Returns a BulkLoadHandle describing what was disabled so enableConstraints(Connection, Database, BulkLoadHandle) can restore it.

      The session-based mechanisms used by PostgreSQL and MySQL are per connection: this must be called on every connection that will insert. DatabaseFiller applies it inside each worker task and restores it in a finally before returning the connection to the pool, so no constraint-disabled connection ever leaks back to the pool.

      The default throws UnsupportedOperationException; only override it alongside supportsBulkLoad() returning true.

      Parameters:
      connection - an open connection whose session enforcement should be disabled
      database - the database metadata (for mechanisms that need per-table information)
      Returns:
      a handle describing what was disabled, for use by enableConstraints(Connection, Database, BulkLoadHandle)
      Throws:
      BulkLoadUnsupportedException - if enforcement cannot be disabled (e.g. missing privilege)
      SQLException - if the underlying statement fails
      Since:
      2.17.0
    • enableConstraints

      default void enableConstraints(Connection connection, Database database, BulkLoadHandle handle) throws SQLException
      Re-enables what disableConstraints(Connection, Database) turned off, on the same connection. Must be safe to call from a finally block.

      The session-based PostgreSQL/MySQL mechanisms simply restore the session setting; they cannot cheaply re-validate already-inserted rows, which is acceptable because bulk-loaded data is referentially consistent by construction.

      Parameters:
      connection - the same connection passed to disableConstraints(Connection, Database)
      database - the database metadata
      handle - the handle returned by disableConstraints(Connection, Database)
      Throws:
      SQLException - if the underlying statement fails
      Since:
      2.17.0
    • forProduct

      static DatabaseSupport forProduct(String productName)
      Selects a DatabaseSupport for the given JDBC product name (as reported by DatabaseMetaData.getDatabaseProductName()), so callers don't have to hardcode a specific implementation.

      Matching is case-insensitive and substring-based: names containing "cockroach" map to CockroachDBSupport, "mariadb" to MariaDBSupport, "mysql" to MySQLSupport, "postgres" to PostgresSupport, "h2" to H2Support, and "sqlite" to SQLiteSupport. Anything else (including null) falls back to DefaultSupport.

      CockroachDB note: CockroachDB is typically reached through the PostgreSQL JDBC driver, which reports its product name as "PostgreSQL", so such connections resolve to PostgresSupport. Because CockroachDBSupport extends PostgresSupport and adds no extra behavior, this resolves the same type handling (uuid, jsonb, inet, intervals, arrays, bit strings, ...); passing new CockroachDBSupport() explicitly is equivalent.

      MariaDB note: the MariaDB Connector/J driver reports "MariaDB", which resolves here to MariaDBSupport. The legacy MySQL Connector/J driver, however, reports "MySQL" even against a MariaDB server, in which case this resolves to MySQLSupport; that is acceptable because MariaDBSupport extends MySQLSupport and the inherited type handling works against MariaDB.

      Parameters:
      productName - the database product name, may be null
      Returns:
      the matching support, or DefaultSupport if none matches
    • forConnection

      static DatabaseSupport forConnection(Connection connection) throws SQLException
      Selects a DatabaseSupport by reading the product name from the connection's metadata. See forProduct(String) for the matching rules and the CockroachDB caveat.
      Parameters:
      connection - an open database connection
      Returns:
      the matching support, or DefaultSupport if none matches
      Throws:
      SQLException - if the connection metadata cannot be read