Fork me on GitHub
#honeysql
<
2024-04-28
>
ag07:04:58

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-himik07:04:00

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.

ag09:04:59

I'm using sqlite.

p-himik09:04:37

How is that relevant?

ag09:04:08

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-himik09:04:12

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.

ag09:04:13

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-himik09:04:33

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

p-himik09:04:14

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

ag09:04:36

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)

ag09:04:04

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-himik09:04:57

> 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".

seancorfield18:04:10

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

😯 1
seancorfield18:04:25

@U0G75ARHC 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?

ag19:04:48

Thank you for your help @U04V70XH6. 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.