Search in sources :

Example 36 with TableDefinition

use of org.jooq.meta.TableDefinition in project jOOQ by jOOQ.

the class JavaGenerator method generateSchema.

protected void generateSchema(SchemaDefinition schema, JavaWriter out) {
    final String catalogId = out.ref(getStrategy().getFullJavaIdentifier(schema.getCatalog()), 2);
    final String schemaId = getStrategy().getJavaIdentifier(schema);
    final String schemaName = !schema.getQualifiedOutputName().isEmpty() ? schema.getQualifiedOutputName() : schemaId;
    final String className = getStrategy().getJavaClassName(schema);
    final List<String> interfaces = out.ref(getStrategy().getJavaClassImplements(schema, Mode.DEFAULT));
    printPackage(out, schema);
    if (scala) {
        out.println("object %s {", className);
        out.javadoc("The reference instance of <code>%s</code>", schemaName);
        out.println("val %s = new %s", schemaId, className);
        out.println("}");
        out.println();
    }
    generateSchemaClassJavadoc(schema, out);
    printClassAnnotations(out, schema, Mode.DEFAULT);
    if (scala) {
        out.println("%sclass %s extends %s(\"%s\", %s)[[before= with ][separator= with ][%s]] {", visibility(), className, SchemaImpl.class, escapeString(schema.getOutputName()), catalogId, interfaces);
    } else if (kotlin) {
        out.println("%sopen class %s : %s(\"%s\", %s)[[before=, ][%s]] {", visibility(), className, SchemaImpl.class, escapeString(schema.getOutputName()), catalogId, interfaces);
        out.println("public companion object {");
        out.javadoc("The reference instance of <code>%s</code>", schemaName);
        out.println("%sval %s: %s = %s()", visibility(), scalaWhitespaceSuffix(schemaId), className, className);
        out.println("}");
    } else {
        out.println("%sclass %s extends %s[[before= implements ][%s]] {", visibility(), className, SchemaImpl.class, interfaces);
        out.printSerial();
        out.javadoc("The reference instance of <code>%s</code>", schemaName);
        out.println("%sstatic final %s %s = new %s();", visibility(), className, schemaId, className);
    }
    if (generateGlobalTableReferences()) {
        Set<String> memberNames = getMemberNames(schema);
        for (TableDefinition table : schema.getTables()) {
            // reference in the schema, and the function call
            if (scala && table.isTableValuedFunction() && table.getParameters().isEmpty())
                continue;
            final String tableClassName = out.ref(getStrategy().getFullJavaClassName(table));
            final String tableId = getStrategy().getJavaIdentifier(table);
            final String tableShortId = getShortId(out, memberNames, table);
            final String tableComment = escapeEntities(comment(table));
            out.javadoc(isBlank(tableComment) ? "The table <code>" + table.getQualifiedOutputName() + "</code>." : tableComment);
            if (scala)
                out.println("%sdef %s = %s", visibility(), tableId, tableShortId);
            else if (kotlin)
                out.println("%sval %s: %s get() = %s", visibility(), scalaWhitespaceSuffix(tableId), tableClassName, tableShortId);
            else
                out.println("%sfinal %s %s = %s;", visibility(), tableClassName, tableId, tableShortId);
            // globalObjectReferences
            if (table.isTableValuedFunction())
                printTableValuedFunction(out, table, getStrategy().getJavaIdentifier(table));
        }
    }
    if (!scala && !kotlin) {
        out.javadoc(NO_FURTHER_INSTANCES_ALLOWED);
        out.println("private %s() {", className);
        out.println("super(\"%s\", null);", escapeString(schema.getOutputName()));
        out.println("}");
    }
    out.println();
    if (scala) {
        out.println("%soverride def getCatalog: %s = %s", visibilityPublic(), Catalog.class, catalogId);
    } else if (kotlin) {
        out.println("%soverride fun getCatalog(): %s = %s", visibilityPublic(), Catalog.class, catalogId);
    } else {
        out.overrideInherit();
        printNonnullAnnotation(out);
        out.println("%s%s getCatalog() {", visibilityPublic(), Catalog.class);
        out.println("return %s;", catalogId);
        out.println("}");
    }
    // [#2255] Avoid referencing sequence literals, if they're not generated
    if (generateGlobalSequenceReferences())
        printReferences(out, database.getSequences(schema), Sequence.class, true);
    // [#681] Avoid referencing domain literals, if they're not generated
    if (generateGlobalDomainReferences())
        printReferences(out, database.getDomains(schema), Domain.class, true);
    // [#9685] Avoid referencing table literals if they're not generated
    if (generateTables())
        printReferences(out, database.getTables(schema), Table.class, true);
    // [#9685] Avoid referencing UDT literals if they're not generated
    if (generateUDTs())
        printReferences(out, database.getUDTs(schema), UDT.class, true);
    generateSchemaClassFooter(schema, out);
    out.println("}");
}
Also used : SchemaImpl(org.jooq.impl.SchemaImpl) Table(org.jooq.Table) UDT(org.jooq.UDT) TableDefinition(org.jooq.meta.TableDefinition) Sequence(org.jooq.Sequence) Domain(org.jooq.Domain) Catalog(org.jooq.Catalog)

