Hi all I just need to manage sql parameters and attach them to legacy queries through the hugsql's snippet machinery, The problem is that queries already have a 'WHERE' clause, and new honeysql's snippets recreate again the 'WHERE' string.
(honeysql.format/format (honeysql.helpers/where [:= :foo "0001"] [:= :bar "0002"]))
[" WHERE (foo = ? AND bar = ?)" "0001" "0002"]
I managed to overcome this through the following
(defmethod honey.format/format-clause :where-append [[_ pred] _]
(str " AND " (honey.format/format-predicate* pred)))
(defn append-and
""
[& args]
(hash-map :where-append (:where (apply honey.helpers/where args))))
so that I can do
(honeysql.format/format (append-and [:= :foo "0001"] [:= :bar "0002"]))
having as a result
[" AND (foo = ? AND bar = ?)" "0001" "0002"]
that I can insert right in the query. So the question is, is this the right thing to do or there is a easier way to achieve that ?
Sounds like you want merge-where
(which already exists in the library) @gon
well I tried but the clause 'WHERE' is still present
(honeysql.format/format (honeysql.helpers/merge-where [:= :foo "0001"] [:= :bar "0002"]))
["WHERE (foo = ? AND bar = ?)" "0001" "0002"]
merge-where
won't add where
if it's already present. How are you building the original piece of the query?
@gon ^
Oh, I see. You already have strings for the legacy SQL. No, HoneySQL is going to help you there, sorry. That's not within its design goals.
I really would not advise trying to use HoneySQL to produce SQL fragments, rather than whole queries.