Interface DatabaseSupport
- All Known Implementing Classes:
AbstractDatabaseSupport, CockroachDBSupport, DefaultSupport, H2Support, MariaDBSupport, MySQLSupport, PostgresSupport, SQLiteSupport
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 Summary
Modifier and TypeMethodDescriptiondefault StringReturns the JDBC-URL parameter name that enables this driver's batch rewrite optimization — collapsing a JDBC batch into a single multi-rowINSERT— ornullif the database has no such parameter.default BulkLoadHandledisableConstraints(Connection connection, Database database) Disables foreign-key (and, where applicable, unique) enforcement for the duration of a bulk load, on the given connection's session.default voidenableConstraints(Connection connection, Database database, BulkLoadHandle handle) Re-enables whatdisableConstraints(Connection, Database)turned off, on the same connection.static DatabaseSupportforConnection(Connection connection) Selects aDatabaseSupportby reading the product name from the connection's metadata.static DatabaseSupportforProduct(String productName) Selects aDatabaseSupportfor the given JDBC product name (as reported byDatabaseMetaData.getDatabaseProductName()), so callers don't have to hardcode a specific implementation.getDataGenerator(Column column, RandomGenerator random) Creates an appropriate data generator for the given column based on itsJDBCType(and, where relevant, its database-specific type name).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).default booleanWhether this support can disable and re-enable foreign-key enforcement for anUNORDERED_BULKfill (seeBulkLoadStrategy).
-
Method Details
-
getDataGenerator
Creates an appropriate data generator for the given column based on itsJDBCType(and, where relevant, its database-specific type name).- Parameters:
column- the column metadata including type and constraintsrandom- 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
Returns the JDBC-URL parameter name that enables this driver's batch rewrite optimization — collapsing a JDBC batch into a single multi-rowINSERT— ornullif the database has no such parameter.Bloviate fills through a
Connection/DataSourceit 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), andJdbcUrlsuses it to help callers build a correctly-parameterized URL.Known values: PostgreSQL
reWriteBatchedInserts, MySQLrewriteBatchedStatements. CockroachDB ignores batch rewrite, so it returnsnull; the genericDefaultSupportalso returnsnull.- Returns:
- the batch-rewrite URL parameter name, or
nullif 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 withschema- 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 anUNORDERED_BULKfill (seeBulkLoadStrategy). The default isfalse; callers must check this before invokingdisableConstraints(Connection, Database)and fall back to the ordered fill path when it returnsfalse.PostgreSQL and MySQL override this to
true; CockroachDB leaves itfalse(nosession_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 aBulkLoadHandledescribing what was disabled soenableConstraints(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.
DatabaseFillerapplies it inside each worker task and restores it in afinallybefore returning the connection to the pool, so no constraint-disabled connection ever leaks back to the pool.The default throws
UnsupportedOperationException; only override it alongsidesupportsBulkLoad()returningtrue.- Parameters:
connection- an open connection whose session enforcement should be disableddatabase- 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 whatdisableConstraints(Connection, Database)turned off, on the same connection. Must be safe to call from afinallyblock.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 todisableConstraints(Connection, Database)database- the database metadatahandle- the handle returned bydisableConstraints(Connection, Database)- Throws:
SQLException- if the underlying statement fails- Since:
- 2.17.0
-
forProduct
Selects aDatabaseSupportfor the given JDBC product name (as reported byDatabaseMetaData.getDatabaseProductName()), so callers don't have to hardcode a specific implementation.Matching is case-insensitive and substring-based: names containing
"cockroach"map toCockroachDBSupport,"mariadb"toMariaDBSupport,"mysql"toMySQLSupport,"postgres"toPostgresSupport,"h2"toH2Support, and"sqlite"toSQLiteSupport. Anything else (includingnull) falls back toDefaultSupport.CockroachDB note: CockroachDB is typically reached through the PostgreSQL JDBC driver, which reports its product name as
"PostgreSQL", so such connections resolve toPostgresSupport. BecauseCockroachDBSupportextendsPostgresSupportand adds no extra behavior, this resolves the same type handling (uuid, jsonb, inet, intervals, arrays, bit strings, ...); passingnew CockroachDBSupport()explicitly is equivalent.MariaDB note: the MariaDB Connector/J driver reports
"MariaDB", which resolves here toMariaDBSupport. The legacy MySQL Connector/J driver, however, reports"MySQL"even against a MariaDB server, in which case this resolves toMySQLSupport; that is acceptable becauseMariaDBSupportextendsMySQLSupportand the inherited type handling works against MariaDB.- Parameters:
productName- the database product name, may be null- Returns:
- the matching support, or
DefaultSupportif none matches
-
forConnection
Selects aDatabaseSupportby reading the product name from the connection's metadata. SeeforProduct(String)for the matching rules and the CockroachDB caveat.- Parameters:
connection- an open database connection- Returns:
- the matching support, or
DefaultSupportif none matches - Throws:
SQLException- if the connection metadata cannot be read
-