Search in sources :

Example 41 with SchemaDefinition

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

the class XMLDatabase method getRoutines0.

@Override
protected List<RoutineDefinition> getRoutines0() {
    List<RoutineDefinition> result = new ArrayList<>();
    for (Routine routine : info().getRoutines()) {
        if (isBlank(routine.getSpecificPackage()) && isBlank(routine.getRoutinePackage())) {
            String schemaName = defaultIfBlank(routine.getSpecificSchema(), routine.getRoutineSchema());
            if (getInputSchemata().contains(schemaName)) {
                SchemaDefinition schema = getSchema(schemaName);
                result.add(new XMLRoutineDefinition(schema, null, info(), routine, routine.getComment()));
            }
        }
    }
    return result;
}
Also used : RoutineDefinition(org.jooq.meta.RoutineDefinition) SchemaDefinition(org.jooq.meta.SchemaDefinition) ArrayList(java.util.ArrayList) Routine(org.jooq.util.xml.jaxb.Routine)

Example 42 with SchemaDefinition

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

the class XMLDatabase method loadForeignKeys.

@Override
protected void loadForeignKeys(DefaultRelations relations) {
    for (ReferentialConstraint fk : info().getReferentialConstraints()) {
        if (getInputSchemata().contains(fk.getConstraintSchema())) {
            for (KeyColumnUsage usage : info().getKeyColumnUsages()) {
                if (StringUtils.equals(defaultIfNull(fk.getConstraintCatalog(), ""), defaultIfNull(usage.getConstraintCatalog(), "")) && StringUtils.equals(defaultIfNull(fk.getConstraintSchema(), ""), defaultIfNull(usage.getConstraintSchema(), "")) && StringUtils.equals(defaultIfNull(fk.getConstraintName(), ""), defaultIfNull(usage.getConstraintName(), ""))) {
                    SchemaDefinition foreignKeySchema = getSchema(fk.getConstraintSchema());
                    SchemaDefinition uniqueKeySchema = getSchema(fk.getUniqueConstraintSchema());
                    String foreignKey = usage.getConstraintName();
                    String foreignKeyTableName = usage.getTableName();
                    String foreignKeyColumn = usage.getColumnName();
                    String uniqueKey = fk.getUniqueConstraintName();
                    TableConstraint fktc = tableConstraint(fk.getConstraintCatalog(), fk.getConstraintSchema(), fk.getConstraintName());
                    TableConstraint uktc = tableConstraint(fk.getUniqueConstraintCatalog(), fk.getUniqueConstraintSchema(), fk.getUniqueConstraintName());
                    if (fktc != null && uktc != null) {
                        TableDefinition foreignKeyTable = getTable(foreignKeySchema, foreignKeyTableName);
                        TableDefinition uniqueKeyTable = getTable(uniqueKeySchema, uktc.getTableName());
                        if (foreignKeyTable != null && uniqueKeyTable != null)
                            relations.addForeignKey(foreignKey, foreignKeyTable, foreignKeyTable.getColumn(foreignKeyColumn), uniqueKey, uniqueKeyTable, !FALSE.equals(fktc.isEnforced()));
                    }
                }
            }
        }
    }
}
Also used : KeyColumnUsage(org.jooq.util.xml.jaxb.KeyColumnUsage) SchemaDefinition(org.jooq.meta.SchemaDefinition) ReferentialConstraint(org.jooq.util.xml.jaxb.ReferentialConstraint) TableDefinition(org.jooq.meta.TableDefinition) TableConstraint(org.jooq.util.xml.jaxb.TableConstraint)

Example 43 with SchemaDefinition

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

the class MySQLDatabase method getRoutines0.

