Search in sources :

Example 1 with DefaultCheckConstraintDefinition

use of org.jooq.meta.DefaultCheckConstraintDefinition in project jOOQ by jOOQ.

the class SQLiteDatabase method loadCheckConstraints.

@Override
protected void loadCheckConstraints(DefaultRelations r) throws SQLException {
    DSLContext ctx = create().configuration().deriveSettings(s -> s.withInterpreterDelayForeignKeyDeclarations(true)).dsl();
    SchemaDefinition schema = getSchemata().get(0);
    for (Record record : ctx.select(SQLiteMaster.TBL_NAME, SQLiteMaster.SQL).from(SQLITE_MASTER).where(SQLiteMaster.SQL.likeIgnoreCase("%CHECK%")).orderBy(SQLiteMaster.TBL_NAME)) {
        TableDefinition table = getTable(schema, record.get(SQLiteMaster.TBL_NAME));
        if (table != null) {
            String sql = record.get(SQLiteMaster.SQL);
            try {
                Query query = ctx.parser().parseQuery(sql);
                for (Table<?> t : ctx.meta(query).getTables(table.getName())) {
                    for (Check<?> check : t.getChecks()) {
                        r.addCheckConstraint(table, new DefaultCheckConstraintDefinition(schema, table, check.getName(), ctx.renderInlined(check.condition()), check.enforced()));
                    }
                }
            } catch (ParserException e) {
                log.info("Cannot parse SQL: " + sql, e);
            } catch (DataDefinitionException e) {
                log.info("Cannot interpret SQL: " + sql, e);
            }
        }
    }
}
Also used : UniqueKey(org.jooq.UniqueKey) DSL(org.jooq.impl.DSL) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) DSL.field(org.jooq.impl.DSL.field) Table(org.jooq.Table) DSL.all(org.jooq.impl.DSL.all) DSL.when(org.jooq.impl.DSL.when) RoutineDefinition(org.jooq.meta.RoutineDefinition) PackageDefinition(org.jooq.meta.PackageDefinition) IndexColumnDefinition(org.jooq.meta.IndexColumnDefinition) Record1(org.jooq.Record1) Map(java.util.Map) DSLContext(org.jooq.DSLContext) DSL.coalesce(org.jooq.impl.DSL.coalesce) SQLDialect(org.jooq.SQLDialect) SortOrder(org.jooq.SortOrder) Select(org.jooq.Select) DSL.name(org.jooq.impl.DSL.name) JooqLogger(org.jooq.tools.JooqLogger) Check(org.jooq.Check) Field(org.jooq.Field) DSL.select(org.jooq.impl.DSL.select) Meta(org.jooq.Meta) Result(org.jooq.Result) TableType(org.jooq.TableOptions.TableType) DomainDefinition(org.jooq.meta.DomainDefinition) List(java.util.List) SQLITE_MASTER(org.jooq.meta.sqlite.sqlite_master.SQLiteMaster.SQLITE_MASTER) TableField(org.jooq.TableField) DSL.noCondition(org.jooq.impl.DSL.noCondition) Entry(java.util.Map.Entry) ParserException(org.jooq.impl.ParserException) SchemaMappingType(org.jooq.meta.jaxb.SchemaMappingType) Query(org.jooq.Query) DefaultCheckConstraintDefinition(org.jooq.meta.DefaultCheckConstraintDefinition) AbstractIndexDefinition(org.jooq.meta.AbstractIndexDefinition) DefaultRelations(org.jooq.meta.DefaultRelations) SequenceDefinition(org.jooq.meta.SequenceDefinition) TableDefinition(org.jooq.meta.TableDefinition) HashMap(java.util.HashMap) DSL.replace(org.jooq.impl.DSL.replace) ForeignKey(org.jooq.ForeignKey) IndexDefinition(org.jooq.meta.IndexDefinition) ArrayList(java.util.ArrayList) SelectConditionStep(org.jooq.SelectConditionStep) SQLException(java.sql.SQLException) DSL.selectFrom(org.jooq.impl.DSL.selectFrom) VARCHAR(org.jooq.impl.SQLDataType.VARCHAR) ArrayDefinition(org.jooq.meta.ArrayDefinition) UDTDefinition(org.jooq.meta.UDTDefinition) DSL.inline(org.jooq.impl.DSL.inline) Record(org.jooq.Record) SettingsTools(org.jooq.conf.SettingsTools) SQLiteMaster(org.jooq.meta.sqlite.sqlite_master.SQLiteMaster) StringUtils(org.jooq.tools.StringUtils) DSL.one(org.jooq.impl.DSL.one) DataDefinitionException(org.jooq.exception.DataDefinitionException) SchemaDefinition(org.jooq.meta.SchemaDefinition) DSL.any(org.jooq.impl.DSL.any) EnumDefinition(org.jooq.meta.EnumDefinition) DSL.table(org.jooq.impl.DSL.table) CommonTableExpression(org.jooq.CommonTableExpression) AbstractDatabase(org.jooq.meta.AbstractDatabase) CatalogDefinition(org.jooq.meta.CatalogDefinition) ParserException(org.jooq.impl.ParserException) SchemaDefinition(org.jooq.meta.SchemaDefinition) DataDefinitionException(org.jooq.exception.DataDefinitionException) Query(org.jooq.Query) DSLContext(org.jooq.DSLContext) TableDefinition(org.jooq.meta.TableDefinition) Record(org.jooq.Record) DefaultCheckConstraintDefinition(org.jooq.meta.DefaultCheckConstraintDefinition)

