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

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

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();

`

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:

确定