Search in sources :

Example 31 with SchemaDefinition

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

Example 32 with SchemaDefinition

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

the class MySQLDatabase method getIndexes0.

@Override
protected List<IndexDefinition> getIndexes0() throws SQLException {
    List<IndexDefinition> result = new ArrayList<>();
    // Same implementation as in H2Database and HSQLDBDatabase
    Map<Record, Result<Record>> indexes = create().selectDistinct(STATISTICS.TABLE_SCHEMA, STATISTICS.TABLE_NAME, STATISTICS.INDEX_NAME, STATISTICS.NON_UNIQUE, STATISTICS.COLUMN_NAME, STATISTICS.SEQ_IN_INDEX).from(STATISTICS).where(STATISTICS.TABLE_SCHEMA.in(workaroundFor5213(getInputSchemata()))).and(getIncludeSystemIndexes() ? noCondition() : row(STATISTICS.INDEX_SCHEMA, STATISTICS.TABLE_NAME, STATISTICS.INDEX_NAME).notIn(select(TABLE_CONSTRAINTS.CONSTRAINT_SCHEMA, TABLE_CONSTRAINTS.TABLE_NAME, TABLE_CONSTRAINTS.CONSTRAINT_NAME).from(TABLE_CONSTRAINTS))).orderBy(STATISTICS.TABLE_SCHEMA, STATISTICS.TABLE_NAME, STATISTICS.INDEX_NAME, STATISTICS.SEQ_IN_INDEX).fetchGroups(new Field[] { STATISTICS.TABLE_SCHEMA, STATISTICS.TABLE_NAME, STATISTICS.INDEX_NAME, STATISTICS.NON_UNIQUE }, new Field[] { STATISTICS.COLUMN_NAME, STATISTICS.SEQ_IN_INDEX });
    indexLoop: for (Entry<Record, Result<Record>> entry : indexes.entrySet()) {
        final Record index = entry.getKey();
        final Result<Record> columns = entry.getValue();
        final SchemaDefinition tableSchema = getSchema(index.get(STATISTICS.TABLE_SCHEMA));
        if (tableSchema == null)
            continue indexLoop;
        final String indexName = index.get(STATISTICS.INDEX_NAME);
        final String tableName = index.get(STATISTICS.TABLE_NAME);
        final TableDefinition table = getTable(tableSchema, tableName);
        if (table == null)
            continue indexLoop;
        final boolean unique = !index.get(STATISTICS.NON_UNIQUE, boolean.class);
        // [#6310] [#6620] Function-based indexes are not yet supported
        for (Record column : columns) if (table.getColumn(column.get(STATISTICS.COLUMN_NAME)) == null)
            continue indexLoop;
        result.add(new AbstractIndexDefinition(tableSchema, indexName, table, unique) {

            List<IndexColumnDefinition> indexColumns = new ArrayList<>();

            {
                for (Record column : columns) {
                    indexColumns.add(new DefaultIndexColumnDefinition(this, table.getColumn(column.get(STATISTICS.COLUMN_NAME)), SortOrder.ASC, column.get(STATISTICS.SEQ_IN_INDEX, int.class)));
                }
            }

            @Override
            protected List<IndexColumnDefinition> getIndexColumns0() {
                return indexColumns;
            }
        });
    }
    return result;
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) ArrayList(java.util.ArrayList) Result(org.jooq.Result) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) IndexColumnDefinition(org.jooq.meta.IndexColumnDefinition) Entry(java.util.Map.Entry) AbstractIndexDefinition(org.jooq.meta.AbstractIndexDefinition) IndexDefinition(org.jooq.meta.IndexDefinition) AbstractIndexDefinition(org.jooq.meta.AbstractIndexDefinition) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) TableDefinition(org.jooq.meta.TableDefinition) Record(org.jooq.Record) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList)

Example 33 with SchemaDefinition

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

the class DerbyDatabase method getSequences0.

@Override
protected List<SequenceDefinition> getSequences0() throws SQLException {
    List<SequenceDefinition> result = new ArrayList<>();
    for (Record record : sequences(getInputSchemata())) {
        SchemaDefinition schema = getSchema(record.get(SYSSEQUENCES.sysschemas().SCHEMANAME));
        DataTypeDefinition type = new DefaultDataTypeDefinition(this, schema, record.get(SYSSEQUENCES.SEQUENCEDATATYPE));
        result.add(new DefaultSequenceDefinition(schema, record.get(SYSSEQUENCES.SEQUENCENAME), type, null, record.get(SYSSEQUENCES.STARTVALUE), record.get(SYSSEQUENCES.INCREMENT), record.get(SYSSEQUENCES.MINIMUMVALUE), record.get(SYSSEQUENCES.MAXIMUMVALUE), record.get(SYSSEQUENCES.CYCLEOPTION, Boolean.class), null));
    }
    return result;
}
Also used : DefaultSequenceDefinition(org.jooq.meta.DefaultSequenceDefinition) SchemaDefinition(org.jooq.meta.SchemaDefinition) DefaultSequenceDefinition(org.jooq.meta.DefaultSequenceDefinition) SequenceDefinition(org.jooq.meta.SequenceDefinition) DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition) ArrayList(java.util.ArrayList) Record(org.jooq.Record) DataTypeDefinition(org.jooq.meta.DataTypeDefinition) DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition)

