如何在自定义的Couchbase Lite插件中在Android(Java)中使用查询限制。

huangapple go评论95阅读模式
英文:

How to use query limit in custom Couchbase Lite plugin in Android (Java)

问题

  1. try {
  2. String dbName = call.getString("dbName");
  3. Integer limit = call.getInt("limit");
  4. Query query = QueryBuilder
  5. .select(
  6. SelectResult.expression(Meta.id),
  7. SelectResult.property("title")
  8. )
  9. .from(DataSource.database(databases.get(dbName)));
  10. if (limit != null) {
  11. query.limit(Expression.intValue(limit));
  12. }
  13. ResultSet resultSet = query.execute();
  14. JSONArray array = new JSONArray();
  15. for (Result result : resultSet) {
  16. array.put(new JSONObject(result.toMap()));
  17. }
  18. JSObject ret = new JSObject();
  19. ret.put("result", true);
  20. ret.put("rows", array);
  21. call.success(ret);
  22. } catch (final Exception e) {
  23. call.reject("Error documents", e);
  24. }
英文:

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?

  1. try {
  2. String dbName = call.getString("dbName");
  3. Integer limit = call.getInt("limit");
  4. Query query = QueryBuilder
  5. .select(
  6. SelectResult.expression(Meta.id),
  7. SelectResult.property("title")
  8. )
  9. .from(DataSource.database(databases.get(dbName)))
  10. if (limit != null){
  11. query.limit(Expression.intValue(limit));
  12. }
  13. ResultSet resultSet = query.execute();
  14. JSONArray array = new JSONArray();
  15. for (Result result : resultSet) {
  16. array.put(new JSONObject(result.toMap()));
  17. }
  18. JSObject ret = new JSObject();
  19. ret.put("result", true);
  20. ret.put("rows", array);
  21. call.success(ret);
  22. } catch (final Exception e) {
  23. call.reject("Error documents ", e);
  24. }

答案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的回答很简洁也是正确的。但如果你想避免强制转换,你可以这样做:

  1. Query query = QueryBuilder
  2. .select(
  3. SelectResult.expression(Meta.id),
  4. SelectResult.property("title")
  5. )
  6. .from(DataSource.database(databases.get(dbName)));
  7. if (limit != null){
  8. query.limit(Expression.intValue(limit));
  9. }
  10. ResultSet resultSet = query.execute();
英文:

@Pasin's answer is simple and correct. If you would like to avoid casting, though, you can do this:`

  1. From query = QueryBuilder
  2. .select(
  3. SelectResult.expression(Meta.id),
  4. SelectResult.property("title")
  5. )
  6. .from(DataSource.database(databases.get(dbName)));
  7. if (limit != null){
  8. query.limit(Expression.intValue(limit));
  9. }
  10. ResultSet resultSet = query.execute();

`

huangapple
  • 本文由 发表于 2020年8月22日 20:28:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63536184.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定