-
Notifications
You must be signed in to change notification settings - Fork 7
/
relation.clj
1227 lines (1048 loc) · 40 KB
/
relation.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
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(ns com.verybigthings.penkala.relation
(:refer-clojure :exclude [group-by extend distinct])
(:require [clojure.spec.alpha :as s]
[camel-snake-kebab.core :refer [->kebab-case-keyword ->kebab-case-string]]
[clojure.string :as str]
[clojure.walk :refer [prewalk]]
[com.verybigthings.penkala.util
:refer [expand-join-path path-prefix-join path-prefix-split joins q as-vec col->alias]]
[com.verybigthings.penkala.statement.select :as sel]
[com.verybigthings.penkala.statement.insert :as ins]
[com.verybigthings.penkala.statement.update :as upd]
[com.verybigthings.penkala.statement.delete :as del]
[clojure.set :as set]))
;; TODO: keyset pagination
(defprotocol IRelation
(-lock [this lock-type locked-rows])
(-join [this join-type join-rel join-alias join-on join-projection])
(-having [this having-expression])
(-or-having [this having-expression])
(-offset [this offset])
(-limit [this limit])
(-order-by [this orders])
(-extend [this col-name extend-expression])
(-extend-with-aggregate [this col-name agg-expression])
(-extend-with-window [this col-name window-expression partitions orders])
(-rename [this prev-col-name next-col-name])
(-select [this projection])
(-distinct [this distinct-expression])
(-union [this other-rel])
(-union-all [this other-rel])
(-intersect [this other-rel])
(-except [this other-rel])
(-wrap [this])
(-with-parent [this parent]))
(defprotocol IWriteable
(-returning [this projection]))
(defprotocol IInsertable
(-with-inserts [this inserts])
(-on-conflict-do [this action conflicts update where-expression]))
(defprotocol IUpdatable
(-with-updates [this updates])
(-from [this from-rel from-alias]))
(defprotocol IDeletable
(-using [this using-rel using-alias]))
(defprotocol IWhere
(-where [this where-expression])
(-or-where [this where-expression]))
(defprotocol IOnly
(-only [this is-only]))
(defrecord Wrapped [subject-type subject])
(defn ex-info-missing-column [rel node]
(let [column-name (if (keyword? node) node (:subject node))]
(ex-info (str "Column " column-name " doesn't exist") {:column column-name :rel rel})))
(def operations
{:unary #{:is-null :is-not-null
:is-true :is-not-true
:is-false :is-not-false
:is-unknown :is-not-unknown}
:binary #{:< :> :<= :>= := :<> :!=
:is-distinct-from :is-not-distinct-from
:like :ilike :not-like :not-ilike
:similar-to :not-similar-to
"~" "~*" "!~" "!~*" "->" "->>" "#>" "#>>"
"@>" "<@" "?" "?|" "||" "-" "#-" "@?" "@@"
"&&" "+" "*" "/" "#" "@-@" "##" "<->" ">>"
"<<" "&<" "&>" "<<|" "|>>" "&<|" "|>&" "<^"
"^>" "?#" "?-" "?-|" "?||" "~=" "%" "^" "|/"
"||/" "!" "!!" "@" "&" "|" "<<=" "=>>" "-|-"}
:ternary #{:between :not-between
:between-symmetric :not-between-symmetric}})
(def all-operations
(set/union (:unary operations) (:binary operations) (:ternary operations)))
(s/def ::spec-map map?)
(s/def ::join-type
#(contains? joins %))
(s/def ::lock-type
#(contains? #{:share :update :no-key-update :key-share} %))
(s/def ::locked-rows
#(contains? #{:nowait :skip-locked} %))
(s/def ::connective
(s/and
vector?
(s/cat
:op #(contains? #{:and :or} %)
:args (s/+ ::value-expression))))
(s/def ::negation
(s/and
vector?
(s/cat
:op #(= :not %)
:arg1 ::value-expression)))
(s/def ::unary-operation
(s/and
vector?
(s/cat
:op (s/or
:operator #(contains? (:unary operations) %)
:wrapped-operator #(and (= Wrapped (type %)) (= :unary-operator (:subject-type %))))
:arg1 ::value-expression)))
(s/def ::binary-operation
(s/and
vector?
(s/cat
:op (s/or
:operator #(contains? (:binary operations) %)
:wrapped-operator #(and (= Wrapped (type %)) (= :binary-operator (:subject-type %))))
:arg1 ::value-expression
:arg2 ::value-expression)))
(s/def ::ternary-operation
(s/and
vector?
(s/cat
:op (s/or
:operator #(contains? (:ternary operations) %)
:wrapped-operator #(and (= Wrapped (type %)) (= :ternary-operator (:subject-type %))))
:arg1 ::value-expression
:arg2 ::value-expression
:arg3 ::value-expression)))
(s/def ::inclusion-operation
(s/and
vector?
(s/cat
:op #(contains? #{:in :not-in} %)
:column ::value-expression
:in (s/or
:relation ::relation
:value-expressions (s/coll-of ::value-expression)))))
(s/def ::cast
(s/and
vector?
(s/cat
:fn #(= :cast %)
:value ::value-expression
:cast-type string?)))
(s/def ::function-call
(s/and
vector?
(s/cat
:fn #(and (keyword? %) (not (contains? all-operations %)))
:args (s/+ ::value-expression))))
(s/def ::parent-scope
(s/and
vector?
(s/cat
:op #(= :parent-scope %)
:args (s/+ ::value-expression))))
(s/def ::fragment-literal
(s/and
vector?
(s/cat
:op #(= :fragment %)
:fragment-literal string?
:args (s/+ ::value-expression))))
(s/def ::fragment-fn
(s/and
vector?
(s/cat
:op #(= :fragment %)
:fragment-fn fn?
:args (s/+ ::value-expression))))
(s/def ::on-constraint
(s/tuple #(= :on-constraint %) string?))
(s/def ::value-expressions
(s/coll-of ::value-expression))
(s/def ::conflict-target
(s/or
:on-constraint ::on-constraint
:value-expressions ::value-expressions))
(s/def ::relation
#(satisfies? IRelation %))
(s/def ::writeable
#(satisfies? IWriteable %))
(s/def ::insertable
#(satisfies? IInsertable %))
(s/def ::updatable
#(satisfies? IUpdatable %))
(s/def ::deletable
#(satisfies? IDeletable %))
(s/def ::wrapped-column
#(and (= Wrapped (type %)) (= :column (:subject-type %))))
(s/def ::wrapped-value
#(and (= Wrapped (type %)) (= :value (:subject-type %))))
(s/def ::wrapped-param
#(and (= Wrapped (type %)) (= :param (:subject-type %))))
(s/def ::wrapped-literal
#(and (= Wrapped (type %)) (= :literal (:subject-type %))))
(s/def ::column-identifier
(s/or
:keyword keyword?
:wrapped-column ::wrapped-column))
(s/def ::order-direction
#(contains? #{:asc :desc} %))
(s/def ::order-nulls
#(contains? #{:nulls-first :nulls-last} %))
(s/def ::order
(s/or
:column-identifier ::column-identifier
:column (s/cat
:column-identifier ::column-identifier)
:column-direction (s/cat
:column-identifier ::column-identifier
:order-direction ::order-direction)
:column-direction-nulls (s/cat
:column-identifier ::column-identifier
:order-direction ::order-direction
:order-nulls ::order-nulls)))
(s/def ::when
(s/and vector?
(s/cat
:when #(= :when %)
:condition ::value-expression
:then ::value-expression)))
(s/def ::case
(s/and vector?
(s/cat
:case #(= :case %)
:value (s/? (s/and
#(not (and (vector? %) (= :when (first %))))
::value-expression))
:whens (s/+ ::when)
:else (s/? ::value-expression))))
(s/def ::updates
(s/map-of keyword? ::value-expression))
(s/def ::inserts
(s/map-of keyword? ::value-expression))
(s/def ::orders
(s/coll-of ::order))
(s/def ::value-expression
(s/or
:boolean boolean?
:keyword keyword?
:relation ::relation
:connective ::connective
:negation ::negation
:unary-operation ::unary-operation
:binary-operation ::binary-operation
:ternary-operation ::ternary-operation
:inclusion-operation ::inclusion-operation
:parent-scope ::parent-scope
:fragment-fn ::fragment-fn
:fragment-literal ::fragment-literal
:cast ::cast
:case ::case
:function-call ::function-call
:wrapped-literal ::wrapped-literal
:wrapped-column ::wrapped-column
:wrapped-param ::wrapped-param
:wrapped-value ::wrapped-value
:value any?))
(s/def ::column-list
(s/coll-of ::column-identifier))
(defn resolve-column [rel node]
(let [column (if (keyword? node) node (:subject node))
column-ns (namespace column)
column-join-path (when (seq column-ns) (str/split column-ns #"\."))
column-name (name column)]
(if (seq column-join-path)
(let [column-join-path' (map keyword column-join-path)
column-rel (get-in rel (expand-join-path column-join-path'))
id (get-in column-rel [:aliases->ids (keyword column-name)])]
(when id
{:path column-join-path' :id id :name column-name :original column}))
(when-let [id (get-in rel [:aliases->ids (keyword column-name)])]
{:id id :name column-name :original column}))))
(defn extract-operator [[op-type operator]]
(let [extracted-operator (if (= :operator op-type) operator (:subject operator))]
(if (= :!= extracted-operator)
:<>
extracted-operator)))
(defn process-value-expression [rel node]
(let [[node-type args] node]
(case node-type
:wrapped-column
(let [column (resolve-column rel args)]
(when (nil? column)
(throw (ex-info-missing-column rel node)))
[:resolved-column column])
:wrapped-value
[:value (:subject args)]
:wrapped-param
[:param (:subject args)]
:wrapped-literal
[:literal (:subject args)]
:keyword
(let [column (resolve-column rel args)]
(if column
[:resolved-column column]
node))
(:connective :function-call :fragment-literal :fragment-fn)
(update-in node [1 :args] (fn [args] (mapv #(process-value-expression rel %) args)))
:negation
(update-in node [1 :arg1] #(process-value-expression rel %))
:cast
(update-in node [1 :value] #(process-value-expression rel %))
:case
(-> node
(update-in [1 :value] #(when % (process-value-expression rel %)))
(update-in [1 :whens] (fn [whens]
(mapv (fn [when]
(-> when
(update :condition #(process-value-expression rel %))
(update :then #(process-value-expression rel %))))
whens)))
(update-in [1 :else] #(when % (process-value-expression rel %))))
:unary-operation
(-> node
(update-in [1 :op] extract-operator)
(update-in [1 :arg1] #(process-value-expression rel %)))
:binary-operation
(-> node
(update-in [1 :op] extract-operator)
(update-in [1 :arg1] #(process-value-expression rel %))
(update-in [1 :arg2] #(process-value-expression rel %)))
:ternary-operation
(-> node
(update-in [1 :op] extract-operator)
(update-in [1 :arg1] #(process-value-expression rel %))
(update-in [1 :arg2] #(process-value-expression rel %))
(update-in [1 :arg3] #(process-value-expression rel %)))
:parent-scope
(let [parent (:parent rel)]
(when (nil? parent)
(throw (ex-info "Parent scope doesn't exist" {:relation rel})))
(update-in node [1 :args] (fn [args] (mapv #(process-value-expression (:parent rel) %) args))))
:inclusion-operation
(let [in (get-in node [1 :in])
in' (if (= :value-expressions (first in))
(update in 1 (fn [args] (mapv #(process-value-expression rel %) args)))
in)]
(-> node
(update-in [1 :column] #(process-value-expression rel %))
(assoc-in [1 :in] in')))
node)))
(defn process-projection [rel node-list]
(reduce
(fn [acc [_ node]]
(let [column (resolve-column rel node)]
(if (or (nil? column) (-> column :path seq))
(throw (ex-info-missing-column rel node))
(conj acc (if (keyword? node) node (:subject node))))))
#{}
node-list))
(defn process-orders [rel orders]
(map
(fn [[node-type node]]
(let [node' (if (= :column-identifier node-type) {:column-identifier node} node)
column-identifier (-> node' :column-identifier second)
column (resolve-column rel column-identifier)]
(when (nil? column)
(throw (ex-info-missing-column rel column-identifier)))
(assoc node' :column [:resolved-column column])))
orders))
(defn resolve-columns [rel columns]
(reduce
(fn [acc [_ node]]
(let [column (resolve-column rel node)]
(if (or (nil? column))
(throw (ex-info-missing-column rel node))
(conj acc [:resolved-column column]))))
[]
columns))
(defn and-predicate [rel predicate-type predicate-expression]
(s/assert ::value-expression predicate-expression)
(let [prev-predicate (get rel predicate-type)
processed-predicate (process-value-expression rel (s/conform ::value-expression predicate-expression))]
(if prev-predicate
(assoc rel predicate-type [:connective {:op :and :args [prev-predicate processed-predicate]}])
(assoc rel predicate-type processed-predicate))))
(defn or-predicate [rel predicate-type predicate-expression]
(s/assert ::value-expression predicate-expression)
(if-let [prev-predicate (get rel predicate-type)]
(let [processed-predicate (process-value-expression rel (s/conform ::value-expression predicate-expression))]
(assoc rel predicate-type [:connective {:op :or :args [prev-predicate processed-predicate]}]))
(and-predicate rel predicate-type predicate-expression)))
(defn get-projected-columns
([rel] (get-projected-columns #{} rel []))
([acc rel path-prefix]
(let [acc'
(reduce
(fn [acc col]
(conj acc (path-prefix-join (map name (conj path-prefix col)))))
acc
(:projection rel))]
(reduce-kv
(fn [acc join-alias join]
(get-projected-columns acc (:relation join) (conj path-prefix join-alias)))
acc'
(:joins rel)))))
(defn get-select-query
([rel env]
(sel/format-query-without-params-resolution env rel))
([rel env params]
(sel/format-query env rel params)))
(defn get-insert-query [insertable env]
(ins/format-query env insertable))
(defn get-update-query [updatable env params]
(upd/format-query env updatable params))
(defn get-delete-query [deletable env params]
(del/format-query env deletable params))
(defn make-combined-relations-spec [operator rel1 rel2]
(let [rel1-cols (get-projected-columns rel1)
rel2-cols (get-projected-columns rel2)]
(when (not= rel1-cols rel2-cols)
(throw (ex-info (str operator " requires projected columns to match.") {:left-relation rel1 :right-relation rel2})))
(let [rel1-name (get-in rel1 [:spec :name])
rel2-name (get-in rel2 [:spec :name])
rel-name (if (= rel1-name rel2-name) rel1-name (str rel1-name "__" rel2-name))
rel1-pk (get-in rel1 [:spec :pk])
rel2-pk (get-in rel2 [:spec :pk])
query (fn [env]
(let [[query1 & params1] (get-select-query rel1 env)
[query2 & params2] (get-select-query rel2 env)]
(vec (concat [(str query1 " " operator " " query2)] params1 params2))))]
{:name rel-name
:pk (when (= rel1-pk rel2-pk) rel1-pk)
:columns rel1-cols
:query query})))
(declare spec->relation)
(defn -writeable-returning [rel projection]
(if projection
(let [processed-projection (process-projection rel (s/conform ::column-list projection))]
(assoc rel :projection processed-projection))
(assoc rel :projection nil)))
(defn process-on-conflict-column-references [value]
(prewalk
(fn [val]
(if (and (vector? val) (= :resolved-column (first val)))
[:literal (-> val second :name)]
val))
value))
(defn process-conflict-target [insertable conflict-target]
(let [[conflict-target-type & _] conflict-target]
(case conflict-target-type
:value-expressions
(update conflict-target 1 (fn [v] (mapv #(process-value-expression insertable %) v)))
:on-constraint
(last conflict-target)
conflict-target)))
(defn process-inserts [inserts]
(reduce-kv
(fn [m k v]
(if v
(assoc m k (s/conform ::value-expression v))
(assoc m k v)))
{}
inserts))
(defrecord Insertable [spec]
IWriteable
(-returning [this projection]
(-writeable-returning this projection))
IInsertable
(-with-inserts [this inserts]
(let [processed-inserts (if (map? inserts)
(process-inserts inserts)
(mapv process-inserts inserts))]
(assoc this :inserts processed-inserts)))
(-on-conflict-do [this action conflict-target updates where-expression]
(let [excluded (assoc-in this [:spec :name] "EXCLUDED")
this' (dissoc this :joins)
this'' (if updates (assoc this' :joins {:excluded {:relation excluded}}) this')
processed-conflict-target (when conflict-target
(->> conflict-target
(s/conform ::conflict-target)
(process-conflict-target this')
process-on-conflict-column-references))
processed-updates (when updates
(->> updates
(s/conform ::updates)
(filter (fn [[k _]] (contains? (:aliases->ids this'') k)))
(map (fn [[k v]] [k (process-value-expression this'' v)]))
(into {})))
where (when where-expression
(-> this'
(process-value-expression (s/conform ::value-expression where-expression))
process-on-conflict-column-references))]
(when
(and (= :on-constraint (first processed-conflict-target))
where)
(throw (ex-info "ON CONSTRAINT can't be used with a WHERE clause" {:insertable this
:where where-expression
:conflict-target conflict-target})))
(assoc this'' :on-conflict {:action action
:conflict-target processed-conflict-target
:updates processed-updates
:where where}))))
(defrecord Updatable [spec]
IWriteable
(-returning [this projection]
(-writeable-returning this projection))
IWhere
(-where [this where-expression]
(and-predicate this :where where-expression))
(-or-where [this where-expression]
(or-predicate this :where where-expression))
IOnly
(-only [this is-only]
(assoc this :only is-only))
IUpdatable
(-with-updates [this updates]
(let [processed-updates (->> updates
(s/conform ::updates)
(filter (fn [[k _]] (contains? (:aliases->ids this) k)))
(map (fn [[k v]] [k (process-value-expression this v)]))
(into {}))]
(assoc this :updates processed-updates)))
(-from [this from-rel from-alias]
(assoc-in this [:joins from-alias] {:relation from-rel})))
(defrecord Deletable [spec]
IWriteable
(-returning [this projection]
(-writeable-returning this projection))
IWhere
(-where [this where-expression]
(and-predicate this :where where-expression))
(-or-where [this where-expression]
(or-predicate this :where where-expression))
IOnly
(-only [this is-only]
(assoc this :only is-only))
IDeletable
(-using [this using-rel using-alias]
(assoc-in this [:joins using-alias] {:relation using-rel})))
(defn ensure-join-alias-on-projection [join-alias join-projection]
(let [join-alias-name (name join-alias)]
(reduce
(fn [acc col]
(if (keyword? col)
(let [col-ns (namespace col)
col-nss (str/split col-ns #"\.")]
(when (not= join-alias-name (first col-nss))
(throw (ex-info "Columns in join projection must be aliased with the join alias" {:join-alias join-alias :column col})))
(let [col-ns' (->> col-nss rest (str/join "."))
col' (keyword col-ns' (name col))]
(conj acc col')))
(conj acc col)))
[]
join-projection)))
(defrecord Relation [spec]
IRelation
(-lock [this lock-type locked-rows]
(assoc this :lock {:type lock-type :rows locked-rows}))
(-join [this join-type join-rel join-alias join-on join-projection]
(let [join-rel' (if (contains? #{:left-lateral :right-lateral} join-type)
(assoc join-rel :parent this)
join-rel)
processed-join-projection (when join-projection
(process-projection join-rel' (->> join-projection
(ensure-join-alias-on-projection join-alias)
(s/conform ::column-list))))
with-join (assoc-in this [:joins join-alias] {:relation join-rel'
:type join-type
:projection processed-join-projection})
join-on' (process-value-expression with-join (s/conform ::value-expression join-on))]
(assoc-in with-join [:joins join-alias :on] join-on')))
(-having [this having-expression]
(and-predicate this :having having-expression))
(-or-having [this having-expression]
(or-predicate this :having having-expression))
(-rename [this prev-col-name next-col-name]
(let [id (get-in this [:aliases->ids prev-col-name])]
(when (nil? id)
(throw (ex-info-missing-column this prev-col-name)))
(let [this' (-> this
(assoc-in [:ids->aliases id] next-col-name)
(update :aliases->ids #(-> % (dissoc prev-col-name) (assoc next-col-name id))))]
(if (contains? (:projection this') prev-col-name)
(update this' :projection #(-> % (disj prev-col-name) (conj next-col-name)))
this'))))
(-extend [this col-name extend-expression]
(when (contains? (:aliases->ids this) col-name)
(throw (ex-info (str "Column " col-name " already-exists") {:column col-name :relation this})))
(let [processed-extend (process-value-expression this (s/conform ::value-expression extend-expression))
id (keyword (gensym "column-"))]
(-> this
(assoc-in [:columns id] {:type :computed
:value-expression processed-extend})
(assoc-in [:ids->aliases id] col-name)
(assoc-in [:aliases->ids col-name] id)
(update :projection conj col-name))))
(-extend-with-aggregate [this col-name agg-expression]
(when (contains? (:aliases->ids this) col-name)
(throw (ex-info (str "Column " col-name " already-exists") {:column col-name :relation this})))
(let [processed-agg (process-value-expression this (s/conform ::value-expression agg-expression))
id (keyword (gensym "column-"))]
(-> this
(assoc-in [:columns id] {:type :aggregate
:value-expression processed-agg})
(assoc-in [:ids->aliases id] col-name)
(assoc-in [:aliases->ids col-name] id)
(update :projection conj col-name))))
(-extend-with-window [this col-name window-expression partitions orders]
(when (contains? (:aliases->ids this) col-name)
(throw (ex-info (str "Column " col-name " already-exists") {:column col-name :relation this})))
(let [processed-window (process-value-expression this [:function-call (s/conform ::function-call window-expression)])
processed-partitions (when partitions (resolve-columns this (s/conform ::column-list partitions)))
processed-orders (when orders (process-orders this (s/conform ::orders orders)))
id (keyword (gensym "column-"))]
(-> this
(assoc-in [:columns id] {:type :window
:value-expression processed-window
:partition-by processed-partitions
:order-by processed-orders})
(assoc-in [:ids->aliases id] col-name)
(assoc-in [:aliases->ids col-name] id)
(update :projection conj col-name))))
(-select [this projection]
(let [processed-projection (process-projection this (s/conform ::column-list projection))]
(assoc this :projection processed-projection)))
(-distinct [this distinct-expression]
(cond
(boolean? distinct-expression)
(assoc this :distinct distinct-expression)
:else
(let [processed-distinct (resolve-columns this (s/conform ::column-list distinct-expression))]
(assoc this :distinct processed-distinct))))
(-order-by [this orders]
(let [processed-orders (process-orders this (s/conform ::orders orders))]
(assoc this :order-by processed-orders)))
(-offset [this offset]
(assoc this :offset offset))
(-limit [this limit]
(assoc this :limit limit))
(-union [this other-rel]
(spec->relation (make-combined-relations-spec "UNION" this other-rel)))
(-union-all [this other-rel]
(spec->relation (make-combined-relations-spec "UNION ALL" this other-rel)))
(-intersect [this other-rel]
(spec->relation (make-combined-relations-spec "INTERSECT" this other-rel)))
(-except [this other-rel]
(spec->relation (make-combined-relations-spec "EXCEPT" this other-rel)))
(-wrap [this]
(spec->relation {:name (get-in this [:spec :name])
:columns (get-projected-columns this)
:query #(get-select-query this %)
:wrapped this}))
(-with-parent [this parent-rel]
(assoc this :parent parent-rel))
IOnly
(-only [this is-only]
(assoc this :only is-only))
IWhere
(-where [this where-expression]
(and-predicate this :where where-expression))
(-or-where [this where-expression]
(or-predicate this :where where-expression)))
(defn lock
"Lock selected rows"
([rel lock-type]
(-lock rel lock-type nil))
([rel lock-type locked-rows]
(-lock rel lock-type locked-rows)))
(s/fdef lock
:args (s/cat
:rel ::relation
:lock-type ::lock-type
:locked-rows (s/? ::locked-rows))
:ret ::relation)
(defn join
"Joins another relation. Supported join types are:
- :inner
- :inner-lateral
- :left
- :left-lateral
- :right
- :full
- :cross
When joining a relation, columns from the joined relation must be referenced with a namespaced key. Key's namespace
will be the join alias (e.g. :join-alias/column). If you need to reference a column that's deeply joined, you
can use join aliases concatenated with a dot (e.g. :join-alias.another-join-alias/column)
`join-on` can be any value expression, so you can have as complex join predicates as you need."
([rel join-type join-rel join-alias join-on]
(-join rel join-type join-rel join-alias join-on nil))
([rel join-type join-rel join-alias join-on join-projection]
(-join rel join-type join-rel join-alias join-on join-projection)))
(s/fdef join
:args (s/cat
:rel ::relation
:join-type ::join-type
:join-rel ::relation
:join-alias keyword?
:join-on ::value-expression
:join-projection (s/? ::column-list))
:ret ::relation)
(defn where
"And where operation. If there's already a where clause set, this clause will be joined with AND. Accepts a value
expression which can be nested as needed. You can reference a column in a joined relation by using namespaced keys:
```
(-> beta
(r/join :inner alpha :alpha [:= :alpha-id :alpha/id])
(r/where [:= :alpha/id 3]))
```
In this example `:alpha/id` is referencing the `:id` column in the relation joined with the `:alpha` alias."
[rel where-expression]
(-where rel where-expression))
(s/fdef where
:args (s/cat
:rel ::relation
:where-expression ::value-expression)
:ret ::relation)
(defn or-where
"Or where operation. If there's already a where clause set, this clause will be joined with OR"
[rel where-expression]
(-or-where rel where-expression))
(s/fdef or-where
:args (s/cat
:rel ::relation
:where-expression ::value-expression)
:ret ::relation)
(defn having
"And having operation. If there's already a having clause set, this clause will be joined with AND"
[rel having-expression]
(-having rel having-expression))
(s/fdef having
:args (s/cat
:rel ::relation
:having-expression ::value-expression)
:ret ::relation)
(defn or-having
"Or having operation. If there's already a having clause set, this clause will be joined with OR"
[rel having-expression]
(-or-having rel having-expression))
(s/fdef or-having
:args (s/cat
:rel ::relation
:having-expression ::value-expression)
:ret ::relation)
(defn offset
"Sets the offset parameter"
[rel offset]
(-offset rel offset))
(s/fdef offset
:args (s/cat
:rel ::relation
:offset int?)
:ret ::relation)
(defn limit
"Sets the limit parameter."
[rel limit]
(-limit rel limit))
(s/fdef limit
:args (s/cat
:rel ::relation
:limit int?)
:ret ::relation)
(defn order-by
"Sets order by clause. It accepts a vector of columns by which the order will be performed. Columns can be either a
keyword, or vectors if you need to use descending order or you want to set order for null values:
- `(order-by rel [:id])`
- `(order-by rel [[:id :desc]])`
- `(order-by rel [[:id :desc :nulls-first]])`
You can reference columns in joined relations by using namespaced keys"
[rel orders]
(-order-by rel orders))
(s/fdef order-by
:args (s/cat
:rel ::relation
:orders ::orders)
:ret ::relation)
(defn extend
"Extends a relation with a computed column. This column will be automatically selected. Expression can reference
previously extended columns by name:
```
(-> rel
(extend :upper-name [:upper :name])
(extend :lower-upper-name [:lower :upper-name]))
```
You can use reference these columns in any other value expression, and Penkala will correctly compile them in the
generated SQL.
```
(-> rel
(extend :upper-name [:upper :name])
(extend :lower-upper-name [:lower :upper-name])
(where [:= :lower-upper-name \"FOO\"]))
```
"
[rel col-name extend-expression]
(-extend rel col-name extend-expression))
(s/fdef extend
:args (s/cat
:rel ::relation
:col-name keyword?
:extend-expression ::value-expression)
:ret ::relation)
(defn extend-with-aggregate
"Extends the relation with a computed column that is an aggregate value (e.g. sum, max, min...). Root value expression
must be a function. Penkala doesn't have any explicit support for the built in aggregate functions which means you can
use whatever your DB supports, including custom aggregate functions.
If an aggregate column is selected, Penkala will automatically add a GROUP BY clause to the generated SQL.
```
(extend-with-aggregate rel :count [:count 1])
```
This column will be automatically selected."
([rel col-name agg-expression]
(-extend-with-aggregate rel col-name agg-expression)))
(s/fdef extend-with-aggregate
:args (s/cat
:rel ::relation
:col-name keyword?
:agg-expression ::function-call)
:ret ::relation)
(defn extend-with-window
"Extends a relation with a window function column."
([rel col-name window-expression]
(-extend-with-window rel col-name window-expression nil nil))
([rel col-name window-expression partitions]
(-extend-with-window rel col-name window-expression partitions nil))
([rel col-name window-expression partitions orders]
(-extend-with-window rel col-name window-expression partitions orders)))
(s/fdef extend-with-window
:args (s/cat
:rel ::relation
:col-name keyword?
:window-expression ::function-call
:partitions (s/? ::column-list)
:orders (s/? ::orders))
:ret ::relation)
(defn rename
"Renames a column. If you rename a column, you must use a new name to reference it after that
```
(-> rel
(rename :name :product-name)
(where [:= :product-name \"FOO\"]))
```"
[rel prev-col-name next-col-name]
(-rename rel prev-col-name next-col-name))
(s/fdef rename
:args (s/cat
:rel ::relation
:prev-col-name keyword?
:next-col-name keyword?)
:ret ::relation)
(defn select
"Selects columns from the relation. You can reference any extended columns here."
[rel projection]
(-select rel projection))
(s/fdef select
:args (s/cat
:rel ::relation
:projection ::column-list)
:ret ::relation)
(defn returning
"Selects columns from the write operation (insert, update, delete)"
[writeable projection]
(-returning writeable projection))
(s/fdef returning
:args (s/cat
:rel ::writeable
:projection ::column-list)
:ret ::writeable)
(defn distinct
"Adds a distinct or distinct on clause."
([rel]
(-distinct rel true))
([rel distinct-expression]
(-distinct rel distinct-expression)))
(s/fdef distinct
:args (s/cat
:rel ::relation
:distinct-expression (s/or
:boolean boolean?
:distinct-expression ::column-list))
:ret ::relation)
(defn only
"Adds the only clause to limit the inheritance."