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)
;;"The values that you posted are identical.
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"]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)"]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
Thanks for the help yall. Apologies for the typo in the original problem statement.