-
Notifications
You must be signed in to change notification settings - Fork 5
/
pred.cljc
39 lines (33 loc) · 1.07 KB
/
pred.cljc
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
(ns inquery.pred
#?(:clj (:require [clojure.string :as s])
:cljs (:require [clojure.string :as s])))
(defn check-pred [pred]
(if (fn? pred)
(.invoke pred)
(throw (ex-info "predicate should be a function" {:instead-got pred}))))
(defn value? [v]
(or (number? v)
(seq v)))
(defn- remove-start-op
[q op]
(if (and (value? q)
(value? op))
(when (s/starts-with? (s/upper-case q)
(s/upper-case op))
(subs q (count op)))
q))
(defn remove-start-ops [qpart]
"removes an op from part of the query that starts with it (i.e. AND, OR, etc.)
'and dog = :bow' => ' dog = :bow'
'or dog = :bow' => ' dog = :bow'
'xor dog = :bow' => 'xor dog = :bow' ;; xor is not a SQL op
"
(let [ops #{"or" "and"}] ;; TODO: add more when/iff needed
(or (some #(remove-start-op qpart %) ops)
qpart)))
(defn with-prefix [prefix qpart]
(case (s/lower-case (or prefix ""))
"where" (if (seq qpart)
(str prefix " " (remove-start-ops qpart))
qpart)
(str prefix " " qpart)))