我正在使用clojure的datomic.api。我希望重构一个有点复杂的数据日志查询,如下所示:
(datomic.api/q '[:find [?value ...] :in $ ?uid ?component :where
[...some clause...]
[...some other clause...]
(or-join [?entitlement ?component]
(and [...some conditional stuff...])
(and [...some other conditional stuff...]))]
db uid component)...into一些更具可读性的东西。我希望在let中本地绑定查询的(and...)组件,并通过数据日志列表中的名称引用它们。如下所示:
(datomic.api/q '[:find [?value ...] :in $ ?uid ?component :where
[...some clause...]
[...some other clause...]
(or-join [?entitlement ?component]
entitled-for-component
entitled-for-application)]
db uid component)let中的各种引用(以及datomic.api/q列表中的取消引用)都不起作用。有什么建议吗?
发布于 2015-08-06 01:45:34
Datomic使用rules来解决这个问题。
数据日志允许您将:where子句的集合打包到命名规则中。这些规则使得查询逻辑可以重用,也可以组合,这意味着您可以在查询时绑定查询逻辑的各个部分。
规则集被定义为列表列表,然后用作附加输入,并将datomic.api/q绑定到%字符。
(def rules [[(name-for-id restaurant-id?)
[restaurant-id? :restaurant/name name?]]])
(datomic.api/q '[:find ?name . :in $ % ?restaurant-id :where
(name-for-id restaurant-id?)] db rules 42)
=> "Milliways"请注意,datomic.api/q需要一个规则集,传递单个规则将不起作用。
规则中的第一个列表将规则名称定义为第一个项目,后跟一个或多个参数。后续向量包含一个或多个where子句。
另外,
与其他where子句一样,您可以在
-name之前指定一个数据库,以便将规则的范围限定到该数据库。数据库不能用作规则中的参数。
https://stackoverflow.com/questions/31838952
复制相似问题