Search in sources :

Example 6 with Definition

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

the class JavaGenerator method disambiguateJavaMemberName.

private String disambiguateJavaMemberName(Collection<? extends Definition> definitions, String defaultName) {
    // [#2502] - Some name mangling in the event of procedure arguments
    // called "configuration"
    Set<String> names = new HashSet<>();
    for (Definition definition : definitions) names.add(getStrategy().getJavaMemberName(definition));
    String name = defaultName;
    while (names.contains(name)) name += "_";
    return name;
}
Also used : 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) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 7 with Definition

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

the class JavaGenerator method generateRecordConstructor.

private void generateRecordConstructor(Definition tableUdtOrEmbeddable, JavaWriter out, Collection<? extends Definition> columns, boolean pojoArgument) {
    if (pojoArgument && !generatePojos())
        return;
    final String className = getStrategy().getJavaClassName(tableUdtOrEmbeddable, Mode.RECORD);
    final String pojoNameFull = getStrategy().getFullJavaClassName(tableUdtOrEmbeddable, Mode.POJO);
    final String tableIdentifier = !(tableUdtOrEmbeddable instanceof EmbeddableDefinition) ? out.ref(getStrategy().getFullJavaIdentifier(tableUdtOrEmbeddable), 2) : null;
    final int degree = columns.size();
    // they can still have a pojoArgument constructor
    if (pojoArgument || degree > 0 && degree < 256) {
        List<String> arguments = new ArrayList<>(degree);
        List<String> properties = new ArrayList<>(degree);
        for (Definition column : columns) {
            final String columnMember = getStrategy().getJavaMemberName(column, Mode.DEFAULT);
            final String type = getJavaTypeRef(column, out);
            if (scala) {
                arguments.add(columnMember + " : " + type);
            } else if (kotlin) {
                arguments.add(columnMember + ": " + type + "? = null");
            } else {
                final String nullableAnnotation = column instanceof EmbeddableDefinition ? null : nullableOrNonnullAnnotation(out, column);
                arguments.add((nullableAnnotation == null ? "" : "@" + nullableAnnotation + " ") + type + " " + columnMember);
            }
            properties.add("\"" + escapeString(columnMember) + "\"");
        }
        out.javadoc("Create a detached, initialised %s", className);
        if (scala) {
            if (pojoArgument)
                out.println("%sdef this(value: %s) = {", visibility(), out.ref(pojoNameFull));
            else
                out.println("%sdef this([[%s]]) = {", visibility(), arguments);
            out.println("this()", tableIdentifier);
            out.println();
        } else if (kotlin) {
            if (pojoArgument)
                out.println("%sconstructor(value: %s?): this() {", visibility(), out.ref(pojoNameFull));
            else
                out.println("%sconstructor([[%s]]): this() {", visibility(), arguments);
        } else {
            if (generateConstructorPropertiesAnnotationOnRecords())
                out.println("@%s({ [[%s]] })", out.ref("java.beans.ConstructorProperties"), properties);
            if (pojoArgument)
                out.println("%s%s(%s value) {", visibility(), className, out.ref(pojoNameFull));
            else
                out.println("%s%s([[%s]]) {", visibility(), className, arguments);
            if (tableUdtOrEmbeddable instanceof EmbeddableDefinition)
                out.println("this();", tableIdentifier);
            else
                out.println("super(%s);", tableIdentifier);
            out.println();
        }
        if (pojoArgument && degree > 0)
            out.println("if (value != null) {");
        for (Definition column : columns) {
            if (column instanceof EmbeddableDefinition) {
                // TODO: Setters of X properties cannot accept X? in Kotlin: https://twitter.com/lukaseder/status/1296371561214234624
                if (kotlin)
                    if (pojoArgument)
                        out.println("this.%s = %s(%s) ?: %s([[%s]])", getStrategy().getJavaMemberName(column, Mode.POJO), out.ref(getStrategy().getFullJavaClassName(column, Mode.RECORD)), getStrategy().getJavaMemberName(column, Mode.POJO), out.ref(getStrategy().getFullJavaClassName(column, Mode.RECORD)), Collections.nCopies(((EmbeddableDefinition) column).getColumns().size(), "null"));
                    else
                        out.println("this.%s = %s ?: %s([[%s]])", getStrategy().getJavaMemberName(column, Mode.POJO), getStrategy().getJavaMemberName(column, Mode.POJO), out.ref(getStrategy().getFullJavaClassName(column, Mode.RECORD)), Collections.nCopies(((EmbeddableDefinition) column).getColumns().size(), "null"));
                else // In Scala, the setter call can be ambiguous, e.g. when using KeepNamesGeneratorStrategy
                if (scala)
                    if (pojoArgument)
                        out.println("this.%s(new %s(value.%s))", getStrategy().getJavaSetterName(column, Mode.RECORD), out.ref(getStrategy().getFullJavaClassName(column, Mode.RECORD)), getStrategy().getJavaGetterName(column, Mode.DEFAULT));
                    else
                        out.println("this.%s(%s)", getStrategy().getJavaSetterName(column, Mode.RECORD), getStrategy().getJavaMemberName(column, Mode.DEFAULT));
                else if (pojoArgument)
                    out.println("%s(new %s(value.%s()));", getStrategy().getJavaSetterName(column, Mode.RECORD), out.ref(getStrategy().getFullJavaClassName(column, Mode.RECORD)), generatePojosAsJavaRecordClasses() ? getStrategy().getJavaMemberName(column, Mode.POJO) : getStrategy().getJavaGetterName(column, Mode.DEFAULT));
                else
                    out.println("%s(%s);", getStrategy().getJavaSetterName(column, Mode.RECORD), getStrategy().getJavaMemberName(column, Mode.DEFAULT));
            } else {
                final TypedElementDefinition<?> t = (TypedElementDefinition<?>) column;
                final JavaTypeResolver r = resolver(out, Mode.RECORD);
                final boolean isUDT = t.getType(r).isUDT();
                final boolean isArray = t.getType(r).isArray();
                final boolean isUDTArray = t.getType(r).isUDTArray();
                final boolean isArrayOfUDTs = isArrayOfUDTs(t, r);
                final String udtType = (isUDT || isArray) ? out.ref(getJavaType(((TypedElementDefinition<?>) column).getType(r), out, Mode.RECORD)) : "";
                final String udtArrayElementType = isUDTArray ? out.ref(database.getArray(t.getType(r).getSchema(), t.getType(r).getQualifiedUserType()).getElementType(r).getJavaType(r)) : isArrayOfUDTs ? out.ref(getArrayBaseType(t.getType(r).getJavaType(r))) : "";
                if (kotlin) {
                    if (pojoArgument)
                        if (isUDTArray)
                            out.println("this.%s = value.%s?.let { %s(it.map { it?.let { %s(it) } }) }", getStrategy().getJavaMemberName(column, Mode.POJO), getStrategy().getJavaMemberName(column, Mode.POJO), udtType, udtArrayElementType);
                        else if (isArrayOfUDTs)
                            out.println("this.%s = value.%s?.let { it.map { it?.let { %s(it) } }.toTypedArray() }", getStrategy().getJavaMemberName(column, Mode.POJO), getStrategy().getJavaMemberName(column, Mode.POJO), udtArrayElementType);
                        else if (isUDT || isArray)
                            out.println("this.%s = value.%s?.let { %s(it) }", getStrategy().getJavaMemberName(column, Mode.POJO), getStrategy().getJavaMemberName(column, Mode.POJO), udtType);
                        else
                            out.println("this.%s = value.%s", getStrategy().getJavaMemberName(column, Mode.POJO), getStrategy().getJavaMemberName(column, Mode.POJO));
                    else
                        out.println("this.%s = %s", getStrategy().getJavaMemberName(column, Mode.POJO), getStrategy().getJavaMemberName(column, Mode.POJO));
                } else // In Scala, the setter call can be ambiguous, e.g. when using KeepNamesGeneratorStrategy
                if (scala) {
                    if (pojoArgument)
                        if (isUDTArray)
                            out.println("this.%s(if (value.%s == null) null else new %s(value.%s.stream().map { it => new %s(it) }.collect(%s.toList())))", getStrategy().getJavaSetterName(column, Mode.POJO), getStrategy().getJavaGetterName(column, Mode.POJO), udtType, getStrategy().getJavaGetterName(column, Mode.POJO), udtArrayElementType, Collectors.class);
                        else if (isArrayOfUDTs)
                            out.println("this.%s(if (value.%s == null) null else value.%s.map { it => new %s(it) })", getStrategy().getJavaSetterName(column, Mode.POJO), getStrategy().getJavaGetterName(column, Mode.POJO), getStrategy().getJavaGetterName(column, Mode.POJO), udtArrayElementType);
                        else if (isUDT || isArray)
                            out.println("this.%s(if (value.%s == null) null else new %s(value.%s))", getStrategy().getJavaSetterName(column, Mode.RECORD), getStrategy().getJavaGetterName(column, Mode.POJO), udtType, getStrategy().getJavaGetterName(column, Mode.POJO));
                        else
                            out.println("this.%s(value.%s)", getStrategy().getJavaSetterName(column, Mode.RECORD), getStrategy().getJavaGetterName(column, Mode.POJO));
                    else
                        out.println("this.%s(%s)", getStrategy().getJavaSetterName(column, Mode.RECORD), getStrategy().getJavaMemberName(column, Mode.POJO));
                } else {
                    if (pojoArgument)
                        if (isUDTArray)
                            out.println("%s(value.%s() == null ? null : new %s(value.%s().stream().map(%s::new).collect(%s.toList())));", getStrategy().getJavaSetterName(column, Mode.RECORD), getStrategy().getJavaGetterName(column, Mode.POJO), udtType, generatePojosAsJavaRecordClasses() ? getStrategy().getJavaMemberName(column, Mode.POJO) : getStrategy().getJavaGetterName(column, Mode.POJO), udtArrayElementType, Collectors.class);
                        else if (isArrayOfUDTs)
                            out.println("%s(value.%s() == null ? null : %s.of(value.%s()).map(%s::new).toArray(%s[]::new));", getStrategy().getJavaSetterName(column, Mode.RECORD), getStrategy().getJavaGetterName(column, Mode.POJO), Stream.class, generatePojosAsJavaRecordClasses() ? getStrategy().getJavaMemberName(column, Mode.POJO) : getStrategy().getJavaGetterName(column, Mode.POJO), udtArrayElementType, udtArrayElementType);
                        else if (isUDT || isArray)
                            out.println("%s(value.%s() == null ? null : new %s(value.%s()));", getStrategy().getJavaSetterName(column, Mode.RECORD), getStrategy().getJavaGetterName(column, Mode.POJO), udtType, generatePojosAsJavaRecordClasses() ? getStrategy().getJavaMemberName(column, Mode.POJO) : getStrategy().getJavaGetterName(column, Mode.POJO));
                        else
                            out.println("%s(value.%s());", getStrategy().getJavaSetterName(column, Mode.RECORD), generatePojosAsJavaRecordClasses() ? getStrategy().getJavaMemberName(column, Mode.POJO) : getStrategy().getJavaGetterName(column, Mode.POJO));
                    else
                        out.println("%s(%s);", getStrategy().getJavaSetterName(column, Mode.RECORD), getStrategy().getJavaMemberName(column, Mode.POJO));
                }
            }
        }
        if (pojoArgument && degree > 0)
            out.println("}");
        out.println("}");
    }
}
Also used : TypedElementDefinition(org.jooq.meta.TypedElementDefinition) AbstractTypedElementDefinition(org.jooq.meta.AbstractTypedElementDefinition) JavaTypeResolver(org.jooq.meta.JavaTypeResolver) Collectors(java.util.stream.Collectors) EmbeddableDefinition(org.jooq.meta.EmbeddableDefinition) 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)

