spring-data-couchbase
spring-data-couchbase copied to clipboard
Fluent API needs a parametrized Query [DATACOUCH-551]
deniswsrosa opened DATACOUCH-551 and commented
In the new API, if I have N1QL query in a String:
String myQueryStr = "Select * from mybucket"
There is no way to use it with the matching method as it expects an object of the type Query, which uses the Query DSL.
couchbaseTemplate.findByQuery(User.class).matching(?).all();
Ideally, it should have some be similar to the following:
couchbaseTemplate.findByQuery(User.class).matching(N1QL.from(myQueryStr, params)).all();
Without this method, I suppose that a good portion of the clients using SPC3 will be forced to rewrite their queries to use the DSL, which will increase significantly the time necessary to migrate to SPC4.
Affects: 4.0 GA (Neumann)
1 votes, 2 watchers
Luke Kroon commented
I have the same problem, we are trying to migrate from 3.x.x and have a lot of string N1QL queries.
This is what we used to do:
String query = "select * from bucket where name = :name"
N1qlQuery queryWithParameter = N1qlQuery.parameterized(query, params.toJsonObject());
template.findByN1QLProjection(queryWithParameter, My.class)
Are there any way of doing this in the new API ?
deniswsrosa commented
Luke Kroon there is a workaround but I would recommend you to wait until version 4.0.1 gets released. We will also push some fixes and write more extensive documentation.
If you need to do it right now, here is how you could do it:
https://github.com/spring-projects/spring-data-couchbase/pull/220/files
Luke Kroon commented
deniswsrosa thank you very much for the reply, at the moment everything in our project is already migrated except for our Ad Hoc repository. Well rather wait for that and do the migration.
String query = "select * from bucket where name = :name"
N1qlQuery queryWithParameter = N1qlQuery.parameterized(query, params.toJsonObject());
template.findByN1QLProjection(queryWithParameter, My.class)
isn't this (almost) equivalent to the following? The query below will also return meta().id and meta().cas.
Query query = Query.query(QueryCriteria.where("name").eq("somename")); template.findByQuery(My.class).matching(query).all();
One can also use a repository and the SPEL token #{[n]}
@Query("SELECT META(#{#n1ql.bucket}).id AS __id, META(#{#n1ql.bucket}).cas AS __cas, meta().id as id FROM #{#n1ql.bucket} WHERE #{#n1ql.filter} #{[0]}")
@ScanConsistency(query=QueryScanConsistency.NOT_BOUNDED)
Flux<String> findIdByDynamicN1ql(String queryStatement);
airportRepository.findIdByDynamicN1ql("name ='somename' OR state = 'CA'").toStream().collect(Collectors.toList());