Search in sources :

Example 21 with PersistentEntity

use of io.micronaut.data.model.PersistentEntity in project micronaut-data by micronaut-projects.

the class SqlQueryBuilder method join.

private void join(StringBuilder sb, String joinType, QueryState queryState, PersistentEntity associatedEntity, PersistentEntity associationOwner, String leftTableAlias, String rightTableAlias, List<Association> leftPropertyAssociations, PersistentProperty leftProperty, List<Association> rightPropertyAssociations, PersistentProperty rightProperty) {
    final boolean escape = shouldEscape(associationOwner);
    List<String> onLeftColumns = new ArrayList<>();
    List<String> onRightColumns = new ArrayList<>();
    Association association = null;
    if (leftProperty instanceof Association) {
        association = (Association) leftProperty;
    } else if (rightProperty instanceof Association) {
        association = (Association) rightProperty;
    }
    if (association != null) {
        Optional<Association> inverse = association.getInverseSide().map(Function.identity());
        Association owner = inverse.orElse(association);
        boolean isOwner = leftProperty == owner;
        AnnotationValue<Annotation> joinColumnsHolder = owner.getAnnotationMetadata().getAnnotation(ANN_JOIN_COLUMNS);
        if (joinColumnsHolder != null) {
            onLeftColumns.addAll(joinColumnsHolder.getAnnotations("value").stream().map(ann -> ann.stringValue(isOwner ? "name" : "referencedColumnName").orElse(null)).filter(Objects::nonNull).collect(Collectors.toList()));
            onRightColumns.addAll(joinColumnsHolder.getAnnotations("value").stream().map(ann -> ann.stringValue(isOwner ? "referencedColumnName" : "name").orElse(null)).filter(Objects::nonNull).collect(Collectors.toList()));
        }
    }
    if (onLeftColumns.isEmpty()) {
        traversePersistentProperties(leftProperty, (associations, p) -> {
            String column = leftProperty.getOwner().getNamingStrategy().mappedName(merge(leftPropertyAssociations, associations), p);
            onLeftColumns.add(column);
        });
        if (onLeftColumns.isEmpty()) {
            throw new MappingException("Cannot join on entity [" + leftProperty.getOwner().getName() + "] that has no declared ID");
        }
    }
    if (onRightColumns.isEmpty()) {
        traversePersistentProperties(rightProperty, (associations, p) -> {
            String column = rightProperty.getOwner().getNamingStrategy().mappedName(merge(rightPropertyAssociations, associations), p);
            onRightColumns.add(column);
        });
    }
    join(sb, queryState.getQueryModel(), joinType, getTableName(associatedEntity), rightTableAlias, leftTableAlias, escape ? onLeftColumns.stream().map(this::quote).collect(Collectors.toList()) : onLeftColumns, escape ? onRightColumns.stream().map(this::quote).collect(Collectors.toList()) : onRightColumns);
}
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) Association(io.micronaut.data.model.Association) ArrayList(java.util.ArrayList) Objects(java.util.Objects) Annotation(java.lang.annotation.Annotation) MappingException(io.micronaut.data.exceptions.MappingException)

Example 22 with PersistentEntity

use of io.micronaut.data.model.PersistentEntity in project micronaut-data by micronaut-projects.

the class SqlQueryBuilder method flatMapEmbedded.

private Stream<? extends PersistentProperty> flatMapEmbedded(PersistentProperty pp) {
    if (pp instanceof Embedded) {
        Embedded embedded = (Embedded) pp;
        PersistentEntity embeddedEntity = embedded.getAssociatedEntity();
        return embeddedEntity.getPersistentProperties().stream().flatMap(this::flatMapEmbedded);
    }
    return Stream.of(pp);
}
Also used : PersistentEntity(io.micronaut.data.model.PersistentEntity) Embedded(io.micronaut.data.model.Embedded)

Example 23 with PersistentEntity

use of io.micronaut.data.model.PersistentEntity in project micronaut-data by micronaut-projects.

the class AbstractSqlLikeQueryBuilder method buildSelectClause.

private void buildSelectClause(QueryModel query, QueryState queryState, StringBuilder queryString) {
    String logicalName = queryState.getRootAlias();
    PersistentEntity entity = queryState.getEntity();
    buildSelect(queryState, queryString, query.getProjections(), logicalName, entity);
    String tableName = getTableName(entity);
    queryString.append(FROM_CLAUSE).append(tableName).append(getTableAsKeyword()).append(logicalName);
}
Also used : PersistentEntity(io.micronaut.data.model.PersistentEntity)

Example 24 with PersistentEntity

use of io.micronaut.data.model.PersistentEntity in project micronaut-data by micronaut-projects.

the class AbstractSqlLikeQueryBuilder method buildDelete.

