[jOOQ/jOOQ#7639] Generate check constraints in Firebird

This commit is contained in:
Lukas Eder 2019-11-28 17:17:36 +01:00
parent c7378bbc63
commit f530abd831
5 changed files with 360 additions and 4 deletions

View File

@ -41,8 +41,10 @@ import static org.jooq.impl.DSL.choose;
import static org.jooq.impl.DSL.decode;
import static org.jooq.impl.DSL.falseCondition;
import static org.jooq.impl.DSL.inline;
import static org.jooq.impl.DSL.max;
import static org.jooq.impl.DSL.noCondition;
import static org.jooq.impl.DSL.select;
import static org.jooq.meta.firebird.rdb.Tables.RDB$CHECK_CONSTRAINTS;
import static org.jooq.meta.firebird.rdb.Tables.RDB$GENERATORS;
import static org.jooq.meta.firebird.rdb.Tables.RDB$INDEX_SEGMENTS;
import static org.jooq.meta.firebird.rdb.Tables.RDB$INDICES;
@ -50,6 +52,7 @@ import static org.jooq.meta.firebird.rdb.Tables.RDB$PROCEDURES;
import static org.jooq.meta.firebird.rdb.Tables.RDB$REF_CONSTRAINTS;
import static org.jooq.meta.firebird.rdb.Tables.RDB$RELATIONS;
import static org.jooq.meta.firebird.rdb.Tables.RDB$RELATION_CONSTRAINTS;
import static org.jooq.meta.firebird.rdb.Tables.RDB$TRIGGERS;
import java.sql.SQLException;
import java.util.ArrayList;
@ -70,6 +73,7 @@ import org.jooq.meta.AbstractIndexDefinition;
import org.jooq.meta.ArrayDefinition;
import org.jooq.meta.CatalogDefinition;
import org.jooq.meta.DataTypeDefinition;
import org.jooq.meta.DefaultCheckConstraintDefinition;
import org.jooq.meta.DefaultDataTypeDefinition;
import org.jooq.meta.DefaultIndexColumnDefinition;
import org.jooq.meta.DefaultRelations;
@ -84,11 +88,13 @@ import org.jooq.meta.SchemaDefinition;
import org.jooq.meta.SequenceDefinition;
import org.jooq.meta.TableDefinition;
import org.jooq.meta.UDTDefinition;
import org.jooq.meta.firebird.rdb.tables.Rdb$checkConstraints;
import org.jooq.meta.firebird.rdb.tables.Rdb$fields;
import org.jooq.meta.firebird.rdb.tables.Rdb$indexSegments;
import org.jooq.meta.firebird.rdb.tables.Rdb$indices;
import org.jooq.meta.firebird.rdb.tables.Rdb$refConstraints;
import org.jooq.meta.firebird.rdb.tables.Rdb$relationConstraints;
import org.jooq.meta.firebird.rdb.tables.Rdb$triggers;
import org.jooq.meta.jaxb.SchemaMappingType;
import org.jooq.util.firebird.FirebirdDataType;
@ -200,8 +206,44 @@ public class FirebirdDatabase extends AbstractDatabase {
}
@Override
protected void loadCheckConstraints(DefaultRelations r) throws SQLException {
// Currently not supported
protected void loadCheckConstraints(DefaultRelations relations) throws SQLException {
Rdb$relationConstraints r = RDB$RELATION_CONSTRAINTS.as("r");
Rdb$checkConstraints c = RDB$CHECK_CONSTRAINTS.as("c");
Rdb$triggers t = RDB$TRIGGERS.as("t");
// [#7639] RDB$TRIGGERS is not in 3NF. The RDB$TRIGGER_SOURCE is repeated
// for RDB$TRIGGER_TYPE 1 (before insert) and 3 (before update)
for (Record record : create()
.select(
r.RDB$RELATION_NAME.trim().as(r.RDB$RELATION_NAME),
r.RDB$CONSTRAINT_NAME.trim().as(r.RDB$CONSTRAINT_NAME),
max(t.RDB$TRIGGER_SOURCE.trim()).as(t.RDB$TRIGGER_SOURCE)
)
.from(r)
.join(c).on(r.RDB$CONSTRAINT_NAME.eq(c.RDB$CONSTRAINT_NAME))
.join(t).on(c.RDB$TRIGGER_NAME.eq(t.RDB$TRIGGER_NAME))
.where(r.RDB$CONSTRAINT_TYPE.eq(inline("CHECK")))
.groupBy(
r.RDB$RELATION_NAME,
r.RDB$CONSTRAINT_NAME
)
.orderBy(
r.RDB$RELATION_NAME,
r.RDB$CONSTRAINT_NAME
)
) {
SchemaDefinition schema = getSchemata().get(0);
TableDefinition table = getTable(schema, record.get(r.RDB$RELATION_NAME));
if (table != null) {
relations.addCheckConstraint(table, new DefaultCheckConstraintDefinition(
schema,
table,
record.get(r.RDB$CONSTRAINT_NAME),
record.get(t.RDB$TRIGGER_SOURCE)
));
}
}
}
@Override

View File

@ -11,6 +11,7 @@ import java.util.List;
import org.jooq.Catalog;
import org.jooq.Table;
import org.jooq.impl.SchemaImpl;
import org.jooq.meta.firebird.rdb.tables.Rdb$checkConstraints;
import org.jooq.meta.firebird.rdb.tables.Rdb$fields;
import org.jooq.meta.firebird.rdb.tables.Rdb$generators;
import org.jooq.meta.firebird.rdb.tables.Rdb$indexSegments;
@ -21,6 +22,7 @@ import org.jooq.meta.firebird.rdb.tables.Rdb$refConstraints;
import org.jooq.meta.firebird.rdb.tables.Rdb$relationConstraints;
import org.jooq.meta.firebird.rdb.tables.Rdb$relationFields;
import org.jooq.meta.firebird.rdb.tables.Rdb$relations;
import org.jooq.meta.firebird.rdb.tables.Rdb$triggers;
/**
@ -29,13 +31,18 @@ import org.jooq.meta.firebird.rdb.tables.Rdb$relations;
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class DefaultSchema extends SchemaImpl {
private static final long serialVersionUID = 167286145;
private static final long serialVersionUID = 1050592337;
/**
* The reference instance of <code></code>
*/
public static final DefaultSchema DEFAULT_SCHEMA = new DefaultSchema();
/**
* The table <code>RDB$CHECK_CONSTRAINTS</code>.
*/
public final Rdb$checkConstraints RDB$CHECK_CONSTRAINTS = org.jooq.meta.firebird.rdb.tables.Rdb$checkConstraints.RDB$CHECK_CONSTRAINTS;
/**
* The table <code>RDB$FIELDS</code>.
*/
@ -86,6 +93,11 @@ public class DefaultSchema extends SchemaImpl {
*/
public final Rdb$relationFields RDB$RELATION_FIELDS = org.jooq.meta.firebird.rdb.tables.Rdb$relationFields.RDB$RELATION_FIELDS;
/**
* The table <code>RDB$TRIGGERS</code>.
*/
public final Rdb$triggers RDB$TRIGGERS = org.jooq.meta.firebird.rdb.tables.Rdb$triggers.RDB$TRIGGERS;
/**
* No further instances allowed
*/
@ -108,6 +120,7 @@ public class DefaultSchema extends SchemaImpl {
private final List<Table<?>> getTables0() {
return Arrays.<Table<?>>asList(
Rdb$checkConstraints.RDB$CHECK_CONSTRAINTS,
Rdb$fields.RDB$FIELDS,
Rdb$generators.RDB$GENERATORS,
Rdb$indexSegments.RDB$INDEX_SEGMENTS,
@ -117,6 +130,7 @@ public class DefaultSchema extends SchemaImpl {
Rdb$refConstraints.RDB$REF_CONSTRAINTS,
Rdb$relations.RDB$RELATIONS,
Rdb$relationConstraints.RDB$RELATION_CONSTRAINTS,
Rdb$relationFields.RDB$RELATION_FIELDS);
Rdb$relationFields.RDB$RELATION_FIELDS,
Rdb$triggers.RDB$TRIGGERS);
}
}

View File

@ -4,6 +4,7 @@
package org.jooq.meta.firebird.rdb;
import org.jooq.meta.firebird.rdb.tables.Rdb$checkConstraints;
import org.jooq.meta.firebird.rdb.tables.Rdb$fields;
import org.jooq.meta.firebird.rdb.tables.Rdb$generators;
import org.jooq.meta.firebird.rdb.tables.Rdb$indexSegments;
@ -14,6 +15,7 @@ import org.jooq.meta.firebird.rdb.tables.Rdb$refConstraints;
import org.jooq.meta.firebird.rdb.tables.Rdb$relationConstraints;
import org.jooq.meta.firebird.rdb.tables.Rdb$relationFields;
import org.jooq.meta.firebird.rdb.tables.Rdb$relations;
import org.jooq.meta.firebird.rdb.tables.Rdb$triggers;
/**
@ -22,6 +24,11 @@ import org.jooq.meta.firebird.rdb.tables.Rdb$relations;
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class Tables {
/**
* The table <code>RDB$CHECK_CONSTRAINTS</code>.
*/
public static final Rdb$checkConstraints RDB$CHECK_CONSTRAINTS = Rdb$checkConstraints.RDB$CHECK_CONSTRAINTS;
/**
* The table <code>RDB$FIELDS</code>.
*/
@ -71,4 +78,9 @@ public class Tables {
* The table <code>RDB$RELATION_FIELDS</code>.
*/
public static final Rdb$relationFields RDB$RELATION_FIELDS = Rdb$relationFields.RDB$RELATION_FIELDS;
/**
* The table <code>RDB$TRIGGERS</code>.
*/
public static final Rdb$triggers RDB$TRIGGERS = Rdb$triggers.RDB$TRIGGERS;
}

View File

@ -0,0 +1,108 @@
/*
* This file is generated by jOOQ.
*/
package org.jooq.meta.firebird.rdb.tables;
import org.jooq.Field;
import org.jooq.Name;
import org.jooq.Record;
import org.jooq.Schema;
import org.jooq.Table;
import org.jooq.TableField;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.jooq.meta.firebird.rdb.DefaultSchema;
/**
* This class is generated by jOOQ.
*/
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class Rdb$checkConstraints extends TableImpl<Record> {
private static final long serialVersionUID = -198721383;
/**
* The reference instance of <code>RDB$CHECK_CONSTRAINTS</code>
*/
public static final Rdb$checkConstraints RDB$CHECK_CONSTRAINTS = new Rdb$checkConstraints();
/**
* The class holding records for this type
*/
@Override
public Class<Record> getRecordType() {
return Record.class;
}
/**
* The column <code>RDB$CHECK_CONSTRAINTS.RDB$CONSTRAINT_NAME</code>.
*/
public final TableField<Record, String> RDB$CONSTRAINT_NAME = createField(DSL.name("RDB$CONSTRAINT_NAME"), org.jooq.impl.SQLDataType.CHAR(31), this, "");
/**
* The column <code>RDB$CHECK_CONSTRAINTS.RDB$TRIGGER_NAME</code>.
*/
public final TableField<Record, String> RDB$TRIGGER_NAME = createField(DSL.name("RDB$TRIGGER_NAME"), org.jooq.impl.SQLDataType.CHAR(31), this, "");
/**
* Create a <code>RDB$CHECK_CONSTRAINTS</code> table reference
*/
public Rdb$checkConstraints() {
this(DSL.name("RDB$CHECK_CONSTRAINTS"), null);
}
/**
* Create an aliased <code>RDB$CHECK_CONSTRAINTS</code> table reference
*/
public Rdb$checkConstraints(String alias) {
this(DSL.name(alias), RDB$CHECK_CONSTRAINTS);
}
/**
* Create an aliased <code>RDB$CHECK_CONSTRAINTS</code> table reference
*/
public Rdb$checkConstraints(Name alias) {
this(alias, RDB$CHECK_CONSTRAINTS);
}
private Rdb$checkConstraints(Name alias, Table<Record> aliased) {
this(alias, aliased, null);
}
private Rdb$checkConstraints(Name alias, Table<Record> aliased, Field<?>[] parameters) {
super(alias, null, aliased, parameters, DSL.comment(""));
}
@Override
public Schema getSchema() {
return DefaultSchema.DEFAULT_SCHEMA;
}
@Override
public Rdb$checkConstraints as(String alias) {
return new Rdb$checkConstraints(DSL.name(alias), this);
}
@Override
public Rdb$checkConstraints as(Name alias) {
return new Rdb$checkConstraints(alias, this);
}
/**
* Rename this table
*/
@Override
public Rdb$checkConstraints rename(String name) {
return new Rdb$checkConstraints(DSL.name(name), null);
}
/**
* Rename this table
*/
@Override
public Rdb$checkConstraints rename(Name name) {
return new Rdb$checkConstraints(name, null);
}
}

View File

@ -0,0 +1,180 @@
/*
* This file is generated by jOOQ.
*/
package org.jooq.meta.firebird.rdb.tables;
import java.util.Arrays;
import java.util.List;
import org.jooq.Field;
import org.jooq.Name;
import org.jooq.Record;
import org.jooq.Schema;
import org.jooq.Table;
import org.jooq.TableField;
import org.jooq.UniqueKey;
import org.jooq.impl.DSL;
import org.jooq.impl.Internal;
import org.jooq.impl.TableImpl;
import org.jooq.meta.firebird.rdb.DefaultSchema;
/**
* This class is generated by jOOQ.
*/
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class Rdb$triggers extends TableImpl<Record> {
private static final long serialVersionUID = 224941405;
/**
* The reference instance of <code>RDB$TRIGGERS</code>
*/
public static final Rdb$triggers RDB$TRIGGERS = new Rdb$triggers();
/**
* The class holding records for this type
*/
@Override
public Class<Record> getRecordType() {
return Record.class;
}
/**
* The column <code>RDB$TRIGGERS.RDB$TRIGGER_NAME</code>.
*/
public final TableField<Record, String> RDB$TRIGGER_NAME = createField(DSL.name("RDB$TRIGGER_NAME"), org.jooq.impl.SQLDataType.CHAR(31), this, "");
/**
* The column <code>RDB$TRIGGERS.RDB$RELATION_NAME</code>.
*/
public final TableField<Record, String> RDB$RELATION_NAME = createField(DSL.name("RDB$RELATION_NAME"), org.jooq.impl.SQLDataType.CHAR(31), this, "");
/**
* The column <code>RDB$TRIGGERS.RDB$TRIGGER_SEQUENCE</code>.
*/
public final TableField<Record, Short> RDB$TRIGGER_SEQUENCE = createField(DSL.name("RDB$TRIGGER_SEQUENCE"), org.jooq.impl.SQLDataType.SMALLINT, this, "");
/**
* The column <code>RDB$TRIGGERS.RDB$TRIGGER_TYPE</code>.
*/
public final TableField<Record, Long> RDB$TRIGGER_TYPE = createField(DSL.name("RDB$TRIGGER_TYPE"), org.jooq.impl.SQLDataType.BIGINT, this, "");
/**
* The column <code>RDB$TRIGGERS.RDB$TRIGGER_SOURCE</code>.
*/
public final TableField<Record, String> RDB$TRIGGER_SOURCE = createField(DSL.name("RDB$TRIGGER_SOURCE"), org.jooq.impl.SQLDataType.CLOB, this, "");
/**
* The column <code>RDB$TRIGGERS.RDB$TRIGGER_BLR</code>.
*/
public final TableField<Record, byte[]> RDB$TRIGGER_BLR = createField(DSL.name("RDB$TRIGGER_BLR"), org.jooq.impl.SQLDataType.BLOB, this, "");
/**
* The column <code>RDB$TRIGGERS.RDB$DESCRIPTION</code>.
*/
public final TableField<Record, String> RDB$DESCRIPTION = createField(DSL.name("RDB$DESCRIPTION"), org.jooq.impl.SQLDataType.CLOB, this, "");
/**
* The column <code>RDB$TRIGGERS.RDB$TRIGGER_INACTIVE</code>.
*/
public final TableField<Record, Short> RDB$TRIGGER_INACTIVE = createField(DSL.name("RDB$TRIGGER_INACTIVE"), org.jooq.impl.SQLDataType.SMALLINT, this, "");
/**
* The column <code>RDB$TRIGGERS.RDB$SYSTEM_FLAG</code>.
*/
public final TableField<Record, Short> RDB$SYSTEM_FLAG = createField(DSL.name("RDB$SYSTEM_FLAG"), org.jooq.impl.SQLDataType.SMALLINT.nullable(false), this, "");
/**
* The column <code>RDB$TRIGGERS.RDB$FLAGS</code>.
*/
public final TableField<Record, Short> RDB$FLAGS = createField(DSL.name("RDB$FLAGS"), org.jooq.impl.SQLDataType.SMALLINT, this, "");
/**
* The column <code>RDB$TRIGGERS.RDB$VALID_BLR</code>.
*/
public final TableField<Record, Short> RDB$VALID_BLR = createField(DSL.name("RDB$VALID_BLR"), org.jooq.impl.SQLDataType.SMALLINT, this, "");
/**
* The column <code>RDB$TRIGGERS.RDB$DEBUG_INFO</code>.
*/
public final TableField<Record, byte[]> RDB$DEBUG_INFO = createField(DSL.name("RDB$DEBUG_INFO"), org.jooq.impl.SQLDataType.BLOB, this, "");
/**
* The column <code>RDB$TRIGGERS.RDB$ENGINE_NAME</code>.
*/
public final TableField<Record, String> RDB$ENGINE_NAME = createField(DSL.name("RDB$ENGINE_NAME"), org.jooq.impl.SQLDataType.CHAR(31), this, "");
/**
* The column <code>RDB$TRIGGERS.RDB$ENTRYPOINT</code>.
*/
public final TableField<Record, String> RDB$ENTRYPOINT = createField(DSL.name("RDB$ENTRYPOINT"), org.jooq.impl.SQLDataType.CHAR, this, "");
/**
* Create a <code>RDB$TRIGGERS</code> table reference
*/
public Rdb$triggers() {
this(DSL.name("RDB$TRIGGERS"), null);
}
/**
* Create an aliased <code>RDB$TRIGGERS</code> table reference
*/
public Rdb$triggers(String alias) {
this(DSL.name(alias), RDB$TRIGGERS);
}
/**
* Create an aliased <code>RDB$TRIGGERS</code> table reference
*/
public Rdb$triggers(Name alias) {
this(alias, RDB$TRIGGERS);
}
private Rdb$triggers(Name alias, Table<Record> aliased) {
this(alias, aliased, null);
}
private Rdb$triggers(Name alias, Table<Record> aliased, Field<?>[] parameters) {
super(alias, null, aliased, parameters, DSL.comment(""));
}
@Override
public Schema getSchema() {
return DefaultSchema.DEFAULT_SCHEMA;
}
@Override
public List<UniqueKey<Record>> getKeys() {
return Arrays.<UniqueKey<Record>>asList(
Internal.createUniqueKey(org.jooq.meta.firebird.rdb.tables.Rdb$triggers.RDB$TRIGGERS, "RDB$INDEX_8", org.jooq.meta.firebird.rdb.tables.Rdb$triggers.RDB$TRIGGERS.RDB$TRIGGER_NAME)
);
}
@Override
public Rdb$triggers as(String alias) {
return new Rdb$triggers(DSL.name(alias), this);
}
@Override
public Rdb$triggers as(Name alias) {
return new Rdb$triggers(alias, this);
}
/**
* Rename this table
*/
@Override
public Rdb$triggers rename(String name) {
return new Rdb$triggers(DSL.name(name), null);
}
/**
* Rename this table
*/
@Override
public Rdb$triggers rename(Name name) {
return new Rdb$triggers(name, null);
}
}