Search in sources :

Example 1 with IDENTITY

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

the class SqlQueryBuilder method buildInsert.

@NonNull
@Override
public QueryResult buildInsert(AnnotationMetadata repositoryMetadata, PersistentEntity entity) {
    boolean escape = shouldEscape(entity);
    final String unescapedTableName = getUnescapedTableName(entity);
    NamingStrategy namingStrategy = entity.getNamingStrategy();
    Collection<? extends PersistentProperty> persistentProperties = entity.getPersistentProperties();
    List<QueryParameterBinding> parameterBindings = new ArrayList<>();
    List<String> columns = new ArrayList<>();
    List<String> values = new ArrayList<>();
    for (PersistentProperty prop : persistentProperties) {
        if (!prop.isGenerated()) {
            traversePersistentProperties(prop, (associations, property) -> {
                addWriteExpression(values, prop);
                String key = String.valueOf(values.size());
                String[] path = asStringPath(associations, property);
                parameterBindings.add(new QueryParameterBinding() {

                    @Override
                    public String getKey() {
                        return key;
                    }

                    @Override
                    public DataType getDataType() {
                        return property.getDataType();
                    }

                    @Override
                    public String[] getPropertyPath() {
                        return path;
                    }
                });
                String columnName = namingStrategy.mappedName(associations, property);
                if (escape) {
                    columnName = quote(columnName);
                }
                columns.add(columnName);
            });
        }
    }
    PersistentProperty version = entity.getVersion();
    if (version != null) {
        addWriteExpression(values, version);
        String key = String.valueOf(values.size());
        parameterBindings.add(new QueryParameterBinding() {

            @Override
            public String getKey() {
                return key;
            }

            @Override
            public DataType getDataType() {
                return version.getDataType();
            }

            @Override
            public String[] getPropertyPath() {
                return new String[] { version.getName() };
            }
        });
        String columnName = namingStrategy.mappedName(Collections.emptyList(), version);
        if (escape) {
            columnName = quote(columnName);
        }
        columns.add(columnName);
    }
    PersistentProperty identity = entity.getIdentity();
    if (identity != null) {
        traversePersistentProperties(identity, (associations, property) -> {
            boolean isSequence = false;
            if (isNotForeign(associations)) {
                Optional<AnnotationValue<GeneratedValue>> generated = property.findAnnotation(GeneratedValue.class);
                if (generated.isPresent()) {
                    GeneratedValue.Type idGeneratorType = generated.flatMap(av -> av.enumValue(GeneratedValue.Type.class)).orElseGet(() -> selectAutoStrategy(property));
                    if (idGeneratorType == GeneratedValue.Type.SEQUENCE) {
                        isSequence = true;
                    } else if (dialect != Dialect.MYSQL || property.getDataType() != DataType.UUID) {
                        // Property skipped
                        return;
                    }
                }
            }
            if (isSequence) {
                values.add(getSequenceStatement(unescapedTableName, property));
            } else {
                addWriteExpression(values, property);
                String key = String.valueOf(values.size());
                String[] path = asStringPath(associations, property);
                parameterBindings.add(new QueryParameterBinding() {

                    @Override
                    public String getKey() {
                        return key;
                    }

                    @Override
                    public DataType getDataType() {
                        return property.getDataType();
                    }

                    @Override
                    public String[] getPropertyPath() {
                        return path;
                    }
                });
            }
            String columnName = namingStrategy.mappedName(associations, property);
            if (escape) {
                columnName = quote(columnName);
            }
            columns.add(columnName);
        });
    }
    String builder = INSERT_INTO + getTableName(entity) + " (" + String.join(",", columns) + CLOSE_BRACKET + " " + "VALUES (" + String.join(String.valueOf(COMMA), values) + CLOSE_BRACKET;
    return QueryResult.of(builder, Collections.emptyList(), parameterBindings, Collections.emptyMap());
}
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) QueryParameterBinding(io.micronaut.data.model.query.builder.QueryParameterBinding) ArrayList(java.util.ArrayList) PersistentProperty(io.micronaut.data.model.PersistentProperty) GeneratedValue(io.micronaut.data.annotation.GeneratedValue) NamingStrategy(io.micronaut.data.model.naming.NamingStrategy) DataType(io.micronaut.data.model.DataType) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) NonNull(io.micronaut.core.annotation.NonNull)

Example 2 with IDENTITY

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

the class SqlQueryBuilder method buildCreateTableStatements.

/**
 * Builds the create table statement. Designed for testing and not production usage. For production a
 * SQL migration tool such as Flyway or Liquibase is recommended.
 *
 * @param entity The entity
 * @return The tables for the give entity
 */
