/
db.clj
917 lines (836 loc) · 38.5 KB
/
db.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
(ns
^{:doc "Random utils, database helper functions."
:author "Paweł Wilk"
:added "1.0.0"}
io.randomseed.utils.db
(:refer-clojure :exclude [memoize])
(:require [clojure.string :as str]
[clojure.core.memoize :as mem]
[clojure.core.cache :as cache]
[clojure.core.cache.wrapped :as cwr]
[camel-snake-kebab.core :as csk]
[next.jdbc :as jdbc]
[next.jdbc.sql :as sql]
[next.jdbc.result-set :as result-set]
[next.jdbc.sql.builder :as builder]
[taoensso.nippy :as nippy]
[io.randomseed.utils :refer :all]
[io.randomseed.utils.log :as log]
[io.randomseed.utils.var :as var]
[io.randomseed.utils.map :as map]
[io.randomseed.utils.time :as time]
[io.randomseed.utils.nop-cache :as nop-cache])
(:import (javax.sql DataSource)))
(def ^:const underscore (re-pattern "_"))
;; Builder and conversion functions
(defn- tlcs-c [v] (when v (csk/->kebab-case-string (if (ident? v) (name v) v))))
(defn- tlc-c [v] (when v (csk/->kebab-case-string v)))
(defn- tscs-c [v] (when v (csk/->snake_case_string (if (ident? v) (name v) v))))
(defn- tsc-c [v] (when v (csk/->snake_case_string v)))
(def to-lisp-case-simple (mem/fifo tlcs-c {} :fifo/threshold 512))
(def to-snake-case-simple (mem/fifo tscs-c {} :fifo/threshold 512))
(def to-lisp-case (mem/fifo tlc-c {} :fifo/threshold 512))
(def to-snake-case (mem/fifo tsc-c {} :fifo/threshold 512))
(defn as-lisp-vectors
"Result set builder which returns vectors with underscores in names converted to
hyphens."
[rs opts]
(result-set/as-modified-arrays
rs (assoc opts
:qualifier-fn to-lisp-case
:label-fn to-lisp-case)))
(defn as-lisp-simple-vectors
"Result set builder which returns vectors with underscores in names converted to
hyphens and namespaces removed."
[rs opts]
(result-set/as-unqualified-modified-arrays
rs (assoc opts
:qualifier-fn to-lisp-case-simple
:label-fn to-lisp-case-simple)))
(defn as-lisp-maps
"Result set builder which converts underscore characters to hyphens."
[rs opts]
(result-set/as-modified-maps
rs (assoc opts
:qualifier-fn to-lisp-case
:label-fn to-lisp-case)))
(defn as-lisp-simple-maps
"Result set builder which converts underscore characters to hyphens."
[rs opts]
(result-set/as-unqualified-modified-maps
rs (assoc opts
:qualifier-fn to-lisp-case-simple
:label-fn to-lisp-case-simple)))
(def gen-opts-simple
{:return-keys false
:builder-fn as-lisp-simple-maps
:column-fn to-snake-case-simple
:table-fn to-snake-case-simple})
(def gen-opts
{:return-keys false
:builder-fn as-lisp-maps
:column-fn to-snake-case
:table-fn to-snake-case})
(def gen-opts-simple-vec
{:return-keys false
:builder-fn as-lisp-simple-vectors
:column-fn to-snake-case-simple
:table-fn to-snake-case-simple})
(def gen-opts-vec
{:return-keys false
:builder-fn as-lisp-vectors
:column-fn to-snake-case
:table-fn to-snake-case})
(defn join-col-names
[cols]
(str/join "," (map to-snake-case-simple cols)))
(defn braced-join-col-names
[cols]
(str "(" (str/join "," (map to-snake-case-simple cols)) ")"))
(defn braced-join-col-names-no-conv
[cols]
(str "(" (str/join "," cols) ")"))
(defn join-?
[ids]
(str/join "," (map (constantly "?") ids)))
(defn braced-join-?
[ids]
(str "(" (str/join "," (map (constantly "?") ids)) ")"))
(defn join-v=?
[ids]
(str/join ", " (map #(str % " = ?") ids)))
(defn values-?
[coll]
(str "VALUES (" (str/join "," (map (constantly "?") coll)) ")"))
(defn braced-?
[coll]
(str "(" (str/join "," (map (constantly "?") coll)) ")"))
;; Type checks
(defn data-source?
[v]
(instance? DataSource v))
;; Memoization
(defn memoize
"Creates memoized version of a database accessing or other function."
([f]
(memoize f 256 150000))
([f queue-size]
(memoize f queue-size 150000))
([f queue-size ttl]
(mem/memoizer f (-> {}
(cache/fifo-cache-factory :threshold (long queue-size))
(cache/ttl-cache-factory :ttl (time/millis ttl))))))
(defn memoizer
"Creates a function for creating memoized functions with predefined TTL and queue
size taken from config. If the function is not given it will try to dereference
symbol present in the config under the :memoizer key."
([config]
(when-some [f (var/deref-symbol (:memoizer config))]
(memoizer f config)))
([f config]
(let [cache-size (:cache-size config)
cache-ttl (:cache-ttl config)
cache-ttl (when cache-ttl (time/millis cache-ttl))]
(if (and (pos-int? cache-size) (pos-int? cache-ttl))
(memoize f cache-size cache-ttl)
f))))
(defn invalidate!
[f key-params]
(if (seq key-params)
(mem/memo-clear! f key-params)
(mem/memo-clear! f)))
(defn invalidator
[f]
(if f
(fn [& key-params] (invalidate! f key-params))
(constantly nil)))
;; Getter and setter generators
(defn make-getter-coll
"Creates a database getter suitable for use with get-cached-coll- functions. The
returned function should accept an argument containing multiple identifiers."
([id-col]
(make-getter-coll nil id-col nil))
([id-col cols]
(make-getter-coll nil id-col cols))
([table id-col cols]
(let [id-col (keyword id-col)
cols (if (map? cols) (keys cols) cols)
cols (if (coll? cols) (seq cols) cols)
cols (if (coll? cols) cols [(or cols "*")])
table (to-snake-case-simple table)
q (str-spc "SELECT" (join-col-names cols)
"FROM" (or table "?")
"WHERE" (to-snake-case-simple id-col)
"IN (")]
(if table
(fn db-getter-coll
([db ids]
(db-getter-coll db nil ids))
([db _ ids]
(when-some [ids (seq ids)]
(let [ids (map some-str ids)
query (str q (join-? ids) ")")]
(->> (sql/query db (cons query ids) gen-opts-simple)
(reduce #(assoc %1 (keyword (get %2 id-col)) %2) {}))))))
(fn [db table ids]
(when-some [ids (seq ids)]
(let [ids (map some-str ids)
table (to-snake-case-simple table)
query (str q (join-? ids) ")")]
(->> (sql/query db (cons query (cons table ids)) gen-opts-simple)
(reduce #(assoc %1 (keyword (get %2 id-col)) %2) {})))))))))
(defn make-getter
([id-col cols]
(make-getter nil id-col cols nil))
([table id-col cols]
(make-getter table id-col cols nil))
([table id-col cols getter-coll-fn]
(let [id-col (keyword id-col)
cols (if (map? cols) (keys cols) cols)
cols (if (coll? cols) (seq cols) cols)
cols (if (coll? cols) cols [(or cols "*")])
table (to-snake-case-simple table)
q (str-spc "SELECT" (join-col-names cols)
"FROM" (or table "?")
"WHERE" (to-snake-case-simple id-col) "= ?")]
(if table
(if getter-coll-fn
(fn db-getter
([db id] (db-getter db nil id))
([db _ id] (jdbc/execute-one! db [q (some-str id)] gen-opts-simple))
([db _ id & more] (getter-coll-fn db (cons id more))))
(fn [db _ id]
(jdbc/execute-one! db [q (some-str id)] gen-opts-simple)))
(if getter-coll-fn
(fn
([db table id]
(jdbc/execute-one! db [q (to-snake-case-simple table) (some-str id)]
gen-opts-simple))
([db table id & more]
(getter-coll-fn db table (cons id more))))
(fn [db table id]
(jdbc/execute-one! db [q (to-snake-case-simple table) (some-str id)]
gen-opts-simple)))))))
(defn make-setter
([id-col]
(make-setter nil id-col nil nil))
([id-col cols]
(make-setter nil id-col cols nil))
([table id-col cols]
(make-setter table id-col cols nil))
([table id-col cols setter-coll-fn]
(let [id-col-orig id-col
id-col (to-snake-case-simple id-col)
tbremoved (hash-set id-col (keyword id-col) (keyword id-col-orig) id-col-orig)
remo #(apply dissoc % tbremoved)
cols (if (map? cols) (keys cols) cols)
cols (if (coll? cols) (seq cols) cols)
cols (if (coll? cols) cols [(or cols "*")])
cols (map to-snake-case-simple cols)
cols (remove tbremoved cols)
table (to-snake-case-simple table)
star? (= (first cols) "*")
cols (if star? cols (cons id-col cols))
rcols (rest cols)
insr (str-spc "INSERT INTO" (or table "?"))
ondup (str-spc "ON DUPLICATE KEY UPDATE" (join-v=? rcols))
q (if star? insr (str-spc insr
(braced-join-col-names-no-conv cols)
(values-? cols) ondup))]
(if table
(fn db-setter
([db _ id values]
(db-setter db id values))
([db id values]
(if (and (map? values) (seq values))
(when-some [values (seq (remo values))]
(let [mk (map #(to-snake-case-simple (nth % 0)) values)
mv (map #(nth % 1) values)
mk-f (cons id-col mk)
mv-f (cons (some-str id) mv)
q (str-spc insr (braced-join-col-names-no-conv mk-f) (values-? mv-f)
"ON DUPLICATE KEY UPDATE" (join-v=? mk))]
(jdbc/execute-one! db (cons q (concat mv-f mv)) gen-opts-simple)))
(let [values (if (coll? values) values [values])]
(if star?
(jdbc/execute-one! db (cons (str q " " (values-? values)) values)
gen-opts-simple)
(jdbc/execute-one! db (cons q (cons (some-str id) (concat values (rest values))))
gen-opts-simple))))))
(fn db-setter-table
([db table id values]
(if (and (map? values) (seq values))
(when-some [values (seq (remo values))]
(let [mk (map #(to-snake-case-simple (nth % 0)) values)
mv (map #(nth % 1) values)
mk-f (cons id-col mk)
mv-f (cons (some-str id) mv)
q (str-spc insr (braced-join-col-names-no-conv mk-f) (values-? mv-f)
"ON DUPLICATE KEY UPDATE" (join-v=? mk))]
(jdbc/execute-one! db (cons q (cons table (concat mv-f mv))) gen-opts-simple)))
(let [table (to-snake-case-simple table)
values (if (coll? values) values [values])]
(if star?
(jdbc/execute-one! db (cons (str q " " (values-? values)) (cons table values))
gen-opts-simple)
(jdbc/execute-one! db (cons q (cons table (cons (some-str id) (concat values (rest values)))))
gen-opts-simple))))))))))
(defn make-deleter
([id-col]
(make-deleter nil id-col))
([table id-col]
(let [id-col (keyword id-col)
table (to-snake-case-simple table)
q (str-spc "DELETE FROM" (or table "?")
"WHERE" (to-snake-case-simple id-col) "= ?")]
(if table
(fn db-deleter
([db _ id]
(db-deleter db id))
([db id]
(jdbc/execute-one! db [q (some-str id)] gen-opts-simple)))
(fn db-deleter-table
([db table id]
(jdbc/execute-one! db [q (to-snake-case-simple table) (some-str id)]
gen-opts-simple)))))))
;; Generic getters
(defn get-ids
"Gets a map of ID-to-properties from a database for the given IDs and a
table. Assumes each result will be related to a single, unique ID."
[db table ids]
(when (seq ids)
(let [ids (map some-str ids)]
(->> (sql/find-by-keys db table (cons (str "id IN " (braced-join-? ids)) ids)
gen-opts-simple)
(reduce #(assoc %1 (keyword (:id %2)) %2) {})))))
(defn get-id
"Gets properties of the given ID from a database table. For multiple IDs, calls
get-ids."
([db table id]
(sql/get-by-id db table (some-str id) gen-opts-simple))
([db table id & more]
(apply get-ids db table (cons id more))))
;; Caching (more precise and granulate control over caching than memoization)
(defn cache-prepare
"Prepares a cache object of the given TTL and/or queue size. Optionally it can get an
initial map of entries. Returns a cache object."
([ttl]
(cache-prepare ttl nil nil))
([ttl queue-size]
(cache-prepare ttl queue-size nil))
([ttl queue-size initial-map]
(let [ttl (when ttl (time/millis ttl))
ttl (when (pos-int? ttl) ttl)
qsize (when (pos-int? queue-size) queue-size)
initial-map (or initial-map {})
c initial-map
c (if qsize (cache/fifo-cache-factory c :threshold qsize) c)
c (if ttl (cache/ttl-cache-factory c :ttl ttl) c)]
(if (identical? c initial-map)
(nop-cache/factory)
c))))
(defn cache-create
"Creates a cache object of the given TTL and/or queue size. Optionally it can get an
initial map of entries. Returns cache object encapsulated in an atom."
([ttl]
(atom (cache-prepare ttl nil nil)))
([ttl queue-size]
(atom (cache-prepare ttl queue-size nil)))
([ttl queue-size initial-map]
(atom (cache-prepare ttl queue-size initial-map))))
(defn cache-evict!
"Removes entry or entries from the cache. Returns the updated cache from the atom."
([cache-atom entry]
(cwr/evict cache-atom entry))
([cache-atom entry & more]
(swap! cache-atom (partial reduce cache/evict) (cons entry more))))
(defn cache-lookup-coll
"Looks for a collection of entries identified by the given ID in a cache which should
be a cache object encapsulated in an atom."
[cache ids]
(when (seq ids)
(let [ids (map keyword ids)]
(reduce (fn [m id]
(let [props (cwr/lookup cache id false)]
(if (false? props)
(update m false conj id)
(assoc m id props))))
{} ids))))
(defn cache-lookup
"Looks for the entry of the given ID in a cache which should be a cache object
encapsulated in an atom. For multiple IDs, calls cache-lookup-coll."
([cache id]
(cwr/lookup cache (keyword id) false))
([cache id & ids]
(cache-lookup-coll cache (cons id ids))))
;; Cached getters and setters
(defn get-cached-coll
"Returns a (possibly cached) sequence of maps requested using db-getter for the given
IDs. When called with a table name it will be passed as a second argument to the
given getter function. When no getter is given the standard one (get-ids) is
used. Use make-getter-coll to create getter (with or without predefined table
name)."
{:arglists '([cache db-getter db ids]
[cache table db ids]
[cache table db-getter db ids])}
([cache db-getter-or-table db ids]
(if (fn? db-getter-or-table)
(get-cached-coll cache nil db-getter-or-table db ids)
(get-cached-coll cache db-getter-or-table get-ids db ids)))
([cache table db-getter db ids]
(let [looked-up (cache-lookup-coll cache ids)
not-found (seq (get looked-up false))]
(if-not not-found
looked-up
(let [from-db (db-getter db table not-found)]
(reduce #(assoc %1 %2 (cwr/lookup-or-miss cache %2 from-db))
(or (dissoc looked-up false) {})
not-found))))))
(defn get-cached
"Returns a (possibly cached) sequence of maps requested using db-getter for the given
IDs. A database connection and a table name can be passed to be used with the
standard get-id getter function instead. When multiple IDs are given, calls
get-cached-coll."
{:arglists '([cache db-getter db id]
[cache db-getter db id & more]
[cache table db id]
[cache table db id & more]
[cache table db-getter db id]
[cache table db-getter db id & more])}
([cache db-getter-or-table db id]
(if (fn? db-getter-or-table)
(cwr/lookup-or-miss cache (keyword id) #(db-getter-or-table db nil %))
(cwr/lookup-or-miss cache (keyword id) #(get-id db db-getter-or-table %))))
([cache db-getter-or-table db-or-getter id-or-db id2-or-id]
(if (data-source? db-or-getter)
(get-cached-coll cache nil db-getter-or-table db-or-getter [id-or-db id2-or-id])
(cwr/lookup-or-miss cache (keyword id2-or-id) #(db-or-getter id-or-db db-getter-or-table %))))
([cache db-getter-or-table db-or-getter id-or-db id2-or-id & more]
(if (data-source? db-or-getter)
(get-cached-coll cache db-getter-or-table db-or-getter (cons id-or-db (cons id2-or-id more)))
(get-cached-coll cache db-getter-or-table db-or-getter id-or-db (cons id2-or-id more)))))
(defn get-cached-coll-prop
"Uses get-cached-coll to retrieve a map with keys being IDs and values being
requested properties. If there is no data for the given ID, corresponding entry is
not added to the resulting map. If the property does not exist, nil is added."
{:arglists '([cache db-getter db property ids]
[cache table db property ids]
[cache table db-getter db property ids])}
([cache db-getter-or-table db prop ids]
(let [prop (keyword prop)
ids (get-cached-coll cache db-getter-or-table db ids)]
(reduce-kv #(if (nil? %3) (dissoc %1 %2) (assoc %1 %2 (get %3 prop))) ids ids)))
([cache table db-getter db prop ids]
(let [prop (keyword prop)
ids (get-cached-coll cache table db-getter db ids)]
(reduce-kv #(if (nil? %3) (dissoc %1 %2) (assoc %1 %2 (get %3 prop))) ids ids))))
(defn get-cached-prop
"Same as get-cached but retrieves a single property from the result by using the get
function. When multiple IDs are given it calls get-cached-coll-prop to handle it."
{:arglists '([cache db-getter db property id]
[cache db-getter db property id & more]
[cache table db property id]
[cache table db property id & more]
[cache table db-getter db property id]
[cache table db-getter db property id & more])}
([cache db-getter-or-table db prop id]
(get (get-cached cache db-getter-or-table db id) (keyword prop)))
([cache db-getter-or-table db-or-getter prop-or-db id-or-prop id2-or-id]
(if (data-source? db-or-getter)
(get-cached-coll-prop cache db-getter-or-table db-or-getter prop-or-db [id-or-prop id2-or-id])
(get (get-cached cache db-getter-or-table db-or-getter prop-or-db id2-or-id) (keyword id-or-prop))))
([cache db-getter-or-table db-or-getter prop-or-db id-or-prop id2-or-id & more]
(if (data-source? db-or-getter)
(get-cached-coll-prop cache db-getter-or-table db-or-getter prop-or-db (list id-or-prop id2-or-id more))
(get-cached-coll-prop cache db-getter-or-table db-or-getter prop-or-db id-or-prop (cons id2-or-id more)))))
(defn get-cached-prop-or-default
"Same as get-cached-prop but when there is no entry for the given ID, it returns the
given default value."
{:arglists '([cache db-getter db property default id]
[cache db-getter db property default id & more]
[cache table db property default id]
[cache table db property default id & more]
[cache table db-getter db property default id]
[cache table db-getter db property default id & more])}
([cache db-getter-or-table db prop default id]
(if-some [props (get-cached cache db-getter-or-table db id)]
(get props (keyword prop))
default))
([cache db-getter-or-table db-or-getter prop-or-db default-or-prop id-or-default id2-or-id]
(if (data-source? db-or-getter)
(let [ids (map keyword [id-or-default id2-or-id])
m (get-cached-coll-prop cache db-getter-or-table db-or-getter prop-or-db ids)]
(reduce #(if (contains? %1 %2) %1 (assoc %1 %2 default-or-prop)) m ids))
(if-some [props (get-cached cache db-getter-or-table db-or-getter prop-or-db id2-or-id)]
(get props (keyword default-or-prop))
id-or-default)))
([cache db-getter-or-table db-or-getter prop-or-db default-or-prop id-or-default id2-or-id & more]
(if (data-source? db-or-getter)
(let [ids (cons id-or-default (cons id2-or-id more))
m (get-cached-coll-prop cache db-getter-or-table db-or-getter prop-or-db ids)]
(reduce #(if (contains? %1 %2) %1 (assoc %1 %2 default-or-prop)) m ids))
(let [ids (cons id2-or-id more)
m (get-cached-coll-prop cache db-getter-or-table db-or-getter prop-or-db default-or-prop ids)]
(reduce #(if (contains? %1 %2) %1 (assoc %1 %2 id-or-default)) m ids)))))
;; SQL helpers
(defn for-replace
"Given a table name and a hash map of column names and their values,
return a vector of the full `REPLACE` SQL string and its parameters.
Applies any `:table-fn` / `:column-fn` supplied in the options. If `:suffix` is
provided in `opts`, that string is appended to the `INSERT ...` statement."
[table key-map opts]
(let [entity-fn (:table-fn opts identity)
params (builder/as-keys key-map opts)
places (builder/as-? key-map opts)]
(assert (seq key-map) "key-map may not be empty")
(into [(str "REPLACE INTO " (entity-fn (name table))
" (" params ")"
" VALUES (" places ")"
(when-let [suffix (:suffix opts)]
(str " " suffix)))]
(vals key-map))))
(defn for-insert-or
"Given a table name and a hash map of column names and their values,
return a vector of the full `INSERT OR IGNORE` SQL string and its parameters.
Applies any `:table-fn` / `:column-fn` supplied in the options. If `:suffix` is
provided in `opts`, that string is appended to the `INSERT ...` statement. If
`:alt-clause` is provided in `opts`, it will replace the default IGNORE string."
[table key-map opts]
(let [entity-fn (:table-fn opts identity)
alt-clause (:alt-clause opts "IGNORE")
params (builder/as-keys key-map opts)
places (builder/as-? key-map opts)]
(assert (seq key-map) "key-map may not be empty")
(into [(str "INSERT " alt-clause " INTO " (entity-fn (name table))
" (" params ")"
" VALUES (" places ")"
(when-let [suffix (:suffix opts)]
(str " " suffix)))]
(vals key-map))))
(defn for-replace-multi
"Given a table name, a vector of column names, and a vector of row values
(each row is a vector of its values), return a vector of the full `REPLACE` SQL
string and its parameters. Applies any `:table-fn` / `:column-fn` supplied in the
options. If `:suffix` is provided in `opts`, that string is appended to the
`REPLACE ...` statement."
[table cols rows opts]
(assert (apply = (count cols) (map count rows))
"column counts are not consistent across cols and rows")
(assert (seq cols) "cols may not be empty")
(assert (seq rows) "rows may not be empty")
(let [table-fn (:table-fn opts identity)
column-fn (:column-fn opts identity)
params (str/join ", " (map (comp column-fn name) cols))
places (builder/as-? (first rows) opts)]
(into [(str "REPLACE INTO " (table-fn (name table))
" (" params ")"
" VALUES "
(str/join ", " (repeat (count rows) (str "(" places ")")))
(when-let [suffix (:suffix opts)]
(str " " suffix)))]
cat
rows)))
(defn for-insert-multi-or
"Given a table name, a vector of column names, and a vector of row values
(each row is a vector of its values), return a vector of the full `INSERT` SQL
string and its parameters. Applies any `:table-fn` / `:column-fn` supplied in the
options. If `:suffix` is provided in `opts`, that string is appended to the
`INSERT IGNORE ...` statement. The `IGNORE` part can be replaced by
supplying :alt-clause option key."
[table cols rows opts]
(assert (apply = (count cols) (map count rows))
"column counts are not consistent across cols and rows")
(assert (seq cols) "cols may not be empty")
(assert (seq rows) "rows may not be empty")
(let [table-fn (:table-fn opts identity)
column-fn (:column-fn opts identity)
alt-clause (:alt-clause opts "IGNORE")
params (str/join ", " (map (comp column-fn name) cols))
places (builder/as-? (first rows) opts)]
(into [(str "INSERT " alt-clause " INTO " (table-fn (name table))
" (" params ")"
" VALUES "
(str/join ", " (repeat (count rows) (str "(" places ")")))
(when-let [suffix (:suffix opts)]
(str " " suffix)))]
cat
rows)))
(defn insert-or!
"Syntactic sugar over `execute-one!` to make inserting hash maps easier.
Given a connectable object, a table name, and a data hash map, inserts the data as
a single row in the database and attempts to return a map of generated keys. By
default it uses `INSERT OR IGNORE` but the `IGNORE` can be changed to anything by
supplying :alt-clause option in opts map."
([connectable table key-map]
(insert-or! connectable table key-map {:alt-clause "IGNORE"}))
([connectable table key-map opts]
(let [opts (merge (:options connectable) opts)]
(jdbc/execute-one! connectable
(for-insert-or table key-map opts)
(merge {:return-keys true} opts)))))
(defn insert-multi-or!
"Syntactic sugar over `execute!` to make inserting columns/rows easier.
Same as insert-or! but supports multiple rows to be inserted at once."
([connectable table cols rows]
(insert-multi-or! connectable table cols rows {:alt-clause "IGNORE"}))
([connectable table cols rows opts]
(if (seq rows)
(let [opts (merge (:options connectable) opts)]
(jdbc/execute! connectable
(for-insert-multi-or table cols rows opts)
(merge {:return-keys true} opts)))
[])))
(defn insert-or-replace-multi!
"Syntactic sugar over `execute!` to make inserting columns/rows easier.
Same as insert-multi! but supports :alt-clause option key."
([connectable table cols rows]
(insert-multi-or! connectable table cols rows {:alt-clause "REPLACE"}))
([connectable table cols rows opts]
(insert-multi-or! connectable table cols rows (assoc opts :alt-clause "REPLACE"))))
(defn insert-or-ignore-multi!
"Syntactic sugar over `execute!` to make inserting columns/rows easier.
Same as insert-multi! but supports :alt-clause option key."
([connectable table cols rows]
(insert-multi-or! connectable table cols rows {:alt-clause "IGNORE"}))
([connectable table cols rows opts]
(insert-multi-or! connectable table cols rows (assoc opts :alt-clause "IGNORE"))))
(defn insert-or-replace!
"Syntactic sugar over `execute-one!` to make inserting hash maps easier.
Given a connectable object, a table name, and a data hash map, inserts the data as
a single row in the database and attempts to return a map of generated keys."
([connectable table key-map]
(insert-or! connectable table key-map {:alt-clause "REPLACE"}))
([connectable table key-map opts]
(insert-or! connectable table key-map (assoc opts :alt-clause "REPLACE"))))
(defn insert-or-ignore!
"Syntactic sugar over `execute-one!` to make inserting hash maps easier.
Given a connectable object, a table name, and a data hash map, inserts the data as
a single row in the database and attempts to return a map of generated keys."
([connectable table key-map]
(insert-or! connectable table key-map {:alt-clause "IGNORE"}))
([connectable table key-map opts]
(insert-or! connectable table key-map (assoc opts :alt-clause "IGNORE"))))
(defn replace!
"Syntactic sugar over `execute-one!` to make inserting hash maps easier.
Given a connectable object, a table name, and a data hash map, inserts the data as
a single row in the database and attempts to return a map of generated keys. By
default it uses `REPLACE`."
([connectable table key-map]
(replace! connectable table key-map {}))
([connectable table key-map opts]
(let [opts (merge (:options connectable) opts)]
(jdbc/execute-one! connectable
(for-replace table key-map opts)
(merge {:return-keys true} opts)))))
(defn replace-multi!
"Syntactic sugar over `execute!` to make inserting columns/rows easier.
Same as replace! but supports multiple rows to be inserted at once."
([connectable table cols rows]
(replace-multi! connectable table cols rows {}))
([connectable table cols rows opts]
(if (seq rows)
(let [opts (merge (:options connectable) opts)]
(jdbc/execute! connectable
(for-replace-multi table cols rows opts)
(merge {:return-keys true} opts)))
[])))
;; Settings abstraction
(def listed-nil '(nil))
(defn- prep-pairs
[coll]
(when (coll? coll)
(seq (if (map? coll) (mapcat seq coll) coll))))
(defn- prep-names
[coll]
(when (coll? coll)
(seq (if (map? coll) (keys coll) coll))))
(defn get-failed?
"Returns true if getting from a database failed in post-processing
phase (e.g. de-serialization) and the data were broken."
[v]
(= ::get-failed v))
(defn make-setting-getter
"Returns a function which gets a setting for the given entity and de-serializes it to
a Clojure data structure. Table name and entity column name must be quoted if
needed before passing to this function."
[table entity-column]
(let [table (to-snake-case-simple table)
entity-column (to-snake-case-simple entity-column)
ret-value-key (keyword (name table) "value")
getter-query (str-spc "SELECT value FROM" table
"WHERE" entity-column "= ? AND id = ?")]
(fn [db entity-id setting-id]
(when-some [entity-id (some-str entity-id)]
(when-some [setting-id (some-str setting-id)]
(if db
(when-some [r (ret-value-key
(jdbc/execute-one! db [getter-query entity-id setting-id]))]
(try
(nippy/thaw r)
(catch Throwable _
(log/err "Error de-serializing setting" setting-id "for" entity-id "in" table)
::get-failed)))
(log/err "Cannot get setting" setting-id "in" table "for" entity-id
"because database connection is not set")))))))
(defn make-setting-setter
"Returns a function which stores one or more settings for a given entity in a
database. Max. object size is 32 KB. Table name and entity column name must be
quoted if needed before passing to this function."
[table entity-column]
(let [table (some-keyword-simple table)
entity-column (some-keyword-simple entity-column)]
(fn put-setting
([db entity-id setting-id value]
(when-some [entity-id (some-str entity-id)]
(when-some [setting-id (some-str setting-id)]
(if-not db
(log/err "Cannot store setting" setting-id "in" table "for" entity-id
"because database connection is not set")
(or (pos-int?
(::jdbc/update-count
(replace! db table
{:id setting-id
entity-column entity-id
:value (nippy/freeze value)}
gen-opts)))
(log/err "Error putting setting" setting-id
"into a database table" table))))))
([db entity-id setting-id value & pairs]
(if-let [pairs (prep-pairs pairs)]
(when-some [entity-id (some-str entity-id)]
(when-some [setting-id (some-str setting-id)]
(if-not db
(log/err "Cannot store setting" setting-id "in" table "for" entity-id
"because database connection is not set")
(let [r (replace-multi!
db table
[entity-column :id :value]
(->> pairs (cons value) (cons setting-id)
(partition 2 2 listed-nil)
(map (juxt-seq (constantly entity-id)
(comp some-str first)
(comp nippy/freeze second))))
gen-opts)]
(or (pos-int? (::jdbc/update-count (first r)))
(log/err "Error putting settings into a database table" table
"for" entity-id))))))
(put-setting db entity-id setting-id value))))))
(defn make-setting-deleter
"Creates a setting deleter on a basis of the given table and entity column."
[table entity-column]
(let [table-str (to-snake-case-simple table)
table (some-keyword-simple table-str)
entity-column-str (to-snake-case-simple entity-column)
entity-column (some-keyword-simple entity-column-str)
ret-subquery "RETURNING id"
deleter-subquery (str-spc "DELETE FROM" table-str
"WHERE" entity-column-str "= ? AND id IN ")
db-opts (assoc gen-opts-simple :return-keys false)
db-opts-ret (assoc gen-opts-simple-vec :return-keys false :suffix ret-subquery)]
(fn del-setting
([db entity-id]
(when-some [entity-id (some-str entity-id)]
(if-not db
(log/err "Cannot delete settings in" table "for" entity-id
"because database connection is not set")
(sql/delete! db table {entity-column entity-id} db-opts-ret))))
([db entity-id setting-id]
(when-some [entity-id (some-str entity-id)]
(when-some [setting-id (some-str setting-id)]
(if-not db
(log/err "Cannot delete setting" setting-id "in" table "for" entity-id
"because database connection is not set")
(-> (sql/delete! db table {:id setting-id entity-column entity-id} db-opts)
::jdbc/update-count
pos-int?)))))
([db entity-id setting-id & setting-ids]
(if-let [setting-ids (prep-names setting-ids)]
(when-some [entity-id (some-str entity-id)]
(when-some [setting-id (some-str setting-id)]
(if-not db
(log/err "Cannot delete settings in" table "for" entity-id
"because database connection is not set")
(let [setting-ids (mapv some-str (cons setting-id setting-ids))
valsqls (str "(" (str/join "," (repeat (count setting-ids) "?")) ")")
query (cons (str-spc deleter-subquery valsqls) (cons entity-id setting-ids))]
(-> (jdbc/execute-one! db query db-opts)
::jdbc/update-count
pos-int?)))))
(del-setting db entity-id setting-id))))))
;; Cached settings
(defn cached-setting-get
"Gets the cached result of calling the given setting getter. Updates cache when
necessary."
[cache getter db entity-id setting-id]
(let [k [(keyword entity-id) (keyword setting-id)]]
(cwr/lookup-or-miss cache k #(apply getter db %))))
(defn cached-setting-set
"Sets the cached result of calling the given setting setter. Purges cache entry after
operation succeeded."
([cache setter db entity-id setting-id value]
(let [r (setter db entity-id setting-id value)]
(cache-evict! cache [(keyword entity-id) (keyword setting-id)]) r))
([cache setter db entity-id setting-id value & pairs]
(let [r (apply setter db entity-id setting-id value pairs)
entity-id (keyword entity-id)
seed-vec (vector entity-id)]
(apply cache-evict! cache
[entity-id (keyword setting-id)]
(map #(conj seed-vec (keyword %))
(take-nth 2 pairs)))
r)))
(defn cached-setting-del
"Deletes the cached result of calling the given setting deleter. Purges cache entry
after operation succeeded."
([cache deleter db entity-id]
(when-some [r (deleter db entity-id)]
(let [seed-vec (vector (keyword entity-id))]
(apply cache-evict! cache (map #(conj seed-vec (keyword %)) r)) true)))
([cache deleter db entity-id setting-id]
(let [r (deleter db entity-id setting-id)]
(cache-evict! cache [(keyword entity-id) (keyword setting-id)]) r))
([cache deleter db entity-id setting-id & pairs]
(let [r (apply deleter db entity-id setting-id pairs)
entity-id (keyword entity-id)
seed-vec (vector entity-id)]
(apply cache-evict! cache
[entity-id (keyword setting-id)]
(map #(conj seed-vec (keyword %))
(take-nth 2 pairs)))
r)))
(defn init-cache
"Initializes single cache by parsing TTL and queue size."
[{:keys [size ttl seed]}]
(cache-prepare (time/parse-duration ttl)
(safe-parse-long size)
(or seed {})))
(defn init-caches
"Initializes in-memory caches by resetting atoms associated with cache parameters."
[config]
(log/msg "Initializing abstract in-memory caches" (str "(" (count config) " slots)"))
(reduce-kv
(fn [acc id cfg]
(let [cache-atom (var/deref id)
cache-atom (if (atom? cache-atom) cache-atom (var/make id (atom nil)))
cfg (if (map? cfg) cfg {})]
(reset! cache-atom (init-cache cfg))
(assoc acc (symbol id) cache-atom)))
{} config))
(defn purge-caches
"Removes everything from caches."
[config]
(log/msg "Purging abstract in-memory caches")
(doseq [c (vals config)]
(when (atom? c)
(reset! c nil)))
nil)
(defn print-caches
[caches]
(doseq [[id c] caches]
(let [c (if (atom? c) (deref c) c)
t (peek (str/split (pr-str (type c)) #"\."))
cfg (get-in (var/deref 'app/config) [::caches (keyword id)])
ttl (some->> cfg :ttl (str/join " ") (str ", TTL: "))
qsize (some->> cfg :size (str ", Size: "))]
(println (str id " (" t ttl qsize "): "
(count c) " entries.")))))