Search in sources :

Example 16 with TableDefinition

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

the class HSQLDBDatabase method loadPrimaryKeys.

@Override
protected void loadPrimaryKeys(DefaultRelations relations) throws SQLException {
    for (Record record : primaryKeys(getInputSchemata())) {
        SchemaDefinition schema = getSchema(record.get(KEY_COLUMN_USAGE.TABLE_SCHEMA));
        String key = record.get(KEY_COLUMN_USAGE.CONSTRAINT_NAME);
        String tableName = record.get(KEY_COLUMN_USAGE.TABLE_NAME);
        String columnName = record.get(KEY_COLUMN_USAGE.COLUMN_NAME);
        TableDefinition table = getTable(schema, tableName);
        if (table != null)
            relations.addPrimaryKey(key, table, table.getColumn(columnName));
    }
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) TableDefinition(org.jooq.meta.TableDefinition) Record(org.jooq.Record)

Example 17 with TableDefinition

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

the class PostgresDatabase method loadUniqueKeys.

@Override
protected void loadUniqueKeys(DefaultRelations relations) throws SQLException {
    for (Record record : uniqueKeys(getInputSchemata())) {
        SchemaDefinition schema = getSchema(record.get(KEY_COLUMN_USAGE.TABLE_SCHEMA));
        String key = record.get(KEY_COLUMN_USAGE.CONSTRAINT_NAME);
        String tableName = record.get(KEY_COLUMN_USAGE.TABLE_NAME);
        String columnName = record.get(KEY_COLUMN_USAGE.COLUMN_NAME);
        TableDefinition table = getTable(schema, tableName);
        if (table != null)
            relations.addUniqueKey(key, table, table.getColumn(columnName));
    }
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) TableDefinition(org.jooq.meta.TableDefinition) Record(org.jooq.Record)

Example 18 with TableDefinition

use of org.jooq.meta.TableDefinition 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 19 with TableDefinition

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

the class MySQLDatabase method loadForeignKeys.

@Override
protected void loadForeignKeys(DefaultRelations relations) throws SQLException {
    for (Record record : create().select(REFERENTIAL_CONSTRAINTS.CONSTRAINT_SCHEMA, REFERENTIAL_CONSTRAINTS.CONSTRAINT_NAME, REFERENTIAL_CONSTRAINTS.TABLE_NAME, REFERENTIAL_CONSTRAINTS.REFERENCED_TABLE_NAME, REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_NAME, REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_SCHEMA, KEY_COLUMN_USAGE.COLUMN_NAME).from(REFERENTIAL_CONSTRAINTS).join(KEY_COLUMN_USAGE).on(REFERENTIAL_CONSTRAINTS.CONSTRAINT_SCHEMA.equal(KEY_COLUMN_USAGE.CONSTRAINT_SCHEMA)).and(REFERENTIAL_CONSTRAINTS.CONSTRAINT_NAME.equal(KEY_COLUMN_USAGE.CONSTRAINT_NAME)).and(REFERENTIAL_CONSTRAINTS.TABLE_NAME.equal(KEY_COLUMN_USAGE.TABLE_NAME)).where(REFERENTIAL_CONSTRAINTS.CONSTRAINT_SCHEMA.in(workaroundFor5213(getInputSchemata()))).orderBy(KEY_COLUMN_USAGE.CONSTRAINT_SCHEMA.asc(), KEY_COLUMN_USAGE.CONSTRAINT_NAME.asc(), KEY_COLUMN_USAGE.ORDINAL_POSITION.asc()).fetch()) {
        SchemaDefinition foreignKeySchema = getSchema(record.get(REFERENTIAL_CONSTRAINTS.CONSTRAINT_SCHEMA));
        SchemaDefinition uniqueKeySchema = getSchema(record.get(REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_SCHEMA));
        String foreignKey = record.get(REFERENTIAL_CONSTRAINTS.CONSTRAINT_NAME);
        String foreignKeyColumn = record.get(KEY_COLUMN_USAGE.COLUMN_NAME);
        String foreignKeyTableName = record.get(REFERENTIAL_CONSTRAINTS.TABLE_NAME);
        String uniqueKey = record.get(REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_NAME);
        String uniqueKeyTableName = record.get(REFERENTIAL_CONSTRAINTS.REFERENCED_TABLE_NAME);
        TableDefinition foreignKeyTable = getTable(foreignKeySchema, foreignKeyTableName);
        TableDefinition uniqueKeyTable = getTable(uniqueKeySchema, uniqueKeyTableName);
        if (foreignKeyTable != null)
            relations.addForeignKey(foreignKey, foreignKeyTable, foreignKeyTable.getColumn(foreignKeyColumn), getKeyName(uniqueKeyTableName, uniqueKey), uniqueKeyTable);
    }
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) TableDefinition(org.jooq.meta.TableDefinition) Record(org.jooq.Record)

