I tend to use the query data structure directly, personally. When the readme just shows how a helper is used, I usually evaluate it at the repl to see the structure of the map it returns.
Does / should honeysql support inlining arrays?
(def sql (sql/format (-> (h/insert-into :users)
;; (sql/columns :foo)
(h/values [{:foo (into-array [1 2 3])}]))
{:inline true}))
["INSERT INTO users (foo) VALUES ([Ljava.lang.Long;@34b3d918)"]
Does it work in v1 and just not in v2?
@seancorfield Just tried with v1, same thing
There are some situations where HoneySQL expands collections and others where it assumes the JDBC library will handle the value.
Oh, is this just about inlining? What should an array inline into? And is it portable across other databases?
(I'm pretty sure the answer to 3) is "no")
There is an ANSI array standard now. I tried this:
(def array-sql2
(sql/format {:insert-into :foo
:values [{:foo [:raw (format "array [%s]" (str/join "," [1 2 3]))]}]}))
and that worked, but I haven't tested with e.g. postgrescan I do something like this?
:select [:raw "ARRAY['hello','bob','and','sally']"]
This comes back with: column :raw does not exist
i think there's a (sql/raw "...")
Anyway, this seemed to work:
(pg/execute! db ["select ARRAY['hello','bob','and','sally'] from users limit 1"])
so I think the standard (ANSI) sql notation for arrays is ARRAY[1,2,3]
. Maybe :inline
could do that. More research needed probably.{:select [[[:raw ...]]]}
because SELECT takes a sequence of things, and those things can be aliases (which are wrapped in a sequence) and the thing you would alias is a function call [:raw ...]
That looks slightly less weird with the select
helper but still a bit odd.
and in v2 there's :array
confirming that :select [[[:raw "ARRAY['hello','bob','and','sally']"]]]
works.
also :select [[[:array [1 2 3]]]]
works
and also [:array (into-array [1 2 3])]
works. TIL!
I'm not sure how this would compose with :inline
still, e.g. when you insert values. Can you do {:values [{:foo [:array [1 2 3]]}]}
?
it worked!
(def sql (sql/format {:insert-into :foo
:values [{:foo [:array [1 2 3]]}]}
{:inline true}))
["INSERT INTO foo (foo) VALUES (ARRAY[1, 2, 3])"]
This lib is truly amazing ;)
Yeah, :inline
knows how to inline basic stuff: strings, numbers, booleans, but not much else.
https://github.com/seancorfield/honeysql/blob/v2/src/honey/sql.cljc#L149-L155
Since [:array [1 2 3]]
itself expands to ARRAY[?, ?, ?]
and params, then :inline
can handle it (by just inlining those numbers back into the SQL).