Search in sources :

Example 1 with Identity

use of org.jooq.Identity 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)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1 ArrayList (java.util.ArrayList)1 ForeignKey (org.jooq.ForeignKey)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