diff --git a/jOOQ-meta/src/main/java/org/jooq/meta/AbstractDatabase.java b/jOOQ-meta/src/main/java/org/jooq/meta/AbstractDatabase.java index 80be11e954..ac422d7458 100644 --- a/jOOQ-meta/src/main/java/org/jooq/meta/AbstractDatabase.java +++ b/jOOQ-meta/src/main/java/org/jooq/meta/AbstractDatabase.java @@ -228,6 +228,7 @@ public abstract class AbstractDatabase implements Database { included = new ArrayList<>(); excluded = new ArrayList<>(); unusedForcedTypes = new HashSet<>(); + orderProvider = new DefaultOrderProvider(); } @Override diff --git a/jOOQ-meta/src/main/java/org/jooq/meta/AttributeDefinition.java b/jOOQ-meta/src/main/java/org/jooq/meta/AttributeDefinition.java index e2ee253888..d2cb3486ed 100644 --- a/jOOQ-meta/src/main/java/org/jooq/meta/AttributeDefinition.java +++ b/jOOQ-meta/src/main/java/org/jooq/meta/AttributeDefinition.java @@ -42,7 +42,6 @@ package org.jooq.meta; * * @author Lukas Eder */ -public interface AttributeDefinition extends TypedElementDefinition { - +public interface AttributeDefinition extends TypedElementDefinition, PositionedDefinition { } diff --git a/jOOQ-meta/src/main/java/org/jooq/meta/ColumnDefinition.java b/jOOQ-meta/src/main/java/org/jooq/meta/ColumnDefinition.java index bc2e0b325f..d1c6c0f541 100644 --- a/jOOQ-meta/src/main/java/org/jooq/meta/ColumnDefinition.java +++ b/jOOQ-meta/src/main/java/org/jooq/meta/ColumnDefinition.java @@ -45,12 +45,7 @@ import java.util.List; * * @author Lukas Eder */ -public interface ColumnDefinition extends TypedElementDefinition { - - /** - * The column position in the table. - */ - int getPosition(); +public interface ColumnDefinition extends TypedElementDefinition, PositionedDefinition { /** * A definition for the primary key that this column is part of, or diff --git a/jOOQ-meta/src/main/java/org/jooq/meta/DefaultAttributeDefinition.java b/jOOQ-meta/src/main/java/org/jooq/meta/DefaultAttributeDefinition.java index 3beedbf6ae..4b838f9595 100644 --- a/jOOQ-meta/src/main/java/org/jooq/meta/DefaultAttributeDefinition.java +++ b/jOOQ-meta/src/main/java/org/jooq/meta/DefaultAttributeDefinition.java @@ -47,7 +47,16 @@ package org.jooq.meta; */ public class DefaultAttributeDefinition extends AbstractTypedElementDefinition implements AttributeDefinition { + private final int position; + public DefaultAttributeDefinition(UDTDefinition udt, String name, int position, DataTypeDefinition type) { super(udt, name, position, type, null); + + this.position = position; + } + + @Override + public int getPosition() { + return position; } } diff --git a/jOOQ-meta/src/main/java/org/jooq/meta/DefaultOrderProvider.java b/jOOQ-meta/src/main/java/org/jooq/meta/DefaultOrderProvider.java new file mode 100644 index 0000000000..879294a5ba --- /dev/null +++ b/jOOQ-meta/src/main/java/org/jooq/meta/DefaultOrderProvider.java @@ -0,0 +1,101 @@ +/* + * 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.meta; + +import java.util.Comparator; + +/** + * A default order provider that allows for comparing arbitrary definitions. + *

+ * jOOQ's code generator, by default, implements ordering of objects in a + * reasonable, predictable way, according to these rules: + *

+ *

    + *
  • Alphabetic when irrelevant:
    + * When the order of objects within a parent is irrelevant, they are ordered + * alphabetically. Such objects include: + *
      + *
    • {@link SchemaDefinition} within {@link CatalogDefinition}
    • + *
    • {@link ArrayDefinition} within {@link SchemaDefinition}
    • + *
    • {@link DomainDefinition} within {@link SchemaDefinition}
    • + *
    • {@link EnumDefinition} within {@link SchemaDefinition}
    • + *
    • {@link PackageDefinition} within {@link SchemaDefinition}
    • + *
    • {@link RoutineDefinition} within {@link SchemaDefinition}
    • + *
    • {@link UDTDefinition} within {@link SchemaDefinition}
    • + *
    • {@link TableDefinition} within {@link SchemaDefinition}
    • + *
    • {@link ConstraintDefinition} within {@link TableDefinition}
    • + *
    • {@link IndexDefinition} within {@link TableDefinition}
    • + *
    • {@link SequenceDefinition} within {@link SchemaDefinition}
    • + *
    + *
  • + *
  • In given order when relevant:
    + * When the order of objects within a parent is relevant, and provided by the + * database vendor, then that order is used. Such objects include: + *
      + *
    • {@link AttributeDefinition} within {@link UDTDefinition}
    • + *
    • {@link ColumnDefinition} within {@link TableDefinition}
    • + *
    • {@link IndexColumnDefinition} within {@link IndexDefinition}
    • + *
    • {@link ParameterDefinition} within {@link RoutineDefinition}
    • + *
    + *
  • + *
+ * + * @author Lukas Eder + */ +public class DefaultOrderProvider implements Comparator { + + @Override + public int compare(Definition o1, Definition o2) { + if (o1 instanceof ColumnDefinition && o2 instanceof ColumnDefinition) + return compare0((ColumnDefinition) o1, (ColumnDefinition) o2); + else if (o1 instanceof EmbeddableColumnDefinition && o2 instanceof EmbeddableColumnDefinition) + return compare0((EmbeddableColumnDefinition) o1, (EmbeddableColumnDefinition) o2); + else if (o1 instanceof AttributeDefinition && o2 instanceof AttributeDefinition) + return compare0((AttributeDefinition) o1, (AttributeDefinition) o2); + else if (o1 instanceof IndexColumnDefinition && o2 instanceof IndexColumnDefinition) + return compare0((IndexColumnDefinition) o1, (IndexColumnDefinition) o2); + else if (o1 instanceof ParameterDefinition && o2 instanceof ParameterDefinition) + return compare0((ParameterDefinition) o1, (ParameterDefinition) o2); + else + return o1.getQualifiedInputName().compareTo(o2.getQualifiedInputName()); + } + + private int compare0(PositionedDefinition i1, PositionedDefinition i2) { + return Integer.valueOf(i1.getPosition()).compareTo(i2.getPosition()); + } +} diff --git a/jOOQ-meta/src/main/java/org/jooq/meta/DefaultParameterDefinition.java b/jOOQ-meta/src/main/java/org/jooq/meta/DefaultParameterDefinition.java index 171527ddf0..71c7aa8f95 100644 --- a/jOOQ-meta/src/main/java/org/jooq/meta/DefaultParameterDefinition.java +++ b/jOOQ-meta/src/main/java/org/jooq/meta/DefaultParameterDefinition.java @@ -48,6 +48,7 @@ public class DefaultParameterDefinition extends AbstractTypedElementDefinition implements ParameterDefinition { + private final int position; private final boolean isDefaulted; private final boolean isUnnamed; @@ -70,10 +71,16 @@ public class DefaultParameterDefinition public DefaultParameterDefinition(RoutineDefinition routine, String name, int position, DataTypeDefinition type, boolean isDefaulted, boolean isUnnamed, String comment, String overload) { super(routine, name, position, type, comment, overload); + this.position = position; this.isDefaulted = isDefaulted; this.isUnnamed = isUnnamed; } + @Override + public int getPosition() { + return position; + } + @Override public boolean isDefaulted() { return isDefaulted; diff --git a/jOOQ-meta/src/main/java/org/jooq/meta/EmbeddableColumnDefinition.java b/jOOQ-meta/src/main/java/org/jooq/meta/EmbeddableColumnDefinition.java index 8a44d0a5a1..17301ad0d1 100644 --- a/jOOQ-meta/src/main/java/org/jooq/meta/EmbeddableColumnDefinition.java +++ b/jOOQ-meta/src/main/java/org/jooq/meta/EmbeddableColumnDefinition.java @@ -43,12 +43,7 @@ package org.jooq.meta; * * @author Lukas Eder */ -public interface EmbeddableColumnDefinition extends TypedElementDefinition { - - /** - * The column position in the embeddable type. - */ - int getPosition(); +public interface EmbeddableColumnDefinition extends TypedElementDefinition, PositionedDefinition { /** * The backing column definition. diff --git a/jOOQ-meta/src/main/java/org/jooq/meta/IndexColumnDefinition.java b/jOOQ-meta/src/main/java/org/jooq/meta/IndexColumnDefinition.java index 8db99ae43e..b4947d06c3 100644 --- a/jOOQ-meta/src/main/java/org/jooq/meta/IndexColumnDefinition.java +++ b/jOOQ-meta/src/main/java/org/jooq/meta/IndexColumnDefinition.java @@ -45,12 +45,7 @@ import org.jooq.SortOrder; * * @author Lukas Eder */ -public interface IndexColumnDefinition extends TypedElementDefinition { - - /** - * The column position in the index. - */ - int getPosition(); +public interface IndexColumnDefinition extends TypedElementDefinition, PositionedDefinition { /** * The ASC or DESC sort order diff --git a/jOOQ-meta/src/main/java/org/jooq/meta/ParameterDefinition.java b/jOOQ-meta/src/main/java/org/jooq/meta/ParameterDefinition.java index 054d36de2c..546c145ccb 100644 --- a/jOOQ-meta/src/main/java/org/jooq/meta/ParameterDefinition.java +++ b/jOOQ-meta/src/main/java/org/jooq/meta/ParameterDefinition.java @@ -44,7 +44,7 @@ import org.jooq.Parameter; * * @author Lukas Eder */ -public interface ParameterDefinition extends TypedElementDefinition { +public interface ParameterDefinition extends TypedElementDefinition, PositionedDefinition { /** * Whether the parameter has a default value. diff --git a/jOOQ-meta/src/main/java/org/jooq/meta/PositionedDefinition.java b/jOOQ-meta/src/main/java/org/jooq/meta/PositionedDefinition.java new file mode 100644 index 0000000000..7ac6513c05 --- /dev/null +++ b/jOOQ-meta/src/main/java/org/jooq/meta/PositionedDefinition.java @@ -0,0 +1,52 @@ +/* + * 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.meta; + +/** + * A definition that is positioned at an index within its parent. + * + * @author Lukas Eder + */ +public interface PositionedDefinition extends Definition { + + /** + * The object position in the parent. + */ + int getPosition(); + +}