use of io.micronaut.data.model.query.QueryParameter in project micronaut-data by micronaut-projects.
the class AbstractSqlRepositoryOperations method resolveEntityUpdate.
/**
* Resolves a stored update for the given entity.
*
* @param annotationMetadata The repository annotation metadata
* @param repositoryType The repository type
* @param rootEntity The root entity
* @param persistentEntity The persistent entity
* @return The insert
*/
@NonNull
protected DBOperation resolveEntityUpdate(AnnotationMetadata annotationMetadata, Class<?> repositoryType, @NonNull Class<?> rootEntity, @NonNull RuntimePersistentEntity<?> persistentEntity) {
final QueryKey key = new QueryKey(repositoryType, rootEntity);
// noinspection unchecked
return entityUpdates.computeIfAbsent(key, (queryKey) -> {
final SqlQueryBuilder queryBuilder = queryBuilders.getOrDefault(repositoryType, DEFAULT_SQL_BUILDER);
final String idName;
final PersistentProperty identity = persistentEntity.getIdentity();
if (identity != null) {
idName = identity.getName();
} else {
idName = TypeRole.ID;
}
final QueryModel queryModel = QueryModel.from(persistentEntity).idEq(new QueryParameter(idName));
List<String> updateProperties = persistentEntity.getPersistentProperties().stream().filter(p -> !((p instanceof Association) && ((Association) p).isForeignKey()) && p.getAnnotationMetadata().booleanValue(AutoPopulated.class, "updateable").orElse(true)).map(PersistentProperty::getName).collect(Collectors.toList());
final QueryResult queryResult = queryBuilder.buildUpdate(annotationMetadata, queryModel, updateProperties);
return new QueryResultSqlOperation(queryBuilder, queryResult);
});
}
use of io.micronaut.data.model.query.QueryParameter 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