@Experimental
@NonNull
public String[] buildCreateTableStatements(@NonNull PersistentEntity entity) {
    ArgumentUtils.requireNonNull("entity", entity);
    final String unescapedTableName = getUnescapedTableName(entity);
    String tableName = getTableName(entity);
    boolean escape = shouldEscape(entity);
    PersistentProperty identity = entity.getIdentity();
    List<String> createStatements = new ArrayList<>();
    String schema = entity.getAnnotationMetadata().stringValue(MappedEntity.class, SqlMembers.SCHEMA).orElse(null);
    if (StringUtils.isNotEmpty(schema)) {
        if (escape) {
            schema = quote(schema);
        }
        createStatements.add("CREATE SCHEMA " + schema + ";");
    }
    Collection<Association> foreignKeyAssociations = getJoinTableAssociations(entity);
    NamingStrategy namingStrategy = entity.getNamingStrategy();
    if (CollectionUtils.isNotEmpty(foreignKeyAssociations)) {
        for (Association association : foreignKeyAssociations) {
            StringBuilder joinTableBuilder = new StringBuilder("CREATE TABLE ");
            PersistentEntity associatedEntity = association.getAssociatedEntity();
            Optional<Association> inverseSide = association.getInverseSide().map(Function.identity());
            Association owningAssociation = inverseSide.orElse(association);
            AnnotationMetadata annotationMetadata = owningAssociation.getAnnotationMetadata();
            String joinTableName = annotationMetadata.stringValue(ANN_JOIN_TABLE, "name").orElseGet(() -> namingStrategy.mappedName(association));
            if (escape) {
                joinTableName = quote(joinTableName);
            }
            joinTableBuilder.append(joinTableName).append(" (");
            List<PersistentPropertyPath> leftProperties = new ArrayList<>();
            List<PersistentPropertyPath> rightProperties = new ArrayList<>();
            boolean isAssociationOwner = !inverseSide.isPresent();
            List<String> leftJoinTableColumns = resolveJoinTableJoinColumns(annotationMetadata, isAssociationOwner, entity, namingStrategy);
            List<String> rightJoinTableColumns = resolveJoinTableJoinColumns(annotationMetadata, !isAssociationOwner, association.getAssociatedEntity(), namingStrategy);
            traversePersistentProperties(entity.getIdentity(), (associations, property) -> {
                leftProperties.add(PersistentPropertyPath.of(associations, property, ""));
            });
            traversePersistentProperties(associatedEntity.getIdentity(), (associations, property) -> {
                rightProperties.add(PersistentPropertyPath.of(associations, property, ""));
            });
            if (leftJoinTableColumns.size() == leftProperties.size()) {
                for (int i = 0; i < leftJoinTableColumns.size(); i++) {
                    PersistentPropertyPath pp = leftProperties.get(i);
                    String columnName = leftJoinTableColumns.get(i);
                    if (escape) {
                        columnName = quote(columnName);
                    }
                    joinTableBuilder.append(addTypeToColumn(pp.getProperty(), columnName, true)).append(',');
                }
            } else {
                for (PersistentPropertyPath pp : leftProperties) {
                    String columnName = namingStrategy.mappedJoinTableColumn(entity, pp.getAssociations(), pp.getProperty());
                    if (escape) {
                        columnName = quote(columnName);
                    }
                    joinTableBuilder.append(addTypeToColumn(pp.getProperty(), columnName, true)).append(',');
                }
            }
            if (rightJoinTableColumns.size() == rightProperties.size()) {
                for (int i = 0; i < rightJoinTableColumns.size(); i++) {
                    PersistentPropertyPath pp = rightProperties.get(i);
                    String columnName = rightJoinTableColumns.get(i);
                    if (escape) {
                        columnName = quote(columnName);
                    }
                    joinTableBuilder.append(addTypeToColumn(pp.getProperty(), columnName, true)).append(',');
                }
            } else {
                for (PersistentPropertyPath pp : rightProperties) {
                    String columnName = namingStrategy.mappedJoinTableColumn(entity, pp.getAssociations(), pp.getProperty());
                    if (escape) {
                        columnName = quote(columnName);
                    }
                    joinTableBuilder.append(addTypeToColumn(pp.getProperty(), columnName, true)).append(',');
                }
            }
            joinTableBuilder.setLength(joinTableBuilder.length() - 1);
            joinTableBuilder.append(")");
            if (dialect != Dialect.ORACLE) {
                joinTableBuilder.append(';');
            }
            createStatements.add(joinTableBuilder.toString());
        }
    }
    boolean generatePkAfterColumns = false;
    List<String> primaryColumnsName = new ArrayList<>();
    List<String> columns = new ArrayList<>();
    if (identity != null) {
        List<PersistentPropertyPath> ids = new ArrayList<>();
        traversePersistentProperties(identity, (associations, property) -> {
            ids.add(PersistentPropertyPath.of(associations, property, ""));
        });
        if (ids.size() > 1) {
            generatePkAfterColumns = true;
        }
        boolean finalGeneratePkAfterColumns = generatePkAfterColumns;
        for (PersistentPropertyPath pp : ids) {
            String column = namingStrategy.mappedName(pp.getAssociations(), pp.getProperty());
            if (escape) {
                column = quote(column);
            }
            primaryColumnsName.add(column);
            column = addTypeToColumn(pp.getProperty(), column, isRequired(pp.getAssociations(), pp.getProperty()));
            if (isNotForeign(pp.getAssociations())) {
                column = addGeneratedStatementToColumn(pp.getProperty(), column, !finalGeneratePkAfterColumns);
            }
            columns.add(column);
        }
    }
    PersistentProperty version = entity.getVersion();
    if (version != null) {
        String column = namingStrategy.mappedName(Collections.emptyList(), version);
        if (escape) {
            column = quote(column);
        }
        column = addTypeToColumn(version, column, true);
        columns.add(column);
    }
    BiConsumer<List<Association>, PersistentProperty> addColumn = (associations, property) -> {
        String column = namingStrategy.mappedName(associations, property);
        if (escape) {
            column = quote(column);
        }
        column = addTypeToColumn(property, column, isRequired(associations, property));
        if (isNotForeign(associations)) {
            column = addGeneratedStatementToColumn(property, column, false);
        }
        columns.add(column);
    };
    for (PersistentProperty prop : entity.getPersistentProperties()) {
        traversePersistentProperties(prop, addColumn);
    }
    StringBuilder builder = new StringBuilder("CREATE TABLE ").append(tableName).append(" (");
    builder.append(String.join(",", columns));
    if (generatePkAfterColumns) {
        builder.append(", PRIMARY KEY(").append(String.join(",", primaryColumnsName)).append(')');
    }
    if (dialect == Dialect.ORACLE) {
        builder.append(")");
    } else {
        builder.append(");");
    }
    if (identity != null && identity.isGenerated()) {
        GeneratedValue.Type idGeneratorType = identity.getAnnotationMetadata().enumValue(GeneratedValue.class, GeneratedValue.Type.class).orElseGet(() -> selectAutoStrategy(identity));
        boolean isSequence = idGeneratorType == GeneratedValue.Type.SEQUENCE;
        final String generatedDefinition = identity.getAnnotationMetadata().stringValue(GeneratedValue.class, "definition").orElse(null);
        if (generatedDefinition != null) {
            createStatements.add(generatedDefinition);
        } else if (isSequence) {
            final boolean isSqlServer = dialect == Dialect.SQL_SERVER;
            final String sequenceName = quote(unescapedTableName + SEQ_SUFFIX);
            String createSequenceStmt = "CREATE SEQUENCE " + sequenceName;
            if (isSqlServer) {
                createSequenceStmt += " AS BIGINT";
            }
            createSequenceStmt += " MINVALUE 1 START WITH 1";
            if (dialect == Dialect.ORACLE) {
                createSequenceStmt += " NOCACHE NOCYCLE";
            } else {
                if (isSqlServer) {
                    createSequenceStmt += " INCREMENT BY 1";
                }
            }
            createStatements.add(createSequenceStmt);
        }
    }
    createStatements.add(builder.toString());
    addIndexes(entity, tableName, createStatements);
    return createStatements.toArray(new String[0]);
}
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) MappedEntity(io.micronaut.data.annotation.MappedEntity) PersistentEntity(io.micronaut.data.model.PersistentEntity) ArrayList(java.util.ArrayList) PersistentProperty(io.micronaut.data.model.PersistentProperty) PersistentPropertyPath(io.micronaut.data.model.PersistentPropertyPath) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) GeneratedValue(io.micronaut.data.annotation.GeneratedValue) NamingStrategy(io.micronaut.data.model.naming.NamingStrategy) DataType(io.micronaut.data.model.DataType) Association(io.micronaut.data.model.Association) List(java.util.List) ArrayList(java.util.ArrayList) Experimental(io.micronaut.core.annotation.Experimental) NonNull(io.micronaut.core.annotation.NonNull)

Aggregations

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