1{-# language FlexibleInstances #-}
2{-# language MultiParamTypeClasses #-}
3{-# language StandaloneKindSignatures #-}
4{-# language TypeApplications #-}
5{-# language UndecidableInstances #-}
6{-# language UndecidableInstances #-}
9 ( DBType (typeInformation)
14import Data.Aeson ( Value )
15import qualified Data.Aeson as Aeson
18import Data.Int ( Int16, Int32, Int64 )
19import Data.List.NonEmpty ( NonEmpty )
20import Data.Kind ( Constraint, Type )
24import Data.ByteString ( ByteString )
25import qualified Data.ByteString.Lazy as Lazy ( ByteString )
26import qualified Data.ByteString.Lazy as ByteString ( fromStrict, toStrict )
29import Data.CaseInsensitive ( CI )
30import qualified Data.CaseInsensitive as CI
33import qualified Hasql.Decoders as Hasql
36import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
37import qualified Opaleye.Internal.HaskellDB.Sql.Default as Opaleye ( quote )
40import Rel8.Opaque ( Opaque )
41import Rel8.Schema.Nullability
43 , HasNullability, nullabilization
46import Rel8.Type.Array ( listTypeInformation, nonEmptyTypeInformation )
47import Rel8.Type.Information ( TypeInformation(..), mapTypeInformation )
50import Data.Scientific ( Scientific )
53import Data.Text ( Text )
54import qualified Data.Text as Text
55import qualified Data.Text.Lazy as Lazy ( Text, unpack )
56import qualified Data.Text.Lazy as Text ( fromStrict, toStrict )
57import qualified Data.Text.Lazy.Encoding as Lazy ( decodeUtf8 )
60import Data.Time.Calendar ( Day )
61import Data.Time.Clock ( UTCTime )
62import Data.Time.LocalTime
63 ( CalendarDiffTime( CalendarDiffTime )
67import Data.Time.Format ( formatTime, defaultTimeLocale )
70import Data.UUID ( UUID )
71import qualified Data.UUID as UUID
74-- | Haskell types that can be represented as expressions in a database. There
75-- should be an instance of @DBType@ for all column types in your database
76-- schema (e.g., @int@, @timestamptz@, etc).
78-- Rel8 comes with stock instances for most default types in PostgreSQL, so you
79-- should only need to derive instances of this class for custom database
80-- types, such as types defined in PostgreSQL extensions, or custom domain
83-- [ Creating @DBType@s using @newtype@ ]
85-- Generalized newtype deriving can be used when you want use a @newtype@
86-- around a database type for clarity and accuracy in your Haskell code. A
87-- common example is to @newtype@ row id types:
89-- >>> newtype UserId = UserId { toInt32 :: Int32 } deriving newtype (DBType)
91-- You can now write queries using @UserId@ instead of @Int32@, which may help
92-- avoid making bad joins. However, when SQL is generated, it will be as if you
93-- just used integers (the type distinction does not impact query generation).
94type DBType :: Type -> Constraint
95class NotNull a => DBType a where
96 typeInformation :: TypeInformation a
99-- | Corresponds to @bool@
100instance DBType Bool where
101 typeInformation = TypeInformation
102 { encode = Opaleye.ConstExpr . Opaleye.BoolLit
103 , decode = Hasql.bool
109-- | Corresponds to @char@
110instance DBType Char where
111 typeInformation = TypeInformation
112 { encode = Opaleye.ConstExpr . Opaleye.StringLit . pure
113 , decode = Hasql.char
119-- | Corresponds to @int2@
120instance DBType Int16 where
121 typeInformation = TypeInformation
122 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
123 , decode = Hasql.int2
129-- | Corresponds to @int4@
130instance DBType Int32 where
131 typeInformation = TypeInformation
132 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
133 , decode = Hasql.int4
139-- | Corresponds to @int8@
140instance DBType Int64 where
141 typeInformation = TypeInformation
142 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
143 , decode = Hasql.int8
149-- | Corresponds to @float4@
150instance DBType Float where
151 typeInformation = TypeInformation
152 { encode = Opaleye.ConstExpr . Opaleye.NumericLit . realToFrac
153 , decode = Hasql.float4
154 , typeName = "float4"
159-- | Corresponds to @float8@
160instance DBType Double where
161 typeInformation = TypeInformation
162 { encode = Opaleye.ConstExpr . Opaleye.NumericLit . realToFrac
163 , decode = Hasql.float8
164 , typeName = "float8"
169-- | Corresponds to @numeric@
170instance DBType Scientific where
171 typeInformation = TypeInformation
172 { encode = Opaleye.ConstExpr . Opaleye.NumericLit
173 , decode = Hasql.numeric
174 , typeName = "numeric"
179-- | Corresponds to @timestamptz@
180instance DBType UTCTime where
181 typeInformation = TypeInformation
183 Opaleye.ConstExpr . Opaleye.OtherLit .
184 formatTime defaultTimeLocale "'%FT%T%QZ'"
185 , decode = Hasql.timestamptz
186 , typeName = "timestamptz"
191-- | Corresponds to @date@
192instance DBType Day where
193 typeInformation = TypeInformation
195 Opaleye.ConstExpr . Opaleye.OtherLit .
196 formatTime defaultTimeLocale "'%F'"
197 , decode = Hasql.date
203-- | Corresponds to @timestamp@
204instance DBType LocalTime where
205 typeInformation = TypeInformation
207 Opaleye.ConstExpr . Opaleye.OtherLit .
208 formatTime defaultTimeLocale "'%FT%T%Q'"
209 , decode = Hasql.timestamp
210 , typeName = "timestamp"
215-- | Corresponds to @time@
216instance DBType TimeOfDay where
217 typeInformation = TypeInformation
219 Opaleye.ConstExpr . Opaleye.OtherLit .
220 formatTime defaultTimeLocale "'%T%Q'"
221 , decode = Hasql.time
227-- | Corresponds to @interval@
228instance DBType CalendarDiffTime where
229 typeInformation = TypeInformation
231 Opaleye.ConstExpr . Opaleye.OtherLit .
232 formatTime defaultTimeLocale "'%bmon %0Es'"
233 , decode = CalendarDiffTime 0 . realToFrac <$> Hasql.interval
234 , typeName = "interval"
239-- | Corresponds to @text@
240instance DBType Text where
241 typeInformation = TypeInformation
242 { encode = Opaleye.ConstExpr . Opaleye.StringLit . Text.unpack
243 , decode = Hasql.text
249-- | Corresponds to @text@
250instance DBType Lazy.Text where
252 mapTypeInformation Text.fromStrict Text.toStrict typeInformation
255-- | Corresponds to @citext@
256instance DBType (CI Text) where
257 typeInformation = mapTypeInformation CI.mk CI.original typeInformation
258 { typeName = "citext"
262-- | Corresponds to @citext@
263instance DBType (CI Lazy.Text) where
264 typeInformation = mapTypeInformation CI.mk CI.original typeInformation
265 { typeName = "citext"
269-- | Corresponds to @bytea@
270instance DBType ByteString where
271 typeInformation = TypeInformation
272 { encode = Opaleye.ConstExpr . Opaleye.ByteStringLit
273 , decode = Hasql.bytea
279-- | Corresponds to @bytea@
280instance DBType Lazy.ByteString where
282 mapTypeInformation ByteString.fromStrict ByteString.toStrict
286-- | Corresponds to @uuid@
287instance DBType UUID where
288 typeInformation = TypeInformation
289 { encode = Opaleye.ConstExpr . Opaleye.StringLit . UUID.toString
290 , decode = Hasql.uuid
296-- | Corresponds to @jsonb@
297instance DBType Value where
298 typeInformation = TypeInformation
300 Opaleye.ConstExpr . Opaleye.OtherLit .
302 Lazy.unpack . Lazy.decodeUtf8 . Aeson.encode
303 , decode = Hasql.jsonb
309instance Sql DBType a => DBType [a] where
310 typeInformation = listTypeInformation nullabilization typeInformation
313instance Sql DBType a => DBType (NonEmpty a) where
314 typeInformation = nonEmptyTypeInformation nullabilization typeInformation
317instance {-# OVERLAPPING #-} DBType Opaque where
318 typeInformation = error "opaque"
321instance {-# INCOHERENT #-} (HasNullability a, DBType (Unnullify a)) =>
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list