-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdbms.clj
125 lines (115 loc) · 4.57 KB
/
rdbms.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
(ns andrewslai.clj.persistence.rdbms
(:require [camel-snake-kebab.core :as csk]
[camel-snake-kebab.extras :as cske]
[cheshire.core :as json]
[clojure.spec.alpha :as s]
[honeysql.core :as hsql]
[honeysql.helpers :as hh]
[next.jdbc :as next]
[next.jdbc.result-set :as rs]
[next.jdbc.sql :as next.sql]
[slingshot.slingshot :refer [throw+ try+]]
[taoensso.timbre :as log])
(:import
org.postgresql.util.PGobject))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; API for interacting with a relational database
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn transact!
[conn stmt]
(next/execute! conn stmt {:return-keys true
:builder-fn rs/as-unqualified-kebab-maps}))
(defn select
[database m]
(next/execute! database
(hsql/format m)
{:builder-fn rs/as-unqualified-kebab-maps}))
(defn validate [type data ex]
(if (s/valid? type data)
true
(throw+
(let [reason (s/explain-str type data)]
{:type ex
:subtype type
:message {:data data
:reason reason
:feedback (or (:feedback data)
reason)}}))))
(defn find-by-keys
([database table query-map]
(next.sql/find-by-keys database
(csk/->snake_case_keyword table)
(cske/transform-keys csk/->snake_case_keyword query-map)
{:builder-fn rs/as-unqualified-kebab-maps})))
(defn make-finder
[table]
(fn getter
([database]
(next/execute! database
[(format "SELECT * FROM %s" (csk/->snake_case_string table))]
{:builder-fn rs/as-unqualified-kebab-maps}))
([database query-map]
(if (empty? query-map)
(getter database)
(find-by-keys database table query-map)))))
(defn insert! [database table m & {:keys [ex-subtype
input-validation]}]
(when input-validation
(validate input-validation m :IllegalArgumentException))
(try+
(transact! database (-> (hh/insert-into table)
(hh/values (if (map? m)
[m]
m))
hsql/format))
(catch org.postgresql.util.PSQLException e
(log/errorf "Caught Exception while inserting: %s" e)
(throw+ (merge {:type :PersistenceException
:message {:data (select-keys m [:username :email])
:reason (.getMessage e)}}
(when ex-subtype
{:subtype ex-subtype}))))
(catch Object e
(log/errorf "Caught Exception while inserting: %s" e)
(throw+ (merge {:type :PersistenceException
:message {:data (select-keys m [:username :email])
:reason (.getMessage e)}}
(when ex-subtype
{:subtype ex-subtype}))))))
(defn update! [database table m where & {:keys [ex-subtype
input-validation]}]
(when input-validation
(validate input-validation m :IllegalArgumentException))
(try+
(transact! database (-> (hh/update table)
(hh/sset m)
(hh/where where)
hsql/format))
(catch org.postgresql.util.PSQLException e
(throw+ (merge {:type :PersistenceException
:message {:data m
:reason (.getMessage e)}}
(when ex-subtype
{:subtype ex-subtype}))))))
(defn delete! [database table ids & {:keys [ex-subtype]}]
(try+
(transact! database (-> (hh/delete-from table)
(hh/where [:in :id (if (coll? ids)
ids
[ids])])
hsql/format))
(catch org.postgresql.util.PSQLException e
(throw+ (merge {:type :PersistenceException
:message {:data ids
:reason (.getMessage e)}}
(when ex-subtype
{:subtype ex-subtype}))))))
(comment
(def example-user
{:id #uuid "f5778c59-e57d-46f0-b5e5-516e5d36481c"
:first_name "Andrew"
:last_name "Lai"
:username "alai"
:avatar nil
:email "andrew@andrew.com"})
)