honeysql

2024-06-13T06:00:42.116579Z

seancorfield 2024-06-13T06:09:46.048009Z

Mostly a documentation update but a few bug fixes and enhancements too.

2024-06-13T11:05:21.594699Z

2024-06-13T16:28:15.547449Z

István Karaszi 2024-06-13T17:50:59.151219Z

I could not find an example how I could phrase an SQL insert statement like this:

INSERT INTO collection_products(collection_id, product_id)
SELECT
  ?,
  id
FROM
  products
WHERE
  external_id = ANY (?)
RETURNING
  *;

✅ 1
István Karaszi 2024-06-13T17:53:23.061589Z

The following is not working for me:

{:insert-into :collection_products
         :columns [:collection_id :product_id]
         :values [{:collection_id collection-id
                   :product_id {:select [:id]
                                :from :products
                                :where [:= :external_id
                                        [:any (into-array String external-ids)]]}}]}

István Karaszi 2024-06-13T17:53:48.894159Z

It formats this statement:

INSERT INTO collection_products(collection_id, product_id)
  VALUES (?,(
      SELECT
        id
      FROM
        products
      WHERE
        external_id = ANY (?)));
But that can fail if there are no matches

seancorfield 2024-06-13T17:58:24.780839Z

user=> (sql/format {:insert-into [[:collection-products [:collection-id :product-id]] {:select [42, :id] :from :products :where [:= :external-id [:any 13]]}] :returning :*})
["INSERT INTO collection_products (collection_id, product_id) SELECT ?, id FROM products WHERE external_id = ANY(?) RETURNING *" 42 13]
user=>

István Karaszi 2024-06-13T18:01:06.905669Z

Gosh, why couldn’t I find this?!

István Karaszi 2024-06-13T18:01:08.459059Z

Sorry!

István Karaszi 2024-06-13T18:01:15.799019Z

Thank you!

seancorfield 2024-06-13T18:01:28.005579Z

NP. It's quite subtle to get all the pieces together.

István Karaszi 2024-06-13T18:02:48.933989Z

And it does work, thank you again!

seancorfield 2024-06-13T18:07:03.943189Z

I think for your insert into with values, since you're providing hash maps, you don't want :columns in there but, yeah, as you noted the SQL won't work for no matches.

István Karaszi 2024-06-13T18:08:26.143639Z

Yes, very true. That was another mistake.

István Karaszi 2024-06-13T18:20:53.716929Z

I tried to find this example on the site you linked, but I could not find it. Am I blind?

seancorfield 2024-06-13T18:50:25.721609Z

These examples?

seancorfield 2024-06-13T18:50:50.399649Z

Doesn't have :returning (or a where clause on the select) but otherwise is very similar I thought?

István Karaszi 2024-06-13T18:52:44.515139Z

I missed the ' at the beginning and I thought this is the other set of examples with the helper functions

👍🏻 1
seancorfield 2024-06-13T18:54:39.708939Z

The "Datalog"-style DSL with quoted symbols instead of keywords 🙂

👍 1
István Karaszi 2024-06-13T18:55:10.775129Z

Now, I can see that. I haven’t used Datalog yet