Search in sources :

Example 1 with DefaultRelations

use of org.jooq.meta.DefaultRelations 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)

Aggregations

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 Field (org.jooq.Field)1 ForeignKey (org.jooq.ForeignKey)1 Meta (org.jooq.Meta)1 Query (org.jooq.Query)1 Record (org.jooq.Record)1 Record1 (org.jooq.Record1)1 Result (org.jooq.Result)1 SQLDialect (org.jooq.SQLDialect)1 Select (org.jooq.Select)1 SelectConditionStep (org.jooq.SelectConditionStep)1 SortOrder (org.jooq.SortOrder)1