[#5460] Added support for constraints
This commit is contained in:
parent
4b71f10b33
commit
c342bc2c19
@ -40,18 +40,29 @@
|
||||
*/
|
||||
package org.jooq.impl;
|
||||
|
||||
import static org.jooq.util.xml.jaxb.TableConstraintType.FOREIGN_KEY;
|
||||
import static org.jooq.util.xml.jaxb.TableConstraintType.PRIMARY_KEY;
|
||||
import static org.jooq.util.xml.jaxb.TableConstraintType.UNIQUE;
|
||||
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.jooq.Configuration;
|
||||
import org.jooq.Field;
|
||||
import org.jooq.ForeignKey;
|
||||
import org.jooq.Key;
|
||||
import org.jooq.Schema;
|
||||
import org.jooq.Sequence;
|
||||
import org.jooq.Table;
|
||||
import org.jooq.UniqueKey;
|
||||
import org.jooq.tools.StringUtils;
|
||||
import org.jooq.util.xml.jaxb.Column;
|
||||
import org.jooq.util.xml.jaxb.InformationSchema;
|
||||
import org.jooq.util.xml.jaxb.KeyColumnUsage;
|
||||
import org.jooq.util.xml.jaxb.ReferentialConstraint;
|
||||
import org.jooq.util.xml.jaxb.TableConstraint;
|
||||
import org.jooq.util.xml.jaxb.TableConstraintType;
|
||||
|
||||
/**
|
||||
* @author Lukas Eder
|
||||
@ -61,15 +72,17 @@ final class InformationSchemaExport {
|
||||
static final InformationSchema exportTables(Configuration configuration, List<Table<?>> tables) {
|
||||
InformationSchema result = new InformationSchema();
|
||||
|
||||
Set<Schema> schemas = new LinkedHashSet<Schema>();
|
||||
for (Table<?> t : tables)
|
||||
schemas.add(t.getSchema());
|
||||
Set<Schema> includedSchemas = new LinkedHashSet<Schema>();
|
||||
Set<Table<?>> includedTables = new LinkedHashSet<Table<?>>(tables);
|
||||
|
||||
for (Schema s : schemas)
|
||||
for (Table<?> t : tables)
|
||||
includedSchemas.add(t.getSchema());
|
||||
|
||||
for (Schema s : includedSchemas)
|
||||
exportSchema0(result, s);
|
||||
|
||||
for (Table<?> t : tables)
|
||||
exportTable0(configuration, result, t);
|
||||
exportTable0(configuration, result, t, includedTables);
|
||||
|
||||
return result;
|
||||
}
|
||||
@ -77,11 +90,16 @@ final class InformationSchemaExport {
|
||||
static final InformationSchema exportSchemas(Configuration configuration, List<Schema> schemas) {
|
||||
InformationSchema result = new InformationSchema();
|
||||
|
||||
Set<Table<?>> includedTables = new LinkedHashSet<Table<?>>();
|
||||
for (Schema s : schemas)
|
||||
for (Table<?> t : s.getTables())
|
||||
includedTables.add(t);
|
||||
|
||||
for (Schema s : schemas) {
|
||||
exportSchema0(result, s);
|
||||
|
||||
for (Table<?> t : s.getTables())
|
||||
exportTable0(configuration, result, t);
|
||||
exportTable0(configuration, result, t, includedTables);
|
||||
|
||||
for (Sequence<?> q : s.getSequences())
|
||||
exportSequences0(configuration, result, q);
|
||||
@ -90,7 +108,7 @@ final class InformationSchemaExport {
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void exportSequences0(Configuration configuration, InformationSchema result, Sequence<?> q) {
|
||||
private static final void exportSequences0(Configuration configuration, InformationSchema result, Sequence<?> q) {
|
||||
org.jooq.util.xml.jaxb.Sequence iq = new org.jooq.util.xml.jaxb.Sequence();
|
||||
|
||||
if (!StringUtils.isBlank(q.getCatalog().getName()))
|
||||
@ -114,7 +132,19 @@ final class InformationSchemaExport {
|
||||
result.getSequences().add(iq);
|
||||
}
|
||||
|
||||
private static void exportTable0(Configuration configuration, InformationSchema result, Table<?> t) {
|
||||
private static final void exportSchema0(InformationSchema result, Schema s) {
|
||||
org.jooq.util.xml.jaxb.Schema is = new org.jooq.util.xml.jaxb.Schema();
|
||||
|
||||
if (!StringUtils.isBlank(s.getCatalog().getName()))
|
||||
is.setCatalogName(s.getCatalog().getName());
|
||||
|
||||
if (!StringUtils.isBlank(s.getName())) {
|
||||
is.setSchemaName(s.getName());
|
||||
result.getSchemata().add(is);
|
||||
}
|
||||
}
|
||||
|
||||
private static final void exportTable0(Configuration configuration, InformationSchema result, Table<?> t, Set<Table<?>> includedTables) {
|
||||
org.jooq.util.xml.jaxb.Table it = new org.jooq.util.xml.jaxb.Table();
|
||||
|
||||
if (!StringUtils.isBlank(t.getCatalog().getName()))
|
||||
@ -156,17 +186,81 @@ final class InformationSchemaExport {
|
||||
|
||||
result.getColumns().add(ic);
|
||||
}
|
||||
|
||||
for (UniqueKey<?> key : t.getKeys())
|
||||
exportKey0(result, t, key, key.isPrimary() ? PRIMARY_KEY : UNIQUE);
|
||||
|
||||
for (ForeignKey<?, ?> fk : t.getReferences())
|
||||
if (includedTables.contains(fk.getKey().getTable()))
|
||||
exportKey0(result, t, fk, FOREIGN_KEY);
|
||||
}
|
||||
|
||||
private static final void exportSchema0(InformationSchema result, Schema s) {
|
||||
org.jooq.util.xml.jaxb.Schema is = new org.jooq.util.xml.jaxb.Schema();
|
||||
private static final void exportKey0(InformationSchema result, Table<?> t, Key<?> key, TableConstraintType constraintType) {
|
||||
TableConstraint tc = new TableConstraint();
|
||||
|
||||
if (!StringUtils.isBlank(s.getCatalog().getName()))
|
||||
is.setCatalogName(s.getCatalog().getName());
|
||||
String catalogName = t.getCatalog().getName();
|
||||
String schemaName = t.getSchema().getName();
|
||||
|
||||
if (!StringUtils.isBlank(s.getName())) {
|
||||
is.setSchemaName(s.getName());
|
||||
result.getSchemata().add(is);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user