honeysql

plooney 2024-12-20T15:49:22.010819Z

I’ve been working on supporting INSERT functionality for nectar-sql and noticed some interesting behavior WRT insert-into when coupled with the columns helper function. As far as I can tell, these should be equivalent:

(-> (sql/insert-into [:table :t] [:a :b :c])
    (sql/values [[1 2 3] [2 4 6]])
    (honey/format {:inline true :pretty true}))
;;==> "
;;     INSERT INTO table AS t (a, b, c)
;;     VALUES (1, 2, 3), (2, 4, 6)
;;"
But when I take the columns specification out of insert-into and instead use columns it produces:
(-> (sql/insert-into [:table :t])
    (sql/columns :a :b :c)
    (sql/values [[1 2 3] [2 4 6]])
    (honey/format {:inline true :pretty true}))
;;==> "
;;     INSERT INTO TABLE(t) (a, b, c)
;;     VALUES (1, 2, 3), (2, 4, 6)
;;"

p-himik 2024-12-20T15:53:28.390809Z

The values that you posted are identical.

p-himik 2024-12-20T16:00:51.076819Z

Just tried myself to confirm that they're different:

(-> (sql/insert-into [:table :t] [:a :b :c])
    (sql/values [[1 2 3] [2 4 6]])
    (honey/format {:inline true :pretty true}))
=> ["\nINSERT INTO table AS t (a, b, c)\nVALUES (1, 2, 3), (2, 4, 6)\n"]
(-> (sql/insert-into [:table :t])
    (sql/columns :a :b :c)
    (sql/values [[1 2 3] [2 4 6]])
    (honey/format {:inline true :pretty true}))
=> ["\nINSERT INTO TABLE(t) (a, b, c)\nVALUES (1, 2, 3), (2, 4, 6)\n"]

p-himik 2024-12-20T16:07:29.753709Z

It seems that without columns being a part of :insert-into, you have to provide an alias without wrapping it into a vector:

(-> (sql/insert-into :table :t)
    (sql/columns :a :b :c)
    (honey/format))
=> ["INSERT INTO table AS t (a, b, c)"]

🙌 1
seancorfield 2024-12-20T18:23:11.783649Z

Helpers unroll one level of brackets: {:insert-into [:table :t] ..} is what (h/insert-into :table :t) produces. But :insert-into has multiple syntax forms -- see the docs -- and there's the complication of whether to get the column names from hash maps provided to :values or from :columns (or from :insert-into itself). https://cljdoc.org/d/com.github.seancorfield/honeysql/2.6.1243/doc/getting-started/sql-clause-reference#insert-into-replace-into-patch-into

🙌 1
plooney 2024-12-20T22:23:46.163449Z

Thanks for the help yall. Apologies for the typo in the original problem statement.