Class DatabaseUtils

java.lang.Object
io.bloviate.util.DatabaseUtils

public class DatabaseUtils extends Object
Utility class for extracting database metadata through JDBC.

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 Details

    • columnSeed

      public static long columnSeed(Column column, long baseSeed)
      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 the JDBCType name 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 for
      baseSeed - 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

      public static Database getMetadata(DataSource dataSource) throws SQLException
      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

      public static Database getMetadata(Connection connection) throws SQLException
      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

      public static Column getAssociatedPrimaryKeyColumn(Database database, Table table, Column column)
      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 metadata
      table - the table containing the column to analyze
      column - the column to find the associated primary key for
      Returns:
      the root primary key column, or null if no foreign key relationship exists