Example 37 with TableDefinition

use of org.jooq.meta.TableDefinition in project jOOQ by jOOQ.

the class JavaGenerator method generatePojos.

protected void generatePojos(SchemaDefinition schema) {
    log.info("Generating table POJOs");
    for (TableDefinition table : database.getTables(schema)) {
        try {
            generatePojo(table);
        } catch (Exception e) {
            log.error("Error while generating table POJO " + table, e);
        }
    }
    watch.splitInfo("Table POJOs generated");
}
Also used : TableDefinition(org.jooq.meta.TableDefinition) IOException(java.io.IOException) SQLDialectNotSupportedException(org.jooq.exception.SQLDialectNotSupportedException) ReflectException(org.jooq.tools.reflect.ReflectException)

Example 38 with TableDefinition

use of org.jooq.meta.TableDefinition in project jOOQ by jOOQ.

the class JavaGenerator method printReferences.

protected void printReferences(JavaWriter out, List<? extends Definition> definitions, Class<?> type, boolean isGeneric) {
    if (out != null && !definitions.isEmpty()) {
        final String generic = isGeneric ? (scala ? "[_]" : kotlin ? "<*>" : "<?>") : "";
        final List<String> references = new ArrayList<>();
        final Definition first = definitions.get(0);
        // members in this class. Java can resolve the ambiguity.
        if ((scala || kotlin) && (first instanceof TableDefinition || first instanceof UDTDefinition)) {
            final Set<String> memberNames = getMemberNames(first.getSchema());
            for (Definition table : definitions) references.add(getShortId(out, memberNames, table));
        } else if ((scala || kotlin) && first instanceof SchemaDefinition) {
            final Set<String> memberNames = getMemberNames(first.getCatalog());
            for (Definition schema : definitions) references.add(getShortId(out, memberNames, schema));
        } else {
            references.addAll(kotlin ? out.ref(getStrategy().getFullJavaIdentifiers(definitions)) : out.ref(getStrategy().getFullJavaIdentifiers(definitions), 2));
        }
        out.println();
        if (scala) {
            if (definitions.size() > maxMembersPerInitialiser()) {
                out.println("%soverride def get%ss: %s[%s%s] = {", visibilityPublic(), type.getSimpleName(), List.class, type, generic);
                out.println("val result = new %s[%s%s]", ArrayList.class, type, generic);
                for (int i = 0; i < definitions.size(); i += maxMembersPerInitialiser()) out.println("result.addAll(get%ss%s)", type.getSimpleName(), i / maxMembersPerInitialiser());
                out.println("result");
                out.println("}");
            } else {
                out.println("%soverride def get%ss: %s[%s%s] = %s.asList[%s%s](", visibilityPublic(), type.getSimpleName(), List.class, type, generic, Arrays.class, type, generic);
                out.println("[[separator=,\n][%s]]", references);
                out.println(")");
            }
        } else if (kotlin) {
            if (definitions.size() > maxMembersPerInitialiser()) {
                out.println("%soverride fun get%ss(): %s<%s%s> {", visibilityPublic(), type.getSimpleName(), out.ref(KLIST), type, generic);
                out.println("val result = mutableListOf<%s%s>()", type, generic);
                for (int i = 0; i < definitions.size(); i += maxMembersPerInitialiser()) out.println("result.addAll(get%ss%s())", type.getSimpleName(), i / maxMembersPerInitialiser());
                out.println("return result");
                out.println("}");
            } else {
                out.println("%soverride fun get%ss(): %s<%s%s> = listOf(", visibilityPublic(), type.getSimpleName(), out.ref(KLIST), type, generic);
                out.println("[[separator=,\n][%s]]", references);
                out.println(")");
            }
        } else {
            out.override();
            printNonnullAnnotation(out);
            out.println("%sfinal %s<%s%s> get%ss() {", visibilityPublic(), List.class, type, generic, type.getSimpleName());
            if (definitions.size() > maxMembersPerInitialiser()) {
                out.println("%s result = new %s();", List.class, ArrayList.class);
                for (int i = 0; i < definitions.size(); i += maxMembersPerInitialiser()) out.println("result.addAll(get%ss%s());", type.getSimpleName(), i / maxMembersPerInitialiser());
                out.println("return result;");
            } else {
                out.println("return %s.asList(", Arrays.class);
                out.println("[[separator=,\n][%s]]", references);
                out.println(");");
            }
            out.println("}");
        }
        if (definitions.size() > maxMembersPerInitialiser()) {
            for (int i = 0; i < definitions.size(); i += maxMembersPerInitialiser()) {
                out.println();
                if (scala) {
                    out.println("private def get%ss%s(): %s[%s%s] = %s.asList[%s%s](", type.getSimpleName(), i / maxMembersPerInitialiser(), List.class, type, generic, Arrays.class, type, generic);
                    out.println("[[separator=,\n][%s]]", references.subList(i, Math.min(i + maxMembersPerInitialiser(), references.size())));
                    out.println(")");
                } else if (kotlin) {
                    out.println("private fun get%ss%s(): %s<%s%s> = listOf(", type.getSimpleName(), i / maxMembersPerInitialiser(), out.ref(KLIST), type, generic);
                    out.println("[[separator=,\n][%s]]", references.subList(i, Math.min(i + maxMembersPerInitialiser(), references.size())));
                    out.println(")");
                } else {
                    out.println("private final %s<%s%s> get%ss%s() {", List.class, type, generic, type.getSimpleName(), i / maxMembersPerInitialiser());
                    out.println("return %s.asList(", Arrays.class);
                    out.println("[[separator=,\n][%s]]", references.subList(i, Math.min(i + maxMembersPerInitialiser(), references.size())));
                    out.println(");");
                    out.println("}");
                }
            }
        }
    }
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) AttributeDefinition(org.jooq.meta.AttributeDefinition) ColumnDefinition(org.jooq.meta.ColumnDefinition) RoutineDefinition(org.jooq.meta.RoutineDefinition) IndexColumnDefinition(org.jooq.meta.IndexColumnDefinition) IdentityDefinition(org.jooq.meta.IdentityDefinition) EmbeddableColumnDefinition(org.jooq.meta.EmbeddableColumnDefinition) CheckConstraintDefinition(org.jooq.meta.CheckConstraintDefinition) SequenceDefinition(org.jooq.meta.SequenceDefinition) TableDefinition(org.jooq.meta.TableDefinition) IndexDefinition(org.jooq.meta.IndexDefinition) SchemaDefinition(org.jooq.meta.SchemaDefinition) CatalogDefinition(org.jooq.meta.CatalogDefinition) DataTypeDefinition(org.jooq.meta.DataTypeDefinition) PackageDefinition(org.jooq.meta.PackageDefinition) PostgresRoutineDefinition(org.jooq.meta.postgres.PostgresRoutineDefinition) TypedElementDefinition(org.jooq.meta.TypedElementDefinition) DomainDefinition(org.jooq.meta.DomainDefinition) UniqueKeyDefinition(org.jooq.meta.UniqueKeyDefinition) DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition) Definition(org.jooq.meta.Definition) EmbeddableDefinition(org.jooq.meta.EmbeddableDefinition) ArrayDefinition(org.jooq.meta.ArrayDefinition) UDTDefinition(org.jooq.meta.UDTDefinition) ParameterDefinition(org.jooq.meta.ParameterDefinition) ConstraintDefinition(org.jooq.meta.ConstraintDefinition) AbstractTypedElementDefinition(org.jooq.meta.AbstractTypedElementDefinition) EnumDefinition(org.jooq.meta.EnumDefinition) ForeignKeyDefinition(org.jooq.meta.ForeignKeyDefinition) TableDefinition(org.jooq.meta.TableDefinition) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Arrays(java.util.Arrays) UDTDefinition(org.jooq.meta.UDTDefinition)

