-
Notifications
You must be signed in to change notification settings - Fork 0
/
querybuilder.clj
59 lines (49 loc) · 2.72 KB
/
querybuilder.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
(ns sqlforcql.querybuilder
(:require [qbits.hayt :refer [allow-filtering columns count* select where]]
[taoensso.timbre :refer [debug]]
[clojure.spec.alpha :as s]
[sqlforcql.specs :as specs]))
(defn keywordize-table-name
"Converts the name of the table `table-name` into a keyword. If the name
of the keyspace is also specified, then it converts `keyspace.table-name`
into a keyword."
([table-name]
{:pre [(s/valid? ::specs/name-or-with-keyspace [table-name])]}
(keyword table-name))
([keyspace table-name]
{:pre [(s/valid? ::specs/name-or-with-keyspace [keyspace table-name])]}
(keyword (str keyspace "." table-name))))
(defn get-all-query
"Constructs a `select * from table-name` query."
([table-name]
{:pre [(s/valid? ::specs/name-or-with-keyspace [table-name])]}
(select (keywordize-table-name table-name)))
([keyspace table-name]
{:pre [(s/valid? ::specs/name-or-with-keyspace [keyspace table-name])]}
(select (keywordize-table-name keyspace table-name))))
(defn get-count-query
"Constructs a `select count(*) from table-name` query."
([table-name]
{:pre [(s/valid? ::specs/name-or-with-keyspace [table-name])]}
(select (keywordize-table-name table-name) (columns (count*))))
([keyspace table-name]
{:pre [(s/valid? ::specs/name-or-with-keyspace [keyspace table-name])]}
(select (keywordize-table-name keyspace table-name) (columns (count*)))))
(defn get-by-pk-col-query
"Constructs a `select * from table-name where pk-col-name = value` query."
([table-name pk-col-name-value-map]
{:pre [(s/valid? ::specs/name-or-with-session-keyspace-pk-col-val-map [table-name pk-col-name-value-map])]}
(select (keywordize-table-name table-name) (where pk-col-name-value-map)))
([keyspace table-name pk-col-name-value-map]
{:pre [(s/valid? ::specs/name-or-with-session-keyspace-pk-col-val-map [keyspace table-name pk-col-name-value-map])]}
(select (keywordize-table-name keyspace table-name) (where pk-col-name-value-map))))
(defn get-by-non-pk-col-query
"Constructs a `select * from table-name where non-pk-col = value allow filtering` query."
([table-name non-pk-col-name-value-map]
{:pre [(s/valid? ::specs/name-or-with-session-keyspace-pk-col-val-map [table-name non-pk-col-name-value-map])]}
(debug "use allow filtering clause")
(select (keywordize-table-name table-name) (where non-pk-col-name-value-map) (allow-filtering)))
([keyspace table-name non-pk-col-name-value-map]
{:pre [(s/valid? ::specs/name-or-with-session-keyspace-pk-col-val-map [keyspace table-name non-pk-col-name-value-map])]}
(debug "use allow filtering clause")
(select (keywordize-table-name keyspace table-name) (where non-pk-col-name-value-map) (allow-filtering))))