honeysql

Discussion of https://github.com/seancorfield/honeysql :slightly_smiling_face:
gon 2018-10-26T10:10:00.000400Z

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.

gon 2018-10-26T10:10:17.000600Z

(honeysql.format/format (honeysql.helpers/where [:= :foo "0001"] [:= :bar "0002"]))

gon 2018-10-26T10:10:28.000800Z

[" WHERE (foo = ? AND bar = ?)" "0001" "0002"]

gon 2018-10-26T10:10:49.001100Z

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))))

gon 2018-10-26T10:11:38.001700Z

so that I can do

(honeysql.format/format (append-and [:= :foo "0001"] [:= :bar "0002"]))
having as a result
[" AND (foo = ? AND bar = ?)" "0001" "0002"]

gon 2018-10-26T10:11:48.001900Z

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 ?

seancorfield 2018-10-26T14:18:02.003100Z

Sounds like you want merge-where (which already exists in the library) @gon

gon 2018-10-26T14:52:02.004600Z

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"]

seancorfield 2018-10-26T17:15:18.005800Z

merge-where won't add where if it's already present. How are you building the original piece of the query?

seancorfield 2018-10-26T17:15:31.006100Z

@gon ^

seancorfield 2018-10-26T17:16:37.006900Z

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.

seancorfield 2018-10-26T17:17:06.007500Z

I really would not advise trying to use HoneySQL to produce SQL fragments, rather than whole queries.