gpt4 book ai didi

swift - 我可以在运行时根据条件数组在 Couchbase Lite swift 中创建 Where 子句吗?

转载 作者:行者123 更新时间:2023-11-28 13:23:43 25 4
gpt4 key购买 nike

我正在使用 Couchbase Lite 2.6,我想在运行时构建我的查询,我通过 API 接收条件列表([“Key”:“value”]),我需要使用它来过滤我的本地数据库并显示值。

有什么方法可以使用 QueryBuilder 实现吗?

我在 Couchbase 论坛上发现了类似的讨论,但它已经很老了,所以该解决方案不再有效。

https://forums.couchbase.com/t/filter-on-array-filed-in-couchbase-and-couchbase-lite/3983/4

最佳答案

因为这个问题是针对 Swift 的,所以如果能在 swift 中得到答案就好了:它与@deniswsrosa 提到的代码相同,但在 siwft 中。

  let name = "foo";
let surname = "bar";
let ex = Expression.property("type").equalTo(Expression.string("user"));
if(name != nil) {
ex.and(Expression.property("name").equalTo(Expression.string(name)));
}

if(surname != nil) {
ex.and(Expression.property("surname").equalTo(Expression.string(surname)));
}

let query = QueryBuilder
.select(SelectResult.all())
.from(DataSource.database(DatabaseManager.getDatabase()))
.where(ex)
.limit(Expression.int(10));

关于swift - 我可以在运行时根据条件数组在 Couchbase Lite swift 中创建 Where 子句吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58709104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com