Search in sources :

Example 1 with ConstraintForeignKeyOnStep

use of org.jooq.ConstraintForeignKeyOnStep in project jOOQ by jOOQ.

the class DefaultParseContext method parseForeignKeyReferenceSpecification.

private final Constraint parseForeignKeyReferenceSpecification(ConstraintTypeStep constraint, Field<?>[] referencing) {
    Table<?> referencedTable = parseTableName();
    Field<?>[] referencedFields = EMPTY_FIELD;
    if (parseIf('(')) {
        referencedFields = parseList(',', c -> parseFieldName()).toArray(EMPTY_FIELD);
        parse(')');
        if (referencing.length != referencedFields.length)
            throw exception("Number of referencing columns (" + referencing.length + ") must match number of referenced columns (" + referencedFields.length + ")");
    }
    ConstraintForeignKeyOnStep e = constraint == null ? foreignKey(referencing).references(referencedTable, referencedFields) : constraint.foreignKey(referencing).references(referencedTable, referencedFields);
    boolean onDelete = false;
    boolean onUpdate = false;
    while ((!onDelete || !onUpdate) && parseKeywordIf("ON")) {
        if (!onDelete && parseKeywordIf("DELETE")) {
            onDelete = true;
            if (parseKeywordIf("CASCADE"))
                e = e.onDeleteCascade();
            else if (parseKeywordIf("NO ACTION"))
                e = e.onDeleteNoAction();
            else if (parseKeywordIf("RESTRICT"))
                e = e.onDeleteRestrict();
            else if (parseKeywordIf("SET DEFAULT"))
                e = e.onDeleteSetDefault();
            else if (parseKeywordIf("SET NULL"))
                e = e.onDeleteSetNull();
            else
                throw expected("CASCADE", "NO ACTION", "RESTRICT", "SET DEFAULT", "SET NULL");
        } else if (!onUpdate && parseKeywordIf("UPDATE")) {
            onUpdate = true;
            if (parseKeywordIf("CASCADE"))
                e = e.onUpdateCascade();
            else if (parseKeywordIf("NO ACTION"))
                e = e.onUpdateNoAction();
            else if (parseKeywordIf("RESTRICT"))
                e = e.onUpdateRestrict();
            else if (parseKeywordIf("SET DEFAULT"))
                e = e.onUpdateSetDefault();
            else if (parseKeywordIf("SET NULL"))
                e = e.onUpdateSetNull();
            else
                throw expected("CASCADE", "NO ACTION", "RESTRICT", "SET DEFAULT", "SET NULL");
        } else
            throw expected("DELETE", "UPDATE");
    }
    return parseConstraintEnforcementIf(e);
}
Also used : GroupField(org.jooq.GroupField) TableField(org.jooq.TableField) Field(org.jooq.Field) SortField(org.jooq.SortField) SelectField(org.jooq.SelectField) ConstraintForeignKeyOnStep(org.jooq.ConstraintForeignKeyOnStep)

Aggregations

ConstraintForeignKeyOnStep (org.jooq.ConstraintForeignKeyOnStep)1 Field (org.jooq.Field)1 GroupField (org.jooq.GroupField)1 SelectField (org.jooq.SelectField)1 SortField (org.jooq.SortField)1 TableField (org.jooq.TableField)1