Skip to content

Commit 924bcf1

Browse files
authored
Merge pull request #55 from mahinshaw/group-by
Add group-by clause for select statements.
2 parents bafd83a + 43b2b5e commit 924bcf1

File tree

6 files changed

+27
-5
lines changed

6 files changed

+27
-5
lines changed

src/clj/qbits/hayt.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(ns qbits.hayt
22
"This namespace contains aliases for qbits.dsl.*, qbits.fns and qbits.utils"
3-
(:refer-clojure :exclude [update])
3+
(:refer-clojure :exclude [update group-by])
44
(:require
55
[qbits.hayt.ns :as uns]
66
[qbits.commons.jvm :refer [compile-if-ns-exists]]

src/clj/qbits/hayt/cql.clj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@
300300
(-> sb
301301
(str! "SELECT ")
302302
(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])))
304304
:insert
305305
(fn [sb q table]
306306
(-> sb
@@ -546,6 +546,10 @@
546546
(fn [sb q b]
547547
(str! sb " IF" (if (not b) " NOT " " ") "EXISTS"))
548548

549+
:group-by
550+
(fn [sb q columns]
551+
(str! sb " GROUP BY " (cql-identifiers-join-comma columns)))
552+
549553
:order-by
550554
(let [xform-inner (comp map-cql-identifier interpose-space)
551555
xform (comp (map #(transduce xform-inner string-builder %))

src/clj/qbits/hayt/dsl.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(ns qbits.hayt.dsl
2-
(:refer-clojure :exclude [update])
2+
(:refer-clojure :exclude [update group-by])
33
(:require [qbits.hayt.ns :as uns]))
44

55
(doseq [module '(statement clause)]

src/clj/qbits/hayt/dsl/clause.clj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
(ns qbits.hayt.dsl.clause)
1+
(ns qbits.hayt.dsl.clause
2+
(:refer-clojure :exclude [group-by]))
23

34
(defn columns
45
"Clause: takes columns identifiers
@@ -23,6 +24,11 @@ ex: (columns :foo \"bar\" :baz) "
2324
[n]
2425
{:limit n})
2526

27+
(defn group-by
28+
"Clause: expects 1 or more columns.
29+
ex: (group-by col1 col2)"
30+
[& columns] {:group-by columns})
31+
2632
(defn order-by
2733
"Clause: takes vectors of 2 elements, where the first is the column
2834
identifier and the second is the ordering as keyword.

src/clj/qbits/hayt/dsl/statement.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Takes a table identifier and additional clause arguments:
88
99
* columns (defaults to *)
1010
* where
11+
* group-by
1112
* order-by
1213
* limit
1314
* only-if"

test/qbits/hayt/core_test.clj

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(ns qbits.hayt.core-test
2-
(:refer-clojure :exclude [update])
2+
(:refer-clojure :exclude [update group-by])
33
(:use clojure.test
44
qbits.hayt
55
qbits.hayt.codec.joda-time
@@ -41,6 +41,17 @@
4141
(limit 100)
4242
(allow-filtering true))
4343

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+
4455
"SELECT * FROM foo ORDER BY bar desc;"
4556
(select :foo
4657
(order-by [:bar :desc]))

0 commit comments

Comments
 (0)