on master help sign in
Add documentation to DBType instances
commit03d0f406a01492fb47e10153e4a09e8ef27a0831 authorOllie Charles <ollie@ocharles.org.uk> dateTue 23 Mar 2021, 16:15 (6 years ago) parentc118acc
1 file changed+26−5
src/Rel8/Type.hs+26−5
src/Rel8/Type.hs+26−5
@@ -61,11 +61,6 @@ import Data.UUID ( UUID )
6161import qualified Data.UUID as UUID
6262
6363
64−type DBType :: Type -> Constraint
65−class DBType a where
66− typeInformation :: TypeInformation a
67−
68−
6964-- | Haskell types that can be represented as expressions in a database. There
7065-- should be an instance of @DBType@ for all column types in your database
7166-- schema (e.g., @int@, @timestamptz@, etc).
@@ -86,6 +81,12 @@ class DBType a where
8681-- You can now write queries using @UserId@ instead of @Int32@, which may help
8782-- avoid making bad joins. However, when SQL is generated, it will be as if you
8883-- 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@
8990instance DBType Bool where
9091 typeInformation = TypeInformation
9192 { encode = Opaleye.ConstExpr . Opaleye.BoolLit
@@ -95,6 +96,7 @@ instance DBType Bool where
9596 }
9697
9798
99+-- | Corresponds to @char@
98100instance DBType Char where
99101 typeInformation = TypeInformation
100102 { encode = Opaleye.ConstExpr . Opaleye.StringLit . pure
@@ -104,6 +106,7 @@ instance DBType Char where
104106 }
105107
106108
109+-- | Corresponds to @int2@
107110instance DBType Int16 where
108111 typeInformation = TypeInformation
109112 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
@@ -113,6 +116,7 @@ instance DBType Int16 where
113116 }
114117
115118
119+-- | Corresponds to @int4@
116120instance DBType Int32 where
117121 typeInformation = TypeInformation
118122 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
@@ -122,6 +126,7 @@ instance DBType Int32 where
122126 }
123127
124128
129+-- | Corresponds to @int8@
125130instance DBType Int64 where
126131 typeInformation = TypeInformation
127132 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
@@ -131,6 +136,7 @@ instance DBType Int64 where
131136 }
132137
133138
139+-- | Corresponds to @float4@
134140instance DBType Float where
135141 typeInformation = TypeInformation
136142 { encode = Opaleye.ConstExpr . Opaleye.NumericLit . realToFrac
@@ -140,6 +146,7 @@ instance DBType Float where
140146 }
141147
142148
149+-- | Corresponds to @float8@
143150instance DBType Double where
144151 typeInformation = TypeInformation
145152 { encode = Opaleye.ConstExpr . Opaleye.NumericLit . realToFrac
@@ -149,6 +156,7 @@ instance DBType Double where
149156 }
150157
151158
159+-- | Corresponds to @numeric@
152160instance DBType Scientific where
153161 typeInformation = TypeInformation
154162 { encode = Opaleye.ConstExpr . Opaleye.NumericLit
@@ -158,6 +166,7 @@ instance DBType Scientific where
158166 }
159167
160168
169+-- | Corresponds to @timestamptz@
161170instance DBType UTCTime where
162171 typeInformation = TypeInformation
163172 { encode =
@@ -169,6 +178,7 @@ instance DBType UTCTime where
169178 }
170179
171180
181+-- | Corresponds to @date@
172182instance DBType Day where
173183 typeInformation = TypeInformation
174184 { encode =
@@ -180,6 +190,7 @@ instance DBType Day where
180190 }
181191
182192
193+-- | Corresponds to @timestamp@
183194instance DBType LocalTime where
184195 typeInformation = TypeInformation
185196 { encode =
@@ -191,6 +202,7 @@ instance DBType LocalTime where
191202 }
192203
193204
205+-- | Corresponds to @time@
194206instance DBType TimeOfDay where
195207 typeInformation = TypeInformation
196208 { encode =
@@ -202,6 +214,7 @@ instance DBType TimeOfDay where
202214 }
203215
204216
217+-- | Corresponds to @interval@
205218instance DBType DiffTime where
206219 typeInformation = TypeInformation
207220 { encode =
@@ -218,6 +231,7 @@ instance DBType NominalDiffTime where
218231 mapTypeInformation @DiffTime realToFrac realToFrac typeInformation
219232
220233
234+-- | Corresponds to @text@
221235instance DBType Text where
222236 typeInformation = TypeInformation
223237 { encode = Opaleye.ConstExpr . Opaleye.StringLit . Text.unpack
@@ -227,23 +241,27 @@ instance DBType Text where
227241 }
228242
229243
244+-- | Corresponds to @text@
230245instance DBType Lazy.Text where
231246 typeInformation =
232247 mapTypeInformation Text.fromStrict Text.toStrict typeInformation
233248
234249
250+-- | Corresponds to @citext@
235251instance DBType (CI Text) where
236252 typeInformation = mapTypeInformation CI.mk CI.original typeInformation
237253 { typeName = "citext"
238254 }
239255
240256
257+-- | Corresponds to @citext@
241258instance DBType (CI Lazy.Text) where
242259 typeInformation = mapTypeInformation CI.mk CI.original typeInformation
243260 { typeName = "citext"
244261 }
245262
246263
264+-- | Corresponds to @bytea@
247265instance DBType ByteString where
248266 typeInformation = TypeInformation
249267 { encode = Opaleye.ConstExpr . Opaleye.ByteStringLit
@@ -253,12 +271,14 @@ instance DBType ByteString where
253271 }
254272
255273
274+-- | Corresponds to @bytea@
256275instance DBType Lazy.ByteString where
257276 typeInformation =
258277 mapTypeInformation ByteString.fromStrict ByteString.toStrict
259278 typeInformation
260279
261280
281+-- | Corresponds to @uuid@
262282instance DBType UUID where
263283 typeInformation = TypeInformation
264284 { encode = Opaleye.ConstExpr . Opaleye.StringLit . UUID.toString
@@ -268,6 +288,7 @@ instance DBType UUID where
268288 }
269289
270290
291+-- | Corresponds to @jsonb@
271292instance DBType Value where
272293 typeInformation = TypeInformation
273294 { encode =