Class CreateForeignKey


public class CreateForeignKey extends H2SqlPart

Generates an ALTER TABLE...ADD CONSTRAINT statement to add a foreign key constraint in H2.


Sample Generated SQL:

ALTER TABLE products
  ADD CONSTRAINT fk_products_product_groups FOREIGN KEY (product_group_id)
  REFERENCES product_groups
  ON DELETE CASCADE
  ON UPDATE CASCADE
  • Constructor Details

    • CreateForeignKey

      public CreateForeignKey()
    • CreateForeignKey

      public CreateForeignKey(String constraintName, String primaryTableName, String foreignTableName)
      Constructs a CreateForeignKey with the specified constraint, primary table, and foreign table names.
      Parameters:
      constraintName - the constraint name
      primaryTableName - the primary table name
      foreignTableName - the foreign table name
  • Method Details

    • setPretty

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

      Example output (pretty = true):
      ALTER TABLE products
        ADD CONSTRAINT fk_products_product_groups FOREIGN KEY (product_group_id)
        REFERENCES product_groups
        ON DELETE CASCADE
        ON UPDATE CASCADE

      Example output (pretty = false):
      ALTER TABLE products ADD CONSTRAINT fk_products_product_groups FOREIGN KEY (product_group_id) REFERENCES product_groups ON DELETE CASCADE ON UPDATE CASCADE
      Overrides:
      setPretty in class SqlPart
    • collectSqlFragment

      public void collectSqlFragment(CodeWriter writer)
      Description copied from class: SqlPart
      Writes this part's SQL to the writer; used by SqlPart.getSqlFragment() and by enclosing parts.
      Specified by:
      collectSqlFragment in class SqlPart
    • getForeignTableName

      public String getForeignTableName()
      Returns the foreign table name.
      Returns:
      the foreign table name
    • setForeignTableName

      public CreateForeignKey setForeignTableName(String foreignTableName)
      Sets the foreign table name.
      Parameters:
      foreignTableName - the foreign table name
      Returns:
      this instance
    • getPrimaryTableName

      public String getPrimaryTableName()
      Returns the primary table name.
      Returns:
      the primary table name
    • setPrimaryTableName

      public CreateForeignKey setPrimaryTableName(String primaryTableName)
      Sets the primary table name.
      Parameters:
      primaryTableName - the primary table name
      Returns:
      this instance
    • getConstraintName

      public String getConstraintName()
      Returns the constraint name.
      Returns:
      the constraint name
    • setConstraintName

      public CreateForeignKey setConstraintName(String constraintName)
      Sets the constraint name.
      Parameters:
      constraintName - the constraint name
      Returns:
      this instance
    • getForeignKeyColumnNames

      public List<String> getForeignKeyColumnNames()
      Returns the foreign key column names.
      Returns:
      the foreign key column names
    • setForeignKeyColumnNames

      public CreateForeignKey setForeignKeyColumnNames(List<String> foreignKeyColumnNames)
      Sets the foreign key column names.
      Parameters:
      foreignKeyColumnNames - the foreign key column names
      Returns:
      this instance
    • setForeignKeyColumnNames

      public CreateForeignKey setForeignKeyColumnNames(String... foreignKeyColumnNames)
      Sets the foreign key column names.
      Parameters:
      foreignKeyColumnNames - the foreign key column names
      Returns:
      this instance
    • getPrimaryKeyColumnNames

      public List<String> getPrimaryKeyColumnNames()
      Returns the primary key column names.
      Returns:
      the primary key column names
    • setPrimaryKeyColumnNames

      public CreateForeignKey setPrimaryKeyColumnNames(List<String> primaryKeyColumnNames)
      Sets the primary key column names.
      Parameters:
      primaryKeyColumnNames - the primary key column names
      Returns:
      this instance
    • setPrimaryKeyColumnNames

      public CreateForeignKey setPrimaryKeyColumnNames(String... primaryKeyColumnNames)
      Sets the primary key column names.
      Parameters:
      primaryKeyColumnNames - the primary key column names
      Returns:
      this instance
    • getOnUpdateAction

      public ForeignKeyAction getOnUpdateAction()
      Returns the ON UPDATE action.
      Returns:
      the ON UPDATE action
    • setOnUpdateAction

      public CreateForeignKey setOnUpdateAction(ForeignKeyAction onUpdateAction)
      Sets the ON UPDATE action.
      Parameters:
      onUpdateAction - the ON UPDATE action
      Returns:
      this instance
    • getOnDeleteAction

      public ForeignKeyAction getOnDeleteAction()
      Returns the ON DELETE action.
      Returns:
      the ON DELETE action
    • setOnDeleteAction

      public CreateForeignKey setOnDeleteAction(ForeignKeyAction onDeleteAction)
      Sets the ON DELETE action.
      Parameters:
      onDeleteAction - the ON DELETE action
      Returns:
      this instance