@Override
protected List<RoutineDefinition> getRoutines0() throws SQLException {
    List<RoutineDefinition> result = new ArrayList<>();
    Result<Record6<String, String, String, byte[], byte[], ProcType>> records = is8() ? create().select(ROUTINES.ROUTINE_SCHEMA, ROUTINES.ROUTINE_NAME, ROUTINES.ROUTINE_COMMENT, inline(new byte[0]).as(PROC.PARAM_LIST), inline(new byte[0]).as(PROC.RETURNS), ROUTINES.ROUTINE_TYPE.coerce(PROC.TYPE).as(ROUTINES.ROUTINE_TYPE)).from(ROUTINES).where(ROUTINES.ROUTINE_SCHEMA.in(getInputSchemata())).orderBy(1, 2, 6).fetch() : create().select(PROC.DB.as(ROUTINES.ROUTINE_SCHEMA), PROC.NAME.as(ROUTINES.ROUTINE_NAME), PROC.COMMENT.as(ROUTINES.ROUTINE_COMMENT), PROC.PARAM_LIST, PROC.RETURNS, PROC.TYPE.as(ROUTINES.ROUTINE_TYPE)).from(PROC).where(PROC.DB.in(getInputSchemata())).orderBy(1, 2, 6).fetch();
    Map<Record, Result<Record6<String, String, String, byte[], byte[], ProcType>>> groups = records.intoGroups(new Field[] { ROUTINES.ROUTINE_SCHEMA, ROUTINES.ROUTINE_NAME });
    // [#1908] This indirection is necessary as MySQL allows for overloading
    // procedures and functions with the same signature.
    groups.forEach((k, overloads) -> {
        overloads.forEach(record -> {
            SchemaDefinition schema = getSchema(record.get(ROUTINES.ROUTINE_SCHEMA));
            String name = record.get(ROUTINES.ROUTINE_NAME);
            String comment = record.get(ROUTINES.ROUTINE_COMMENT);
            String params = is8() ? "" : new String(record.get(PROC.PARAM_LIST));
            String returns = is8() ? "" : new String(record.get(PROC.RETURNS));
            ProcType type = record.get(ROUTINES.ROUTINE_TYPE.coerce(PROC.TYPE).as(ROUTINES.ROUTINE_TYPE));
            if (overloads.size() > 1)
                result.add(new MySQLRoutineDefinition(schema, name, comment, params, returns, type, "_" + type.name()));
            else
                result.add(new MySQLRoutineDefinition(schema, name, comment, params, returns, type, null));
        });
    });
    return result;
}
Also used : RoutineDefinition(org.jooq.meta.RoutineDefinition) SchemaDefinition(org.jooq.meta.SchemaDefinition) ArrayList(java.util.ArrayList) ProcType(org.jooq.meta.mysql.mysql.enums.ProcType) Record6(org.jooq.Record6) Record(org.jooq.Record) Result(org.jooq.Result)

Example 44 with SchemaDefinition

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

the class MySQLDatabase method getTables0.

@Override
protected List<TableDefinition> getTables0() throws SQLException {
    List<TableDefinition> result = new ArrayList<>();
    for (Record record : create().select(TABLES.TABLE_SCHEMA, TABLES.TABLE_NAME, TABLES.TABLE_COMMENT, when(TABLES.TABLE_TYPE.eq(inline("VIEW")), inline(TableType.VIEW.name())).else_(inline(TableType.TABLE.name())).as("table_type"), when(VIEWS.VIEW_DEFINITION.lower().like(inline("create%")), VIEWS.VIEW_DEFINITION).else_(inline("create view `").concat(TABLES.TABLE_NAME).concat("` as ").concat(VIEWS.VIEW_DEFINITION)).as(VIEWS.VIEW_DEFINITION)).from(TABLES).leftJoin(VIEWS).on(TABLES.TABLE_SCHEMA.eq(VIEWS.TABLE_SCHEMA)).and(TABLES.TABLE_NAME.eq(VIEWS.TABLE_NAME)).where(TABLES.TABLE_SCHEMA.in(workaroundFor5213(getInputSchemata()))).and(TABLES.TABLE_TYPE.ne(inline("SEQUENCE"))).orderBy(TABLES.TABLE_SCHEMA, TABLES.TABLE_NAME)) {
        SchemaDefinition schema = getSchema(record.get(TABLES.TABLE_SCHEMA));
        String name = record.get(TABLES.TABLE_NAME);
        String comment = record.get(TABLES.TABLE_COMMENT);
        TableType tableType = record.get("table_type", TableType.class);
        String source = record.get(VIEWS.VIEW_DEFINITION);
        MySQLTableDefinition table = new MySQLTableDefinition(schema, name, comment, tableType, source);
        result.add(table);
    }
    return result;
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) TableType(org.jooq.TableOptions.TableType) ArrayList(java.util.ArrayList) TableDefinition(org.jooq.meta.TableDefinition) Record(org.jooq.Record)

Example 45 with SchemaDefinition

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

the class MySQLDatabase method loadUniqueKeys.

@Override
protected void loadUniqueKeys(DefaultRelations relations) throws SQLException {
    for (Record record : uniqueKeys(getInputSchemata())) {
        SchemaDefinition schema = getSchema(record.get(STATISTICS.TABLE_SCHEMA));
        String constraintName = record.get(STATISTICS.INDEX_NAME);
        String tableName = record.get(STATISTICS.TABLE_NAME);
        String columnName = record.get(STATISTICS.COLUMN_NAME);
        String key = getKeyName(tableName, constraintName);
        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)

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