-
Notifications
You must be signed in to change notification settings - Fork 8
/
query.clj
847 lines (705 loc) · 21.5 KB
/
query.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
(ns bitemyapp.revise.query
"The clojure REQL API"
(:refer-clojure :exclude [make-array get = not= < <= > >=
not + - * / mod contains? keys
merge reduce map filter mapcat
distinct count empty? nth
group-by type replace time or and
min max])
(:require [bitemyapp.revise.utils.case :refer [snake-case-keys
uppercase-keys]]
clojure.walk))
(defn datum?
[m]
(boolean
(#{:R_STR :R_NUM :R_NULL :R_BOOL :R_ARRAY :R_OBJECT} (::type m))))
(declare parse-val make-obj query)
(defn parse-map
"Decide if a map (not an optargs map) should be made with make-obj
(it has terms inside) or is a simple datum (primitive type)"
[m]
(let [vs (vals m)
vs (clojure.core/map parse-val vs)]
(if (some (complement datum?) vs)
(make-obj m)
{::type :R_OBJECT
::value (clojure.core/mapv (fn [k v]
{:key (name k)
:val v})
;; UUURRRRGH
(clojure.core/keys m) vs)})))
(defn parse-array
"Decide if an array (not an args array) should be made with make-array
(it has terms inside) or is a simple datum (primitive type)"
[sq]
(let [xs (clojure.core/mapv parse-val sq)]
(if (some (complement datum?) xs)
;; Manual invocation of query
{::type :MAKE_ARRAY
::args {::type :args
::value xs}}
{::type :R_ARRAY
::value xs})))
(defn parse-val
[x]
(letfn [(dt-map [t val]
(cond
(clojure.core/= :R_STR t)
{::type t
::value (name val)}
(clojure.core/= :R_ARRAY t)
(parse-array val)
(clojure.core/= :R_OBJECT t)
(parse-map val)
:else
{::type t
::value val}))]
(if (clojure.core/and (clojure.core/map? x) (::type x))
x
(-> (cond (clojure.core/or (keyword? x) (string? x)) :R_STR
(number? x) :R_NUM
(nil? x) :R_NULL
(vector? x) :R_ARRAY
(clojure.core/and (clojure.core/map? x)
(clojure.core/not (::type x))) :R_OBJECT
(clojure.core/or (false? x) (true? x)) :R_BOOL)
(dt-map x)))))
;;; -------------------------------------------------------------------------
;;; General stuff
(defn query
([type]
{::type type})
([type args]
{::type type
::args {::type :args
::value (clojure.core/mapv parse-val args)}})
([type args optargs-map]
(if (seq args)
{::type type
::args {::type :args
::value (clojure.core/mapv parse-val args)}
::optargs {::type :optargs
::value
(zipmap (clojure.core/map name (clojure.core/keys optargs-map))
(clojure.core/map parse-val (vals optargs-map)))}}
{::type type
::optargs {::type :optargs
::value
(zipmap (clojure.core/map name (clojure.core/keys optargs-map))
(clojure.core/map parse-val (vals optargs-map)))}})))
;;; -------------------------------------------------------------------------
;;; Lambdas
(defn index-args
[lambda-args]
(zipmap lambda-args
(clojure.core/map (fn [n]
{::type :var
::number (parse-val (inc n))})
(range))))
(defmacro lambda
[arglist & body]
(let [ret (last body)
arg-replacements (index-args arglist)]
`(query :FUNC [(vec (clojure.core/map inc (range ~(clojure.core/count arglist))))
;; TODO - not the best model of scope
~(clojure.walk/postwalk-replace arg-replacements ret)])))
;;; -------------------------------------------------------------------------
;;; Terms
;;; -- Compound types --
(defn make-array
[& xs]
(query :MAKE_ARRAY xs))
(defn make-obj
"Takes a map and returns an object. Useful for making maps with terms inside
such as the maps returned by lambdas passed as arguments to update."
[m]
(query :MAKE_OBJ nil m))
(defn js
([s] (query :JAVASCRIPT [s]))
([s timeout] (query :JAVASCRIPT [s] {:timeout timeout})))
(defn error
([] (query :ERROR))
([s] (query :ERROR [s])))
(def implicit-var (query :IMPLICIT_VAR))
(defn random
[from to & [float?]]
(let [float? (boolean float?)])
(query :RANDOM [from to] {:float float?}))
;;; -- Data Operators --
(defn db
[db-name]
(query :DB [db-name]))
(defn table
([table-name]
(query :TABLE [table-name]))
([table-name use-outdated?]
(query :TABLE [table-name] {:use_outdated use-outdated?})))
(defn table-db
([db table-name]
(query :TABLE [db table-name]))
([db table-name use-outdated?]
(query :TABLE [db table-name] {:use_outdated use-outdated?})))
(defn get
[table k]
(let [k (name k)]
(query :GET [table k])))
(defn get-all
([table xs]
(query :GET_ALL (concat [table] xs)))
([table xs index]
(query :GET_ALL (concat [table] xs) {:index index})))
;;; -- DATUM Ops --
(defn =
[& args]
(query :EQ args))
(defn not=
[& args]
(query :NE args))
(defn <
[& args]
(query :LT args))
(defn <=
[& args]
(query :LE args))
(defn >
[& args]
(query :GT args))
(defn >=
[& args]
(query :GE args))
(defn not
[bool]
(query :NOT [bool]))
(defn +
"Add two numbers or concatenate two strings"
[& args]
(query :ADD args))
(defn -
[& args]
(query :SUB args))
(defn *
[& args]
(query :MUL args))
;; Weird stuff happens when we redefine / and use it from another namespace
(defn div
[& args]
(query :DIV args))
(defn mod
[n1 n2]
(query :MOD [n1 n2]))
;;; -- Datum Array Ops --
(defn append
[array x]
(query :APPEND [array x]))
(defn prepend
[array x]
(query :PREPEND [array x]))
(defn difference
[array1 array2]
(query :DIFFERENCE [array1 array2]))
;;; -- Set Ops --
;;; No actual sets on rethinkdb, only arrays
(defn set-insert
[array x]
(query :SET_INSERT [array x]))
(defn set-intersection
[array1 array2]
(query :SET_INTERSECTION [array1 array2]))
(defn set-union
[array1 array2]
(query :SET_UNION [array1 array2]))
(defn set-difference
[array1 array2]
(query :SET_DIFFERENCE [array1 array2]))
(defn slice
[sq n1 n2]
(query :SLICE [sq n1 n2]))
(defn skip
[sq n]
(query :SKIP [sq n]))
(defn limit
[sq n]
(query :LIMIT [sq n]))
(defn indexes-of
[sq lambda1-or-x]
(query :INDEXES_OF [sq lambda1-or-x]))
(defn contains?
[sq lambda1-or-x]
(query :CONTAINS [sq lambda1-or-x]))
;;; -- Stream/Object Ops --
(defn get-field
"Get a particular field from an object or map that over a sequence"
[obj-or-sq s]
(query :GET_FIELD [obj-or-sq s]))
(defn keys
"Return an array containing the keys of the object"
[obj]
(query :KEYS [obj]))
(defn has-fields?
"Check whether an object contains all the specified fields or filters a
sequence so that al objects inside of it contain all the specified fields"
[obj & pathspecs]
(query :HAS_FIELDS (concat [obj] pathspecs)))
(defn with-fields
"(with-fields sq pathspecs..) <=> (pluck (has-fields sq pathspecs..) pathspecs..)"
[sq & pathspecs]
(query :HAS_FIELDS (concat [sq] pathspecs)))
(defn pluck
"Get a subset of an object by selecting some attributes to preserve,
or map that over a sequence"
[obj-or-sq & pathspecs]
(query :PLUCK (concat [obj-or-sq] pathspecs)))
(defn without
"Get a subset of an object by selecting some attributes to discard,
or map that over a sequence"
[obj-or-sq & pathspecs]
(query :WITHOUT (concat [obj-or-sq] pathspecs)))
(defn merge
"Merge objects (right-preferential)"
[& objs]
(query :MERGE objs))
(defn literal
"Indicates to MERGE to replace the other object rather than merge it"
[json]
(query :LITERAL [json]))
;;; -- Sequence Ops --
(defn group
[sq k]
(query :GROUP [sq k]))
(defn sum
[sq]
(query :SUM [sq]))
(defn avg
[sq]
(query :AVG [sq]))
(defn min
[sq]
(query :MIN [sq]))
(defn max
[sq]
(query :MAX [sq]))
(defn between
"Get all elements of a sequence between two values"
([stream-selection lower upper]
(query :BETWEEN [stream-selection lower upper]))
([stream-selection lower upper index]
(query :BETWEEN [stream-selection lower upper] {:index index})))
(defn reduce
([sq lambda2]
(query :REDUCE [sq lambda2]))
([sq lambda2 base]
(query :REDUCE [sq lambda2] {:base base})))
(defn map
[sq lambda1]
(query :MAP [sq lambda1]))
(defn filter
"Filter a sequence with either a function or a shortcut object.
The body of filter is wrapped in an implicit (default .. false) and you
can change the default value by specifying the default optarg. If you
make the default (error), all errors caught by default will be rethrown
as if the default did not exist"
([sq lambda1-or-obj]
(query :FILTER [sq lambda1-or-obj]))
([sq lambda1-or-obj default-val]
(query :FILTER [sq lambda1-or-obj] {:default default-val})))
(defn mapcat
"Map a function over a sequence and then concatenate the results together"
[sq lambda1]
(query :CONCATMAP [sq lambda1]))
(defn order-by
"Order a sequence based on one or more attributes"
[sq & strs-or-orderings]
(query :ORDERBY (concat [sq] strs-or-orderings)))
(defn distinct
"Get all distinct elements of a sequence (like uniq)"
[sq]
(query :DISTINCT [sq]))
(defn count
"Count the number of elements in a sequence, or only the elements that match a
given filter"
([sq]
(query :COUNT [sq]))
([sq lambda1-or-x]
(query :COUNT [sq lambda1-or-x])))
(defn empty?
[sq]
(query :IS_EMPTY [sq]))
(defn union
"Take the union of multiple sequences
(preserves duplicate elements (use distinct))"
[& seqs]
(query :UNION seqs))
(defn nth
"Get the nth element of a sequence"
[sq n]
(query :NTH [sq n]))
(defn grouped-map-reduce
"Takes a sequence and three functions:
- a function to group the sequence by
- a function to map over the groups
- a reduction to apply to each of the groups"
([sq lambda1 lambda1-2 lambda2]
(query :GROUPED_MAP_REDUCE [sq lambda1 lambda1-2 lambda2]))
([sq lambda1 lambda1-2 lambda2 base]
(query :GROUPED_MAP_REDUCE [sq lambda1 lambda1-2 lambda2] {:base base})))
(defn group-by
"Groups a sequence by one or more attributes and then applies a reduction.
The third argument is a special object literal giving the kind of operation
to be performed and anay necessary arguments.
At present group-by supports the following operations
- :count - count the size of the group
- {:sum attr} - sum the values of the given attribute accross the group
- {:avg attr} - average the values of the given attribute accross the group"
[sq array operation]
(let [operation-obj
(-> (if (keyword? operation)
{operation operation}
operation)
uppercase-keys)]
(query :GROUPBY [sq array operation-obj])))
(defn inner-join
[sq1 sq2 lambda2]
(query :INNER_JOIN [sq1 sq2 lambda2]))
(defn outer-join
[sq1 sq2 lambda2]
(query :OUTER_JOIN [sq1 sq2 lambda2]))
(defn eq-join
"An inner-join that does an equality comparison on two attributes"
([sq1 str sq2]
(query :EQ_JOIN [sq1 str sq2]))
([sq1 str sq2 index]
(query :EQ_JOIN [sq1 str sq2] {:index index})))
(defn zip
[sq]
(query :ZIP [sq]))
;;; -- Array Ops --
(defn insert-at
"Insert an element in to an array at a given index"
[array n x]
(query :INSERT_AT [array n x]))
(defn delete-at
"Remove an element at a given index from an array"
([array n]
(query :DELETE_AT [array n]))
([array n1 n2]
(query :DELETE_AT [array n1 n2])))
(defn change-at
"Change the element at a given index of an array"
[array n x]
(query :CHANGE_AT [array n x]))
(defn splice-at
"Splice one array in to another array"
[array1 n array2]
(query :SPLICE_AT [array1 n array2]))
;;; -- Type Ops --
;;; Figure out the name of types
(defn coerce-to
"Coerces a datum to a named type (eg bool)"
[x type]
(let [type (name type)]
(query :COERCE_TO [x type])))
(defn type
"Returns the named type of a datum"
[x]
(query :TYPEOF [x]))
;;; -- Write Ops --
(defn update
"Updates all the rows in a selection.
Calls its function with the row to be updated and then merges the result of
that call
Optargs: :non-atomic -> bool - Allow the server to run non-atomic operations
:durability -> :soft or :hard - Override the table durability for this
operation
:return-vals -> bool - Only valid for single-row modifications. If true
return the new value in :new_val and the old one in old_val"
[stream-or-single-selection lambda1-or-obj
& {:as optargs}]
(if-not optargs
(query :UPDATE [stream-or-single-selection lambda1-or-obj])
(query :UPDATE [stream-or-single-selection lambda1-or-obj]
(snake-case-keys optargs))))
(defn delete
"Deletes all the rows in a selection
Optargs: :durability -> :hard or :soft - Override the table or query's default
durability setting.
:return-vals -> bool - Only valid for single row deletions. If true
get the value you deleted in :old_val"
[stream-or-single-selection & {:as optargs}]
(if-not optargs
(query :DELETE [stream-or-single-selection])
(query :DELETE [stream-or-single-selection]
(snake-case-keys optargs))))
(defn replace
"Replaces all the rows in a selection. Calls its function with the row to be
replaced, and then discards it and stores the result of that call
Optargs: :non-atomic -> bool
:durability -> str
:return-vals -> bool"
[stream-or-single-selection lambda1 & {:as optargs}]
(if-not optargs
(query :REPLACE [stream-or-single-selection lambda1])
(query :REPLACE [stream-or-single-selection lambda1]
(snake-case-keys optargs))))
(defn insert
"Insert into a table. If upsert is true, overwrites entries with the
same primary key (otherwise errors)
Optargs: :upsert -> bool - If true -> overwrite the data if it already exists
:durability -> :soft or :hard -> Overrule the durability with which the
table was created
:return-vals -> bool - Only valid for single object inserts. If true
get back the row you inserted on the key :nev_val"
[table obj-or-sq & {:as optargs}]
(if-not optargs
(query :INSERT [table obj-or-sq])
(query :INSERT [table obj-or-sq]
(snake-case-keys optargs))))
;;; -- Administrative Ops --
(defn db-create
"Creates a database with a particular name"
[dbname]
(let [dbname (name dbname)]
(query :DB_CREATE [dbname])))
(defn db-drop
"Drops a database with a particular name"
[dbname]
(let [dbname (name dbname)]
(query :DB_DROP [dbname])))
(defn db-list
"Lists all the databases by name"
[]
(query :DB_LIST))
(defn table-create
"Creates a table with a particular name in the default database
Optargs: :datacenter str
:primary-key str
:cache-size number
:durability str"
[tname & {:as optargs}]
(let [tname (name tname)]
(if-not optargs
(query :TABLE_CREATE [tname])
(query :TABLE_CREATE [tname] (snake-case-keys optargs)))))
(defn table-create-db
"Creates a table with a particular name in a particular database
Optargs: :datacenter str
:primary-key str
:cache-size number
:durability str"
[db tname & {:as optargs}]
(let [tname (name tname)]
(if-not optargs
(query :TABLE_CREATE [db tname])
(query :TABLE_CREATE [db tname]
(snake-case-keys optargs)))))
(defn table-drop
"Drops a table with a particular name from the default database"
[tname]
(let [tname (name tname)]
(query :TABLE_DROP [tname])))
(defn table-drop-db
"Drops a table with a particular name from a particular database"
[db tname]
(let [tname (name tname)]
(query :TABLE_DROP [db tname])))
(defn table-list
"Lists all the tables in the default database"
[]
(query :TABLE_LIST))
(defn table-list-db
"Lists all the tables in a particular database"
[db]
(query :TABLE_LIST [db]))
;;; -- Secondary indexes Ops --
(defn index-create
"Creates a new secondary index with a particular name and definition
Optarg: multi -> bool"
([table idx-name lambda1]
(let [idx-name (name idx-name)]
(query :INDEX_CREATE [table idx-name lambda1])))
([table idx-name lambda1 multi]
(let [idx-name (name idx-name)]
(query :INDEX_CREATE [table idx-name lambda1] {:multi multi}))))
(defn index-drop
"Drops a secondary index with a particular name from the specified table"
[table idx-name]
(query :INDEX_DROP [table idx-name]))
(defn index-list
"Lists all secondary indexes on a particular table"
[table]
(query :INDEX_LIST [table]))
;;; -- Control Operators --
(defn funcall
"Calls a function on data"
[lambda-n & xs]
(query :FUNCALL (concat [lambda-n] xs)))
(defn branch
"An if statement"
[bool then else]
(query :BRANCH [bool then else]))
(defn any
"A short circuiting or that returns a boolean"
[& bools]
(query :ANY bools))
(defn all
"Returns true if all of its arguments are true (short-circuits)"
[& bools]
(query :ALL bools))
(defn foreach
"Calls its function with each entry in the sequence and executes the array of
terms that function returns"
[sq lambda1]
(query :FOREACH [sq lambda1]))
;;; -- Special Ops --
(defn asc
"Indicates to order-by that this attribute is to be sorted in ascending order"
[k]
(query :ASC [k]))
(defn desc
"Indicates to order-by that this attribute is to be sorted in descending order"
[k]
(query :DESC [k]))
(defn info
"Gets info about anything. INFO is most commonly called on tables"
[x]
(query :INFO [x]))
(defn match
"(match a b) returns a match object if the string \"a\" matches the regexp #\"b\""
[s re]
(query :MATCH [s (str re)]))
(defn sample
"Select a number of elements from sequence with uniform distribution"
[sq n]
(query :SAMPLE [sq n]))
(defn default
"Evaluates its first argument. If that argument returns NULL or throws an error
related to the absence of an expected value, default will either return its
second argument or execute it if it's a function. If the second argument is a
function it will be passed either the text of the error or NULL as its argument"
[to-check lambda1-or-x]
(query :DEFAULT [to-check lambda1-or-x]))
(defn json
"Parses its first argument as a json string and returns it as a datum"
[s]
(query :JSON [s]))
;;; -- Date/Time Ops --
(defn iso8601
"Parses its first arguments as an ISO 8601 time and returns it as a datum"
[s]
(query :ISO8601 [s]))
(defn ->iso8601
"Prints a time as an ISO 8601 time"
[t]
(query :TO_ISO8601 [t]))
(defn epoch-time
"Returns a time given seconds since epoch in UTC"
[n]
(query :EPOCH_TIME [n]))
(defn ->epoch-time
"Returns seconds since epoch in UTC given a time"
[t]
(query :TO_EPOCH_TIME [t]))
(defn now
"The time the query was received by the server"
[]
(query :NOW))
(defn in-timezone
"Puts a time into an ISO 8601 timezone"
[t s]
(query :IN_TIMEZONE [t s]))
(defn during
"(during a b c) returns whether a is in the range [b, c)
a b and c are times"
[a b c]
(query :DURING [a b c]))
(defn date
"Retrieves the date portion of a time"
[t]
(query :DATE [t]))
(defn time-of-day
"(time-of-day x) == (- (date x) x)"
[t]
(query :TIME_OF_DAY [t]))
(defn timezone
[t]
(query :TIMEZONE [t]))
;;; -- Accessing time components --
(defn year
[t]
(query :YEAR [t]))
(defn month
[t]
(query :MONTH [t]))
(defn day
[t]
(query :DAY [t]))
(defn day-of-week
[t]
(query :DAY_OF_WEEK [t]))
(defn day-of-year
[t]
(query :DAY_OF_YEAR [t]))
(defn hours
[t]
(query :HOURS [t]))
(defn minutes
[t]
(query :MINUTES [t]))
(defn seconds
[t]
(query :SECONDS [t]))
;;; -- Date construction --
;;; Apparently timezone is obligatory contrary to what the .proto docs say
(defn time
"Construct a time from a date and optional timezone or a date+time and optional
timezone"
([y m d]
(query :TIME [y m d "+00:00"]))
([y m d tz]
(query :TIME [y m d tz]))
([y m d h min s]
(query :TIME [y m d h min s "+00:00"]))
([y m d h min s tz]
(query :TIME [y m d h min s tz])))
;;; -- Constants for ISO 8601 days of the week --
(def monday (query :MONDAY))
(def tuesday (query :TUESDAY))
(def wednesday (query :WEDNESDAY))
(def thursday (query :THURSDAY))
(def friday (query :FRIDAY))
(def saturday (query :SATURDAY))
(def sunday (query :SUNDAY))
;;; -- Constants for ISO 8601 months --
(def january (query :JANUARY))
(def february (query :FEBRUARY))
(def march (query :MARCH))
(def april (query :APRIL))
(def may (query :MAY))
(def june (query :JUNE))
(def july (query :JULY))
(def august (query :AUGUST))
(def september (query :SEPTEMBER))
(def october (query :OCTOBER))
(def november (query :NOVEMBER))
(def december (query :DECEMBER))
;;; -------------------------------------------------------------------------
;;; Custom Helpers
(defn or
"Like clojure's or, short circuiting _inside_ RethinkDB"
[& bools]
(case (clojure.core/count bools)
0 (parse-val nil)
1 (parse-val (first bools))
(let [[fst & rst] bools]
(branch fst
fst
(apply or rst)))))
(defn and
"Like clojure's and, short circuiting _inside_ RethinkDB"
[& bools]
(case (clojure.core/count bools)
0 (parse-val nil)
1 (parse-val (first bools))
(let [[fst & rst] bools]
(branch fst
(apply and rst)
fst))))