Search in sources :

Example 1 with JavaTypeResolver

use of org.jooq.meta.JavaTypeResolver 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)1 Collectors (java.util.stream.Collectors)1 AbstractTypedElementDefinition (org.jooq.meta.AbstractTypedElementDefinition)1 ArrayDefinition (org.jooq.meta.ArrayDefinition)1 AttributeDefinition (org.jooq.meta.AttributeDefinition)1 CatalogDefinition (org.jooq.meta.CatalogDefinition)1 CheckConstraintDefinition (org.jooq.meta.CheckConstraintDefinition)1 ColumnDefinition (org.jooq.meta.ColumnDefinition)1 ConstraintDefinition (org.jooq.meta.ConstraintDefinition)1 DataTypeDefinition (org.jooq.meta.DataTypeDefinition)1 DefaultDataTypeDefinition (org.jooq.meta.DefaultDataTypeDefinition)1 Definition (org.jooq.meta.Definition)1 DomainDefinition (org.jooq.meta.DomainDefinition)1 EmbeddableColumnDefinition (org.jooq.meta.EmbeddableColumnDefinition)1 EmbeddableDefinition (org.jooq.meta.EmbeddableDefinition)1 EnumDefinition (org.jooq.meta.EnumDefinition)1 ForeignKeyDefinition (org.jooq.meta.ForeignKeyDefinition)1 IdentityDefinition (org.jooq.meta.IdentityDefinition)1 IndexColumnDefinition (org.jooq.meta.IndexColumnDefinition)1 IndexDefinition (org.jooq.meta.IndexDefinition)1