Aggregations

ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)5 LinkedHashSet (java.util.LinkedHashSet)5 AbstractTypedElementDefinition (org.jooq.meta.AbstractTypedElementDefinition)5 ArrayDefinition (org.jooq.meta.ArrayDefinition)5 AttributeDefinition (org.jooq.meta.AttributeDefinition)5 CatalogDefinition (org.jooq.meta.CatalogDefinition)5 CheckConstraintDefinition (org.jooq.meta.CheckConstraintDefinition)5 ColumnDefinition (org.jooq.meta.ColumnDefinition)5 ConstraintDefinition (org.jooq.meta.ConstraintDefinition)5 DataTypeDefinition (org.jooq.meta.DataTypeDefinition)5 DefaultDataTypeDefinition (org.jooq.meta.DefaultDataTypeDefinition)5 Definition (org.jooq.meta.Definition)5 DomainDefinition (org.jooq.meta.DomainDefinition)5 EmbeddableColumnDefinition (org.jooq.meta.EmbeddableColumnDefinition)5 EmbeddableDefinition (org.jooq.meta.EmbeddableDefinition)5 EnumDefinition (org.jooq.meta.EnumDefinition)5 ForeignKeyDefinition (org.jooq.meta.ForeignKeyDefinition)5 IdentityDefinition (org.jooq.meta.IdentityDefinition)5 IndexColumnDefinition (org.jooq.meta.IndexColumnDefinition)5