File tree Expand file tree Collapse file tree 6 files changed +27
-5
lines changed Expand file tree Collapse file tree 6 files changed +27
-5
lines changed Original file line number Diff line number Diff line change 1
1
(ns qbits.hayt
2
2
" This namespace contains aliases for qbits.dsl.*, qbits.fns and qbits.utils"
3
- (:refer-clojure :exclude [update])
3
+ (:refer-clojure :exclude [update group-by ])
4
4
(:require
5
5
[qbits.hayt.ns :as uns]
6
6
[qbits.commons.jvm :refer [compile-if-ns-exists]]
Original file line number Diff line number Diff line change 300
300
(-> sb
301
301
(str! " SELECT " )
302
302
(emit-row! (assoc q :from table)
303
- [:columns :from :where :order-by :limit :allow-filtering ])))
303
+ [:columns :from :where :group-by : order-by :limit :allow-filtering ])))
304
304
:insert
305
305
(fn [sb q table]
306
306
(-> sb
546
546
(fn [sb q b]
547
547
(str! sb " IF" (if (not b) " NOT " " " ) " EXISTS" ))
548
548
549
+ :group-by
550
+ (fn [sb q columns]
551
+ (str! sb " GROUP BY " (cql-identifiers-join-comma columns)))
552
+
549
553
:order-by
550
554
(let [xform-inner (comp map-cql-identifier interpose-space)
551
555
xform (comp (map #(transduce xform-inner string-builder %))
Original file line number Diff line number Diff line change 1
1
(ns qbits.hayt.dsl
2
- (:refer-clojure :exclude [update])
2
+ (:refer-clojure :exclude [update group-by ])
3
3
(:require [qbits.hayt.ns :as uns]))
4
4
5
5
(doseq [module '(statement clause)]
Original file line number Diff line number Diff line change 1
- (ns qbits.hayt.dsl.clause )
1
+ (ns qbits.hayt.dsl.clause
2
+ (:refer-clojure :exclude [group-by]))
2
3
3
4
(defn columns
4
5
" Clause: takes columns identifiers
@@ -23,6 +24,11 @@ ex: (columns :foo \"bar\" :baz) "
23
24
[n]
24
25
{:limit n})
25
26
27
+ (defn group-by
28
+ " Clause: expects 1 or more columns.
29
+ ex: (group-by col1 col2)"
30
+ [& columns] {:group-by columns})
31
+
26
32
(defn order-by
27
33
" Clause: takes vectors of 2 elements, where the first is the column
28
34
identifier and the second is the ordering as keyword.
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ Takes a table identifier and additional clause arguments:
8
8
9
9
* columns (defaults to *)
10
10
* where
11
+ * group-by
11
12
* order-by
12
13
* limit
13
14
* only-if"
Original file line number Diff line number Diff line change 1
1
(ns qbits.hayt.core-test
2
- (:refer-clojure :exclude [update])
2
+ (:refer-clojure :exclude [update group-by ])
3
3
(:use clojure.test
4
4
qbits.hayt
5
5
qbits.hayt.codec.joda-time
41
41
(limit 100 )
42
42
(allow-filtering true ))
43
43
44
+ " SELECT * FROM foo GROUP BY bar, \" baz\" ;"
45
+ (select :foo
46
+ (group-by :bar " baz" ))
47
+
48
+ " SELECT * FROM foo WHERE foo > 1 AND foo < 10 GROUP BY foo, \" bar\" LIMIT 10;"
49
+ (select :foo
50
+ (where [[> :foo 1 ]
51
+ [< :foo 10 ]])
52
+ (group-by :foo " bar" )
53
+ (limit 10 ))
54
+
44
55
" SELECT * FROM foo ORDER BY bar desc;"
45
56
(select :foo
46
57
(order-by [:bar :desc ]))
You can’t perform that action at this time.
0 commit comments