use of org.jooq.util.xml.jaxb.ReferentialConstraint 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.ReferentialConstraint in project jOOQ by jOOQ.
the class XMLGenerator method generate.
@Override
public void generate(Database db) {
logDatabaseParameters(db);
log.info("");
logGenerationRemarks(db);
log.info("");
log.info("----------------------------------------------------------");
TextWriter out = new TextWriter(getStrategy().getFile("information_schema.xml"), targetEncoding);
log.info("");
log.info("Generating XML", out.file().getName());
log.info("==========================================================");
InformationSchema is = new InformationSchema();
for (CatalogDefinition c : db.getCatalogs()) {
String catalogName = c.getOutputName();
for (SchemaDefinition s : c.getSchemata()) {
String schemaName = s.getOutputName();
Schema schema = new Schema();
schema.setCatalogName(catalogName);
schema.setSchemaName(schemaName);
is.getSchemata().add(schema);
for (TableDefinition t : s.getTables()) {
String tableName = t.getOutputName();
Table table = new Table();
table.setTableCatalog(catalogName);
table.setTableSchema(schemaName);
table.setTableName(tableName);
is.getTables().add(table);
for (ColumnDefinition co : t.getColumns()) {
String columnName = co.getOutputName();
DataTypeDefinition type = co.getType();
Column column = new Column();
column.setTableCatalog(catalogName);
column.setTableSchema(schemaName);
column.setTableName(tableName);
column.setColumnName(columnName);
column.setCharacterMaximumLength(type.getLength());
column.setColumnDefault(type.getDefaultValue());
column.setDataType(type.getType());
// TODO This is not yet supported
// column.setIdentityGeneration(co.isIdentity());
column.setIsNullable(column.isIsNullable());
column.setNumericPrecision(type.getPrecision());
column.setNumericScale(type.getScale());
column.setOrdinalPosition(co.getPosition());
is.getColumns().add(column);
}
}
for (UniqueKeyDefinition u : db.getUniqueKeys(s)) {
String constraintName = u.getOutputName();
TableDefinition table = u.getTable();
List<ColumnDefinition> columns = u.getKeyColumns();
TableConstraint constraint = new TableConstraint();
constraint.setConstraintCatalog(catalogName);
constraint.setConstraintSchema(schemaName);
constraint.setConstraintName(constraintName);
constraint.setConstraintType(u.isPrimaryKey() ? PRIMARY_KEY : UNIQUE);
constraint.setTableCatalog(table.getCatalog().getOutputName());
constraint.setTableSchema(table.getSchema().getOutputName());
constraint.setTableName(table.getOutputName());
is.getTableConstraints().add(constraint);
for (int i = 0; i < columns.size(); i++) {
ColumnDefinition column = columns.get(i);
KeyColumnUsage kc = new KeyColumnUsage();
kc.setConstraintCatalog(catalogName);
kc.setConstraintSchema(schemaName);
kc.setConstraintName(constraintName);
kc.setColumnName(column.getOutputName());
kc.setOrdinalPosition(i);
kc.setTableCatalog(table.getCatalog().getOutputName());
kc.setTableSchema(table.getSchema().getOutputName());
kc.setTableName(table.getOutputName());
is.getKeyColumnUsages().add(kc);
}
}
for (ForeignKeyDefinition f : db.getForeignKeys(s)) {
String constraintName = f.getOutputName();
UniqueKeyDefinition referenced = f.getReferencedKey();
TableDefinition table = f.getKeyTable();
List<ColumnDefinition> columns = f.getKeyColumns();
TableConstraint tc = new TableConstraint();
tc.setConstraintCatalog(catalogName);
tc.setConstraintSchema(schemaName);
tc.setConstraintName(constraintName);
tc.setConstraintType(FOREIGN_KEY);
tc.setTableCatalog(table.getCatalog().getOutputName());
tc.setTableSchema(table.getSchema().getOutputName());
tc.setTableName(table.getOutputName());
ReferentialConstraint rc = new ReferentialConstraint();
rc.setConstraintCatalog(catalogName);
rc.setConstraintSchema(schemaName);
rc.setConstraintName(constraintName);
rc.setUniqueConstraintCatalog(referenced.getOutputName());
rc.setUniqueConstraintSchema(referenced.getSchema().getOutputName());
rc.setUniqueConstraintName(referenced.getOutputName());
is.getTableConstraints().add(tc);
is.getReferentialConstraints().add(rc);
for (int i = 0; i < columns.size(); i++) {
ColumnDefinition column = columns.get(i);
KeyColumnUsage kc = new KeyColumnUsage();
kc.setConstraintCatalog(catalogName);
kc.setConstraintSchema(schemaName);
kc.setConstraintName(constraintName);
kc.setColumnName(column.getOutputName());
kc.setOrdinalPosition(i);
kc.setTableCatalog(table.getCatalog().getOutputName());
kc.setTableSchema(table.getSchema().getOutputName());
kc.setTableName(table.getOutputName());
is.getKeyColumnUsages().add(kc);
}
}
for (CheckConstraintDefinition ch : db.getCheckConstraints(s)) {
String constraintName = ch.getOutputName();
TableDefinition table = ch.getTable();
TableConstraint constraint = new TableConstraint();
constraint.setConstraintCatalog(catalogName);
constraint.setConstraintSchema(schemaName);
constraint.setConstraintName(constraintName);
constraint.setConstraintType(CHECK);
constraint.setTableCatalog(table.getCatalog().getOutputName());
constraint.setTableSchema(table.getSchema().getOutputName());
constraint.setTableName(table.getOutputName());
is.getTableConstraints().add(constraint);
}
for (SequenceDefinition se : db.getSequences(s)) {
String sequenceName = se.getOutputName();
DataTypeDefinition type = se.getType();
Sequence sequence = new Sequence();
sequence.setSequenceCatalog(catalogName);
sequence.setSequenceSchema(schemaName);
sequence.setSequenceName(sequenceName);
sequence.setCharacterMaximumLength(type.getLength());
sequence.setDataType(type.getType());
sequence.setNumericPrecision(type.getPrecision());
sequence.setNumericScale(type.getScale());
is.getSequences().add(sequence);
}
for (PackageDefinition pkg : db.getPackages(s)) for (RoutineDefinition r : pkg.getRoutines()) exportRoutine(is, r, catalogName, schemaName);
for (RoutineDefinition r : db.getRoutines(s)) exportRoutine(is, r, catalogName, schemaName);
}
}
StringWriter writer = new StringWriter();
JAXB.marshal(is, writer);
out.print(writer.toString());
out.close();
}
use of org.jooq.util.xml.jaxb.ReferentialConstraint in project jOOQ by jOOQ.
the class InformationSchemaExport method exportKey0.
private static final void exportKey0(InformationSchema result, Table<?> t, Key<?> key, TableConstraintType constraintType) {
TableConstraint tc = new TableConstraint();
String catalogName = t.getCatalog().getName();
String schemaName = t.getSchema().getName();
tc.setConstraintName(key.getName());
tc.setConstraintType(constraintType);
if (!StringUtils.isBlank(catalogName)) {
tc.setConstraintCatalog(catalogName);
tc.setTableCatalog(catalogName);
}
if (!StringUtils.isBlank(schemaName)) {
tc.setConstraintSchema(schemaName);
tc.setTableSchema(schemaName);
}
tc.setTableName(t.getName());
result.getTableConstraints().add(tc);
int i = 0;
for (Field<?> f : key.getFields()) {
KeyColumnUsage kc = new KeyColumnUsage();
if (!StringUtils.isBlank(catalogName)) {
kc.setConstraintCatalog(catalogName);
kc.setTableCatalog(catalogName);
}
if (!StringUtils.isBlank(schemaName)) {
kc.setConstraintSchema(schemaName);
kc.setTableSchema(schemaName);
}
kc.setColumnName(f.getName());
kc.setTableName(t.getName());
kc.setOrdinalPosition(++i);
kc.setConstraintName(key.getName());
result.getKeyColumnUsages().add(kc);
}
if (constraintType == FOREIGN_KEY) {
ReferentialConstraint rc = new ReferentialConstraint();
UniqueKey<?> uk = ((ForeignKey<?, ?>) key).getKey();
String ukCatalogName = uk.getTable().getCatalog().getName();
String ukSchemaName = uk.getTable().getSchema().getName();
if (!StringUtils.isBlank(catalogName))
rc.setConstraintCatalog(catalogName);
if (!StringUtils.isBlank(ukCatalogName))
rc.setUniqueConstraintCatalog(ukCatalogName);
if (!StringUtils.isBlank(schemaName))
rc.setConstraintSchema(schemaName);
if (!StringUtils.isBlank(ukSchemaName))
rc.setUniqueConstraintSchema(ukSchemaName);
rc.setConstraintName(key.getName());
rc.setUniqueConstraintName(uk.getName());
result.getReferentialConstraints().add(rc);
}
}
use of org.jooq.util.xml.jaxb.ReferentialConstraint in project jOOQ by jOOQ.
the class InformationSchemaMetaImpl method init.
private final void init(InformationSchema meta) {
List<String> errors = new ArrayList<String>();
// -------------------------------------------------------------------------------------------------------------
for (org.jooq.util.xml.jaxb.Schema xs : meta.getSchemata()) {
InformationSchemaCatalog catalog = new InformationSchemaCatalog(xs.getCatalogName());
if (!catalogs.contains(catalog))
catalogs.add(catalog);
InformationSchemaSchema is = new InformationSchemaSchema(xs.getSchemaName(), catalog);
schemas.add(is);
schemasByName.put(name(xs.getCatalogName(), xs.getSchemaName()), is);
}
// -------------------------------------------------------------------------------------------------------------
tableLoop: for (org.jooq.util.xml.jaxb.Table xt : meta.getTables()) {
Name schemaName = name(xt.getTableCatalog(), xt.getTableSchema());
Schema schema = schemasByName.get(schemaName);
if (schema == null) {
errors.add(String.format("Schema " + schemaName + " not defined for table " + xt.getTableName()));
continue tableLoop;
}
InformationSchemaTable it = new InformationSchemaTable(xt.getTableName(), schema);
tables.add(it);
tablesByName.put(name(xt.getTableCatalog(), xt.getTableSchema(), xt.getTableName()), it);
}
// Columns
// -------------------------------------------------------------------------------------------------------------
List<Column> columns = new ArrayList<Column>(meta.getColumns());
Collections.sort(columns, new Comparator<Column>() {
@Override
public int compare(Column o1, Column o2) {
Integer p1 = o1.getOrdinalPosition();
Integer p2 = o2.getOrdinalPosition();
if (p1 == p2)
return 0;
if (p1 == null)
return -1;
if (p2 == null)
return 1;
return p1.compareTo(p2);
}
});
columnLoop: for (Column xc : columns) {
String typeName = xc.getDataType();
int length = xc.getCharacterMaximumLength() == null ? 0 : xc.getCharacterMaximumLength();
int precision = xc.getNumericPrecision() == null ? 0 : xc.getNumericPrecision();
int scale = xc.getNumericScale() == null ? 0 : xc.getNumericScale();
boolean nullable = xc.isIsNullable() == null ? true : xc.isIsNullable();
// TODO: Exception handling should be moved inside SQLDataType
Name tableName = name(xc.getTableCatalog(), xc.getTableSchema(), xc.getTableName());
InformationSchemaTable table = tablesByName.get(tableName);
if (table == null) {
errors.add(String.format("Table " + tableName + " not defined for column " + xc.getColumnName()));
continue columnLoop;
}
AbstractTable.createField(xc.getColumnName(), type(typeName, length, precision, scale, nullable), table);
}
// Constraints
// -------------------------------------------------------------------------------------------------------------
Map<Name, List<TableField<Record, ?>>> columnsByConstraint = new HashMap<Name, List<TableField<Record, ?>>>();
List<KeyColumnUsage> keyColumnUsages = new ArrayList<KeyColumnUsage>(meta.getKeyColumnUsages());
Collections.sort(keyColumnUsages, new Comparator<KeyColumnUsage>() {
@Override
public int compare(KeyColumnUsage o1, KeyColumnUsage o2) {
int p1 = o1.getOrdinalPosition();
int p2 = o2.getOrdinalPosition();
return (p1 < p2) ? -1 : ((p1 == p2) ? 0 : 1);
}
});
keyColumnLoop: for (KeyColumnUsage xc : keyColumnUsages) {
Name constraintName = name(xc.getConstraintCatalog(), xc.getConstraintSchema(), xc.getConstraintName());
List<TableField<Record, ?>> fields = columnsByConstraint.get(constraintName);
if (fields == null) {
fields = new ArrayList<TableField<Record, ?>>();
columnsByConstraint.put(constraintName, fields);
}
Name tableName = name(xc.getTableCatalog(), xc.getTableSchema(), xc.getTableName());
InformationSchemaTable table = tablesByName.get(tableName);
if (table == null) {
errors.add(String.format("Table " + tableName + " not defined for constraint " + constraintName));
continue keyColumnLoop;
}
TableField<Record, ?> field = (TableField<Record, ?>) table.field(xc.getColumnName());
if (field == null) {
errors.add(String.format("Column " + xc.getColumnName() + " not defined for table " + tableName));
continue keyColumnLoop;
}
fields.add(field);
}
tableConstraintLoop: for (TableConstraint xc : meta.getTableConstraints()) {
switch(xc.getConstraintType()) {
case PRIMARY_KEY:
case UNIQUE:
{
Name tableName = name(xc.getTableCatalog(), xc.getTableSchema(), xc.getTableName());
Name constraintName = name(xc.getConstraintCatalog(), xc.getConstraintSchema(), xc.getConstraintName());
InformationSchemaTable table = tablesByName.get(tableName);
if (table == null) {
errors.add(String.format("Table " + tableName + " not defined for constraint " + constraintName));
continue tableConstraintLoop;
}
List<TableField<Record, ?>> c = columnsByConstraint.get(constraintName);
if (c == null || c.isEmpty()) {
errors.add(String.format("No columns defined for constraint " + constraintName));
continue tableConstraintLoop;
}
UniqueKeyImpl<Record> key = (UniqueKeyImpl<Record>) AbstractKeys.createUniqueKey(table, xc.getConstraintName(), c.toArray(new TableField[0]));
if (xc.getConstraintType() == PRIMARY_KEY) {
table.primaryKey = key;
primaryKeys.add(key);
}
table.uniqueKeys.add(key);
uniqueKeysByName.put(constraintName, key);
break;
}
}
}
for (ReferentialConstraint xr : meta.getReferentialConstraints()) {
referentialKeys.put(name(xr.getConstraintCatalog(), xr.getConstraintSchema(), xr.getConstraintName()), name(xr.getUniqueConstraintCatalog(), xr.getUniqueConstraintSchema(), xr.getUniqueConstraintName()));
}
tableConstraintLoop: for (TableConstraint xc : meta.getTableConstraints()) {
switch(xc.getConstraintType()) {
case FOREIGN_KEY:
{
Name tableName = name(xc.getTableCatalog(), xc.getTableSchema(), xc.getTableName());
Name constraintName = name(xc.getConstraintCatalog(), xc.getConstraintSchema(), xc.getConstraintName());
InformationSchemaTable table = tablesByName.get(tableName);
if (table == null) {
errors.add(String.format("Table " + tableName + " not defined for constraint " + constraintName));
continue tableConstraintLoop;
}
List<TableField<Record, ?>> c = columnsByConstraint.get(constraintName);
if (c == null || c.isEmpty()) {
errors.add(String.format("No columns defined for constraint " + constraintName));
continue tableConstraintLoop;
}
UniqueKeyImpl<Record> uniqueKey = uniqueKeysByName.get(referentialKeys.get(constraintName));
if (uniqueKey == null) {
errors.add(String.format("No unique key defined for foreign key " + constraintName));
continue tableConstraintLoop;
}
ForeignKey<Record, Record> key = AbstractKeys.createForeignKey(uniqueKey, table, xc.getConstraintName(), c.toArray(new TableField[0]));
table.foreignKeys.add(key);
break;
}
}
}
// -------------------------------------------------------------------------------------------------------------
sequenceLoop: for (org.jooq.util.xml.jaxb.Sequence xs : meta.getSequences()) {
Name schemaName = name(xs.getSequenceCatalog(), xs.getSequenceSchema());
Schema schema = schemasByName.get(schemaName);
if (schema == null) {
errors.add(String.format("Schema " + schemaName + " not defined for sequence " + xs.getSequenceName()));
continue sequenceLoop;
}
String typeName = xs.getDataType();
int length = xs.getCharacterMaximumLength() == null ? 0 : xs.getCharacterMaximumLength();
int precision = xs.getNumericPrecision() == null ? 0 : xs.getNumericPrecision();
int scale = xs.getNumericScale() == null ? 0 : xs.getNumericScale();
boolean nullable = true;
@SuppressWarnings({ "rawtypes", "unchecked" }) InformationSchemaSequence is = new InformationSchemaSequence(xs.getSequenceName(), schema, type(typeName, length, precision, scale, nullable));
sequences.add(is);
}
// -------------------------------------------------------------------------------------------------------------
for (Schema s : schemas) {
Catalog c = s.getCatalog();
List<Schema> list = schemasPerCatalog.get(c);
if (list == null) {
list = new ArrayList<Schema>();
schemasPerCatalog.put(c, list);
}
list.add(s);
}
for (InformationSchemaTable t : tables) {
Schema s = t.getSchema();
List<InformationSchemaTable> list = tablesPerSchema.get(s);
if (list == null) {
list = new ArrayList<InformationSchemaTable>();
tablesPerSchema.put(s, list);
}
list.add(t);
}
for (Sequence<?> q : sequences) {
Schema s = q.getSchema();
List<Sequence<?>> list = sequencesPerSchema.get(s);
if (list == null) {
list = new ArrayList<Sequence<?>>();
sequencesPerSchema.put(s, list);
}
list.add(q);
}
if (!errors.isEmpty())
throw new IllegalArgumentException(errors.toString());
}
Aggregations