From d60e67abc629f1f944e8cf552b29dc57ecf90a78 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Tue, 10 Dec 2019 14:57:40 +0100 Subject: [PATCH] [jOOQ/jOOQ#9434] Fixed DDLExportConfiguration.respectConstraintOrder --- .../main/java/org/jooq/impl/Comparators.java | 149 ++++++++++++++++++ jOOQ/src/main/java/org/jooq/impl/DDL.java | 20 ++- jOOQ/src/main/java/org/jooq/impl/Diff.java | 71 --------- .../java/org/jooq/impl/NamedComparator.java | 55 ------- 4 files changed, 166 insertions(+), 129 deletions(-) create mode 100644 jOOQ/src/main/java/org/jooq/impl/Comparators.java delete mode 100644 jOOQ/src/main/java/org/jooq/impl/NamedComparator.java diff --git a/jOOQ/src/main/java/org/jooq/impl/Comparators.java b/jOOQ/src/main/java/org/jooq/impl/Comparators.java new file mode 100644 index 0000000000..7b69cc0baa --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/Comparators.java @@ -0,0 +1,149 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Other licenses: + * ----------------------------------------------------------------------------- + * Commercial licenses for this work are available. These replace the above + * ASL 2.0 and offer limited warranties, support, maintenance, and commercial + * database integrations. + * + * For more information, please visit: http://www.jooq.org/licenses + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + */ +package org.jooq.impl; + +import static org.jooq.impl.DSL.noCondition; +import static org.jooq.tools.StringUtils.defaultIfNull; + +import java.util.Comparator; +import java.util.List; + +import org.jooq.Check; +import org.jooq.ForeignKey; +import org.jooq.Index; +import org.jooq.Key; +import org.jooq.Named; +import org.jooq.SortField; + +/** + * Commonly used comparators and related utilities. + * + * @author Lukas Eder + */ +final class Comparators { + + static final Comparator NAMED_COMP = new NamedComparator(); + static final Comparator> KEY_COMP = new KeyComparator(); + static final Comparator> FOREIGN_KEY_COMP = new ForeignKeyComparator(); + static final Comparator> CHECK_COMP = new CheckComparator(); + static final Comparator INDEX_COMP = new IndexComparator(); + + private static final class NamedComparator implements Comparator { + @Override + public final int compare(Named o1, Named o2) { + return o1.getQualifiedName().compareTo(o2.getQualifiedName()); + } + } + + private static final class KeyComparator implements Comparator> { + @Override + public int compare(Key o1, Key o2) { + List f1 = o1.getFields(); + List f2 = o2.getFields(); + + int c = f1.size() - f2.size(); + if (c != 0) + return c; + + for (int i = 0; i < f1.size(); i++) { + c = NAMED_COMP.compare(f1.get(i), f2.get(i)); + + if (c != 0) + return c; + } + + return 0; + } + } + + private static final class ForeignKeyComparator implements Comparator> { + @Override + public int compare(ForeignKey o1, ForeignKey o2) { + int c = KEY_COMP.compare(o1, o2); + + if (c != 0) + return c; + else + return KEY_COMP.compare(o1.getKey(), o2.getKey()); + } + } + + private static final class CheckComparator implements Comparator> { + @Override + public int compare(Check o1, Check o2) { + return o1.condition().toString().compareTo(o2.condition().toString()); + } + } + + private static final class IndexComparator implements Comparator { + @Override + public int compare(Index o1, Index o2) { + int c; + + c = Boolean.valueOf(o1.getUnique()).compareTo(o2.getUnique()); + if (c != 0) + return c; + + c = defaultIfNull(o1.getWhere(), noCondition()).toString().compareTo(defaultIfNull(o2.getWhere(), noCondition()).toString()); + if (c != 0) + return c; + + List> f1 = o1.getFields(); + List> f2 = o2.getFields(); + + c = f1.size() - f2.size(); + if (c != 0) + return c; + + for (int i = 0; i < f1.size(); i++) { + SortField s1 = f1.get(i); + SortField s2 = f2.get(i); + + c = s1.getName().compareTo(s2.getName()); + if (c != 0) + return c; + + c = s1.getOrder().compareTo(s2.getOrder()); + if (c != 0) + return c; + } + + return 0; + } + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/DDL.java b/jOOQ/src/main/java/org/jooq/impl/DDL.java index f866858ea1..29db2e96f4 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DDL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DDL.java @@ -46,6 +46,8 @@ import static org.jooq.DDLFlag.SCHEMA; import static org.jooq.DDLFlag.SEQUENCE; import static org.jooq.DDLFlag.TABLE; import static org.jooq.DDLFlag.UNIQUE; +import static org.jooq.impl.Comparators.KEY_COMP; +import static org.jooq.impl.Comparators.NAMED_COMP; import static org.jooq.impl.DSL.constraint; import java.util.ArrayList; @@ -64,6 +66,7 @@ import org.jooq.DSLContext; import org.jooq.Field; import org.jooq.ForeignKey; import org.jooq.Index; +import org.jooq.Key; import org.jooq.Meta; import org.jooq.Named; import org.jooq.Queries; @@ -226,7 +229,7 @@ final class DDL { List result = new ArrayList<>(); if (configuration.flags().contains(UNIQUE)) - for (UniqueKey key : sortIf(table.getKeys(), !configuration.respectConstraintOrder())) + for (UniqueKey key : sortKeysIf(table.getKeys(), !configuration.respectConstraintOrder())) if (!key.isPrimary()) result.add(constraint(key.getUnqualifiedName()).unique(key.getFieldsArray())); @@ -237,7 +240,7 @@ final class DDL { List result = new ArrayList<>(); if (configuration.flags().contains(FOREIGN_KEY)) - for (ForeignKey key : sortIf(table.getReferences(), !configuration.respectConstraintOrder())) + for (ForeignKey key : sortKeysIf(table.getReferences(), !configuration.respectConstraintOrder())) result.add(constraint(key.getUnqualifiedName()).foreignKey(key.getFieldsArray()).references(key.getKey().getTable(), key.getKey().getFieldsArray())); return result; @@ -359,10 +362,21 @@ final class DDL { return ctx.queries(queries); } + private final > List sortKeysIf(List input, boolean sort) { + if (sort) { + List result = new ArrayList<>(input); + Collections.sort(result, KEY_COMP); + Collections.sort(result, NAMED_COMP); + return result; + } + + return input; + } + private final List sortIf(List input, boolean sort) { if (sort) { List result = new ArrayList<>(input); - Collections.sort(result, NamedComparator.INSTANCE); + Collections.sort(result, NAMED_COMP); return result; } diff --git a/jOOQ/src/main/java/org/jooq/impl/Diff.java b/jOOQ/src/main/java/org/jooq/impl/Diff.java index 32652b4b3c..4d0b13ae38 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Diff.java +++ b/jOOQ/src/main/java/org/jooq/impl/Diff.java @@ -649,77 +649,6 @@ package org.jooq.impl; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/jOOQ/src/main/java/org/jooq/impl/NamedComparator.java b/jOOQ/src/main/java/org/jooq/impl/NamedComparator.java deleted file mode 100644 index 38038a8794..0000000000 --- a/jOOQ/src/main/java/org/jooq/impl/NamedComparator.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Other licenses: - * ----------------------------------------------------------------------------- - * Commercial licenses for this work are available. These replace the above - * ASL 2.0 and offer limited warranties, support, maintenance, and commercial - * database integrations. - * - * For more information, please visit: http://www.jooq.org/licenses - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - */ -package org.jooq.impl; - -import java.util.Comparator; - -import org.jooq.Named; - -/** - * @author Lukas Eder - */ -final class NamedComparator implements Comparator { - - static final NamedComparator INSTANCE = new NamedComparator(); - - @Override - public final int compare(Named o1, Named o2) { - return o1.getQualifiedName().compareTo(o2.getQualifiedName()); - } -} \ No newline at end of file