Example 2 with DefaultCheckConstraintDefinition

use of org.jooq.meta.DefaultCheckConstraintDefinition in project jOOQ by jOOQ.

the class H2Database method loadCheckConstraints2_0.

private void loadCheckConstraints2_0(DefaultRelations relations) {
    CheckConstraints cc = CHECK_CONSTRAINTS.as("cc");
    for (Record record : create().select(cc.tableConstraints().TABLE_SCHEMA, cc.tableConstraints().TABLE_NAME, cc.CONSTRAINT_NAME, cc.CHECK_CLAUSE).from(cc).where(cc.tableConstraints().TABLE_SCHEMA.in(getInputSchemata()))) {
        SchemaDefinition schema = getSchema(record.get(cc.tableConstraints().TABLE_SCHEMA));
        TableDefinition table = getTable(schema, record.get(cc.tableConstraints().TABLE_NAME));
        if (table != null) {
            relations.addCheckConstraint(table, new DefaultCheckConstraintDefinition(schema, table, record.get(cc.CONSTRAINT_NAME), record.get(cc.CHECK_CLAUSE)));
        }
    }
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) TableDefinition(org.jooq.meta.TableDefinition) Record(org.jooq.Record) DefaultCheckConstraintDefinition(org.jooq.meta.DefaultCheckConstraintDefinition) CheckConstraints(org.jooq.meta.hsqldb.information_schema.tables.CheckConstraints)

Example 3 with DefaultCheckConstraintDefinition

use of org.jooq.meta.DefaultCheckConstraintDefinition in project jOOQ by jOOQ.

the class MySQLDatabase method loadCheckConstraints.

@Override
protected void loadCheckConstraints(DefaultRelations relations) throws SQLException {
    if (is8_0_16()) {
        for (Record record : create().select(TABLE_CONSTRAINTS.TABLE_SCHEMA, TABLE_CONSTRAINTS.TABLE_NAME, CHECK_CONSTRAINTS.CONSTRAINT_NAME, CHECK_CONSTRAINTS.CHECK_CLAUSE, // https://jira.mariadb.org/browse/MDEV-21201
        TABLE_CONSTRAINTS.CONSTRAINT_CATALOG, TABLE_CONSTRAINTS.CONSTRAINT_SCHEMA).from(TABLE_CONSTRAINTS).join(CHECK_CONSTRAINTS).using(this instanceof MariaDBDatabase ? new Field[] { TABLE_CONSTRAINTS.CONSTRAINT_CATALOG, TABLE_CONSTRAINTS.CONSTRAINT_SCHEMA, // MariaDB has this column, but not MySQL
        TABLE_CONSTRAINTS.TABLE_NAME, TABLE_CONSTRAINTS.CONSTRAINT_NAME } : new Field[] { TABLE_CONSTRAINTS.CONSTRAINT_CATALOG, TABLE_CONSTRAINTS.CONSTRAINT_SCHEMA, TABLE_CONSTRAINTS.CONSTRAINT_NAME }).where(TABLE_CONSTRAINTS.TABLE_SCHEMA.in(getInputSchemata())).orderBy(TABLE_CONSTRAINTS.TABLE_SCHEMA, TABLE_CONSTRAINTS.TABLE_NAME, TABLE_CONSTRAINTS.CONSTRAINT_NAME)) {
            SchemaDefinition schema = getSchema(record.get(TABLE_CONSTRAINTS.TABLE_SCHEMA));
            TableDefinition table = getTable(schema, record.get(TABLE_CONSTRAINTS.TABLE_NAME));
            if (table != null) {
                relations.addCheckConstraint(table, new DefaultCheckConstraintDefinition(schema, table, record.get(CHECK_CONSTRAINTS.CONSTRAINT_NAME), record.get(CHECK_CONSTRAINTS.CHECK_CLAUSE)));
            }
        }
    }
}
Also used : MariaDBDatabase(org.jooq.meta.mariadb.MariaDBDatabase) Field(org.jooq.Field) TableField(org.jooq.TableField) SchemaDefinition(org.jooq.meta.SchemaDefinition) TableDefinition(org.jooq.meta.TableDefinition) Record(org.jooq.Record) DefaultCheckConstraintDefinition(org.jooq.meta.DefaultCheckConstraintDefinition)