Example 39 with TableDefinition

use of org.jooq.meta.TableDefinition in project jOOQ by jOOQ.

the class JavaGenerator method generateRecords.

protected void generateRecords(SchemaDefinition schema) {
    log.info("Generating table records");
    for (TableDefinition table : database.getTables(schema)) {
        try {
            generateRecord(table);
        } catch (Exception e) {
            log.error("Error while generating table record " + table, e);
        }
    }
    watch.splitInfo("Table records generated");
}
Also used : TableDefinition(org.jooq.meta.TableDefinition) IOException(java.io.IOException) SQLDialectNotSupportedException(org.jooq.exception.SQLDialectNotSupportedException) ReflectException(org.jooq.tools.reflect.ReflectException)

Example 40 with TableDefinition

use of org.jooq.meta.TableDefinition in project jOOQ by jOOQ.

the class FirebirdDatabase method loadUniqueKeys.

@Override
protected void loadUniqueKeys(DefaultRelations r) throws SQLException {
    for (Record record : uniqueKeys(Collections.<String>emptyList())) {
        String tableName = record.get(RDB$RELATION_CONSTRAINTS.RDB$RELATION_NAME);
        String fieldName = record.get(RDB$INDEX_SEGMENTS.RDB$FIELD_NAME);
        String key = record.get(RDB$RELATION_CONSTRAINTS.RDB$CONSTRAINT_NAME);
        TableDefinition td = getTable(this.getSchemata().get(0), tableName);
        if (td != null)
            r.addUniqueKey(key, td, td.getColumn(fieldName));
    }
}
Also used : TableDefinition(org.jooq.meta.TableDefinition) Record(org.jooq.Record)

