Search in sources :

Example 56 with SchemaDefinition

use of org.jooq.meta.SchemaDefinition 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 57 with SchemaDefinition

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

the class JavaGenerator method getMemberNames.

private Set<String> getMemberNames(CatalogDefinition catalog) {
    Set<String> members = new HashSet<>();
    members.add(getStrategy().getJavaIdentifier(catalog));
    for (SchemaDefinition table : catalog.getSchemata()) members.add(getStrategy().getJavaIdentifier(table));
    return members;
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 58 with SchemaDefinition

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

the class FirebirdDatabase method getSequences0.

@Override
protected List<SequenceDefinition> getSequences0() throws SQLException {
    List<SequenceDefinition> result = new ArrayList<>();
    for (Record record : sequences(getInputSchemata())) {
        SchemaDefinition schema = getSchemata().get(0);
        DataTypeDefinition type = new DefaultDataTypeDefinition(this, schema, FirebirdDataType.BIGINT.getTypeName());
        result.add(new DefaultSequenceDefinition(schema, record.get(RDB$GENERATORS.RDB$GENERATOR_NAME), type, null, record.get(RDB$GENERATORS.RDB$INITIAL_VALUE), record.get(RDB$GENERATORS.RDB$GENERATOR_INCREMENT), null, null, false, null));
    }
    return result;
}
Also used : DefaultSequenceDefinition(org.jooq.meta.DefaultSequenceDefinition) SchemaDefinition(org.jooq.meta.SchemaDefinition) DefaultSequenceDefinition(org.jooq.meta.DefaultSequenceDefinition) SequenceDefinition(org.jooq.meta.SequenceDefinition) DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition) ArrayList(java.util.ArrayList) Record(org.jooq.Record) DataTypeDefinition(org.jooq.meta.DataTypeDefinition) DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition)

Example 59 with SchemaDefinition

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

the class FirebirdDatabase method getIndexes0.

@Override
protected List<IndexDefinition> getIndexes0() throws SQLException {
    final List<IndexDefinition> result = new ArrayList<>();
    final Rdb$relationConstraints c = RDB$RELATION_CONSTRAINTS.as("c");
    final Rdb$indices i = RDB$INDICES.as("i");
    final Rdb$indexSegments s = RDB$INDEX_SEGMENTS.as("s");
    Map<Record, Result<Record>> indexes = create().select(s.rdb$indices().RDB$RELATION_NAME.trim().as(i.RDB$RELATION_NAME), s.rdb$indices().RDB$INDEX_NAME.trim().as(i.RDB$INDEX_NAME), s.rdb$indices().RDB$UNIQUE_FLAG, s.RDB$FIELD_NAME.trim().as(s.RDB$FIELD_NAME), s.RDB$FIELD_POSITION).from(s).where(s.rdb$indices().RDB$INDEX_NAME.notIn(select(c.RDB$CONSTRAINT_NAME).from(c))).orderBy(s.rdb$indices().RDB$RELATION_NAME, s.rdb$indices().RDB$INDEX_NAME, s.RDB$FIELD_POSITION).fetchGroups(new Field[] { i.RDB$RELATION_NAME, i.RDB$INDEX_NAME, i.RDB$UNIQUE_FLAG }, new Field[] { s.RDB$FIELD_NAME, s.RDB$FIELD_POSITION });
    indexLoop: for (Entry<Record, Result<Record>> entry : indexes.entrySet()) {
        final Record index = entry.getKey();
        final Result<Record> columns = entry.getValue();
        final SchemaDefinition schema = getSchemata().get(0);
        final String indexName = index.get(i.RDB$INDEX_NAME);
        final String tableName = index.get(i.RDB$RELATION_NAME);
        final TableDefinition table = getTable(schema, tableName);
        if (table == null)
            continue indexLoop;
        final boolean unique = index.get(i.RDB$UNIQUE_FLAG, boolean.class);
        // [#6310] [#6620] Function-based indexes are not yet supported
        for (Record column : columns) if (table.getColumn(column.get(s.RDB$FIELD_NAME)) == null)
            continue indexLoop;
        result.add(new AbstractIndexDefinition(schema, indexName, table, unique) {

            List<IndexColumnDefinition> indexColumns = new ArrayList<>();

            {
                for (Record column : columns) {
                    indexColumns.add(new DefaultIndexColumnDefinition(this, table.getColumn(column.get(s.RDB$FIELD_NAME)), SortOrder.ASC, column.get(s.RDB$FIELD_POSITION, int.class)));
                }
            }

            @Override
            protected List<IndexColumnDefinition> getIndexColumns0() {
                return indexColumns;
            }
        });
    }
    return result;
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) ArrayList(java.util.ArrayList) Rdb$indexSegments(org.jooq.meta.firebird.rdb.tables.Rdb$indexSegments) Result(org.jooq.Result) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) IndexColumnDefinition(org.jooq.meta.IndexColumnDefinition) Entry(java.util.Map.Entry) AbstractIndexDefinition(org.jooq.meta.AbstractIndexDefinition) IndexDefinition(org.jooq.meta.IndexDefinition) AbstractIndexDefinition(org.jooq.meta.AbstractIndexDefinition) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) Rdb$indices(org.jooq.meta.firebird.rdb.tables.Rdb$indices) TableDefinition(org.jooq.meta.TableDefinition) Record(org.jooq.Record) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) Rdb$relationConstraints(org.jooq.meta.firebird.rdb.tables.Rdb$relationConstraints)

