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;
}
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));
}
}
}
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);
}
}
}
}
}
}
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;
}
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()));
}
}
}
}
}
}
Aggregations