on master help sign in
Add documentation to DBType instances
03d0f40Ollie Charles
src/Rel8/Type.hs+26−5
@@ -61,11 +61,6 @@ import Data.UUID ( UUID )
61import qualified Data.UUID as UUID
61import qualified Data.UUID as UUID
62
62
63
63
64−type DBType :: Type -> Constraint
65−class DBType a where
66− typeInformation :: TypeInformation a
67−
68−
69-- | Haskell types that can be represented as expressions in a database. There
64-- | Haskell types that can be represented as expressions in a database. There
70-- should be an instance of @DBType@ for all column types in your database
65-- should be an instance of @DBType@ for all column types in your database
71-- schema (e.g., @int@, @timestamptz@, etc).
66-- schema (e.g., @int@, @timestamptz@, etc).
@@ -86,6 +81,12 @@ class DBType a where
86-- You can now write queries using @UserId@ instead of @Int32@, which may help
81-- You can now write queries using @UserId@ instead of @Int32@, which may help
87-- avoid making bad joins. However, when SQL is generated, it will be as if you
82-- avoid making bad joins. However, when SQL is generated, it will be as if you
88-- just used integers (the type distinction does not impact query generation).
83-- just used integers (the type distinction does not impact query generation).
84+type DBType :: Type -> Constraint
85+class DBType a where
86+ typeInformation :: TypeInformation a
87+
88+
89+-- | Corresponds to @bool@
89instance DBType Bool where
90instance DBType Bool where
90 typeInformation = TypeInformation
91 typeInformation = TypeInformation
91 { encode = Opaleye.ConstExpr . Opaleye.BoolLit
92 { encode = Opaleye.ConstExpr . Opaleye.BoolLit
@@ -95,6 +96,7 @@ instance DBType Bool where
95 }
96 }
96
97
97
98
99+-- | Corresponds to @char@
98instance DBType Char where
100instance DBType Char where
99 typeInformation = TypeInformation
101 typeInformation = TypeInformation
100 { encode = Opaleye.ConstExpr . Opaleye.StringLit . pure
102 { encode = Opaleye.ConstExpr . Opaleye.StringLit . pure
@@ -104,6 +106,7 @@ instance DBType Char where
104 }
106 }
105
107
106
108
109+-- | Corresponds to @int2@
107instance DBType Int16 where
110instance DBType Int16 where
108 typeInformation = TypeInformation
111 typeInformation = TypeInformation
109 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
112 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
@@ -113,6 +116,7 @@ instance DBType Int16 where
113 }
116 }
114
117
115
118
119+-- | Corresponds to @int4@
116instance DBType Int32 where
120instance DBType Int32 where
117 typeInformation = TypeInformation
121 typeInformation = TypeInformation
118 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
122 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
@@ -122,6 +126,7 @@ instance DBType Int32 where
122 }
126 }
123
127
124
128
129+-- | Corresponds to @int8@
125instance DBType Int64 where
130instance DBType Int64 where
126 typeInformation = TypeInformation
131 typeInformation = TypeInformation
127 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
132 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
@@ -131,6 +136,7 @@ instance DBType Int64 where
131 }
136 }
132
137
133
138
139+-- | Corresponds to @float4@
134instance DBType Float where
140instance DBType Float where
135 typeInformation = TypeInformation
141 typeInformation = TypeInformation
136 { encode = Opaleye.ConstExpr . Opaleye.NumericLit . realToFrac
142 { encode = Opaleye.ConstExpr . Opaleye.NumericLit . realToFrac
@@ -140,6 +146,7 @@ instance DBType Float where
140 }
146 }
141
147
142
148
149+-- | Corresponds to @float8@
143instance DBType Double where
150instance DBType Double where
144 typeInformation = TypeInformation
151 typeInformation = TypeInformation
145 { encode = Opaleye.ConstExpr . Opaleye.NumericLit . realToFrac
152 { encode = Opaleye.ConstExpr . Opaleye.NumericLit . realToFrac
@@ -149,6 +156,7 @@ instance DBType Double where
149 }
156 }
150
157
151
158
159+-- | Corresponds to @numeric@
152instance DBType Scientific where
160instance DBType Scientific where
153 typeInformation = TypeInformation
161 typeInformation = TypeInformation
154 { encode = Opaleye.ConstExpr . Opaleye.NumericLit
162 { encode = Opaleye.ConstExpr . Opaleye.NumericLit
@@ -158,6 +166,7 @@ instance DBType Scientific where
158 }
166 }
159
167
160
168
169+-- | Corresponds to @timestamptz@
161instance DBType UTCTime where
170instance DBType UTCTime where
162 typeInformation = TypeInformation
171 typeInformation = TypeInformation
163 { encode =
172 { encode =
@@ -169,6 +178,7 @@ instance DBType UTCTime where
169 }
178 }
170
179
171
180
181+-- | Corresponds to @date@
172instance DBType Day where
182instance DBType Day where
173 typeInformation = TypeInformation
183 typeInformation = TypeInformation
174 { encode =
184 { encode =
@@ -180,6 +190,7 @@ instance DBType Day where
180 }
190 }
181
191
182
192
193+-- | Corresponds to @timestamp@
183instance DBType LocalTime where
194instance DBType LocalTime where
184 typeInformation = TypeInformation
195 typeInformation = TypeInformation
185 { encode =
196 { encode =
@@ -191,6 +202,7 @@ instance DBType LocalTime where
191 }
202 }
192
203
193
204
205+-- | Corresponds to @time@
194instance DBType TimeOfDay where
206instance DBType TimeOfDay where
195 typeInformation = TypeInformation
207 typeInformation = TypeInformation
196 { encode =
208 { encode =
@@ -202,6 +214,7 @@ instance DBType TimeOfDay where
202 }
214 }
203
215
204
216
217+-- | Corresponds to @interval@
205instance DBType DiffTime where
218instance DBType DiffTime where
206 typeInformation = TypeInformation
219 typeInformation = TypeInformation
207 { encode =
220 { encode =
@@ -218,6 +231,7 @@ instance DBType NominalDiffTime where
218 mapTypeInformation @DiffTime realToFrac realToFrac typeInformation
231 mapTypeInformation @DiffTime realToFrac realToFrac typeInformation
219
232
220
233
234+-- | Corresponds to @text@
221instance DBType Text where
235instance DBType Text where
222 typeInformation = TypeInformation
236 typeInformation = TypeInformation
223 { encode = Opaleye.ConstExpr . Opaleye.StringLit . Text.unpack
237 { encode = Opaleye.ConstExpr . Opaleye.StringLit . Text.unpack
@@ -227,23 +241,27 @@ instance DBType Text where
227 }
241 }
228
242
229
243
244+-- | Corresponds to @text@
230instance DBType Lazy.Text where
245instance DBType Lazy.Text where
231 typeInformation =
246 typeInformation =
232 mapTypeInformation Text.fromStrict Text.toStrict typeInformation
247 mapTypeInformation Text.fromStrict Text.toStrict typeInformation
233
248
234
249
250+-- | Corresponds to @citext@
235instance DBType (CI Text) where
251instance DBType (CI Text) where
236 typeInformation = mapTypeInformation CI.mk CI.original typeInformation
252 typeInformation = mapTypeInformation CI.mk CI.original typeInformation
237 { typeName = "citext"
253 { typeName = "citext"
238 }
254 }
239
255
240
256
257+-- | Corresponds to @citext@
241instance DBType (CI Lazy.Text) where
258instance DBType (CI Lazy.Text) where
242 typeInformation = mapTypeInformation CI.mk CI.original typeInformation
259 typeInformation = mapTypeInformation CI.mk CI.original typeInformation
243 { typeName = "citext"
260 { typeName = "citext"
244 }
261 }
245
262
246
263
264+-- | Corresponds to @bytea@
247instance DBType ByteString where
265instance DBType ByteString where
248 typeInformation = TypeInformation
266 typeInformation = TypeInformation
249 { encode = Opaleye.ConstExpr . Opaleye.ByteStringLit
267 { encode = Opaleye.ConstExpr . Opaleye.ByteStringLit
@@ -253,12 +271,14 @@ instance DBType ByteString where
253 }
271 }
254
272
255
273
274+-- | Corresponds to @bytea@
256instance DBType Lazy.ByteString where
275instance DBType Lazy.ByteString where
257 typeInformation =
276 typeInformation =
258 mapTypeInformation ByteString.fromStrict ByteString.toStrict
277 mapTypeInformation ByteString.fromStrict ByteString.toStrict
259 typeInformation
278 typeInformation
260
279
261
280
281+-- | Corresponds to @uuid@
262instance DBType UUID where
282instance DBType UUID where
263 typeInformation = TypeInformation
283 typeInformation = TypeInformation
264 { encode = Opaleye.ConstExpr . Opaleye.StringLit . UUID.toString
284 { encode = Opaleye.ConstExpr . Opaleye.StringLit . UUID.toString
@@ -268,6 +288,7 @@ instance DBType UUID where
268 }
288 }
269
289
270
290
291+-- | Corresponds to @jsonb@
271instance DBType Value where
292instance DBType Value where
272 typeInformation = TypeInformation
293 typeInformation = TypeInformation
273 { encode =
294 { encode =