Class CreatePostgreSqlDdlFromSchemaDef

java.lang.Object
com.northconcepts.datapipeline.jdbc.sql.SqlPart
com.northconcepts.datapipeline.sql.postgresql.CreatePostgreSqlDdlFromSchemaDef

public class CreatePostgreSqlDdlFromSchemaDef extends SqlPart

Generates PostgreSQL DDL from SchemaDef


Sample Generated SQL:

DROP TABLE IF EXISTS table_name;

CREATE TABLE IF NOT EXISTS table_name (
  user_id serial,
  email text,
  PRIMARY KEY (user_id)
);

CREATE INDEX IF NOT EXISTS idx_email ON table_name (email);

ALTER TABLE IF EXISTS foreign_table_name
  ADD CONSTRAINT fk_constraint_name FOREIGN KEY(fk_field_name)
  REFERENCES primary_table_name (pk_field_name)
  ON DELETE CASCADE
  ON UPDATE RESTRICT;
  • Constructor Details

    • CreatePostgreSqlDdlFromSchemaDef

      public CreatePostgreSqlDdlFromSchemaDef(SchemaDef schemaDef)
  • Method Details

    • collectSqlFragment

      public void collectSqlFragment(CodeWriter writer)
      Generates DDL from schemaDef and writes the output to writer.
      Specified by:
      collectSqlFragment in class SqlPart
    • getSchemaDef

      public SchemaDef getSchemaDef()
      Returns provided schemaDef.
    • isGenerateIndexes

      public boolean isGenerateIndexes()
      Indicates if DDL should be generated for indexes in the schemaDef (default is true).
    • setGenerateIndexes

      public CreatePostgreSqlDdlFromSchemaDef setGenerateIndexes(boolean generateIndexes)
      Indicates if DDL should be generated for indexes in the schemaDef (default is true).
    • isGenerateRelationships

      public boolean isGenerateRelationships()
      Indicates if DDL should be generated for relationships in the schemaDef (default is true).
    • setGenerateRelationships

      public CreatePostgreSqlDdlFromSchemaDef setGenerateRelationships(boolean generateRelationships)
      Indicates if DDL should be generated for indexes in the schemaDef (default is true).
    • isCheckIfTableNotExists

      public boolean isCheckIfTableNotExists()
      Indicates if IF NOT EXISTS clause should be included in the CREATE TABLE statement (default is true).
    • setCheckIfTableNotExists

      public CreatePostgreSqlDdlFromSchemaDef setCheckIfTableNotExists(boolean checkIfTableNotExists)
      Indicates if IF NOT EXISTS clause should be included in the CREATE TABLE statement (default is true).
    • isCheckIfDropTableExists

      public boolean isCheckIfDropTableExists()
      Indicates if IF EXISTS clause should be included in the DROP TABLE statement (default is true).
    • setCheckIfDropTableExists

      public CreatePostgreSqlDdlFromSchemaDef setCheckIfDropTableExists(boolean checkIfDropTableExists)
      Indicates if IF EXISTS clause should be included in the DROP TABLE statement (default is true).
    • isCheckIfIndexNotExists

      public boolean isCheckIfIndexNotExists()
      Indicates if IF NOT EXISTS clause should be included in the CREATE INDEX statement (default is true).
    • setCheckIfIndexNotExists

      public CreatePostgreSqlDdlFromSchemaDef setCheckIfIndexNotExists(boolean checkIfIndexNotExists)
      Indicates if IF NOT EXISTS clause should be included in the CREATE INDEX statement (default is true).
    • isCheckIfForeignTableExists

      public boolean isCheckIfForeignTableExists()
      Indicates if IF EXISTS clause should be included in the ALTER TABLE statement (default is true).
    • setCheckIfForeignTableExists

      public CreatePostgreSqlDdlFromSchemaDef setCheckIfForeignTableExists(boolean checkIfForeignTableExists)
      Indicates if IF EXISTS clause should be included in the ALTER TABLE statement (default is true).
    • getEntityNames

      public List<String> getEntityNames()
      Returns entity names that are specified by setEntityNames(java.util.List<java.lang.String>) to be included in the output DDL.
    • setEntityNames

      public CreatePostgreSqlDdlFromSchemaDef setEntityNames(List<String> entityNames)

      Set entity names that should be included in the output DDL (all entities will be included by default).



      Entities that are not listed in entityNames will be excluded from the generated SQL including relationships that involve any of them.
    • setEntityNames

      public CreatePostgreSqlDdlFromSchemaDef setEntityNames(String... entityNames)

      Set entity names that should be included in the output DDL (all entities will be included by default).



      Entities that are not listed in entityNames will be excluded from the generated SQL including relationships that involve any of them.
    • setPretty

      public CreatePostgreSqlDdlFromSchemaDef setPretty(boolean pretty)
      Indicates if line breaks and indentations should be added to output DDL (default false).

      Example output (pretty = true):
      CREATE TABLE IF NOT EXISTS table_name (
        id serial,
        email text
        PRIMARY KEY (user_id)
      );

      Example output (pretty = false):
      CREATE TABLE IF NOT EXISTS table_name (id serial, email text, PRIMARY KEY (user_id));
      Overrides:
      setPretty in class SqlPart
    • isDropTable

      public boolean isDropTable()
      Indicates if drop table statement should be included in the output DDL for each entity in the schemaDef (default true).
    • setDropTable

      public CreatePostgreSqlDdlFromSchemaDef setDropTable(boolean dropTable)
      Indicates if drop table statement should be included in the output DDL for each entity in the schemaDef (default true).
    • getDefaultBigIntegerPrecision

      public Integer getDefaultBigIntegerPrecision()
      Returns the default precision of FieldType.BIG_INTEGER (default 20).
    • setDefaultBigIntegerPrecision

      public CreatePostgreSqlDdlFromSchemaDef setDefaultBigIntegerPrecision(int defaultBigIntegerPrecision)
      Sets a default precision for FieldType.BIG_INTEGER (default 20).
      This value will be used if no precision is specified for BIG_INTEGER fields.
    • getDefaultBigDecimalPrecision

      public Integer getDefaultBigDecimalPrecision()
      Returns the default precision of FieldType.BIG_DECIMAL (default 20).
    • setDefaultBigDecimalPrecision

      public CreatePostgreSqlDdlFromSchemaDef setDefaultBigDecimalPrecision(int defaultBigDecimalPrecision)
      Sets a default precision for FieldType.BIG_DECIMAL.
      This value will be used if no precision is specified for BIG_DECIMAL fields (default 20).
    • getDefaultBigDecimalScale

      public Integer getDefaultBigDecimalScale()
      Sets the default scale of FieldType.BIG_DECIMAL (default 5).
    • setDefaultBigDecimalScale

      public CreatePostgreSqlDdlFromSchemaDef setDefaultBigDecimalScale(Integer defaultBigDecimalScale)
      Sets a default scale for FieldType.BIG_DECIMAL (default 5).
      This value will be used if no scale is specified for BIG_DECIMAL fields.