[jOOQ/jOOQ#9434] Fixed DDLExportConfiguration.respectConstraintOrder

This commit is contained in:
Lukas Eder 2019-12-10 14:57:40 +01:00
parent b7b8d5bb28
commit d60e67abc6
4 changed files with 166 additions and 129 deletions

View File

@ -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> NAMED_COMP = new NamedComparator();
static final Comparator<Key<?>> KEY_COMP = new KeyComparator();
static final Comparator<ForeignKey<?, ?>> FOREIGN_KEY_COMP = new ForeignKeyComparator();
static final Comparator<Check<?>> CHECK_COMP = new CheckComparator();
static final Comparator<Index> INDEX_COMP = new IndexComparator();
private static final class NamedComparator implements Comparator<Named> {
@Override
public final int compare(Named o1, Named o2) {
return o1.getQualifiedName().compareTo(o2.getQualifiedName());
}
}
private static final class KeyComparator implements Comparator<Key<?>> {
@Override
public int compare(Key<?> o1, Key<?> o2) {
List<? extends Named> f1 = o1.getFields();
List<? extends Named> 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<ForeignKey<?, ?>> {
@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<Check<?>> {
@Override
public int compare(Check<?> o1, Check<?> o2) {
return o1.condition().toString().compareTo(o2.condition().toString());
}
}
private static final class IndexComparator implements Comparator<Index> {
@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<SortField<?>> f1 = o1.getFields();
List<SortField<?>> 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;
}
}
}

View File

@ -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<Constraint> 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<Constraint> 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 <K extends Key<?>> List<K> sortKeysIf(List<K> input, boolean sort) {
if (sort) {
List<K> result = new ArrayList<>(input);
Collections.sort(result, KEY_COMP);
Collections.sort(result, NAMED_COMP);
return result;
}
return input;
}
private final <N extends Named> List<N> sortIf(List<N> input, boolean sort) {
if (sort) {
List<N> result = new ArrayList<>(input);
Collections.sort(result, NamedComparator.INSTANCE);
Collections.sort(result, NAMED_COMP);
return result;
}

View File

@ -649,77 +649,6 @@ package org.jooq.impl;

View File

@ -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<Named> {
static final NamedComparator INSTANCE = new NamedComparator();
@Override
public final int compare(Named o1, Named o2) {
return o1.getQualifiedName().compareTo(o2.getQualifiedName());
}
}