Search in sources :

Example 1 with KeyColumnUsage

use of org.jooq.util.xml.jaxb.KeyColumnUsage in project jOOQ by jOOQ.

the class XMLDatabase method keyColumnUsage.

private List<KeyColumnUsage> keyColumnUsage(TableConstraintType constraintType) {
    List<KeyColumnUsage> result = new ArrayList<KeyColumnUsage>();
    for (TableConstraint constraint : info().getTableConstraints()) {
        if (constraintType == constraint.getConstraintType() && getInputSchemata().contains(constraint.getConstraintSchema())) {
            for (KeyColumnUsage usage : info().getKeyColumnUsages()) {
                if (StringUtils.equals(constraint.getConstraintCatalog(), usage.getConstraintCatalog()) && StringUtils.equals(constraint.getConstraintSchema(), usage.getConstraintSchema()) && StringUtils.equals(constraint.getConstraintName(), usage.getConstraintName())) {
                    result.add(usage);
                }
            }
        }
    }
    Collections.sort(result, new Comparator<KeyColumnUsage>() {

        @Override
        public int compare(KeyColumnUsage o1, KeyColumnUsage o2) {
            int r = 0;
            r = defaultIfNull(o1.getConstraintCatalog(), "").compareTo(defaultIfNull(o2.getConstraintCatalog(), ""));
            if (r != 0)
                return r;
            r = defaultIfNull(o1.getConstraintSchema(), "").compareTo(defaultIfNull(o2.getConstraintSchema(), ""));
            if (r != 0)
                return r;
            r = defaultIfNull(o1.getConstraintName(), "").compareTo(defaultIfNull(o2.getConstraintName(), ""));
            if (r != 0)
                return r;
            return Integer.valueOf(o1.getOrdinalPosition()).compareTo(o2.getOrdinalPosition());
        }
    });
    return result;
}
Also used : KeyColumnUsage(org.jooq.util.xml.jaxb.KeyColumnUsage) ArrayList(java.util.ArrayList) TableConstraint(org.jooq.util.xml.jaxb.TableConstraint)

Example 2 with KeyColumnUsage

use of org.jooq.util.xml.jaxb.KeyColumnUsage in project jOOQ by jOOQ.

the class XMLDatabase method loadPrimaryKeys.

@Override
protected void loadPrimaryKeys(DefaultRelations relations) {
    for (KeyColumnUsage usage : keyColumnUsage(PRIMARY_KEY)) {
        SchemaDefinition schema = getSchema(usage.getConstraintSchema());
        String key = usage.getConstraintName();
        String tableName = usage.getTableName();
        String columnName = usage.getColumnName();
        TableDefinition table = getTable(schema, tableName);
        if (table != null) {
            relations.addPrimaryKey(key, table.getColumn(columnName));
        }
    }
}
Also used : KeyColumnUsage(org.jooq.util.xml.jaxb.KeyColumnUsage) SchemaDefinition(org.jooq.util.SchemaDefinition) TableDefinition(org.jooq.util.TableDefinition)

Example 3 with KeyColumnUsage

use of org.jooq.util.xml.jaxb.KeyColumnUsage in project jOOQ by jOOQ.

the class XMLDatabase method loadForeignKeys.

@Override
protected void loadForeignKeys(DefaultRelations relations) {
    for (ReferentialConstraint constraint : info().getReferentialConstraints()) {
        if (getInputSchemata().contains(constraint.getConstraintSchema())) {
            for (KeyColumnUsage usage : info().getKeyColumnUsages()) {
                if (StringUtils.equals(constraint.getConstraintCatalog(), usage.getConstraintCatalog()) && StringUtils.equals(constraint.getConstraintSchema(), usage.getConstraintSchema()) && StringUtils.equals(constraint.getConstraintName(), usage.getConstraintName())) {
                    SchemaDefinition foreignKeySchema = getSchema(constraint.getConstraintSchema());
                    SchemaDefinition uniqueKeySchema = getSchema(constraint.getUniqueConstraintSchema());
                    String foreignKey = usage.getConstraintName();
                    String foreignKeyTable = usage.getTableName();
                    String foreignKeyColumn = usage.getColumnName();
                    String uniqueKey = constraint.getUniqueConstraintName();
                    TableDefinition referencingTable = getTable(foreignKeySchema, foreignKeyTable);
                    if (referencingTable != null) {
                        ColumnDefinition referencingColumn = referencingTable.getColumn(foreignKeyColumn);
                        relations.addForeignKey(foreignKey, uniqueKey, referencingColumn, uniqueKeySchema);
                    }
                }
            }
        }
    }
}
Also used : KeyColumnUsage(org.jooq.util.xml.jaxb.KeyColumnUsage) SchemaDefinition(org.jooq.util.SchemaDefinition) ReferentialConstraint(org.jooq.util.xml.jaxb.ReferentialConstraint) TableDefinition(org.jooq.util.TableDefinition) ColumnDefinition(org.jooq.util.ColumnDefinition)

