use of io.micronaut.data.model.query.BindingParameter in project micronaut-data by micronaut-projects.
the class MongoQueryBuilder method buildUpdate.
@Override
public QueryResult buildUpdate(AnnotationMetadata annotationMetadata, QueryModel query, Map<String, Object> propertiesToUpdate) {
ArgumentUtils.requireNonNull("annotationMetadata", annotationMetadata);
ArgumentUtils.requireNonNull("query", query);
ArgumentUtils.requireNonNull("propertiesToUpdate", propertiesToUpdate);
QueryState queryState = new QueryState(query, true);
QueryModel.Junction criteria = query.getCriteria();
String predicateQuery = "";
if (!criteria.isEmpty()) {
Map<String, Object> predicate = buildWhereClause(annotationMetadata, criteria, queryState);
predicateQuery = toJsonString(predicate);
}
Map<String, Object> sets = new LinkedHashMap<>();
for (Map.Entry<String, Object> e : propertiesToUpdate.entrySet()) {
if (e.getValue() instanceof BindingParameter) {
PersistentPropertyPath propertyPath = findProperty(queryState, e.getKey(), null);
int index = queryState.pushParameter((BindingParameter) e.getValue(), newBindingContext(propertyPath));
sets.put(e.getKey(), singletonMap(QUERY_PARAMETER_PLACEHOLDER, index));
} else {
sets.put(e.getKey(), e.getValue());
}
}
String update = toJsonString(singletonMap("$set", sets));
String finalPredicateQuery = predicateQuery;
return new QueryResult() {
@NonNull
@Override
public String getQuery() {
return finalPredicateQuery;
}
@Override
public String getUpdate() {
return update;
}
@Override
public List<String> getQueryParts() {
return Collections.emptyList();
}
@Override
public List<QueryParameterBinding> getParameterBindings() {
return queryState.getParameterBindings();
}
@Override
public Map<String, String> getAdditionalRequiredParameters() {
return Collections.emptyMap();
}
};
}
use of io.micronaut.data.model.query.BindingParameter in project micronaut-data by micronaut-projects.
the class AbstractSqlLikeQueryBuilder method appendCriteriaForOperator.
private void appendCriteriaForOperator(StringBuilder whereClause, PropertyParameterCreator propertyParameterCreator, PersistentPropertyPath parameterPropertyPath, QueryPropertyPath propertyPath, Object value, String operator) {
if (value instanceof BindingParameter) {
BindingParameter bindingParameter = (BindingParameter) value;
boolean computePropertyPaths = computePropertyPaths();
if (!computePropertyPaths) {
appendPropertyRef(whereClause, propertyPath);
whereClause.append(operator);
propertyParameterCreator.pushParameter(bindingParameter, newBindingContext(parameterPropertyPath, propertyPath.propertyPath));
return;
}
String currentAlias = propertyPath.getTableAlias();
NamingStrategy namingStrategy = propertyPath.getNamingStrategy();
boolean shouldEscape = propertyPath.shouldEscape();
int length = whereClause.length();
traversePersistentProperties(propertyPath.getAssociations(), propertyPath.getProperty(), (associations, property) -> {
String readTransformer = getDataTransformerReadValue(currentAlias, property).orElse(null);
if (readTransformer != null) {
whereClause.append(readTransformer);
} else {
if (currentAlias != null) {
whereClause.append(currentAlias).append(DOT);
}
String columnName = namingStrategy.mappedName(associations, property);
if (shouldEscape) {
columnName = quote(columnName);
}
whereClause.append(columnName);
}
whereClause.append(operator);
propertyParameterCreator.pushParameter(bindingParameter, newBindingContext(parameterPropertyPath, PersistentPropertyPath.of(associations, property)));
whereClause.append(LOGICAL_AND);
});
int newLength = whereClause.length();
if (newLength != length) {
whereClause.setLength(newLength - LOGICAL_AND.length());
}
} else {
appendPropertyRef(whereClause, propertyPath);
whereClause.append(operator).append(asLiteral(value));
}
}
use of io.micronaut.data.model.query.BindingParameter in project micronaut-data by micronaut-projects.
the class AbstractSqlLikeQueryBuilder method buildUpdateStatement.
private void buildUpdateStatement(QueryState queryState, Map<String, Object> propertiesToUpdate) {
StringBuilder queryString = queryState.getQuery();
queryString.append(SPACE).append("SET").append(SPACE);
// keys need to be sorted before query is built
List<Map.Entry<QueryPropertyPath, Object>> update = propertiesToUpdate.entrySet().stream().map(e -> {
QueryPropertyPath propertyPath = findProperty(queryState, e.getKey(), null);
if (propertyPath.getProperty() instanceof Association && ((Association) propertyPath.getProperty()).isForeignKey()) {
throw new IllegalArgumentException("Foreign key associations cannot be updated as part of a batch update statement");
}
return new AbstractMap.SimpleEntry<>(propertyPath, e.getValue());
}).filter(e -> !(e.getValue() instanceof QueryParameter) || !e.getKey().getProperty().isGenerated()).collect(Collectors.toList());
int length = queryString.length();
if (!computePropertyPaths()) {
for (Map.Entry<QueryPropertyPath, Object> entry : update) {
QueryPropertyPath propertyPath = entry.getKey();
PersistentProperty prop = propertyPath.getProperty();
String tableAlias = propertyPath.getTableAlias();
if (tableAlias != null) {
queryString.append(tableAlias).append(DOT);
}
queryString.append(propertyPath.getPath()).append('=');
if (entry.getValue() instanceof BindingParameter) {
appendUpdateSetParameter(queryString, tableAlias, prop, () -> {
queryState.pushParameter((BindingParameter) entry.getValue(), newBindingContext(propertyPath.propertyPath));
});
} else {
queryString.append(asLiteral(entry.getValue()));
}
queryString.append(COMMA);
}
} else {
NamingStrategy namingStrategy = queryState.getEntity().getNamingStrategy();
for (Map.Entry<QueryPropertyPath, Object> entry : update) {
QueryPropertyPath propertyPath = entry.getKey();
if (entry.getValue() instanceof BindingParameter) {
traversePersistentProperties(propertyPath.getAssociations(), propertyPath.getProperty(), (associations, property) -> {
String tableAlias = propertyPath.getTableAlias();
if (tableAlias != null) {
queryString.append(tableAlias).append(DOT);
}
String columnName = namingStrategy.mappedName(associations, property);
if (queryState.escape) {
columnName = quote(columnName);
}
queryString.append(columnName).append('=');
appendUpdateSetParameter(queryString, tableAlias, property, () -> {
queryState.pushParameter((BindingParameter) entry.getValue(), newBindingContext(propertyPath.propertyPath, PersistentPropertyPath.of(associations, property, asPath(associations, property))));
});
queryString.append(COMMA);
});
} else {
String tableAlias = propertyPath.getTableAlias();
if (tableAlias != null) {
queryString.append(tableAlias).append(DOT);
}
queryString.append(propertyPath.getPath()).append('=');
queryString.append(asLiteral(entry.getValue()));
queryString.append(COMMA);
}
}
}
int newLength = queryString.length();
if (length != newLength) {
queryString.setLength(newLength - 1);
}
}
Aggregations