Search in sources :

Example 21 with TableDefinition

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

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

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

Example 24 with TableDefinition

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

the class XMLDatabase method getTables0.

@Override
protected List<TableDefinition> getTables0() {
    List<TableDefinition> result = new ArrayList<>();
    for (Table table : info().getTables()) {
        if (getInputSchemata().contains(table.getTableSchema())) {
            SchemaDefinition schema = getSchema(table.getTableSchema());
            TableType tableType;
            switch(table.getTableType()) {
                case GLOBAL_TEMPORARY:
                    tableType = TableType.TEMPORARY;
                    break;
                case VIEW:
                    tableType = TableType.VIEW;
                    break;
                case BASE_TABLE:
                default:
                    tableType = TableType.TABLE;
                    break;
            }
            String source = null;
            if (tableType == TableType.VIEW) {
                viewLoop: for (View view : info().getViews()) {
                    if (StringUtils.equals(defaultIfNull(table.getTableCatalog(), ""), defaultIfNull(view.getTableCatalog(), "")) && StringUtils.equals(defaultIfNull(table.getTableSchema(), ""), defaultIfNull(view.getTableSchema(), "")) && StringUtils.equals(defaultIfNull(table.getTableName(), ""), defaultIfNull(view.getTableName(), ""))) {
                        source = view.getViewDefinition();
                        break viewLoop;
                    }
                }
            }
            result.add(new XMLTableDefinition(schema, info(), table, table.getComment(), tableType, source));
        }
    }
    return result;
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) Table(org.jooq.util.xml.jaxb.Table) TableType(org.jooq.TableOptions.TableType) ArrayList(java.util.ArrayList) TableDefinition(org.jooq.meta.TableDefinition) View(org.jooq.util.xml.jaxb.View)

Example 25 with TableDefinition

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

the class XMLDatabase method getIndexes0.