Example 20 with TableDefinition

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

the class MySQLDatabase method getEnums0.

@Override
protected List<EnumDefinition> getEnums0() throws SQLException {
    List<EnumDefinition> result = new ArrayList<>();
    for (ColumnRecord r : create().select(COLUMNS.TABLE_SCHEMA, COLUMNS.TABLE_NAME, COLUMNS.COLUMN_NAME, COLUMNS.COLUMN_TYPE, COLUMNS.COLUMN_COMMENT).from(COLUMNS).where(COLUMNS.COLUMN_TYPE.like("enum(%)").and(COLUMNS.TABLE_SCHEMA.in(workaroundFor5213(getInputSchemata())))).orderBy(COLUMNS.TABLE_SCHEMA.asc(), COLUMNS.TABLE_NAME.asc(), COLUMNS.COLUMN_NAME.asc()).fetch(mapping(ColumnRecord::new))) {
        SchemaDefinition schema = getSchema(r.schema);
        String name = r.table + "_" + r.column;
        // [#1237] Don't generate enum classes for columns in MySQL tables
        // that are excluded from code generation
        TableDefinition tableDefinition = getTable(schema, r.table);
        if (tableDefinition != null) {
            ColumnDefinition columnDefinition = tableDefinition.getColumn(r.column);
            if (columnDefinition != null) {
                // are explicitly forced to another type
                if (getConfiguredForcedType(columnDefinition, columnDefinition.getType()) == null) {
                    DefaultEnumDefinition definition = new DefaultEnumDefinition(schema, name, r.comment);
                    CSVReader reader = new CSVReader(new StringReader(r.type.replaceAll("(^enum\\()|(\\)$)", "")), // Separator
                    ',', // Quote character
                    '\'', // Strict quotes
                    true);
                    for (String string : reader.next()) definition.addLiteral(string);
                    result.add(definition);
                }
            }
        }
    }
    return result;
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) CSVReader(org.jooq.tools.csv.CSVReader) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) TableDefinition(org.jooq.meta.TableDefinition) DefaultEnumDefinition(org.jooq.meta.DefaultEnumDefinition) DefaultEnumDefinition(org.jooq.meta.DefaultEnumDefinition) EnumDefinition(org.jooq.meta.EnumDefinition) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) ColumnDefinition(org.jooq.meta.ColumnDefinition) IndexColumnDefinition(org.jooq.meta.IndexColumnDefinition)

Aggregations

TableDefinition (org.jooq.meta.TableDefinition)67 SchemaDefinition (org.jooq.meta.SchemaDefinition)47 Record (org.jooq.Record)44 ArrayList (java.util.ArrayList)23 IndexColumnDefinition (org.jooq.meta.IndexColumnDefinition)16 IndexDefinition (org.jooq.meta.IndexDefinition)14 List (java.util.List)12 ColumnDefinition (org.jooq.meta.ColumnDefinition)12 IOException (java.io.IOException)10 DefaultIndexColumnDefinition (org.jooq.meta.DefaultIndexColumnDefinition)10 Result (org.jooq.Result)9 CatalogDefinition (org.jooq.meta.CatalogDefinition)9 EnumDefinition (org.jooq.meta.EnumDefinition)9 RoutineDefinition (org.jooq.meta.RoutineDefinition)9 Arrays.asList (java.util.Arrays.asList)8 TableType (org.jooq.TableOptions.TableType)8 AbstractIndexDefinition (org.jooq.meta.AbstractIndexDefinition)8 DefaultCheckConstraintDefinition (org.jooq.meta.DefaultCheckConstraintDefinition)8 PackageDefinition (org.jooq.meta.PackageDefinition)8 SequenceDefinition (org.jooq.meta.SequenceDefinition)8