Example 60 with SchemaDefinition

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

the class FirebirdDatabase method loadCheckConstraints.

@Override
protected void loadCheckConstraints(DefaultRelations relations) throws SQLException {
    Rdb$relationConstraints r = RDB$RELATION_CONSTRAINTS.as("r");
    Rdb$checkConstraints c = RDB$CHECK_CONSTRAINTS.as("c");
    Rdb$triggers t = RDB$TRIGGERS.as("t");
    // for RDB$TRIGGER_TYPE 1 (before insert) and 3 (before update)
    for (Record record : create().select(r.RDB$RELATION_NAME.trim().as(r.RDB$RELATION_NAME), r.RDB$CONSTRAINT_NAME.trim().as(r.RDB$CONSTRAINT_NAME), max(t.RDB$TRIGGER_SOURCE.trim()).as(t.RDB$TRIGGER_SOURCE)).from(r).join(c).on(r.RDB$CONSTRAINT_NAME.eq(c.RDB$CONSTRAINT_NAME)).join(t).on(c.RDB$TRIGGER_NAME.eq(t.RDB$TRIGGER_NAME)).where(r.RDB$CONSTRAINT_TYPE.eq(inline("CHECK"))).groupBy(r.RDB$RELATION_NAME, r.RDB$CONSTRAINT_NAME).orderBy(r.RDB$RELATION_NAME, r.RDB$CONSTRAINT_NAME)) {
        SchemaDefinition schema = getSchemata().get(0);
        TableDefinition table = getTable(schema, record.get(r.RDB$RELATION_NAME));
        if (table != null) {
            relations.addCheckConstraint(table, new DefaultCheckConstraintDefinition(schema, table, record.get(r.RDB$CONSTRAINT_NAME), record.get(t.RDB$TRIGGER_SOURCE)));
        }
    }
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) TableDefinition(org.jooq.meta.TableDefinition) Record(org.jooq.Record) Rdb$relationConstraints(org.jooq.meta.firebird.rdb.tables.Rdb$relationConstraints) Rdb$checkConstraints(org.jooq.meta.firebird.rdb.tables.Rdb$checkConstraints) DefaultCheckConstraintDefinition(org.jooq.meta.DefaultCheckConstraintDefinition) Rdb$triggers(org.jooq.meta.firebird.rdb.tables.Rdb$triggers)

Aggregations

SchemaDefinition (org.jooq.meta.SchemaDefinition)81 Record (org.jooq.Record)51 ArrayList (java.util.ArrayList)46 TableDefinition (org.jooq.meta.TableDefinition)44 DefaultDataTypeDefinition (org.jooq.meta.DefaultDataTypeDefinition)22 DataTypeDefinition (org.jooq.meta.DataTypeDefinition)21 ColumnDefinition (org.jooq.meta.ColumnDefinition)14 IndexColumnDefinition (org.jooq.meta.IndexColumnDefinition)14 IndexDefinition (org.jooq.meta.IndexDefinition)12 SequenceDefinition (org.jooq.meta.SequenceDefinition)12 List (java.util.List)10 DefaultIndexColumnDefinition (org.jooq.meta.DefaultIndexColumnDefinition)10 DomainDefinition (org.jooq.meta.DomainDefinition)9 RoutineDefinition (org.jooq.meta.RoutineDefinition)9 Result (org.jooq.Result)8 AbstractIndexDefinition (org.jooq.meta.AbstractIndexDefinition)8 DefaultCheckConstraintDefinition (org.jooq.meta.DefaultCheckConstraintDefinition)8 DefaultSequenceDefinition (org.jooq.meta.DefaultSequenceDefinition)8 PackageDefinition (org.jooq.meta.PackageDefinition)8 TableType (org.jooq.TableOptions.TableType)7