@Override
public QueryResult buildDelete(@NonNull AnnotationMetadata annotationMetadata, @NonNull QueryModel query) {
    PersistentEntity entity = query.getPersistentEntity();
    QueryState queryState = newQueryState(query, false, isAliasForBatch());
    StringBuilder queryString = queryState.getQuery();
    String tableAlias = queryState.getRootAlias();
    StringBuilder buffer = appendDeleteClause(queryString);
    String tableName = getTableName(entity);
    buffer.append(tableName).append(SPACE);
    if (tableAlias != null) {
        buffer.append(getTableAsKeyword()).append(tableAlias);
    }
    buildWhereClause(annotationMetadata, query.getCriteria(), queryState);
    return QueryResult.of(queryState.getFinalQuery(), queryState.getQueryParts(), queryState.getParameterBindings(), queryState.getAdditionalRequiredParameters());
}
Also used : PersistentEntity(io.micronaut.data.model.PersistentEntity)

Example 25 with PersistentEntity

use of io.micronaut.data.model.PersistentEntity in project micronaut-data by micronaut-projects.

the class SqlQueryBuilder method selectAllColumns.

@Override
protected void selectAllColumns(QueryState queryState, StringBuilder queryBuffer) {
    PersistentEntity entity = queryState.getEntity();
    selectAllColumns(entity, queryState.getRootAlias(), queryBuffer);
    QueryModel queryModel = queryState.getQueryModel();
    Collection<JoinPath> allPaths = queryModel.getJoinPaths();
    if (CollectionUtils.isNotEmpty(allPaths)) {
        Collection<JoinPath> joinPaths = allPaths.stream().filter(jp -> {
            Join.Type jt = jp.getJoinType();
            return jt.name().contains("FETCH");
        }).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(joinPaths)) {
            for (JoinPath joinPath : joinPaths) {
                Association association = joinPath.getAssociation();
                if (association instanceof Embedded) {
                    // joins on embedded don't make sense
                    continue;
                }
                PersistentEntity associatedEntity = association.getAssociatedEntity();
                NamingStrategy namingStrategy = associatedEntity.getNamingStrategy();
                String aliasName = getAliasName(joinPath);
                String joinPathAlias = getPathOnlyAliasName(joinPath);
                queryBuffer.append(COMMA);
                boolean includeIdentity = false;
                if (association.isForeignKey()) {
                    // in the case of a foreign key association the ID is not in the table
                    // so we need to retrieve it
                    includeIdentity = true;
                }
                traversePersistentProperties(associatedEntity, includeIdentity, true, (propertyAssociations, prop) -> {
                    String columnName;
                    if (computePropertyPaths()) {
                        columnName = namingStrategy.mappedName(propertyAssociations, prop);
                    } else {
                        columnName = asPath(propertyAssociations, prop);
                    }
                    queryBuffer.append(aliasName).append(DOT).append(queryState.shouldEscape() ? quote(columnName) : columnName).append(AS_CLAUSE).append(joinPathAlias).append(columnName).append(COMMA);
                });
                queryBuffer.setLength(queryBuffer.length() - 1);
            }
        }
    }
}
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) NamingStrategy(io.micronaut.data.model.naming.NamingStrategy) DataType(io.micronaut.data.model.DataType) Association(io.micronaut.data.model.Association) JoinPath(io.micronaut.data.model.query.JoinPath) PersistentEntity(io.micronaut.data.model.PersistentEntity) Embedded(io.micronaut.data.model.Embedded) QueryModel(io.micronaut.data.model.query.QueryModel)

Aggregations

PersistentEntity (io.micronaut.data.model.PersistentEntity)27 PersistentProperty (io.micronaut.data.model.PersistentProperty)15 Association (io.micronaut.data.model.Association)13 Embedded (io.micronaut.data.model.Embedded)11 List (java.util.List)11 ArrayList (java.util.ArrayList)10 Collection (java.util.Collection)9 CollectionUtils (io.micronaut.core.util.CollectionUtils)8 MappedEntity (io.micronaut.data.annotation.MappedEntity)8 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)7 NonNull (io.micronaut.core.annotation.NonNull)7 ArrayUtils (io.micronaut.core.util.ArrayUtils)7 Relation (io.micronaut.data.annotation.Relation)7 PersistentPropertyPath (io.micronaut.data.model.PersistentPropertyPath)7 NamingStrategy (io.micronaut.data.model.naming.NamingStrategy)7 JoinPath (io.micronaut.data.model.query.JoinPath)7 ArgumentUtils (io.micronaut.core.util.ArgumentUtils)6 StringUtils (io.micronaut.core.util.StringUtils)6 MappingException (io.micronaut.data.exceptions.MappingException)6 Pageable (io.micronaut.data.model.Pageable)6