Aggregations

TableDefinition (org.jooq.meta.TableDefinition)67 SchemaDefinition (org.jooq.meta.SchemaDefinition)47 Record (org.jooq.Record)44 ArrayList (java.util.ArrayList)23 IndexColumnDefinition (org.jooq.meta.IndexColumnDefinition)16 IndexDefinition (org.jooq.meta.IndexDefinition)14 List (java.util.List)12 ColumnDefinition (org.jooq.meta.ColumnDefinition)12 IOException (java.io.IOException)10 DefaultIndexColumnDefinition (org.jooq.meta.DefaultIndexColumnDefinition)10 Result (org.jooq.Result)9 CatalogDefinition (org.jooq.meta.CatalogDefinition)9 EnumDefinition (org.jooq.meta.EnumDefinition)9 RoutineDefinition (org.jooq.meta.RoutineDefinition)9 Arrays.asList (java.util.Arrays.asList)8 TableType (org.jooq.TableOptions.TableType)8 AbstractIndexDefinition (org.jooq.meta.AbstractIndexDefinition)8 DefaultCheckConstraintDefinition (org.jooq.meta.DefaultCheckConstraintDefinition)8 PackageDefinition (org.jooq.meta.PackageDefinition)8 SequenceDefinition (org.jooq.meta.SequenceDefinition)8