Example 4 with KeyColumnUsage

use of org.jooq.util.xml.jaxb.KeyColumnUsage in project jOOQ by jOOQ.

the class XMLDatabase method keyColumnUsage.

private List<KeyColumnUsage> keyColumnUsage(TableConstraintType constraintType) {
    List<KeyColumnUsage> result = new ArrayList<>();
    for (TableConstraint constraint : info().getTableConstraints()) if (constraintType == constraint.getConstraintType() && getInputSchemata().contains(constraint.getConstraintSchema()))
        for (KeyColumnUsage usage : info().getKeyColumnUsages()) if (StringUtils.equals(defaultIfNull(constraint.getConstraintCatalog(), ""), defaultIfNull(usage.getConstraintCatalog(), "")) && StringUtils.equals(defaultIfNull(constraint.getConstraintSchema(), ""), defaultIfNull(usage.getConstraintSchema(), "")) && StringUtils.equals(defaultIfNull(constraint.getConstraintName(), ""), defaultIfNull(usage.getConstraintName(), "")))
            result.add(usage);
    result.sort((o1, o2) -> {
        int r = 0;
        r = defaultIfNull(o1.getConstraintCatalog(), "").compareTo(defaultIfNull(o2.getConstraintCatalog(), ""));
        if (r != 0)
            return r;
        r = defaultIfNull(o1.getConstraintSchema(), "").compareTo(defaultIfNull(o2.getConstraintSchema(), ""));
        if (r != 0)
            return r;
        r = defaultIfNull(o1.getConstraintName(), "").compareTo(defaultIfNull(o2.getConstraintName(), ""));
        if (r != 0)
            return r;
        return Integer.compare(o1.getOrdinalPosition(), o2.getOrdinalPosition());
    });
    return result;
}
Also used : KeyColumnUsage(org.jooq.util.xml.jaxb.KeyColumnUsage) ArrayList(java.util.ArrayList) TableConstraint(org.jooq.util.xml.jaxb.TableConstraint) CheckConstraint(org.jooq.util.xml.jaxb.CheckConstraint) ReferentialConstraint(org.jooq.util.xml.jaxb.ReferentialConstraint) TableConstraint(org.jooq.util.xml.jaxb.TableConstraint)

Example 5 with KeyColumnUsage

use of org.jooq.util.xml.jaxb.KeyColumnUsage 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)

Aggregations

KeyColumnUsage (org.jooq.util.xml.jaxb.KeyColumnUsage)12 TableConstraint (org.jooq.util.xml.jaxb.TableConstraint)9 ReferentialConstraint (org.jooq.util.xml.jaxb.ReferentialConstraint)7 SchemaDefinition (org.jooq.meta.SchemaDefinition)4 TableDefinition (org.jooq.meta.TableDefinition)4 CheckConstraint (org.jooq.util.xml.jaxb.CheckConstraint)4 ArrayList (java.util.ArrayList)3 Column (org.jooq.util.xml.jaxb.Column)3 InformationSchema (org.jooq.util.xml.jaxb.InformationSchema)3 StringWriter (java.io.StringWriter)2 BigInteger (java.math.BigInteger)2 ForeignKey (org.jooq.ForeignKey)2 SchemaDefinition (org.jooq.util.SchemaDefinition)2 TableDefinition (org.jooq.util.TableDefinition)2 IndexColumnUsage (org.jooq.util.xml.jaxb.IndexColumnUsage)2 Schema (org.jooq.util.xml.jaxb.Schema)2 Sequence (org.jooq.util.xml.jaxb.Sequence)2 Table (org.jooq.util.xml.jaxb.Table)2 FALSE (java.lang.Boolean.FALSE)1 TRUE (java.lang.Boolean.TRUE)1