From ec3a19fcd670f54851a5643c012d3d7c3e40ed76 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Tue, 15 Oct 2019 16:05:55 +0200 Subject: [PATCH] [jOOQ/jOOQ#8528] Interpret ALTER TABLE .. ADD FOREIGN KEY --- .../java/org/jooq/impl/DDLInterpreter.java | 58 ++++++++++--------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/impl/DDLInterpreter.java b/jOOQ/src/main/java/org/jooq/impl/DDLInterpreter.java index 6173ac01fc..14365c7c5b 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DDLInterpreter.java +++ b/jOOQ/src/main/java/org/jooq/impl/DDLInterpreter.java @@ -229,37 +229,14 @@ final class DDLInterpreter { for (Constraint constraint : query.$constraints()) { ConstraintImpl impl = (ConstraintImpl) constraint; - if (impl.$primaryKey() != null) { + if (impl.$primaryKey() != null) mt.primaryKey = new MutableUniqueKey((UnqualifiedName) impl.getUnqualifiedName(), mt, mt.fields(impl.$primaryKey(), true)); - } - else if (impl.$unique() != null) { + else if (impl.$unique() != null) mt.uniqueKeys.add(new MutableUniqueKey((UnqualifiedName) impl.getUnqualifiedName(), mt, mt.fields(impl.$unique(), true))); - } - else if (impl.$foreignKey() != null) { - MutableTable mrf = schema.table(impl.$referencesTable()); - MutableUniqueKey mu = null; - - if (mrf == null) - throw tableNotExists(impl.$referencesTable()); - - List mfs = mt.fields(impl.$foreignKey(), true); - List mrfs = mrf.fields(impl.$references(), true); - - if (!mrfs.isEmpty()) - mu = mrf.uniqueKey(mrfs); - else if (mrf.primaryKey != null && mrf.primaryKey.keyFields.size() == mfs.size()) - mu = mrf.primaryKey; - - if (mu == null) - throw primaryKeyNotExists(); - - mt.foreignkeys.add(new MutableForeignKey( - (UnqualifiedName) impl.getUnqualifiedName(), mt, mfs, mu, impl.$onDelete(), impl.$onUpdate() - )); - } - else { + else if (impl.$foreignKey() != null) + addForeignKey(schema, mt, impl); + else throw unsupportedQuery(query); - } } for (Index index : query.$indexes()) { @@ -268,6 +245,29 @@ final class DDLInterpreter { } } + private final void addForeignKey(MutableSchema schema, MutableTable mt, ConstraintImpl impl) { + MutableTable mrf = schema.table(impl.$referencesTable()); + MutableUniqueKey mu = null; + + if (mrf == null) + throw tableNotExists(impl.$referencesTable()); + + List mfs = mt.fields(impl.$foreignKey(), true); + List mrfs = mrf.fields(impl.$references(), true); + + if (!mrfs.isEmpty()) + mu = mrf.uniqueKey(mrfs); + else if (mrf.primaryKey != null && mrf.primaryKey.keyFields.size() == mfs.size()) + mu = mrf.primaryKey; + + if (mu == null) + throw primaryKeyNotExists(); + + mt.foreignkeys.add(new MutableForeignKey( + (UnqualifiedName) impl.getUnqualifiedName(), mt, mfs, mu, impl.$onDelete(), impl.$onUpdate() + )); + } + @SuppressWarnings({ "rawtypes", "unchecked" }) private final void accept0(AlterTableImpl query) { Table table = query.$table(); @@ -325,6 +325,8 @@ final class DDLInterpreter { existing.primaryKey = new MutableUniqueKey((UnqualifiedName) impl.getUnqualifiedName(), existing, existing.fields(impl.$primaryKey(), true)); else if (impl.$unique() != null) existing.uniqueKeys.add(new MutableUniqueKey((UnqualifiedName) impl.getUnqualifiedName(), existing, existing.fields(impl.$unique(), true))); + else if (impl.$foreignKey() != null) + addForeignKey(schema, existing, impl); else throw unsupportedQuery(query); }