[jOOQ/jOOQ#9425] Diff check constraints.
This change includes a set of prerequisites that had to be implemented in order to be able to create a diff of check constraints: - [jOOQ/jOOQ#7629] Export check constraints through DSLContext.ddl() - [jOOQ/jOOQ#8528] Interpret check constraints in DDL - [jOOQ/jOOQ#9562] Add org.jooq.Check as a meta model - [jOOQ/jOOQ#9562] Add Table.getChecks()
This commit is contained in:
parent
38e458066f
commit
653faba4f8
65
jOOQ/src/main/java/org/jooq/Check.java
Normal file
65
jOOQ/src/main/java/org/jooq/Check.java
Normal file
@ -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.
|
||||
* <p>
|
||||
* Instances of this type cannot be created directly. They are available from
|
||||
* generated code.
|
||||
*
|
||||
* @param <R> The <code>CHECK</code>'s owner table record
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
public interface Check<R extends Record> extends Named {
|
||||
|
||||
/**
|
||||
* The <code>Key</code>'s owner table
|
||||
*/
|
||||
Table<R> getTable();
|
||||
|
||||
/**
|
||||
* The <code>CHECK</code> constraint's condition.
|
||||
*/
|
||||
Condition condition();
|
||||
|
||||
/**
|
||||
* Get this <code>CHECK</code> as a formal {@link Constraint} specification.
|
||||
*/
|
||||
Constraint constraint();
|
||||
}
|
||||
@ -67,6 +67,11 @@ public enum DDLFlag {
|
||||
*/
|
||||
FOREIGN_KEY,
|
||||
|
||||
/**
|
||||
* Whether <code>CHECK</code> constraints should be generated.
|
||||
*/
|
||||
CHECK,
|
||||
|
||||
/**
|
||||
* Whether <code>INDEX</code> definitions should be generated.
|
||||
*/
|
||||
|
||||
@ -296,6 +296,11 @@ public interface Table<R extends Record> extends TableLike<R>, Named {
|
||||
*/
|
||||
<O extends Record> List<ForeignKey<R, O>> getReferencesTo(Table<O> other);
|
||||
|
||||
/**
|
||||
* Get a list of <code>CHECK</code> constraints of this table.
|
||||
*/
|
||||
List<Check<R>> getChecks();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -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<R extends Record> extends AbstractNamed implements
|
||||
|
||||
return Collections.unmodifiableList(result);
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* <p>
|
||||
* Subclasses should override this method
|
||||
*/
|
||||
@Override
|
||||
public List<Check<R>> getChecks() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Subclasses may call this method to create {@link TableField} objects that
|
||||
|
||||
131
jOOQ/src/main/java/org/jooq/impl/CheckImpl.java
Normal file
131
jOOQ/src/main/java/org/jooq/impl/CheckImpl.java
Normal file
@ -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<R extends Record> extends AbstractNamed implements Check<R> {
|
||||
|
||||
/**
|
||||
* Generated UID
|
||||
*/
|
||||
private static final long serialVersionUID = 162853300137140844L;
|
||||
|
||||
final Table<R> table;
|
||||
final Condition condition;
|
||||
|
||||
CheckImpl(Table<R> table, Condition condition) {
|
||||
this(table, null, condition);
|
||||
}
|
||||
|
||||
CheckImpl(Table<R> table, Name name, Condition condition) {
|
||||
super(name, null);
|
||||
|
||||
this.table = table;
|
||||
this.condition = condition;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Table<R> 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();
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
|
||||
@ -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<Constraint> checks(Table<?> table) {
|
||||
List<Constraint> 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<Query> 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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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<MutableCheck> 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<MutableUniqueKey> uniqueKeys = new ArrayList<>();
|
||||
List<MutableForeignKey> foreignkeys = new ArrayList<>();
|
||||
List<MutableCheck> checks = new ArrayList<>();
|
||||
List<MutableIndex> 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<Check<Record>> getChecks() {
|
||||
List<Check<Record>> result = new ArrayList<>();
|
||||
|
||||
for (MutableCheck c : MutableTable.this.checks)
|
||||
result.add(new CheckImpl<>(this, c.name, c.condition));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final List<Index> getIndexes() {
|
||||
List<Index> 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<MutableField> keyFields) {
|
||||
super(name, keyTable, keyFields);
|
||||
|
||||
@ -539,6 +539,38 @@ package org.jooq.impl;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user