Search in sources :

Example 1 with ArrayDefinition

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

the class JavaGenerator method getTypeReference.

protected String getTypeReference(Database db, SchemaDefinition schema, JavaWriter out, String t, int p, int s, int l, boolean n, boolean i, boolean r, String g, GenerationOption go, String d, Name u) {
    StringBuilder sb = new StringBuilder();
    if (db.getArray(schema, u) != null) {
        ArrayDefinition array = database.getArray(schema, u);
        sb.append(getJavaTypeReference(db, array.getElementType(resolver(out)), out));
        sb.append(array.getIndexType() != null ? ".asAssociativeArrayDataType(" : ".asArrayDataType(");
        sb.append(classOf(getStrategy().getFullJavaClassName(array, Mode.RECORD)));
        sb.append(")");
    } else if (db.getDomain(schema, u) != null) {
        sb.append(getStrategy().getFullJavaIdentifier(db.getDomain(schema, u)));
        sb.append(".getDataType()");
    } else if (db.getUDT(schema, u) != null) {
        sb.append(getStrategy().getFullJavaIdentifier(db.getUDT(schema, u)));
        sb.append(".getDataType()");
    } else // [#5334] In MySQL, the user type is (ab)used for synthetic enum types. This can lead to accidental matches here
    if (SUPPORT_TABLE_AS_UDT.contains(db.getDialect()) && db.getTable(schema, u) != null) {
        sb.append(getStrategy().getFullJavaIdentifier(db.getTable(schema, u)));
        sb.append(".getDataType()");
    } else if (db.getEnum(schema, u) != null) {
        sb.append(getJavaTypeReference(db, new DefaultDataTypeDefinition(db, schema, DefaultDataType.getDataType(db.getDialect(), String.class).getTypeName(), l, p, s, n, d, (Name) null), out));
        sb.append(".asEnumDataType(");
        sb.append(classOf(getStrategy().getFullJavaClassName(db.getEnum(schema, u), Mode.ENUM)));
        sb.append(")");
    } else {
        DataType<?> dataType;
        String sqlDataTypeRef;
        try {
            dataType = mapJavaTimeTypes(getDataType(db, t, p, s));
        }// Mostly because of unsupported data types.
         catch (SQLDialectNotSupportedException ignore) {
            dataType = SQLDataType.OTHER.nullable(n).identity(i);
            sb = new StringBuilder();
            sb.append(DefaultDataType.class.getName());
            sb.append(".getDefaultDataType(\"");
            sb.append(escapeString(u != null ? u.toString() : t));
            sb.append("\")");
        }
        dataType = dataType.nullable(n).identity(i);
        if (d != null)
            dataType = dataType.defaultValue((Field) DSL.field(d, dataType));
        // specific DataType t, then reference that one.
        if (dataType.getSQLDataType() != null && sb.length() == 0) {
            DataType<?> sqlDataType = dataType.getSQLDataType();
            String literal = SQLDATATYPE_LITERAL_LOOKUP.get(sqlDataType);
            sqlDataTypeRef = out.ref(SQLDataType.class) + '.' + (literal == null ? "OTHER" : literal);
            sb.append(sqlDataTypeRef);
            if (dataType.hasPrecision() && (dataType.isTimestamp() || p > 0)) {
                // [#6411] Call static method if available, rather than instance method
                if (SQLDATATYPE_WITH_PRECISION.contains(literal))
                    sb.append('(').append(p);
                else
                    sb.append(".precision(").append(p);
                if (dataType.hasScale() && s > 0)
                    sb.append(", ").append(s);
                sb.append(')');
            }
            if (dataType.hasLength() && l > 0)
                // [#6411] Call static method if available, rather than instance method
                if (SQLDATATYPE_WITH_LENGTH.contains(literal))
                    sb.append("(").append(l).append(")");
                else
                    sb.append(".length(").append(l).append(")");
        } else {
            sqlDataTypeRef = SQLDataType.class.getCanonicalName() + ".OTHER";
            if (sb.length() == 0)
                sb.append(sqlDataTypeRef);
        }
        if (!dataType.nullable())
            sb.append(".nullable(false)");
        if (dataType.identity())
            sb.append(".identity(true)");
        // report actual values (e.g. MySQL).
        if (dataType.defaulted()) {
            sb.append(".defaultValue(");
            if (asList(MYSQL).contains(db.getDialect().family()))
                // a CURRENT_TIMESTAMP expression, inconsistently
                if (d != null && d.toLowerCase(getStrategy().getTargetLocale()).startsWith("current_timestamp"))
                    sb.append(out.ref(DSL.class)).append(".field(\"").append(escapeString(d)).append("\"");
                else
                    sb.append(out.ref(DSL.class)).append(".inline(\"").append(escapeString(d)).append("\"");
            else
                sb.append(out.ref(DSL.class)).append(".field(\"").append(escapeString(d)).append("\"");
            sb.append(", ").append(sqlDataTypeRef).append(")").append(kotlin && dataType.getType() == Object.class ? " as Any?" : "").append(")");
        }
    }
    return sb.toString();
}
Also used : SQLDialectNotSupportedException(org.jooq.exception.SQLDialectNotSupportedException) SQLDataType(org.jooq.impl.SQLDataType) DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition) ArrayDefinition(org.jooq.meta.ArrayDefinition) DSL(org.jooq.impl.DSL)

