honeysql

ag 2024-04-28T07:03:58.536609Z

I can't find a way to do "INSERT OR IGNORE INTO", is there really no straightforward way? I ended up string replacing INSERT INTO after sql/format, and it looks lame.

p-himik 2024-04-28T07:46:00.146159Z

String replacement is not something you should ever do with HoneySQL. There's a chance that INSERT OR IGNORE can be replaced with INSERT ... ON CONFLICT ... DO NOTHING. If that's not the case, you can extend HoneySQL with your own clause, it's trivial.

ag 2024-04-28T09:00:59.063969Z

I'm using sqlite.

p-himik 2024-04-28T09:23:37.404679Z

How is that relevant?

ag 2024-04-28T09:28:08.333169Z

I think insert or ignore is the only more or less straightforward way with sqlite. I think I figured out register-clause! thing. It's not as trivial like you say. I mean after I figured it out, yes, now it looks trivial

p-himik 2024-04-28T09:32:12.243199Z

It depends on the things that you want to ignore. If there's an explicit list of unique columns, you can just do INSERT INTO t VALUES (...) ON CONFLICT (col1, col2,...) DO NOTHING. SQLite supports that syntax just fine.

ag 2024-04-28T09:36:13.524879Z

Ah... okay. Thank you for your help. I almost plucked my eyebrow trying to figure it out before I did the lame thing with str/replace. I just needed a custom clause.

p-himik 2024-04-28T09:37:33.197529Z

Just in case - HoneySQL docs are great. :) Lots of example of pretty much every scenario.

p-himik 2024-04-28T09:39:14.757679Z

And don't ignore modern LLM tools for cases when an answer is easy and quick to check. I haven't looked carefully enough at the answer to see whether it's perfect, but at least it describes the building blocks: https://www.phind.com/search?cache=i0byi836jc6grrt3vhzt5fcc

ag 2024-04-28T09:49:36.152069Z

That one could be for v1 of honeysql, because the formatter there takes a single param. What I did is that I simply grabbed the example from v2 readme and changed it very slightly:

(sql/register-clause!
 :insert-or-ignore-into
 (fn [clause x]
   (let [[sql & params]
         (if (ident? x)
           (sql/format-expr x)
           (sql/format-dsl x))]
     (into [(str (sql/sql-kw clause) " " sql)] params)))
 :insert-into)

ag 2024-04-28T09:53:04.457219Z

And don't ignore modern LLM tools for cases when an answer is easy and quick to check.chatgpt4 hallucinated horribly on this for me

p-himik 2024-04-28T09:57:57.900469Z

> That one could be for v1 of honeysql It's a mix of the two, I think. v1 didn't have honey.sql/register-clause! at all. I find Phind slightly better at coding than ChatGPT4. But yeah, hallucination is a problem - that's why I edited my comment above to include "when an answer is easy and quick to check".

seancorfield 2024-04-28T18:39:20.950019Z

https://cljdoc.org/d/com.github.seancorfield/honeysql/2.6.1126/doc/getting-started/other-databases#sqlite specifically talks about INSERT OR IGNORE INTO.

seancorfield 2024-04-28T18:40:10.082529Z

(so no, register-clause! isn't even needed here!)

😯 1
seancorfield 2024-04-28T18:42:25.114059Z

@ag Since this is in the docs, my question is: did you look and not find it? If so, how can I improve the docs so that you would have found this?

ag 2024-04-28T19:20:48.700829Z

Thank you for your help @seancorfield. You always so beautifully come to save the day, it never ceases to amaze me. The docs are great, just like most of the things you write and maintain. I'm not sure why I couldn't find that specific piece. I'm prototyping something, and instead of searching the documentation, I tried to find relevant code snippets on GitHub.