-
Notifications
You must be signed in to change notification settings - Fork 7
/
helpers.clj
68 lines (55 loc) · 1.68 KB
/
helpers.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
(ns com.verybigthings.penkala.helpers
(:require [com.verybigthings.penkala.relation :refer [->Wrapped]]
[com.verybigthings.penkala.util :refer [q]]
[clojure.string :as str]))
(defn column
"Mark the subject as a column"
[subject]
(->Wrapped :column subject))
(defn value
"Mark the subject as a value"
[subject]
(->Wrapped :value subject))
(defn param
"Mark the subject as a param"
[subject]
(->Wrapped :param subject))
(defn literal
"Mark the subject as a literal"
[subject]
(->Wrapped :literal subject))
(defn unary-operator
"Mark the subject as an unary operator"
[subject]
(->Wrapped :unary-operator subject))
(defn binary-operator
"Mark the subject as a binary operator"
[subject]
(->Wrapped :binary-operator subject))
(defn ternary-operator
"Mark the subject as a ternary operator"
[subject]
(->Wrapped :ternary-operator subject))
(defn quoted-literal
"Quote the subject and mark it as a literal"
[subject]
(let [subject' (if (keyword? subject) (name subject) subject)]
(->Wrapped :literal (str "'" subject' "'"))))
(def match-to-escape-re (re-pattern "[,\\{}\\s\\\\\"]"))
(def escape-re (re-pattern "([\\\\\"])"))
(defn array-literal
"Stringify a collection as PostgreSQL array and mark it as a literal"
[value]
(let [sanitized-values
(map
(fn [v]
(cond
(nil? v)
"null"
(or (= "" v) (= "null" v) (and (string? v) (re-find match-to-escape-re v)))
(-> v (str/replace escape-re "$1") q)
:else v))
value)]
(quoted-literal (str "{" (str/join "," sanitized-values) "}"))))
(def l literal)
(def ql quoted-literal)