Search in sources :

Example 1 with ForeignKey

use of org.jooq.ForeignKey in project jOOQ by jOOQ.

the class JavaGenerator method generateRelations.

protected void generateRelations(SchemaDefinition schema) {
    log.info("Generating Keys");
    JavaWriter out = newJavaWriter(new File(getFile(schema).getParentFile(), "Keys.java"));
    printPackage(out, schema);
    printClassJavadoc(out, "A class modelling foreign key relationships between tables of the <code>" + schema.getOutputName() + "</code> schema");
    printClassAnnotations(out, schema);
    if (scala)
        out.println("object Keys {");
    else
        out.println("public class Keys {");
    out.tab(1).header("IDENTITY definitions");
    out.println();
    List<IdentityDefinition> allIdentities = new ArrayList<IdentityDefinition>();
    List<UniqueKeyDefinition> allUniqueKeys = new ArrayList<UniqueKeyDefinition>();
    List<ForeignKeyDefinition> allForeignKeys = new ArrayList<ForeignKeyDefinition>();
    for (TableDefinition table : database.getTables(schema)) {
        try {
            IdentityDefinition identity = table.getIdentity();
            if (identity != null) {
                final String identityType = out.ref(getStrategy().getFullJavaClassName(identity.getColumn().getContainer(), Mode.RECORD));
                final String columnType = out.ref(getJavaType(identity.getColumn().getType()));
                final String identityId = getStrategy().getJavaIdentifier(identity.getColumn().getContainer());
                final int block = allIdentities.size() / INITIALISER_SIZE;
                if (scala)
                    out.tab(1).println("val IDENTITY_%s = Identities%s.IDENTITY_%s", identityId, block, identityId);
                else
                    out.tab(1).println("public static final %s<%s, %s> IDENTITY_%s = Identities%s.IDENTITY_%s;", Identity.class, identityType, columnType, identityId, block, identityId);
                allIdentities.add(identity);
            }
        } catch (Exception e) {
            log.error("Error while generating table " + table, e);
        }
    }
    // Unique keys
    out.tab(1).header("UNIQUE and PRIMARY KEY definitions");
    out.println();
    for (TableDefinition table : database.getTables(schema)) {
        try {
            List<UniqueKeyDefinition> uniqueKeys = table.getUniqueKeys();
            for (UniqueKeyDefinition uniqueKey : uniqueKeys) {
                final String keyType = out.ref(getStrategy().getFullJavaClassName(uniqueKey.getTable(), Mode.RECORD));
                final String keyId = getStrategy().getJavaIdentifier(uniqueKey);
                final int block = allUniqueKeys.size() / INITIALISER_SIZE;
                if (scala)
                    out.tab(1).println("val %s = UniqueKeys%s.%s", keyId, block, keyId);
                else
                    out.tab(1).println("public static final %s<%s> %s = UniqueKeys%s.%s;", UniqueKey.class, keyType, keyId, block, keyId);
                allUniqueKeys.add(uniqueKey);
            }
        } catch (Exception e) {
            log.error("Error while generating table " + table, e);
        }
    }
    // Foreign keys
    out.tab(1).header("FOREIGN KEY definitions");
    out.println();
    for (TableDefinition table : database.getTables(schema)) {
        try {
            List<ForeignKeyDefinition> foreignKeys = table.getForeignKeys();
            for (ForeignKeyDefinition foreignKey : foreignKeys) {
                final String keyType = out.ref(getStrategy().getFullJavaClassName(foreignKey.getKeyTable(), Mode.RECORD));
                final String referencedType = out.ref(getStrategy().getFullJavaClassName(foreignKey.getReferencedTable(), Mode.RECORD));
                final String keyId = getStrategy().getJavaIdentifier(foreignKey);
                final int block = allForeignKeys.size() / INITIALISER_SIZE;
                if (scala)
                    out.tab(1).println("val %s = ForeignKeys%s.%s", keyId, block, keyId);
                else
                    out.tab(1).println("public static final %s<%s, %s> %s = ForeignKeys%s.%s;", ForeignKey.class, keyType, referencedType, keyId, block, keyId);
                allForeignKeys.add(foreignKey);
            }
        } catch (Exception e) {
            log.error("Error while generating reference " + table, e);
        }
    }
    // [#1459] Print nested classes for actual static field initialisations
    // keeping top-level initialiser small
    int identityCounter = 0;
    int uniqueKeyCounter = 0;
    int foreignKeyCounter = 0;
    out.tab(1).header("[#1459] distribute members to avoid static initialisers > 64kb");
    for (IdentityDefinition identity : allIdentities) {
        printIdentity(out, identityCounter, identity);
        identityCounter++;
    }
    if (identityCounter > 0) {
        out.tab(1).println("}");
    }
    for (UniqueKeyDefinition uniqueKey : allUniqueKeys) {
        printUniqueKey(out, uniqueKeyCounter, uniqueKey);
        uniqueKeyCounter++;
    }
    if (uniqueKeyCounter > 0) {
        out.tab(1).println("}");
    }
    for (ForeignKeyDefinition foreignKey : allForeignKeys) {
        printForeignKey(out, foreignKeyCounter, foreignKey);
        foreignKeyCounter++;
    }
    if (foreignKeyCounter > 0) {
        out.tab(1).println("}");
    }
    out.println("}");
    closeJavaWriter(out);
    watch.splitInfo("Keys generated");
}
Also used : ArrayList(java.util.ArrayList) StringUtils.defaultString(org.jooq.tools.StringUtils.defaultString) ForeignKey(org.jooq.ForeignKey) SQLDialectNotSupportedException(org.jooq.exception.SQLDialectNotSupportedException) IOException(java.io.IOException) ReflectException(org.jooq.tools.reflect.ReflectException) UniqueKey(org.jooq.UniqueKey) Identity(org.jooq.Identity) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 2 with ForeignKey

use of org.jooq.ForeignKey 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);
    }
}
Also used : KeyColumnUsage(org.jooq.util.xml.jaxb.KeyColumnUsage) ReferentialConstraint(org.jooq.util.xml.jaxb.ReferentialConstraint) ForeignKey(org.jooq.ForeignKey) TableConstraint(org.jooq.util.xml.jaxb.TableConstraint) ReferentialConstraint(org.jooq.util.xml.jaxb.ReferentialConstraint) TableConstraint(org.jooq.util.xml.jaxb.TableConstraint)

Aggregations

ForeignKey (org.jooq.ForeignKey)2 File (java.io.File)1 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1 ArrayList (java.util.ArrayList)1 Identity (org.jooq.Identity)1 UniqueKey (org.jooq.UniqueKey)1 SQLDialectNotSupportedException (org.jooq.exception.SQLDialectNotSupportedException)1 StringUtils.defaultString (org.jooq.tools.StringUtils.defaultString)1 ReflectException (org.jooq.tools.reflect.ReflectException)1 KeyColumnUsage (org.jooq.util.xml.jaxb.KeyColumnUsage)1 ReferentialConstraint (org.jooq.util.xml.jaxb.ReferentialConstraint)1 TableConstraint (org.jooq.util.xml.jaxb.TableConstraint)1