Class DatabaseUtils
DatabaseUtils provides static methods for analyzing database structure
and relationships by interrogating JDBC metadata. It converts raw JDBC
metadata into structured Database, Table, Column,
and key relationship objects.
Key capabilities include:
- Database metadata extraction from connections or data sources
- Table discovery and column analysis
- Primary and foreign key relationship mapping
- Foreign key chain traversal for data generation dependencies
The extracted metadata is used by DatabaseFiller to understand
table dependencies and generate appropriate test data that respects
referential integrity constraints.
- Author:
- Tim Veil
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic longcolumnSeed(Column column, long baseSeed) Computes a stable, reproducible generation seed for a column.static ColumngetAssociatedPrimaryKeyColumn(Database database, Table table, Column column) Traverses foreign key relationships to find the root primary key column.static DatabasegetMetadata(Connection connection) Extracts complete database metadata from a Connection.static DatabasegetMetadata(DataSource dataSource) Extracts complete database metadata from a DataSource.
-
Method Details
-
columnSeed
Computes a stable, reproducible generation seed for a column.The seed combines a caller-supplied base seed (from
DatabaseConfiguration.seed()) with a deterministic hash of the column's identity. The identity intentionally uses theJDBCTypename rather than the enum constant:Enum.hashCode()is identity-based and therefore varies between JVM runs, which would make generated data non-reproducible. Every component used here (String,Integer, and the type name) has a hash that is stable across runs, so the same schema and base seed always yield the same data.Because this is a pure function of the column, a foreign-key column seeded from its associated primary-key column resolves to the same seed the primary key itself uses, preserving referential fidelity.
- Parameters:
column- the column to derive a seed forbaseSeed- the configured base seed; vary it to produce a different but still reproducible dataset- Returns:
- the seed to construct the column's generator with
-
getMetadata
Extracts complete database metadata from a DataSource.- Parameters:
dataSource- the data source to analyze- Returns:
- a Database object containing all discovered metadata
- Throws:
SQLException- if database access fails
-
getMetadata
Extracts complete database metadata from a Connection.Analyzes the database structure including tables, columns, primary keys, and foreign key relationships. The resulting Database object provides a complete view of the database schema suitable for data generation planning.
- Parameters:
connection- the database connection to analyze- Returns:
- a Database object containing all discovered metadata
- Throws:
SQLException- if database access fails
-
getAssociatedPrimaryKeyColumn
Traverses foreign key relationships to find the root primary key column.Given a column that may be part of a foreign key, this method follows the foreign key chain to find the ultimate primary key column that should be used as the source for generating related data values.
This is particularly useful for ensuring referential integrity when generating test data across related tables.
- Parameters:
database- the database containing all table metadatatable- the table containing the column to analyzecolumn- the column to find the associated primary key for- Returns:
- the root primary key column, or null if no foreign key relationship exists
-