Example 34 with SchemaDefinition

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

the class DerbyDatabase method loadForeignKeys.

@Override
protected void loadForeignKeys(DefaultRelations relations) throws SQLException {
    Field<String> fkName = field("fc.constraintname", String.class);
    Field<String> fkTable = field("ft.tablename", String.class);
    Field<String> fkSchema = field("fs.schemaname", String.class);
    Field<?> fkDescriptor = field("fg.descriptor");
    Field<String> ukName = field("pc.constraintname", String.class);
    Field<String> ukTable = field("pt.tablename", String.class);
    Field<String> ukSchema = field("ps.schemaname", String.class);
    for (Record record : create().select(fkName, fkTable, fkSchema, fkDescriptor, ukName, ukTable, ukSchema).from("sys.sysconstraints   fc").join("sys.sysforeignkeys   f ").on("f.constraintid = fc.constraintid").join("sys.sysconglomerates fg").on("fg.conglomerateid = f.conglomerateid").join("sys.systables        ft").on("ft.tableid = fg.tableid").join("sys.sysschemas       fs").on("ft.schemaid = fs.schemaid").join("sys.sysconstraints   pc").on("pc.constraintid = f.keyconstraintid").join("sys.systables        pt").on("pt.tableid = pc.tableid").join("sys.sysschemas       ps").on("ps.schemaid = pt.schemaid").where("cast(fc.type as varchar(32672)) = 'F'").fetch()) {
        SchemaDefinition foreignKeySchema = getSchema(record.get(fkSchema));
        SchemaDefinition uniqueKeySchema = getSchema(record.get(ukSchema));
        String foreignKeyName = record.get(fkName);
        String foreignKeyTableName = record.get(fkTable);
        List<Integer> foreignKeyIndexes = decode(record.get(fkDescriptor, String.class));
        String uniqueKeyName = record.get(ukName);
        String uniqueKeyTableName = record.get(ukTable);
        TableDefinition foreignKeyTable = getTable(foreignKeySchema, foreignKeyTableName);
        TableDefinition uniqueKeyTable = getTable(uniqueKeySchema, uniqueKeyTableName);
        if (foreignKeyTable != null && uniqueKeyTable != null)
            for (int i = 0; i < foreignKeyIndexes.size(); i++) relations.addForeignKey(foreignKeyName, foreignKeyTable, foreignKeyTable.getColumn(foreignKeyIndexes.get(i)), uniqueKeyName, uniqueKeyTable);
    }
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) TableDefinition(org.jooq.meta.TableDefinition) Record(org.jooq.Record)

Example 35 with SchemaDefinition

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

the class DerbyDatabase method loadUniqueKeys.

/**
 * {@inheritDoc}
 */
@Override
protected void loadUniqueKeys(DefaultRelations relations) throws SQLException {
    for (Record record : fetchKeys("U")) {
        SchemaDefinition schema = getSchema(record.get(SYSSCHEMAS.SCHEMANAME));
        String key = record.get(SYSCONSTRAINTS.CONSTRAINTNAME);
        String tableName = record.get(SYSTABLES.TABLENAME);
        String descriptor = record.get(SYSCONGLOMERATES.DESCRIPTOR, String.class);
        TableDefinition table = getTable(schema, tableName);
        if (table != null)
            for (int index : decode(descriptor)) relations.addUniqueKey(key, table, table.getColumn(index));
    }
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) TableDefinition(org.jooq.meta.TableDefinition) Record(org.jooq.Record)

Aggregations

SchemaDefinition (org.jooq.meta.SchemaDefinition)81 Record (org.jooq.Record)51 ArrayList (java.util.ArrayList)46 TableDefinition (org.jooq.meta.TableDefinition)44 DefaultDataTypeDefinition (org.jooq.meta.DefaultDataTypeDefinition)22 DataTypeDefinition (org.jooq.meta.DataTypeDefinition)21 ColumnDefinition (org.jooq.meta.ColumnDefinition)14 IndexColumnDefinition (org.jooq.meta.IndexColumnDefinition)14 IndexDefinition (org.jooq.meta.IndexDefinition)12 SequenceDefinition (org.jooq.meta.SequenceDefinition)12 List (java.util.List)10 DefaultIndexColumnDefinition (org.jooq.meta.DefaultIndexColumnDefinition)10 DomainDefinition (org.jooq.meta.DomainDefinition)9 RoutineDefinition (org.jooq.meta.RoutineDefinition)9 Result (org.jooq.Result)8 AbstractIndexDefinition (org.jooq.meta.AbstractIndexDefinition)8 DefaultCheckConstraintDefinition (org.jooq.meta.DefaultCheckConstraintDefinition)8 DefaultSequenceDefinition (org.jooq.meta.DefaultSequenceDefinition)8 PackageDefinition (org.jooq.meta.PackageDefinition)8 TableType (org.jooq.TableOptions.TableType)7