Enum Class DatabaseSchemaGenerator

java.lang.Object
java.lang.Enum<DatabaseSchemaGenerator>
dimstyl.orm.internal.sql.generator.DatabaseSchemaGenerator
All Implemented Interfaces:
SqlQueryGenerator<List<String>,DatabaseMetadata>, Serializable, Comparable<DatabaseSchemaGenerator>, Constable

public enum DatabaseSchemaGenerator extends Enum<DatabaseSchemaGenerator> implements SqlQueryGenerator<List<String>,DatabaseMetadata>
Singleton-based SQL query generator responsible for creating database schema scripts.

This generator produces `CREATE TABLE` SQL queries based on metadata and supports different database engines such as H2, SQLite, and Derby.

  • Enum Constant Details

    • INSTANCE

      public static final DatabaseSchemaGenerator INSTANCE
      The singleton instance of the database schema generator.
  • Constructor Details

    • DatabaseSchemaGenerator

      private DatabaseSchemaGenerator()
  • Method Details

    • values

      public static DatabaseSchemaGenerator[] values()
      Returns an array containing the constants of this enum class, in the order they are declared.
      Returns:
      an array containing the constants of this enum class, in the order they are declared
    • valueOf

      public static DatabaseSchemaGenerator valueOf(String name)
      Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)
      Parameters:
      name - the name of the enum constant to be returned.
      Returns:
      the enum constant with the specified name
      Throws:
      IllegalArgumentException - if this enum class has no constant with the specified name
      NullPointerException - if the argument is null
    • generate

      public List<String> generate(DatabaseMetadata databaseMetadata) throws InvalidColumnNameException
      Generates SQL `CREATE TABLE` queries based on DatabaseMetadata.

      The generated queries are also saved as SQL script files based on the selected database engine.

      Specified by:
      generate in interface SqlQueryGenerator<List<String>,DatabaseMetadata>
      Parameters:
      databaseMetadata - Metadata describing the database schema.
      Returns:
      A list of SQL `CREATE TABLE` statements as strings.
      Throws:
      InvalidColumnNameException - If any column in constraints does not exist in the table definition.
    • generateCreateTableQuery

      private String generateCreateTableQuery(TableMetadata tableMetadata, DatabaseEngine databaseEngine) throws InvalidColumnNameException
      Generates an SQL `CREATE TABLE` query based on TableMetadata and DatabaseEngine.
      Parameters:
      tableMetadata - The metadata for the table.
      databaseEngine - The database engine for which the query is generated.
      Returns:
      The SQL `CREATE TABLE` query as a string.
      Throws:
      InvalidColumnNameException - If an invalid column name is referenced.
    • addColumnDefinitions

      private Set<String> addColumnDefinitions(StringBuilder sqlBuilder, List<ColumnMetadata> columnMetadataList)
      Appends column definitions to the SQL query based on the provided list of ColumnMetadata.
      Parameters:
      sqlBuilder - The StringBuilder for the SQL query.
      columnMetadataList - The list of column metadata.
      Returns:
      A set of column names used in the table.
    • addPrimaryKeyConstraint

      private void addPrimaryKeyConstraint(StringBuilder sqlBuilder, List<String> primaryKeys)
      Adds a `PRIMARY KEY` constraint to the SQL query if necessary.
      Parameters:
      sqlBuilder - The StringBuilder for the SQL query.
      primaryKeys - A list of primary key column names.
    • addUniqueConstraints

      private void addUniqueConstraints(StringBuilder sqlBuilder, UniqueConstraint[] uniqueConstraints, Set<String> tableColumnNames, String tableName) throws InvalidColumnNameException
      Adds `UNIQUE` constraints to the table definition based on UniqueConstraint.
      Parameters:
      sqlBuilder - The StringBuilder for the SQL query.
      uniqueConstraints - The array of unique constraints.
      tableColumnNames - The set of column names in the table.
      tableName - The name of the table.
      Throws:
      InvalidColumnNameException - If an invalid column name is referenced in the constraint.
    • validateConstraint

      private List<String> validateConstraint(Set<String> tableColumnNames, String tableName, UniqueConstraint uniqueConstraint) throws InvalidColumnNameException
      Validates that all columns referenced in a UniqueConstraint exist in the table.
      Parameters:
      tableColumnNames - The set of column names in the table.
      tableName - The name of the table.
      uniqueConstraint - The unique constraint being validated.
      Returns:
      A list of valid column names for the unique constraint.
      Throws:
      InvalidColumnNameException - If a referenced column does not exist in the table.