英文:
How to use query limit in custom Couchbase Lite plugin in Android (Java)
问题
try {
String dbName = call.getString("dbName");
Integer limit = call.getInt("limit");
Query query = QueryBuilder
.select(
SelectResult.expression(Meta.id),
SelectResult.property("title")
)
.from(DataSource.database(databases.get(dbName)));
if (limit != null) {
query.limit(Expression.intValue(limit));
}
ResultSet resultSet = query.execute();
JSONArray array = new JSONArray();
for (Result result : resultSet) {
array.put(new JSONObject(result.toMap()));
}
JSObject ret = new JSObject();
ret.put("result", true);
ret.put("rows", array);
call.success(ret);
} catch (final Exception e) {
call.reject("Error documents", e);
}
英文:
I'm working on a custom plugin, Couchbase Lite, for Ionic Capacitor. I'm working on the Android version right now and I cannot get the query limit to work. The limit parameter is optional so I check if it is null or not. If it is not null, I want to append the limit to the existing query variable. But I get the error of "Cannot resolve method 'limit' in 'Query'". Is there any way I can do this without writing two query builder with one having limit and the other one not having limit?
try {
String dbName = call.getString("dbName");
Integer limit = call.getInt("limit");
Query query = QueryBuilder
.select(
SelectResult.expression(Meta.id),
SelectResult.property("title")
)
.from(DataSource.database(databases.get(dbName)))
if (limit != null){
query.limit(Expression.intValue(limit));
}
ResultSet resultSet = query.execute();
JSONArray array = new JSONArray();
for (Result result : resultSet) {
array.put(new JSONObject(result.toMap()));
}
JSObject ret = new JSObject();
ret.put("result", true);
ret.put("rows", array);
call.success(ret);
} catch (final Exception e) {
call.reject("Error documents ", e);
}
答案1
得分: 1
你可以在执行限制之前将查询转换为From对象,如:((From) query).limit(Expression.intValue(limit))。
英文:
You can cast query to From object before doing the limit as ((From) query).limit(Expression.intValue(limit)).
答案2
得分: 0
@Pasin的回答很简洁也是正确的。但如果你想避免强制转换,你可以这样做:
Query query = QueryBuilder
.select(
SelectResult.expression(Meta.id),
SelectResult.property("title")
)
.from(DataSource.database(databases.get(dbName)));
if (limit != null){
query.limit(Expression.intValue(limit));
}
ResultSet resultSet = query.execute();
英文:
@Pasin's answer is simple and correct. If you would like to avoid casting, though, you can do this:`
From query = QueryBuilder
.select(
SelectResult.expression(Meta.id),
SelectResult.property("title")
)
.from(DataSource.database(databases.get(dbName)));
if (limit != null){
query.limit(Expression.intValue(limit));
}
ResultSet resultSet = query.execute();
`
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论