Beginner Issues

Hi everyone,

I am a carpenter, have no formal programming education and am just really enjoying learning about programming and seeing what I can actually implement in my small business. As such, my questions might be very stupid.
For my business I’m trying to implement a small database which contains information about projects etc. XTDB and its approach have just seemed very clever to me, so that’s what I tried using. So far I’ve tried building sort of an API that can take variables, which are then used to construct queries to the DB. I can’t seem to make it work though, and I’ll try to explain where I’m stuck with this example.

I have this filtering query, which works perfectly like it is:

(defn example-filter
(xt/q my-node '(-> (from :clients [client/name client/city xt/id])
(where (= client/name “Name”)
(= client/city “Village”)))))

I would now like to create a function that takes the value for the client/name field and the client/city as an argument, and constructs the query dynamically. The simplest approach I’ve tried looked like this:

(defn filter-fields-for-condition [table return-fields condition]
(let [query (list 'from table return-fields)
predicate condition
]
(xt/q my-node `(-> ~query ~predicate))))

Sadly, it didn’t work, and I’ve iterated over it for a full day without finding a working solution. Am I missing something obvious? Is this kind of approach even sensible? It might absolutely be my lacking experience with clojure or programming in general that’s causing this.
After having no success with this I tried to make it work with honeysql and next.jdcb, which basically worked immediately.

I’d appreciate any pointers, thanks a lot in advance!

Best,

Florian

1 Like

Hey @florian thanks for sharing some of your story - it’s great that you have already discovered Clojure and XTDB in your programming adventures :slightly_smiling_face:

Short of booting up a REPL and working through your example slowly myself, I had a quick back-and-forth with ChatGPT and I think its reasoning is sound: take a look - what do you think?

tl:dr;

because query was constructed using list, the ~query expands into ((from :clients [client/name client/city xt/id]))

Does that match up with the error you observed? Sharing any errors you see is always helpful, btw.

Normally I shy away from macros and elegant (when it works) syntax quoting entirely :sweat_smile:

Hey Jeremy, thanks a lot for taking the time to answer - really appreciate it! Honestly Chatgpt has been my primary teacher to get me to this example, i’ve been going back and forth with it for almost a day. I do think that you’re right though, something goes wrong during the expansion, and no combination of quoting etc. has solved it for me. It made me think that either I’m missing some critical piece of the puzzle, or this approach is running contrary to the design philosophy. Sadly there is not yet a large number of tutorials or courses available for v2, so I wasn’t able to confirm which one it is. I just wonder what is the XTDB way to create an API that constructs queries etc? Thanks a lot!

1 Like

I don’t think this is the case (XTQL has been designed for programmatic composition with Clojure, after all), it’s much more likely that ChatGPT is simply not so smart with subtly complex Clojure and is getting some important details wrong. I expect it’s probably easier to learn about the likes of macros and syntax quoting by reading real docs/blogs etc. and then only return to ChatGPT once you have the deeper insights into how these things work and how to debug them.

Right now I’d say use SQL for the time being, and for Clojure go with HoneySQL over JDBC - this will give you the best stable experience, as the XTQL API is naturally more experimental / subject to change. Also ChatGPT is pretty great with SQL (though it’s remarkably good at XTQL also…somehow :sweat_smile:)

Yeah, much more likely that my lacking experience is the cause for my issues - I’ll stick with HoneySQL for now. Thanks a lot for your advice, and for taking the time to answer. Really looking forward to see where XTDB is going, and to maybe trying out XTQL again once more ressources are available on it!

1 Like