/
specs.clj
160 lines (136 loc) · 6 KB
/
specs.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
;; copyright (c) 2019 Sean Corfield, all rights reserved
(ns next.jdbc.specs
"Specs for the core API of next.jdbc.
The functions from `next.jdbc` and `next.jdbc.sql` have specs here.
Just `:args` are spec'd. These specs are intended to aid development
with `next.jdbc` by catching simple errors in calling the library.
The `connectable` argument is currently just `any?` but both
`get-datasource` and `get-connection` have stricter specs. If you
extend `Sourceable` or `Connectable`, those specs will likely be too strict.
In addition, there is an `instrument` function that provides a simple
way to instrument all of the `next.jdbc` functions."
(:require [clojure.spec.alpha :as s]
[clojure.spec.test.alpha :as st]
[next.jdbc :as jdbc]
[next.jdbc.sql :as sql])
(:import (java.sql Connection PreparedStatement)
(javax.sql DataSource)))
(s/def ::dbtype string?)
(s/def ::dbname string?)
(s/def ::classname string?)
(s/def ::user string?)
(s/def ::password string?)
(s/def ::host string?)
(s/def ::port pos-int?)
(s/def ::db-spec-map (s/keys :req-un [::dbtype ::dbname]
:opt-un [::classname
::user ::password
::host ::port]))
(s/def ::connection #(instance? Connection %))
(s/def ::datasource #(instance? DataSource %))
(s/def ::prepared-statement #(instance? PreparedStatement %))
(s/def ::db-spec (s/or :db-spec ::db-spec-map
:string string?
:ds ::datasource))
(s/def ::connectable any?)
(s/def ::key-map (s/map-of keyword? any?))
(s/def ::opts-map (s/map-of keyword? any?))
(s/def ::sql-params (s/and vector?
(s/cat :sql string?
:params (s/* any?))))
(s/fdef jdbc/get-datasource
:args (s/cat :spec ::db-spec))
(s/fdef jdbc/get-connection
:args (s/cat :spec ::db-spec
:opts (s/? ::opts-map)))
(s/fdef jdbc/prepare
:args (s/cat :connection ::connection
:sql-params ::sql-params
:opts (s/? ::opts-map)))
(s/fdef jdbc/plan
:args (s/alt :prepared (s/cat :stmt ::prepared-statement)
:sql (s/cat :connectable ::connectable
:sql-params ::sql-params
:opts (s/? ::opts-map))))
(s/fdef jdbc/execute!
:args (s/alt :prepared (s/cat :stmt ::prepared-statement)
:sql (s/cat :connectable ::connectable
:sql-params ::sql-params
:opts (s/? ::opts-map))))
(s/fdef jdbc/execute-one!
:args (s/alt :prepared (s/cat :stmt ::prepared-statement)
:sql (s/cat :connectable ::connectable
:sql-params ::sql-params
:opts (s/? ::opts-map))))
(s/fdef jdbc/transact
:args (s/cat :connectable ::connectable
:f fn?
:opts (s/? ::opts-map)))
(s/fdef jdbc/with-transaction
:args (s/cat :binding (s/and vector?
(s/cat :sym simple-symbol?
:connectable ::connectable
:opts ::opts-map))
:body (s/* any?)))
(s/fdef sql/insert!
:args (s/cat :connectable ::connectable
:table keyword?
:key-map ::key-map
:opts (s/? ::opts-map)))
(s/fdef sql/insert-multi!
:args (s/and (s/cat :connectable ::connectable
:table keyword?
:cols (s/coll-of keyword? :kind vector?)
:rows (s/coll-of (s/coll-of any? :kind vector?) :kind vector?)
:opts (s/? ::opts-map))
#(apply = (count (:cols %))
(map count (:rows %)))))
(s/fdef sql/query
:args (s/cat :connectable ::connectable
:sql-params ::sql-params
:opts (s/? ::opts-map)))
(s/fdef sql/find-by-keys
:args (s/cat :connectable ::connectable
:table keyword?
:key-map (s/or :example ::key-map
:where ::sql-params)
:opts (s/? ::opts-map)))
(s/fdef sql/get-by-id
:args (s/alt :with-id (s/cat :connectable ::connectable
:table keyword?
:pk any?
:opts (s/? ::opts-map))
:pk-name (s/cat :connectable ::connectable
:table keyword?
:pk any?
:pk-name keyword?
:opts ::opts-map)))
(s/fdef sql/update!
:args (s/cat :connectable ::connectable
:table keyword?
:key-map ::key-map
:where-params (s/or :example ::key-map
:where ::sql-params)
:opts (s/? ::opts-map)))
(s/fdef sql/delete!
:args (s/cat :connectable ::connectable
:table keyword?
:where-params (s/or :example ::key-map
:where ::sql-params)
:opts (s/? ::opts-map)))
(defn instrument []
(st/instrument [`jdbc/get-datasource
`jdbc/get-connection
`jdbc/prepare
`jdbc/plan
`jdbc/execute!
`jdbc/execute-one!
`jdbc/transact
`jdbc/with-transaction
`sql/insert!
`sql/insert-multi!
`sql/query
`sql/find-by-keys
`sql/get-by-id
`sql/update!
`sql/delete!]))