This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-10-08
Channels
- # asami (22)
- # babashka (35)
- # beginners (4)
- # calva (76)
- # cider (7)
- # clj-on-windows (89)
- # clojure (30)
- # clojure-europe (25)
- # clojurescript (10)
- # conjure (46)
- # fulcro (13)
- # gratitude (5)
- # lambdaisland (4)
- # lsp (13)
- # malli (5)
- # membrane (6)
- # nbb (1)
- # off-topic (11)
- # re-frame (2)
- # releases (1)
- # shadow-cljs (45)
- # xtdb (4)
Ok, another fantastically basic question - I need help with using parameters/external data/inputs in queries. This is either from 1) not knowing Datomic or Datalog at all, or I’m just very very slow. Anyway… some examples needed. There is one example of adding #“somestring” at the end of a query, but how that works eludes me. All pointers appreciated 😊
Thank you! So, this example does not do what I naively wanted:
(let [blah "01"]
(d/q '[:find ?text :where [?m :text ?text][?m :code blah]] @asami-conn))
So… in another world (SQL) it would be something like a prepared statement with parameters and stuff. But here I’m guessing something like appending parameters at the end and they get bound to… things?
So you can either modify the string, to include some variable from the local context, or you prepare a query, and then pass the variables along args, similarly to how you might with clojure.core/format
I could try the latter, but it seems redundant when Asami (and Datomic) don't use query strings. We use query structures
The first approach that some people use in the above is to unquote the symbol into place
Avoiding the query call for a moment, you can use:
(let [blah "01"]
`[:find ?text :where [?m :text ?text][?m :code ~blah]])
I usually build the query a little more manually. In this case, the query is a vector so you can just conj
the last constraint on:
(conj '[:find ?text :where [?m :text ?text]] ['?m :code blah])
but when I'm building a query like that, then I'll usually use the map form instead:
{:find '[?text]
:where (conj ['[?m :text ?text]] ['?m :code blah])}
(sorry for going back to edit some of these. I had a late night and I only just got up) 🙂
No worries, thanks so much for taking the time. If I want to study this more, why is it that my initial attempt doesn’t work? Anything I can read up on regarding the execution order or something? I’m missing the forest for the trees, I think. 🙂
And thanks for the examples, that helps me understand what you mean by query structures. I’m getting there, slowly 😋
execution order is actually calculated, unless you explicitly ask it not to. That's done by adding, :planner :user
to the end of the query arguments