@Override
protected List<IndexDefinition> getIndexes0() throws SQLException {
    List<IndexDefinition> result = new ArrayList<>();
    final Map<Name, SortedSet<IndexColumnUsage>> indexColumnUsage = new HashMap<>();
    for (IndexColumnUsage ic : info().getIndexColumnUsages()) {
        Name name = name(ic.getIndexCatalog(), ic.getIndexSchema(), ic.getTableName(), ic.getIndexName());
        SortedSet<IndexColumnUsage> list = indexColumnUsage.computeIfAbsent(name, k -> new TreeSet<>((o1, o2) -> {
            int r = 0;
            r = defaultIfNull(o1.getIndexCatalog(), "").compareTo(defaultIfNull(o2.getIndexCatalog(), ""));
            if (r != 0)
                return r;
            r = defaultIfNull(o1.getIndexSchema(), "").compareTo(defaultIfNull(o2.getIndexSchema(), ""));
            if (r != 0)
                return r;
            r = defaultIfNull(o1.getTableName(), "").compareTo(defaultIfNull(o2.getTableName(), ""));
            if (r != 0)
                return r;
            r = defaultIfNull(o1.getIndexName(), "").compareTo(defaultIfNull(o2.getIndexName(), ""));
            if (r != 0)
                return r;
            return Integer.compare(o1.getOrdinalPosition(), o2.getOrdinalPosition());
        }));
        list.add(ic);
    }
    indexLoop: for (Index i : info().getIndexes()) {
        if (getInputSchemata().contains(i.getTableSchema())) {
            final SchemaDefinition schema = getSchema(i.getTableSchema());
            final TableDefinition table = getTable(schema, i.getTableName());
            if (table == null)
                continue indexLoop;
            final Name name = name(i.getIndexCatalog(), i.getIndexSchema(), i.getTableName(), i.getIndexName());
            IndexDefinition index = new AbstractIndexDefinition(schema, i.getIndexName(), table, Boolean.TRUE.equals(i.isIsUnique()), i.getComment()) {

                private final List<IndexColumnDefinition> indexColumns;

                {
                    indexColumns = new ArrayList<>();
                    SortedSet<IndexColumnUsage> list = indexColumnUsage.get(name);
                    if (list != null)
                        for (IndexColumnUsage ic : list) {
                            ColumnDefinition column = table.getColumn(ic.getColumnName());
                            if (column != null)
                                indexColumns.add(new DefaultIndexColumnDefinition(this, column, Boolean.TRUE.equals(ic.isIsDescending()) ? SortOrder.DESC : SortOrder.ASC, ic.getOrdinalPosition()));
                            else
                                log.error(String.format("Column %s not found in table %s.", ic.getColumnName(), table));
                        }
                    else
                        log.error(String.format("No columns found for index %s.", name));
                }

                @Override
                protected List<IndexColumnDefinition> getIndexColumns0() {
                    return indexColumns;
                }
            };
            result.add(index);
        }
    }
    return result;
}
Also used : Transformer(javax.xml.transform.Transformer) DSL(org.jooq.impl.DSL) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) SortedSet(java.util.SortedSet) StringUtils.isBlank(org.jooq.tools.StringUtils.isBlank) XMLInputFactory(javax.xml.stream.XMLInputFactory) StreamResult(javax.xml.transform.stream.StreamResult) MiniJAXB(org.jooq.util.jaxb.tools.MiniJAXB) DefaultSequenceDefinition(org.jooq.meta.DefaultSequenceDefinition) Table(org.jooq.util.xml.jaxb.Table) DataTypeDefinition(org.jooq.meta.DataTypeDefinition) ColumnDefinition(org.jooq.meta.ColumnDefinition) RoutineDefinition(org.jooq.meta.RoutineDefinition) PackageDefinition(org.jooq.meta.PackageDefinition) IndexColumnDefinition(org.jooq.meta.IndexColumnDefinition) IOException(org.jooq.exception.IOException) InformationSchema(org.jooq.util.xml.jaxb.InformationSchema) XMLStreamReader(javax.xml.stream.XMLStreamReader) Map(java.util.Map) XMLStreamException(javax.xml.stream.XMLStreamException) DSLContext(org.jooq.DSLContext) SQLDialect(org.jooq.SQLDialect) SortOrder(org.jooq.SortOrder) DSL.name(org.jooq.impl.DSL.name) JooqLogger(org.jooq.tools.JooqLogger) Routine(org.jooq.util.xml.jaxb.Routine) View(org.jooq.util.xml.jaxb.View) Name(org.jooq.Name) CheckConstraint(org.jooq.util.xml.jaxb.CheckConstraint) Set(java.util.Set) Constants(org.jooq.Constants) UNIQUE(org.jooq.util.xml.jaxb.TableConstraintType.UNIQUE) Reader(java.io.Reader) Schema(org.jooq.util.xml.jaxb.Schema) TableConstraintType(org.jooq.util.xml.jaxb.TableConstraintType) PRIMARY_KEY(org.jooq.util.xml.jaxb.TableConstraintType.PRIMARY_KEY) JDBCUtils(org.jooq.tools.jdbc.JDBCUtils) FilePattern(org.jooq.FilePattern) TableType(org.jooq.TableOptions.TableType) DomainDefinition(org.jooq.meta.DomainDefinition) List(java.util.List) StringUtils.defaultIfNull(org.jooq.tools.StringUtils.defaultIfNull) IndexColumnUsage(org.jooq.util.xml.jaxb.IndexColumnUsage) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DefaultCheckConstraintDefinition(org.jooq.meta.DefaultCheckConstraintDefinition) AbstractIndexDefinition(org.jooq.meta.AbstractIndexDefinition) DefaultRelations(org.jooq.meta.DefaultRelations) SequenceDefinition(org.jooq.meta.SequenceDefinition) TransformerException(javax.xml.transform.TransformerException) StreamSource(javax.xml.transform.stream.StreamSource) DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition) TableDefinition(org.jooq.meta.TableDefinition) HashMap(java.util.HashMap) Sequence(org.jooq.util.xml.jaxb.Sequence) IndexDefinition(org.jooq.meta.IndexDefinition) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) SQLException(java.sql.SQLException) ArrayDefinition(org.jooq.meta.ArrayDefinition) UDTDefinition(org.jooq.meta.UDTDefinition) KeyColumnUsage(org.jooq.util.xml.jaxb.KeyColumnUsage) FALSE(java.lang.Boolean.FALSE) StringUtils.defaultIfBlank(org.jooq.tools.StringUtils.defaultIfBlank) ReferentialConstraint(org.jooq.util.xml.jaxb.ReferentialConstraint) StringWriter(java.io.StringWriter) FileInputStream(java.io.FileInputStream) StringUtils(org.jooq.tools.StringUtils) File(java.io.File) Index(org.jooq.util.xml.jaxb.Index) Sort(org.jooq.FilePattern.Sort) StringReader(java.io.StringReader) SchemaDefinition(org.jooq.meta.SchemaDefinition) TableConstraint(org.jooq.util.xml.jaxb.TableConstraint) EnumDefinition(org.jooq.meta.EnumDefinition) TransformerFactory(javax.xml.transform.TransformerFactory) Collections(java.util.Collections) AbstractDatabase(org.jooq.meta.AbstractDatabase) CatalogDefinition(org.jooq.meta.CatalogDefinition) InputStream(java.io.InputStream) SchemaDefinition(org.jooq.meta.SchemaDefinition) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Index(org.jooq.util.xml.jaxb.Index) SortedSet(java.util.SortedSet) IndexColumnUsage(org.jooq.util.xml.jaxb.IndexColumnUsage) Name(org.jooq.Name) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) ColumnDefinition(org.jooq.meta.ColumnDefinition) IndexColumnDefinition(org.jooq.meta.IndexColumnDefinition) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) IndexColumnDefinition(org.jooq.meta.IndexColumnDefinition) 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) List(java.util.List) ArrayList(java.util.ArrayList)

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