Example 4 with DefaultCheckConstraintDefinition

use of org.jooq.meta.DefaultCheckConstraintDefinition in project jOOQ by jOOQ.

the class FirebirdDatabase method loadCheckConstraints.

@Override
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");
    // 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)));
        }
    }
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) TableDefinition(org.jooq.meta.TableDefinition) Record(org.jooq.Record) Rdb$relationConstraints(org.jooq.meta.firebird.rdb.tables.Rdb$relationConstraints) Rdb$checkConstraints(org.jooq.meta.firebird.rdb.tables.Rdb$checkConstraints) DefaultCheckConstraintDefinition(org.jooq.meta.DefaultCheckConstraintDefinition) Rdb$triggers(org.jooq.meta.firebird.rdb.tables.Rdb$triggers)

Example 5 with DefaultCheckConstraintDefinition

use of org.jooq.meta.DefaultCheckConstraintDefinition in project jOOQ by jOOQ.

the class DerbyDatabase method loadCheckConstraints.

@Override
protected void loadCheckConstraints(DefaultRelations relations) throws SQLException {
    for (Record record : create().select(SYSCHECKS.sysconstraints().systables().sysschemas().SCHEMANAME, SYSCHECKS.sysconstraints().systables().TABLENAME, SYSCHECKS.sysconstraints().CONSTRAINTNAME, SYSCHECKS.CHECKDEFINITION).from(SYSCHECKS).where(SYSCHECKS.sysconstraints().systables().sysschemas().SCHEMANAME.in(getInputSchemata()))) {
        SchemaDefinition schema = getSchema(record.get(SYSCHECKS.sysconstraints().systables().sysschemas().SCHEMANAME));
        TableDefinition table = getTable(schema, record.get(SYSCHECKS.sysconstraints().systables().TABLENAME));
        if (table != null) {
            relations.addCheckConstraint(table, new DefaultCheckConstraintDefinition(schema, table, record.get(SYSCHECKS.sysconstraints().CONSTRAINTNAME), record.get(SYSCHECKS.CHECKDEFINITION)));
        }
    }
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) TableDefinition(org.jooq.meta.TableDefinition) Record(org.jooq.Record) DefaultCheckConstraintDefinition(org.jooq.meta.DefaultCheckConstraintDefinition)

Aggregations

Record (org.jooq.Record)7 DefaultCheckConstraintDefinition (org.jooq.meta.DefaultCheckConstraintDefinition)7 SchemaDefinition (org.jooq.meta.SchemaDefinition)7 TableDefinition (org.jooq.meta.TableDefinition)7 Field (org.jooq.Field)2 TableField (org.jooq.TableField)2 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Check (org.jooq.Check)1 CommonTableExpression (org.jooq.CommonTableExpression)1 DSLContext (org.jooq.DSLContext)1 ForeignKey (org.jooq.ForeignKey)1 Meta (org.jooq.Meta)1 Query (org.jooq.Query)1 Record1 (org.jooq.Record1)1 Result (org.jooq.Result)1