JsonObject paramsObject = JsonObject.create();
paramsObject.put("name",args[0]);
JsonArray paramsArray = JsonArray.create();
paramsArray.add(args[0]);
Query query = ParametrizedQuery.parametrized("SELECT * FROM bucket where field='$name' limit 1", paramsObject);
System.out.println(query.n1ql().toString());
QueryResult res = bucket.query(query);
for ( QueryRow row : res.allRows()) {
System.out.println("RESULT: "+row.value());
}
query = ParametrizedQuery.parametrized("SELECT * FROM bucket where field='$1' limit 1", paramsArray);
System.out.println(query.n1ql().toString());
res = bucket.query(query);
for ( QueryRow row : res.allRows()) {
System.out.println("RESULT: "+row.value());
}
query = ParametrizedQuery.parametrized("SELECT * FROM bucket where field='"+args[0]+"' limit 1", paramsArray);
System.out.println(query.n1ql().toString());
res = bucket.query(query);
for ( QueryRow row : res.allRows()) {
System.out.println("RESULT: "+row.value());
}
And just the last one works properly (the output):
{"args":["any_command_line_value"],"statement":"SELECT * FROM bucket where field='$name' limit 1"}
{"args":["any_command_line_value"],"statement":"SELECT * FROM bucket where field='$1' limit 1"}
{"args":["any_command_line_value"],"statement":"SELECT * FROM bucket where field='any_command_line_value' limit 1"}
RESULT: {"bucket": ....
hi @huhh,
there is a bug in N1QL DP4 for the parametrized query with named placeholders, it doesn’t work (which is why we used $1 format in our examples in the blog post).
it’s only true for querying via the POST interface with JSON request format, but that’s unfortunately the one we are using in the SDK (since it allows the maximum range of features to be used), so it may be the case that the same query works in cbq client.
Best workaround for Java is to use the $1 syntax. The issue was since fixed (Loading...) and should be available in the next public release of N1QL (either as a separate download or part of developer preview for next major version of Couchbase Server).
I am facing the problem with below query parametrized query.
String ramainingPackageByExceptionQuery = "SELECT shuTckNr AS trackingNumber FROM “+”"+bucketName+" "+
“WHERE ANY sort IN busOp SATISFIES sort.opPlnDt = now_str(‘1111-11-11’) END AND evtTypCd = $1”; log.info(“ramainingPackageByExceptionQuery============================>>>>>>>”+ramainingPackageByExceptionQuery);
int count = 03;
The problem is when i see log.info(“placeholderValue===========”+placeholderValue); its printing ["“03"”]. however my eventcode value is eventcode=“03”.
Subhashni as per your reply we can not pass String as parameter in N1Q1L query. Is it a restriction?? What if i have string field in my docs and i want to use that in where clause?
I found the solution for above problem, it was not the couchbase issue instead it was issue with the query parameter which is getting passed from rest Layer. Can you suggest me options of passing array as parameter for IN. Like for below query
SELECT count(*) FROM default
WHERE ANY sort IN busOp SATISFIES sort.opPlnDt = now_str(‘1111-11-11’) END
AND eqpNr IN [“04856374”,“04856375”,“04856378”] AND pkgPrcStsCd = “01”