Example 2 with ArrayDefinition

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

the class JavaGenerator method generateRecordSetter0.

private final void generateRecordSetter0(TypedElementDefinition<?> column, int index, JavaWriter out) {
    final String className = getStrategy().getJavaClassName(column.getContainer(), Mode.RECORD);
    // [#12459] Kotlin setters must return Unit
    final String setterReturnType = generateFluentSetters() && !kotlin ? className : tokenVoid;
    final String setter = getStrategy().getJavaSetterName(column, Mode.RECORD);
    final String member = getStrategy().getJavaMemberName(column, Mode.POJO);
    final String typeFull = getJavaType(column.getType(resolver(out)), out);
    final String type = out.ref(typeFull);
    final String name = column.getQualifiedOutputName();
    final boolean isUDT = column.getType(resolver(out)).isUDT();
    final boolean isArray = column.getType(resolver(out)).isArray();
    final boolean isUDTArray = column.getType(resolver(out)).isUDTArray();
    final boolean override = generateInterfaces() && !generateImmutableInterfaces() && !isUDT;
    // We cannot have covariant setters for arrays because of type erasure
    if (!(generateInterfaces() && isArray)) {
        if (!kotlin && !printDeprecationIfUnknownType(out, typeFull))
            out.javadoc("Setter for <code>%s</code>.[[before= ][%s]]", name, list(escapeEntities(comment(column))));
        if (scala) {
            out.println("%sdef %s(value: %s): %s = {", visibility(override), setter, type, setterReturnType);
            out.println("set(%s, value)", index);
            if (generateFluentSetters())
                out.println("this");
            out.println("}");
        } else if (kotlin) {
            out.println();
            if (column instanceof ColumnDefinition)
                printColumnJPAAnnotation(out, (ColumnDefinition) column);
            printValidationAnnotation(out, column);
            printKotlinSetterAnnotation(out, column, Mode.RECORD);
            out.println("%s%svar %s: %s?", visibility(generateInterfaces()), (generateInterfaces() ? "override " : ""), member, type);
            out.tab(1).println("set(value): %s = set(%s, value)", setterReturnType, index);
        } else {
            final String nullableAnnotation = nullableOrNonnullAnnotation(out, column);
            out.overrideIf(override);
            out.println("%s%s %s([[before=@][after= ][%s]]%s value) {", visibility(override), setterReturnType, setter, list(nullableAnnotation), varargsIfArray(type));
            out.println("set(%s, value);", index);
            if (generateFluentSetters())
                out.println("return this;");
            out.println("}");
        }
    }
    // [#3117] Avoid covariant setters for UDTs when generating interfaces
    if (generateInterfaces() && !generateImmutableInterfaces() && (isUDT || isArray)) {
        final String columnTypeFull = getJavaType(column.getType(resolver(out, Mode.RECORD)), out, Mode.RECORD);
        final String columnType = out.ref(columnTypeFull);
        final String columnTypeInterface = out.ref(getJavaType(column.getType(resolver(out, Mode.INTERFACE)), out, Mode.INTERFACE));
        if (!printDeprecationIfUnknownType(out, columnTypeFull))
            out.javadoc("Setter for <code>%s</code>.[[before= ][%s]]", name, list(escapeEntities(comment(column))));
        out.override();
        if (scala) {
            // [#3082] TODO Handle <interfaces/> + ARRAY also for Scala
            out.println("%sdef %s(value: %s): %s = {", visibility(), setter, columnTypeInterface, setterReturnType);
            out.println("if (value == null)");
            out.println("set(%s, null)", index);
            out.println("else");
            out.println("set(%s, value.into(new %s()))", index, type);
            if (generateFluentSetters())
                out.println("this");
            out.println("}");
        } else // TODO
        if (kotlin) {
        } else {
            final String nullableAnnotation = nullableOrNonnullAnnotation(out, column);
            out.println("%s%s %s([[before=@][after= ][%s]]%s value) {", visibility(), setterReturnType, setter, list(nullableAnnotation), varargsIfArray(columnTypeInterface));
            out.println("if (value == null)");
            out.println("set(%s, null);", index);
            if (isUDT) {
                out.println("else");
                out.println("set(%s, value.into(new %s()));", index, type);
            } else if (isArray) {
                final ArrayDefinition array = database.getArray(column.getType(resolver(out)).getSchema(), column.getType(resolver(out)).getQualifiedUserType());
                final String componentType = out.ref(getJavaType(array.getElementType(resolver(out, Mode.RECORD)), out, Mode.RECORD));
                final String componentTypeInterface = out.ref(getJavaType(array.getElementType(resolver(out, Mode.INTERFACE)), out, Mode.INTERFACE));
                out.println("else {");
                out.println("%s a = new %s();", columnType, columnType);
                out.println();
                out.println("for (%s i : value)", componentTypeInterface);
                if (isUDTArray)
                    out.println("a.add(i.into(new %s()));", componentType);
                else
                    out.println("a.add(i);", componentType);
                out.println();
                out.println("set(1, a);");
                out.println("}");
            }
            if (generateFluentSetters())
                out.println("return this;");
            out.println("}");
        }
    }
}
Also used : ArrayDefinition(org.jooq.meta.ArrayDefinition) ColumnDefinition(org.jooq.meta.ColumnDefinition) IndexColumnDefinition(org.jooq.meta.IndexColumnDefinition) EmbeddableColumnDefinition(org.jooq.meta.EmbeddableColumnDefinition)

