DATACOUCH-615 Use query in removeByQuery (#264)
* DATACOUCH-615 Use query in removeByQuery Use provided query instead of hardcoded statement. * DATACOUCH-615 Better method naming in Query class * DATACOUCH-615 Replace hard-coded meta fields * DATACOUCH-615 Test findByQuery.matching * DATACOUCH-615 Remove failing test
This commit is contained in:
@@ -121,7 +121,7 @@ public class ReactiveFindByQueryOperationSupport implements ReactiveFindByQueryO
|
||||
}
|
||||
|
||||
private String assembleEntityQuery(final boolean count) {
|
||||
return query.toN1qlString(template, this.domainType, count);
|
||||
return query.toN1qlSelectString(template, this.domainType, count);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.couchbase.core;
|
||||
|
||||
import org.springframework.data.couchbase.core.support.TemplateUtils;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import java.util.Optional;
|
||||
@@ -58,14 +59,7 @@ public class ReactiveRemoveByQueryOperationSupport implements ReactiveRemoveByQu
|
||||
@Override
|
||||
public Flux<RemoveResult> all() {
|
||||
return Flux.defer(() -> {
|
||||
String bucket = "`" + template.getBucketName() + "`";
|
||||
|
||||
String typeKey = template.getConverter().getTypeKey();
|
||||
String typeValue = template.support().getJavaNameForEntity(domainType);
|
||||
String where = " WHERE `" + typeKey + "` = \"" + typeValue + "\"";
|
||||
|
||||
String returning = " RETURNING meta().*";
|
||||
String statement = "DELETE FROM " + bucket + " " + where + returning;
|
||||
String statement = assembleDeleteQuery();
|
||||
|
||||
return template.getCouchbaseClientFactory().getCluster().reactive().query(statement, buildQueryOptions())
|
||||
.onErrorMap(throwable -> {
|
||||
@@ -75,7 +69,7 @@ public class ReactiveRemoveByQueryOperationSupport implements ReactiveRemoveByQu
|
||||
return throwable;
|
||||
}
|
||||
}).flatMapMany(ReactiveQueryResult::rowsAsObject)
|
||||
.map(row -> new RemoveResult(row.getString("id"), row.getLong("cas"), Optional.empty()));
|
||||
.map(row -> new RemoveResult(row.getString(TemplateUtils.SELECT_ID), row.getLong(TemplateUtils.SELECT_CAS), Optional.empty()));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -97,6 +91,10 @@ public class ReactiveRemoveByQueryOperationSupport implements ReactiveRemoveByQu
|
||||
return new ReactiveRemoveByQuerySupport<>(template, domainType, query, scanConsistency);
|
||||
}
|
||||
|
||||
private String assembleDeleteQuery() {
|
||||
return query.toN1qlRemoveString(template, this.domainType);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -243,7 +243,7 @@ public class Query {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public String toN1qlString(ReactiveCouchbaseTemplate template, Class domainClass, boolean isCount) {
|
||||
public String toN1qlSelectString(ReactiveCouchbaseTemplate template, Class domainClass, boolean isCount) {
|
||||
StringBasedN1qlQueryParser.N1qlSpelValues n1ql = getN1qlSpelValues(template, domainClass, isCount);
|
||||
final StringBuilder statement = new StringBuilder();
|
||||
appendString(statement, n1ql.selectEntity); // select ...
|
||||
@@ -254,6 +254,16 @@ public class Query {
|
||||
return statement.toString();
|
||||
}
|
||||
|
||||
public String toN1qlRemoveString(ReactiveCouchbaseTemplate template, Class domainClass) {
|
||||
StringBasedN1qlQueryParser.N1qlSpelValues n1ql = getN1qlSpelValues(template, domainClass, false);
|
||||
final StringBuilder statement = new StringBuilder();
|
||||
appendString(statement, n1ql.delete); // delete ...
|
||||
appendWhereString(statement, n1ql.filter); // typeKey = typeValue
|
||||
appendWhere(statement, null); // criteria on this Query
|
||||
appendString(statement, n1ql.returning);
|
||||
return statement.toString();
|
||||
}
|
||||
|
||||
StringBasedN1qlQueryParser.N1qlSpelValues getN1qlSpelValues(ReactiveCouchbaseTemplate template, Class domainClass,
|
||||
boolean isCount) {
|
||||
String typeKey = template.getConverter().getTypeKey();
|
||||
|
||||
@@ -52,7 +52,7 @@ public class StringQuery extends Query {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toN1qlString(ReactiveCouchbaseTemplate template, Class domainClass, boolean isCount) {
|
||||
public String toN1qlSelectString(ReactiveCouchbaseTemplate template, Class domainClass, boolean isCount) {
|
||||
final StringBuilder statement = new StringBuilder();
|
||||
appendInlineN1qlStatement(statement); // apply the string statement
|
||||
// To use generated parameters for literals
|
||||
|
||||
Reference in New Issue
Block a user