Search in sources :

Example 1 with SQLDataType

use of org.jooq.impl.SQLDataType 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)

Aggregations

SQLDialectNotSupportedException (org.jooq.exception.SQLDialectNotSupportedException)1 DSL (org.jooq.impl.DSL)1 SQLDataType (org.jooq.impl.SQLDataType)1 ArrayDefinition (org.jooq.meta.ArrayDefinition)1 DefaultDataTypeDefinition (org.jooq.meta.DefaultDataTypeDefinition)1