-
Notifications
You must be signed in to change notification settings - Fork 0
/
hack_hugsql.clj
29 lines (26 loc) · 1.01 KB
/
hack_hugsql.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
(ns org.tovictory.db.hack-hugsql
(:require [robert.hooke :refer [add-hook]]
[clojure.string :as str]
[hugsql.parser :as hp]
[org.tovictory.db.hug-params]))
(defn- hack-pdef [{sql :sql {req :require nm :name} :hdr :as pdef}]
(let [dynamic-where (= ":D" (last nm))
req (if dynamic-where (conj req "[org.tovictory.db.hugwhere :refer [where]]") req)
sql (if dynamic-where
(mapv #(if (vector? %)
(let [[s e] %
s (str/triml s)]
(if (and (= :end e) (not= \( (first s)))
[(format "(where params \"%s\")" s) :end]
%))
%)
sql)
sql)]
(assoc (assoc-in pdef [:hdr :require] req) :sql sql)))
(defn- parse-hook
([f sql] (parse-hook f sql {}))
([f sql opts]
(let [pdefs (f sql opts)]
(mapv hack-pdef pdefs))))
(defn hack-hugsql []
(add-hook #'hp/parse #'parse-hook))