Example 3 with ArrayDefinition

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

the class JavaGenerator method generateArrays.

protected void generateArrays(SchemaDefinition schema) {
    log.info("Generating ARRAYs");
    for (ArrayDefinition array : database.getArrays(schema)) {
        try {
            generateArray(schema, array);
        } catch (Exception e) {
            log.error("Error while generating ARRAY record " + array, e);
        }
    }
    watch.splitInfo("ARRAYs generated");
}
Also used : ArrayDefinition(org.jooq.meta.ArrayDefinition) IOException(java.io.IOException) SQLDialectNotSupportedException(org.jooq.exception.SQLDialectNotSupportedException) ReflectException(org.jooq.tools.reflect.ReflectException)

Aggregations

ArrayDefinition (org.jooq.meta.ArrayDefinition)3 SQLDialectNotSupportedException (org.jooq.exception.SQLDialectNotSupportedException)2 IOException (java.io.IOException)1 DSL (org.jooq.impl.DSL)1 SQLDataType (org.jooq.impl.SQLDataType)1 ColumnDefinition (org.jooq.meta.ColumnDefinition)1 DefaultDataTypeDefinition (org.jooq.meta.DefaultDataTypeDefinition)1 EmbeddableColumnDefinition (org.jooq.meta.EmbeddableColumnDefinition)1 IndexColumnDefinition (org.jooq.meta.IndexColumnDefinition)1 ReflectException (org.jooq.tools.reflect.ReflectException)1