honeysql 2023-08-10

Hi. Is it possible to express this syntax (`merge into`) in Honey SQL? I'm using Snowflake as my data storage and I think https://docs.snowflake.com/en/sql-reference/sql/merge. I'm trying to implement an upsert operation with a single query. Thanks!

MERGE INTO t1 USING t2 ON t1.t1Key = t2.t2Key
    WHEN MATCHED AND t2.marked = 1 THEN DELETE
    WHEN MATCHED AND t2.isNewStatus = 1 THEN UPDATE SET val = t2.newVal, status = t2.newStatus
    WHEN MATCHED THEN UPDATE SET val = t2.newVal
    WHEN NOT MATCHED THEN INSERT (val, status) VALUES (t2.newVal, t2.newStatus);

Yeah, definitely non-standard. I don't think there's a built-in way to do something like that in an ergonomic way. But you can always extend HoneySQL by writing your own project-local custom formatter so that something like

{:merge-into [:t1 [:t2 [:= :t1/t1Key :t2/t2Key]]
              [[:and :matched [:= :t2/marked 1]] :delete]
              ...]}
becomes the string that you want.

Oh... what?! PG also has it!

Ahhh, in PG it's only in version 15, the latest one.

@joshuam Create a GH issue and I'll add it into the core.

Wow! Yes, thank you!

I'm planning a big HoneySQL blitz soon to try to get through the backlog of issues. Esp. the temporal stuff since I will need that for XTDB 2.0 🙂

> It was officially introduced in the https://en.wikipedia.org/wiki/SQL:2003 standard, and expanded in the https://en.wikipedia.org/wiki/SQL:2008 standard Wow... It's been there for 20 years. And when I needed something that INSERT ... ON CONFLICT ... couldn't handle, I had to write a terrible CTE or straight up split the thing into multiple expressions.

Thanks!

Thank you sir 🫡