diff --git a/jOOQ/src/main/java/org/jooq/Check.java b/jOOQ/src/main/java/org/jooq/Check.java new file mode 100644 index 0000000000..4bb24a0239 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/Check.java @@ -0,0 +1,65 @@ +/* + * 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; + +/** + * A check constraint. + *

+ * Instances of this type cannot be created directly. They are available from + * generated code. + * + * @param The CHECK's owner table record + * @author Lukas Eder + */ +public interface Check extends Named { + + /** + * The Key's owner table + */ + Table getTable(); + + /** + * The CHECK constraint's condition. + */ + Condition condition(); + + /** + * Get this CHECK as a formal {@link Constraint} specification. + */ + Constraint constraint(); +} diff --git a/jOOQ/src/main/java/org/jooq/DDLFlag.java b/jOOQ/src/main/java/org/jooq/DDLFlag.java index 2c8007568e..e19e1b73dd 100644 --- a/jOOQ/src/main/java/org/jooq/DDLFlag.java +++ b/jOOQ/src/main/java/org/jooq/DDLFlag.java @@ -67,6 +67,11 @@ public enum DDLFlag { */ FOREIGN_KEY, + /** + * Whether CHECK constraints should be generated. + */ + CHECK, + /** * Whether INDEX definitions should be generated. */ diff --git a/jOOQ/src/main/java/org/jooq/Table.java b/jOOQ/src/main/java/org/jooq/Table.java index f925fcae2a..5b2c0a3ba1 100644 --- a/jOOQ/src/main/java/org/jooq/Table.java +++ b/jOOQ/src/main/java/org/jooq/Table.java @@ -296,6 +296,11 @@ public interface Table extends TableLike, Named { */ List> getReferencesTo(Table other); + /** + * Get a list of CHECK constraints of this table. + */ + List> getChecks(); + diff --git a/jOOQ/src/main/java/org/jooq/impl/AbstractTable.java b/jOOQ/src/main/java/org/jooq/impl/AbstractTable.java index cf30508d9d..fd0222a500 100644 --- a/jOOQ/src/main/java/org/jooq/impl/AbstractTable.java +++ b/jOOQ/src/main/java/org/jooq/impl/AbstractTable.java @@ -72,6 +72,7 @@ import java.util.stream.Stream; import org.jooq.Binding; import org.jooq.Catalog; +import org.jooq.Check; import org.jooq.Clause; import org.jooq.Comment; import org.jooq.Comparator; @@ -104,9 +105,9 @@ import org.jooq.TableLike; import org.jooq.TableOnStep; import org.jooq.TableOptionalOnStep; import org.jooq.TableOptions; +import org.jooq.TableOptions.TableType; import org.jooq.TableOuterJoinStep; import org.jooq.TablePartitionByStep; -import org.jooq.TableOptions.TableType; import org.jooq.UniqueKey; // ... // ... @@ -514,6 +515,15 @@ abstract class AbstractTable extends AbstractNamed implements return Collections.unmodifiableList(result); } + /** + * {@inheritDoc} + *

+ * Subclasses should override this method + */ + @Override + public List> getChecks() { + return Collections.emptyList(); + } /** * Subclasses may call this method to create {@link TableField} objects that diff --git a/jOOQ/src/main/java/org/jooq/impl/CheckImpl.java b/jOOQ/src/main/java/org/jooq/impl/CheckImpl.java new file mode 100644 index 0000000000..003addab2f --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/CheckImpl.java @@ -0,0 +1,131 @@ +/* + * 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 org.jooq.Check; +import org.jooq.Condition; +import org.jooq.Constraint; +import org.jooq.Context; +import org.jooq.Name; +import org.jooq.Record; +import org.jooq.Table; + +/** + * @author Lukas Eder + */ +final class CheckImpl extends AbstractNamed implements Check { + + /** + * Generated UID + */ + private static final long serialVersionUID = 162853300137140844L; + + final Table table; + final Condition condition; + + CheckImpl(Table table, Condition condition) { + this(table, null, condition); + } + + CheckImpl(Table table, Name name, Condition condition) { + super(name, null); + + this.table = table; + this.condition = condition; + } + + @Override + public Table getTable() { + return table; + } + + @Override + public Condition condition() { + return condition; + } + + @Override + public Constraint constraint() { + return DSL.constraint(getName()).check(condition); + } + + @Override + public final void accept(Context ctx) { + ctx.visit(getUnqualifiedName()); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + ((condition == null) ? 0 : condition.hashCode()); + result = prime * result + ((table == null) ? 0 : table.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!super.equals(obj)) + return false; + if (getClass() != obj.getClass()) + return false; + CheckImpl other = (CheckImpl) obj; + if (!getQualifiedName().equals(other.getQualifiedName())) + return false; + if (condition == null) { + if (other.condition != null) + return false; + } + else if (!condition.equals(other.condition)) + return false; + if (table == null) { + if (other.table != null) + return false; + } + else if (!table.equals(other.table)) + return false; + return true; + } + + @Override + public String toString() { + return constraint().toString(); + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/ConstraintImpl.java b/jOOQ/src/main/java/org/jooq/impl/ConstraintImpl.java index e233db81aa..7f1491c522 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ConstraintImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/ConstraintImpl.java @@ -175,6 +175,7 @@ implements final Field[] $references() { return references; } final Action $onDelete() { return onDelete; } final Action $onUpdate() { return onUpdate; } + final Condition $check() { return check; } // ------------------------------------------------------------------------ // XXX: QueryPart API diff --git a/jOOQ/src/main/java/org/jooq/impl/DDL.java b/jOOQ/src/main/java/org/jooq/impl/DDL.java index a5b1535056..a93d4c5304 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DDL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DDL.java @@ -37,6 +37,7 @@ */ package org.jooq.impl; +import static org.jooq.DDLFlag.CHECK; import static org.jooq.DDLFlag.COMMENT; import static org.jooq.DDLFlag.FOREIGN_KEY; import static org.jooq.DDLFlag.INDEX; @@ -53,6 +54,7 @@ import java.util.Collection; import java.util.Collections; import java.util.List; +import org.jooq.Check; import org.jooq.Constraint; import org.jooq.CreateSequenceFlagsStep; import org.jooq.CreateTableOnCommitStep; @@ -187,6 +189,7 @@ final class DDL { result.addAll(primaryKeys(table)); result.addAll(uniqueKeys(table)); result.addAll(foreignKeys(table)); + result.addAll(checks(table)); return result; } @@ -223,6 +226,16 @@ final class DDL { return result; } + private final List checks(Table table) { + List result = new ArrayList<>(); + + if (configuration.flags().contains(CHECK)) + for (Check check : sortIf(table.getChecks(), !configuration.respectConstraintOrder())) + result.add(constraint(check.getName()).check(check.condition())); + + return result; + } + final Queries queries(Table... tables) { List queries = new ArrayList<>(); @@ -280,6 +293,7 @@ final class DDL { constraints.addAll(primaryKeys(table)); constraints.addAll(uniqueKeys(table)); + constraints.addAll(checks(table)); queries.add(createTable(table, constraints)); } @@ -296,6 +310,11 @@ final class DDL { for (Table table : sortIf(schema.getTables(), !configuration.respectTableOrder())) for (Constraint constraint : sortIf(uniqueKeys(table), !configuration.respectConstraintOrder())) queries.add(ctx.alterTable(table).add(constraint)); + + if (configuration.flags().contains(CHECK)) + for (Table table : sortIf(schema.getTables(), !configuration.respectTableOrder())) + for (Constraint constraint : sortIf(checks(table), !configuration.respectConstraintOrder())) + queries.add(ctx.alterTable(table).add(constraint)); } } diff --git a/jOOQ/src/main/java/org/jooq/impl/DDLInterpreter.java b/jOOQ/src/main/java/org/jooq/impl/DDLInterpreter.java index 38faa79fd9..e5cf78b4ee 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DDLInterpreter.java +++ b/jOOQ/src/main/java/org/jooq/impl/DDLInterpreter.java @@ -55,7 +55,9 @@ import java.util.List; import java.util.Map; import org.jooq.Catalog; +import org.jooq.Check; import org.jooq.Comment; +import org.jooq.Condition; import org.jooq.Configuration; import org.jooq.Constraint; import org.jooq.DataType; @@ -248,6 +250,8 @@ final class DDLInterpreter { mt.uniqueKeys.add(new MutableUniqueKey((UnqualifiedName) impl.getUnqualifiedName(), mt, mt.fields(impl.$unique(), true))); else if (impl.$foreignKey() != null) addForeignKey(getSchema(impl.$referencesTable().getSchema(), false), mt, impl); + else if (impl.$check() != null) + mt.checks.add(new MutableCheck((UnqualifiedName) impl.getUnqualifiedName(), mt, impl.$check())); else throw unsupportedQuery(query); } @@ -411,6 +415,8 @@ final class DDLInterpreter { existing.uniqueKeys.add(new MutableUniqueKey((UnqualifiedName) impl.getUnqualifiedName(), existing, existing.fields(impl.$unique(), true))); else if (impl.$foreignKey() != null) addForeignKey(schema, existing, impl); + else if (impl.$check() != null) + existing.checks.add(new MutableCheck((UnqualifiedName) impl.getUnqualifiedName(), existing, impl.$check())); else throw unsupportedQuery(query); } @@ -449,7 +455,7 @@ final class DDLInterpreter { mf.name = (UnqualifiedName) query.$renameColumnTo().getUnqualifiedName(); } else if (query.$renameConstraint() != null) { - MutableKey mk = existing.constraint(query.$renameConstraint()); + MutableNamed mk = existing.constraint(query.$renameConstraint()); if (mk == null) throw constraintNotExists(query.$renameConstraint()); @@ -491,6 +497,17 @@ final class DDLInterpreter { } } + Iterator chks = existing.checks.iterator(); + + while (chks.hasNext()) { + MutableCheck check = chks.next(); + + if (check.name.equals(impl.getUnqualifiedName())) { + chks.remove(); + break removal; + } + } + if (existing.primaryKey != null) { if (existing.primaryKey.name.equals(impl.getUnqualifiedName())) { cascade(existing.primaryKey, null, query.$dropCascade()); @@ -1116,6 +1133,7 @@ final class DDLInterpreter { MutableUniqueKey primaryKey; List uniqueKeys = new ArrayList<>(); List foreignkeys = new ArrayList<>(); + List checks = new ArrayList<>(); List indexes = new ArrayList<>(); TableOptions options; @@ -1127,7 +1145,7 @@ final class DDLInterpreter { schema.tables.add(this); } - final MutableKey constraint(Constraint constraint) { + final MutableNamed constraint(Constraint constraint) { for (MutableForeignKey mfk : foreignkeys) if (mfk.name.equals(constraint.getUnqualifiedName())) return mfk; @@ -1136,6 +1154,10 @@ final class DDLInterpreter { if (muk.name.equals(constraint.getUnqualifiedName())) return muk; + for (MutableCheck chk : checks) + if (chk.name.equals(constraint.getUnqualifiedName())) + return chk; + if (primaryKey != null && primaryKey.name.equals(constraint.getUnqualifiedName())) return primaryKey; else @@ -1242,6 +1264,16 @@ final class DDLInterpreter { return result; } + @Override + public List> getChecks() { + List> result = new ArrayList<>(); + + for (MutableCheck c : MutableTable.this.checks) + result.add(new CheckImpl<>(this, c.name, c.condition)); + + return result; + } + @Override public final List getIndexes() { List result = new ArrayList<>(); @@ -1341,6 +1373,18 @@ final class DDLInterpreter { } } + private static final class MutableCheck extends MutableNamed { + MutableTable table; + Condition condition; + + MutableCheck(UnqualifiedName name, MutableTable table, Condition condition) { + super(name); + + this.table = table; + this.condition = condition; + } + } + private static final class MutableUniqueKey extends MutableKey { MutableUniqueKey(UnqualifiedName name, MutableTable keyTable, List keyFields) { super(name, keyTable, keyFields); diff --git a/jOOQ/src/main/java/org/jooq/impl/Diff.java b/jOOQ/src/main/java/org/jooq/impl/Diff.java index 8fa39105cd..cae440d9bc 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Diff.java +++ b/jOOQ/src/main/java/org/jooq/impl/Diff.java @@ -539,6 +539,38 @@ package org.jooq.impl; + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +