Search in sources :

Example 1 with UUID

use of io.micronaut.data.annotation.GeneratedValue.Type.UUID in project micronaut-data by micronaut-projects.

the class SqlQueryBuilder method addTypeToColumn.

private String addTypeToColumn(PersistentProperty prop, String column, boolean required) {
    if (prop instanceof Association) {
        throw new IllegalStateException("Association is not supported here");
    }
    AnnotationMetadata annotationMetadata = prop.getAnnotationMetadata();
    String definition = annotationMetadata.stringValue(MappedProperty.class, "definition").orElse(null);
    DataType dataType = prop.getDataType();
    if (definition != null) {
        return column + " " + definition;
    }
    OptionalInt precision = annotationMetadata.intValue("javax.persistence.Column", "precision");
    OptionalInt scale = annotationMetadata.intValue("javax.persistence.Column", "scale");
    switch(dataType) {
        case STRING:
            int stringLength = annotationMetadata.findAnnotation("javax.validation.constraints.Size$List").flatMap(v -> {
                Optional value = v.getValue(AnnotationValue.class);
                return (Optional<AnnotationValue<Annotation>>) value;
            }).map(v -> v.intValue("max")).orElseGet(() -> annotationMetadata.intValue("javax.persistence.Column", "length")).orElse(255);
            column += " VARCHAR(" + stringLength + ")";
            if (required) {
                column += " NOT NULL";
            }
            break;
        case UUID:
            if (dialect == Dialect.ORACLE || dialect == Dialect.MYSQL) {
                column += " VARCHAR(36)";
            } else if (dialect == Dialect.SQL_SERVER) {
                column += " UNIQUEIDENTIFIER";
            } else {
                column += " UUID";
            }
            if (required) {
                column += " NOT NULL";
            }
            break;
        case BOOLEAN:
            if (dialect == Dialect.ORACLE) {
                column += " NUMBER(3)";
            } else if (dialect == Dialect.SQL_SERVER) {
                column += " BIT NOT NULL";
            } else {
                column += " BOOLEAN";
                if (required) {
                    column += " NOT NULL";
                }
            }
            break;
        case TIMESTAMP:
            if (dialect == Dialect.ORACLE) {
                column += " TIMESTAMP";
                if (required) {
                    column += " NOT NULL";
                }
            } else if (dialect == Dialect.SQL_SERVER) {
                // sql server timestamp is an internal type, use datetime instead
                column += " DATETIME2";
                if (required) {
                    column += " NOT NULL";
                }
            } else if (dialect == Dialect.MYSQL) {
                // mysql doesn't allow timestamp without default
                column += " TIMESTAMP(6) DEFAULT NOW(6)";
            } else {
                column += " TIMESTAMP";
                if (required) {
                    column += " NOT NULL";
                }
            }
            break;
        case DATE:
            column += " DATE";
            if (required) {
                column += " NOT NULL";
            }
            break;
        case LONG:
            if (dialect == Dialect.ORACLE) {
                column += " NUMBER(19)";
            } else {
                column += " BIGINT";
            }
            if (required) {
                column += " NOT NULL";
            }
            break;
        case CHARACTER:
            column += " CHAR(1)";
            if (required) {
                column += " NOT NULL";
            }
            break;
        case INTEGER:
            if (precision.isPresent()) {
                String numericName = dialect == Dialect.ORACLE ? "NUMBER" : "NUMERIC";
                column += " " + numericName + "(" + precision.getAsInt() + ")";
            } else if (dialect == Dialect.ORACLE) {
                column += " NUMBER(10)";
            } else if (dialect == Dialect.POSTGRES) {
                column += " INTEGER";
            } else {
                column += " INT";
            }
            if (required) {
                column += " NOT NULL";
            }
            break;
        case BIGDECIMAL:
            if (precision.isPresent()) {
                if (scale.isPresent()) {
                    String numericName = dialect == Dialect.ORACLE ? "NUMBER" : "NUMERIC";
                    column += " " + numericName + "(" + precision.getAsInt() + "," + scale.getAsInt() + ")";
                } else {
                    column += " FLOAT(" + precision.getAsInt() + ")";
                }
            } else if (dialect == Dialect.ORACLE) {
                column += " FLOAT(126)";
            } else {
                column += " DECIMAL";
            }
            if (required) {
                column += " NOT NULL";
            }
            break;
        case FLOAT:
            if (precision.isPresent()) {
                if (scale.isPresent()) {
                    String numericName = dialect == Dialect.ORACLE ? "NUMBER" : "NUMERIC";
                    column += " " + numericName + "(" + precision.getAsInt() + "," + scale.getAsInt() + ")";
                } else {
                    column += " FLOAT(" + precision.getAsInt() + ")";
                }
            } else if (dialect == Dialect.ORACLE || dialect == Dialect.SQL_SERVER) {
                column += " FLOAT(53)";
            } else if (dialect == Dialect.POSTGRES) {
                column += " REAL";
            } else {
                column += " FLOAT";
            }
            if (required) {
                column += " NOT NULL";
            }
            break;
        case BYTE_ARRAY:
            if (dialect == Dialect.POSTGRES) {
                column += " BYTEA";
            } else if (dialect == Dialect.SQL_SERVER) {
                column += " VARBINARY(MAX)";
            } else if (dialect == Dialect.ORACLE) {
                column += " BLOB";
            } else {
                column += " BLOB";
            }
            if (required) {
                column += " NOT NULL";
            }
            break;
        case DOUBLE:
            if (precision.isPresent()) {
                if (scale.isPresent()) {
                    String numericName = dialect == Dialect.ORACLE ? "NUMBER" : "NUMERIC";
                    column += " " + numericName + "(" + precision.getAsInt() + "," + scale.getAsInt() + ")";
                } else {
                    column += " FLOAT(" + precision.getAsInt() + ")";
                }
            } else if (dialect == Dialect.ORACLE) {
                column += " FLOAT(23)";
            } else if (dialect == Dialect.MYSQL || dialect == Dialect.H2) {
                column += " DOUBLE";
            } else {
                column += " DOUBLE PRECISION";
            }
            if (required) {
                column += " NOT NULL";
            }
            break;
        case SHORT:
        case BYTE:
            if (dialect == Dialect.ORACLE) {
                column += " NUMBER(5)";
            } else if (dialect == Dialect.POSTGRES) {
                column += " SMALLINT";
            } else {
                column += " TINYINT";
            }
            if (required) {
                column += " NOT NULL";
            }
            break;
        case JSON:
            switch(dialect) {
                case POSTGRES:
                    column += " JSONB";
                    break;
                case SQL_SERVER:
                    column += " NVARCHAR(MAX)";
                    break;
                case ORACLE:
                    column += " CLOB";
                    break;
                default:
                    column += " JSON";
                    break;
            }
            if (required) {
                column += " NOT NULL";
            }
            break;
        case STRING_ARRAY:
        case CHARACTER_ARRAY:
            if (dialect == Dialect.H2) {
                column += " ARRAY";
            } else {
                column += " VARCHAR(255) ARRAY";
            }
            if (required) {
                column += " NOT NULL";
            }
            break;
        case SHORT_ARRAY:
            if (dialect == Dialect.H2) {
                column += " ARRAY";
            } else if (dialect == Dialect.POSTGRES) {
                column += " SMALLINT ARRAY";
            } else {
                column += " TINYINT ARRAY";
            }
            if (required) {
                column += " NOT NULL";
            }
            break;
        case INTEGER_ARRAY:
            if (dialect == Dialect.H2) {
                column += " ARRAY";
            } else if (dialect == Dialect.POSTGRES) {
                column += " INTEGER ARRAY";
            } else {
                column += " INT ARRAY";
            }
            if (required) {
                column += " NOT NULL";
            }
            break;
        case LONG_ARRAY:
            if (dialect == Dialect.H2) {
                column += " ARRAY";
            } else {
                column += " BIGINT ARRAY";
            }
            if (required) {
                column += " NOT NULL";
            }
            break;
        case FLOAT_ARRAY:
            if (dialect == Dialect.H2) {
                column += " ARRAY";
            } else if (dialect == Dialect.POSTGRES) {
                column += " REAL ARRAY";
            } else {
                column += " FLOAT ARRAY";
            }
            if (required) {
                column += " NOT NULL";
            }
            break;
        case DOUBLE_ARRAY:
            if (dialect == Dialect.H2) {
                column += " ARRAY";
            } else if (dialect == Dialect.POSTGRES) {
                column += " DOUBLE PRECISION ARRAY";
            } else {
                column += " DOUBLE ARRAY";
            }
            if (required) {
                column += " NOT NULL";
            }
            break;
        case BOOLEAN_ARRAY:
            if (dialect == Dialect.H2) {
                column += " ARRAY";
            } else {
                column += " BOOLEAN ARRAY";
            }
            if (required) {
                column += " NOT NULL";
            }
            break;
        default:
            if (prop.isEnum()) {
                column += " VARCHAR(255)";
                if (required) {
                    column += " NOT NULL";
                }
                break;
            } else if (prop.isAssignable(Clob.class)) {
                if (dialect == Dialect.POSTGRES) {
                    column += " TEXT";
                } else {
                    column += " CLOB";
                }
                if (required) {
                    column += " NOT NULL";
                }
                break;
            } else if (prop.isAssignable(Blob.class)) {
                if (dialect == Dialect.POSTGRES) {
                    column += " BYTEA";
                } else {
                    column += " BLOB";
                }
                if (required) {
                    column += " NOT NULL";
                }
                break;
            } else {
                throw new MappingException("Unable to create table column for property [" + prop.getName() + "] of entity [" + prop.getOwner().getName() + "] with unknown data type: " + dataType);
            }
    }
    return column;
}
Also used : DataType(io.micronaut.data.model.DataType) SqlMembers(io.micronaut.data.annotation.sql.SqlMembers) Arrays(java.util.Arrays) IDENTITY(io.micronaut.data.annotation.GeneratedValue.Type.IDENTITY) ListIterator(java.util.ListIterator) ArrayUtils(io.micronaut.core.util.ArrayUtils) SEQUENCE(io.micronaut.data.annotation.GeneratedValue.Type.SEQUENCE) MappedProperty(io.micronaut.data.annotation.MappedProperty) GeneratedValue(io.micronaut.data.annotation.GeneratedValue) Locale(java.util.Locale) Map(java.util.Map) QueryResult(io.micronaut.data.model.query.builder.QueryResult) ArgumentUtils(io.micronaut.core.util.ArgumentUtils) PersistentPropertyPath(io.micronaut.data.model.PersistentPropertyPath) PersistentProperty(io.micronaut.data.model.PersistentProperty) Index(io.micronaut.data.annotation.Index) MappingException(io.micronaut.data.exceptions.MappingException) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) AUTO(io.micronaut.data.annotation.GeneratedValue.Type.AUTO) StringUtils(io.micronaut.core.util.StringUtils) AbstractSqlLikeQueryBuilder(io.micronaut.data.model.query.builder.AbstractSqlLikeQueryBuilder) List(java.util.List) Stream(java.util.stream.Stream) UUID(io.micronaut.data.annotation.GeneratedValue.Type.UUID) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) Experimental(io.micronaut.core.annotation.Experimental) Pattern(java.util.regex.Pattern) Relation(io.micronaut.data.annotation.Relation) IntStream(java.util.stream.IntStream) QueryParameterBinding(io.micronaut.data.model.query.builder.QueryParameterBinding) Join(io.micronaut.data.annotation.Join) Creator(io.micronaut.core.annotation.Creator) QueryModel(io.micronaut.data.model.query.QueryModel) MappedEntity(io.micronaut.data.annotation.MappedEntity) HashMap(java.util.HashMap) OptionalInt(java.util.OptionalInt) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Embedded(io.micronaut.data.model.Embedded) Pageable(io.micronaut.data.model.Pageable) BiConsumer(java.util.function.BiConsumer) Clob(java.sql.Clob) NamingStrategy(io.micronaut.data.model.naming.NamingStrategy) Indexes(io.micronaut.data.annotation.Indexes) NonNull(io.micronaut.core.annotation.NonNull) JoinPath(io.micronaut.data.model.query.JoinPath) Association(io.micronaut.data.model.Association) PersistentEntity(io.micronaut.data.model.PersistentEntity) CollectionUtils(io.micronaut.core.util.CollectionUtils) StringJoiner(java.util.StringJoiner) QueryBuilder(io.micronaut.data.model.query.builder.QueryBuilder) Repository(io.micronaut.data.annotation.Repository) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) Blob(java.sql.Blob) Collections(java.util.Collections) Optional(java.util.Optional) OptionalInt(java.util.OptionalInt) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) MappingException(io.micronaut.data.exceptions.MappingException) Association(io.micronaut.data.model.Association) MappedProperty(io.micronaut.data.annotation.MappedProperty) DataType(io.micronaut.data.model.DataType) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) Clob(java.sql.Clob)

