Skip to content

Commit

Permalink
Refactor SQL queries (#1565)
Browse files Browse the repository at this point in the history
  • Loading branch information
dstepanov authored Jun 8, 2022
1 parent 2c64ea8 commit bbc603c
Show file tree
Hide file tree
Showing 38 changed files with 2,431 additions and 1,950 deletions.
529 changes: 292 additions & 237 deletions config/accepted-api-changes.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.micronaut.data.model.Embedded;
import io.micronaut.data.model.Pageable;
import io.micronaut.data.model.PersistentEntity;
import io.micronaut.data.model.PersistentEntityUtils;
import io.micronaut.data.model.PersistentProperty;
import io.micronaut.data.model.PersistentPropertyPath;
import io.micronaut.data.model.Sort;
Expand Down Expand Up @@ -1019,35 +1020,7 @@ private void traversePersistentProperties(PersistentProperty property, BiConsume
private void traversePersistentProperties(List<Association> associations,
PersistentProperty property,
BiConsumer<List<Association>, PersistentProperty> consumerProperty) {
if (property instanceof Embedded) {
Embedded embedded = (Embedded) property;
PersistentEntity embeddedEntity = embedded.getAssociatedEntity();
Collection<? extends PersistentProperty> embeddedProperties = embeddedEntity.getPersistentProperties();
List<Association> newAssociations = new ArrayList<>(associations);
newAssociations.add((Association) property);
for (PersistentProperty embeddedProperty : embeddedProperties) {
traversePersistentProperties(newAssociations, embeddedProperty, consumerProperty);
}
} else if (property instanceof Association) {
Association association = (Association) property;
if (association.isForeignKey()) {
return;
}
List<Association> newAssociations = new ArrayList<>(associations);
newAssociations.add((Association) property);
PersistentEntity associatedEntity = association.getAssociatedEntity();
PersistentProperty assocIdentity = associatedEntity.getIdentity();
if (assocIdentity == null) {
throw new IllegalStateException("Identity cannot be missing for: " + associatedEntity);
}
if (assocIdentity instanceof Association) {
traversePersistentProperties(newAssociations, assocIdentity, consumerProperty);
} else {
consumerProperty.accept(newAssociations, assocIdentity);
}
} else {
consumerProperty.accept(associations, property);
}
PersistentEntityUtils.traversePersistentProperties(associations, property, consumerProperty);
}

private static final class RawJsonValue {
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright 2017-2022 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.data.model;

import io.micronaut.core.annotation.Internal;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.BiConsumer;

/**
* Persistent entity utils.
*
* @author Denis Stepanov
* @since 3.5.0
*/
@Internal
public final class PersistentEntityUtils {

private PersistentEntityUtils() {
}

/**
* Traverses properties that should be persisted.
*
* @param property The property to start traversing from
* @param consumer The function to invoke on every property
*/
public static void traversePersistentProperties(PersistentProperty property, BiConsumer<List<Association>, PersistentProperty> consumer) {
traversePersistentProperties(Collections.emptyList(), property, consumer);
}

/**
* Traverses properties that should be persisted.
*
* @param persistentEntity The persistent entity
* @param consumer The function to invoke on every property
*/
public static void traversePersistentProperties(PersistentEntity persistentEntity, BiConsumer<List<Association>, PersistentProperty> consumer) {
if (persistentEntity.getIdentity() != null) {
traversePersistentProperties(Collections.emptyList(), persistentEntity.getIdentity(), consumer);
}
if (persistentEntity.getVersion() != null) {
traversePersistentProperties(Collections.emptyList(), persistentEntity.getVersion(), consumer);
}
for (PersistentProperty property : persistentEntity.getPersistentProperties()) {
traversePersistentProperties(Collections.emptyList(), property, consumer);
}
}

/**
* Traverses properties that should be persisted.
*
* @param persistentEntity The persistent entity
* @param includeIdentity Should be identifier included
* @param includeVersion Should be version included
* @param consumer The function to invoke on every property
*/
public static void traversePersistentProperties(PersistentEntity persistentEntity, boolean includeIdentity, boolean includeVersion, BiConsumer<List<Association>, PersistentProperty> consumer) {
if (includeIdentity && persistentEntity.getIdentity() != null) {
traversePersistentProperties(Collections.emptyList(), persistentEntity.getIdentity(), consumer);
}
if (includeVersion && persistentEntity.getVersion() != null) {
traversePersistentProperties(Collections.emptyList(), persistentEntity.getVersion(), consumer);
}
for (PersistentProperty property : persistentEntity.getPersistentProperties()) {
traversePersistentProperties(Collections.emptyList(), property, consumer);
}
}

public static void traversePersistentProperties(List<Association> associations,
PersistentProperty property,
BiConsumer<List<Association>, PersistentProperty> consumerProperty) {
if (property instanceof Embedded) {
Embedded embedded = (Embedded) property;
PersistentEntity embeddedEntity = embedded.getAssociatedEntity();
Collection<? extends PersistentProperty> embeddedProperties = embeddedEntity.getPersistentProperties();
List<Association> newAssociations = new ArrayList<>(associations);
newAssociations.add((Association) property);
for (PersistentProperty embeddedProperty : embeddedProperties) {
traversePersistentProperties(newAssociations, embeddedProperty, consumerProperty);
}
} else if (property instanceof Association) {
Association association = (Association) property;
if (association.isForeignKey()) {
return;
}
List<Association> newAssociations = new ArrayList<>(associations);
newAssociations.add((Association) property);
PersistentEntity associatedEntity = association.getAssociatedEntity();
PersistentProperty assocIdentity = associatedEntity.getIdentity();
if (assocIdentity == null) {
throw new IllegalStateException("Identity cannot be missing for: " + associatedEntity);
}
if (assocIdentity instanceof Association) {
traversePersistentProperties(newAssociations, assocIdentity, consumerProperty);
} else {
consumerProperty.accept(newAssociations, assocIdentity);
}
} else {
consumerProperty.accept(associations, property);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.micronaut.data.model.naming.NamingStrategy;
import io.micronaut.data.model.runtime.RuntimePersistentProperty;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Optional;
Expand Down Expand Up @@ -188,6 +189,25 @@ public String getPath() {
return path;
}

/**
* @return The array path
*/
@NonNull
public String[] getArrayPath() {
if (path == null) {
if (associations.isEmpty()) {
return new String[]{property.getName()};
}
List<String> strings = new ArrayList<>(associations.size() + 1);
for (Association association : associations) {
strings.add(association.getName());
}
strings.add(property.getName());
return strings.toArray(new String[0]);
}
return new String[0];
}

/**
* Find the owner of the possible embedded property.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.micronaut.data.model.Association;
import io.micronaut.data.model.Embedded;
import io.micronaut.data.model.PersistentEntity;
import io.micronaut.data.model.PersistentEntityUtils;
import io.micronaut.data.model.PersistentProperty;
import io.micronaut.data.model.PersistentPropertyPath;
import io.micronaut.data.model.Sort;
Expand All @@ -46,7 +47,6 @@
import javax.validation.constraints.NotNull;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
Expand Down Expand Up @@ -1475,15 +1475,7 @@ protected void traversePersistentProperties(PersistentProperty property, BiConsu
* @param consumer The function to invoke on every property
*/
protected void traversePersistentProperties(PersistentEntity persistentEntity, BiConsumer<List<Association>, PersistentProperty> consumer) {
if (persistentEntity.getIdentity() != null) {
traversePersistentProperties(Collections.emptyList(), persistentEntity.getIdentity(), consumer);
}
if (persistentEntity.getVersion() != null) {
traversePersistentProperties(Collections.emptyList(), persistentEntity.getVersion(), consumer);
}
for (PersistentProperty property : persistentEntity.getPersistentProperties()) {
traversePersistentProperties(Collections.emptyList(), property, consumer);
}
PersistentEntityUtils.traversePersistentProperties(persistentEntity, consumer);
}

/**
Expand All @@ -1495,49 +1487,13 @@ protected void traversePersistentProperties(PersistentEntity persistentEntity, B
* @param consumer The function to invoke on every property
*/
protected void traversePersistentProperties(PersistentEntity persistentEntity, boolean includeIdentity, boolean includeVersion, BiConsumer<List<Association>, PersistentProperty> consumer) {
if (includeIdentity && persistentEntity.getIdentity() != null) {
traversePersistentProperties(Collections.emptyList(), persistentEntity.getIdentity(), consumer);
}
if (includeVersion && persistentEntity.getVersion() != null) {
traversePersistentProperties(Collections.emptyList(), persistentEntity.getVersion(), consumer);
}
for (PersistentProperty property : persistentEntity.getPersistentProperties()) {
traversePersistentProperties(Collections.emptyList(), property, consumer);
}
PersistentEntityUtils.traversePersistentProperties(persistentEntity, includeIdentity, includeVersion, consumer);
}

private void traversePersistentProperties(List<Association> associations,
PersistentProperty property,
BiConsumer<List<Association>, PersistentProperty> consumerProperty) {
if (property instanceof Embedded) {
Embedded embedded = (Embedded) property;
PersistentEntity embeddedEntity = embedded.getAssociatedEntity();
Collection<? extends PersistentProperty> embeddedProperties = embeddedEntity.getPersistentProperties();
List<Association> newAssociations = new ArrayList<>(associations);
newAssociations.add((Association) property);
for (PersistentProperty embeddedProperty : embeddedProperties) {
traversePersistentProperties(newAssociations, embeddedProperty, consumerProperty);
}
} else if (property instanceof Association) {
Association association = (Association) property;
if (association.isForeignKey()) {
return;
}
List<Association> newAssociations = new ArrayList<>(associations);
newAssociations.add((Association) property);
PersistentEntity associatedEntity = association.getAssociatedEntity();
PersistentProperty assocIdentity = associatedEntity.getIdentity();
if (assocIdentity == null) {
throw new IllegalStateException("Identity cannot be missing for: " + associatedEntity);
}
if (assocIdentity instanceof Association) {
traversePersistentProperties(newAssociations, assocIdentity, consumerProperty);
} else {
consumerProperty.accept(newAssociations, assocIdentity);
}
} else {
consumerProperty.accept(associations, property);
}
PersistentEntityUtils.traversePersistentProperties(associations, property, consumerProperty);
}

private Optional<String> getDataTransformerValue(String alias, PersistentProperty prop, String val) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public interface QueryParameterBinding {
* @return The name of the parameter
*/
@Nullable
String getName();
default String getName() {
return null;
}

/**
* @return The required name of the parameter or throws exception
Expand All @@ -51,30 +53,40 @@ default String getRequiredName() {
* @return The data type
*/
@Nullable
DataType getDataType();
default DataType getDataType() {
return null;
}

/**
* @return The parameter converter class
*/
@Nullable
Class<?> getParameterConverterClass();
default Class<?> getParameterConverterClass() {
return null;
}

/**
* @return The parameter index
*/
int getParameterIndex();
default int getParameterIndex() {
return -1;
}

/**
* @return The parameter binding property path.
*/
@Nullable
String[] getParameterBindingPath();
default String[] getParameterBindingPath() {
return null;
}

/**
* @return The property path.
*/
@Nullable
String[] getPropertyPath();
default String[] getPropertyPath() {
return null;
}

/**
* @return The required property path or throws and exception.
Expand All @@ -91,23 +103,34 @@ default String[] getRequiredPropertyPath() {
/**
* @return if property is auto-populated
*/
boolean isAutoPopulated();
default boolean isAutoPopulated() {
return false;
}

/**
* @return if property is auto-populated and binding requires previous value to be set.
*/
boolean isRequiresPreviousPopulatedValue();
default boolean isRequiresPreviousPopulatedValue() {
return false;
}

/**
* @return The previous value of the auto-populated property for cases when the property is mapped to the method parameter.
*/
@Nullable
QueryParameterBinding getPreviousPopulatedValueParameter();
default QueryParameterBinding getPreviousPopulatedValueParameter() {
return null;
}

/**
* @return Is expandable parameter
*/
default boolean isExpandable() {
return false;
}

@Nullable
default Object getValue() {
return null;
}
}
Loading

0 comments on commit bbc603c

Please sign in to comment.