【问题标题】:Passing ?input map as Datomic query argument将 ?input 映射作为 Datomic 查询参数传递
【发布时间】:2019-08-19 10:52:01
【问题描述】:

如何将映射作为?input 传递给 Datomic 查询并解构映射以匹配事实?

当我运行以下查询时,我得到一个NullPointerException

(d/q '[:find ?e
       :where
       :in $ ?input
       [?e :amount ?amount]
       [(:amount ?input) ?amount]]
     (d/db conn)
     {:amount 123.0M})
=> Syntax error (NullPointerException) compiling at ...

但是,将金额作为输入参数传递是有效的:

(d/q '[:find ?e
       :where
       :in $ ?amount
       [?e :amount ?amount]
     (d/db conn)
     123.0M)
=> [[1234]]

【问题讨论】:

  • 也许您只需要在 Datomic 之外进行解构,就像您的工作示例中一样

标签: database clojure datomic


【解决方案1】:

您不能将地图传递到 Datalog 查询中,您只能使用标量、元组、集合和关系:

Datomic Query Bindings

如果您的地图比您的示例复杂得多,并且需要使用其中的许多值,则必须在查询之外(如@Alan Thompson 建议的那样)解构它并将值作为元组传递:

(let [input-fn (juxt :amount :timestamp :quantity)
      input-data {:timestamp "29/08/2019" :quantity 3 :amount 123.0}
      inputs (input-fn input-data)]
  (d/q '[:find ?e
         :in $ [?amount ?timestamp ?quantity]
         :where
         [?e :amount ?amount]
         [?e :timestamp ?timestamp]
         [?e :quantity ?quantity]]
    (d/db conn)
    inputs))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    相关资源
    最近更新 更多