Aggregations

AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)1 AnnotationValue (io.micronaut.core.annotation.AnnotationValue)1 Creator (io.micronaut.core.annotation.Creator)1 Experimental (io.micronaut.core.annotation.Experimental)1 NonNull (io.micronaut.core.annotation.NonNull)1 ArgumentUtils (io.micronaut.core.util.ArgumentUtils)1 ArrayUtils (io.micronaut.core.util.ArrayUtils)1 CollectionUtils (io.micronaut.core.util.CollectionUtils)1 StringUtils (io.micronaut.core.util.StringUtils)1 GeneratedValue (io.micronaut.data.annotation.GeneratedValue)1 AUTO (io.micronaut.data.annotation.GeneratedValue.Type.AUTO)1 IDENTITY (io.micronaut.data.annotation.GeneratedValue.Type.IDENTITY)1 SEQUENCE (io.micronaut.data.annotation.GeneratedValue.Type.SEQUENCE)1 UUID (io.micronaut.data.annotation.GeneratedValue.Type.UUID)1 Index (io.micronaut.data.annotation.Index)1 Indexes (io.micronaut.data.annotation.Indexes)1 Join (io.micronaut.data.annotation.Join)1 MappedEntity (io.micronaut.data.annotation.MappedEntity)1 MappedProperty (io.micronaut.data.annotation.MappedProperty)1 Relation (io.micronaut.data.annotation.Relation)1