Enum Class 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.
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Enum
Enum.EnumDesc<E extends Enum<E>>
-
Enum Constant Summary
Enum Constants -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddColumnDefinitions
(StringBuilder sqlBuilder, List<ColumnMetadata> columnMetadataList) Appends column definitions to the SQL query based on the provided list ofColumnMetadata
.private void
addPrimaryKeyConstraint
(StringBuilder sqlBuilder, List<String> primaryKeys) Adds a `PRIMARY KEY` constraint to the SQL query if necessary.private void
addUniqueConstraints
(StringBuilder sqlBuilder, UniqueConstraint[] uniqueConstraints, Set<String> tableColumnNames, String tableName) Adds `UNIQUE` constraints to the table definition based onUniqueConstraint
.generate
(DatabaseMetadata databaseMetadata) Generates SQL `CREATE TABLE` queries based onDatabaseMetadata
.private String
generateCreateTableQuery
(TableMetadata tableMetadata, DatabaseEngine databaseEngine) Generates an SQL `CREATE TABLE` query based onTableMetadata
andDatabaseEngine
.validateConstraint
(Set<String> tableColumnNames, String tableName, UniqueConstraint uniqueConstraint) Validates that all columns referenced in aUniqueConstraint
exist in the table.static DatabaseSchemaGenerator
Returns the enum constant of this class with the specified name.static DatabaseSchemaGenerator[]
values()
Returns an array containing the constants of this enum class, in the order they are declared.
-
Enum Constant Details
-
INSTANCE
The singleton instance of the database schema generator.
-
-
Constructor Details
-
DatabaseSchemaGenerator
private DatabaseSchemaGenerator()
-
-
Method Details
-
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
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 nameNullPointerException
- if the argument is null
-
generate
Generates SQL `CREATE TABLE` queries based onDatabaseMetadata
.The generated queries are also saved as SQL script files based on the selected database engine.
- Specified by:
generate
in interfaceSqlQueryGenerator<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 onTableMetadata
andDatabaseEngine
.- 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 ofColumnMetadata
.- Parameters:
sqlBuilder
- TheStringBuilder
for the SQL query.columnMetadataList
- The list of column metadata.- Returns:
- A set of column names used in the table.
-
addPrimaryKeyConstraint
Adds a `PRIMARY KEY` constraint to the SQL query if necessary.- Parameters:
sqlBuilder
- TheStringBuilder
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 onUniqueConstraint
.- Parameters:
sqlBuilder
- TheStringBuilder
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 aUniqueConstraint
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.
-