1{-# language FlexibleInstances #-}
2{-# language StandaloneKindSignatures #-}
3{-# language TypeApplications #-}
4{-# language UndecidableInstances #-}
7 ( DBType (typeInformation)
12import Data.Aeson ( Value )
13import qualified Data.Aeson as Aeson
16import Data.Int ( Int16, Int32, Int64 )
17import Data.List.NonEmpty ( NonEmpty )
18import Data.Kind ( Constraint, Type )
22import Data.ByteString ( ByteString )
23import qualified Data.ByteString.Lazy as Lazy ( ByteString )
24import qualified Data.ByteString.Lazy as ByteString ( fromStrict, toStrict )
27import Data.CaseInsensitive ( CI )
28import qualified Data.CaseInsensitive as CI
31import qualified Hasql.Decoders as Hasql
34import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
35import qualified Opaleye.Internal.HaskellDB.Sql.Default as Opaleye ( quote )
38import Rel8.Opaque ( Opaque )
39import Rel8.Schema.Nullability ( Nullabilizes, nullabilization )
40import Rel8.Type.Array ( listTypeInformation, nonEmptyTypeInformation )
41import Rel8.Type.Information ( TypeInformation(..), mapTypeInformation )
44import Data.Scientific ( Scientific )
47import Data.Text ( Text )
48import qualified Data.Text as Text
49import qualified Data.Text.Lazy as Lazy ( Text, unpack )
50import qualified Data.Text.Lazy as Text ( fromStrict, toStrict )
51import qualified Data.Text.Lazy.Encoding as Lazy ( decodeUtf8 )
54import Data.Time.Calendar ( Day )
55import Data.Time.Clock ( DiffTime, NominalDiffTime, UTCTime )
56import Data.Time.LocalTime ( LocalTime, TimeOfDay )
57import Data.Time.Format ( formatTime, defaultTimeLocale )
60import Data.UUID ( UUID )
61import qualified Data.UUID as UUID
64type DBType :: Type -> Constraint
66 typeInformation :: TypeInformation a
69instance DBType Bool where
70 typeInformation = TypeInformation
71 { encode = Opaleye.ConstExpr . Opaleye.BoolLit
78instance DBType Char where
79 typeInformation = TypeInformation
80 { encode = Opaleye.ConstExpr . Opaleye.StringLit . pure
87instance DBType Int16 where
88 typeInformation = TypeInformation
89 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
96instance DBType Int32 where
97 typeInformation = TypeInformation
98 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
105instance DBType Int64 where
106 typeInformation = TypeInformation
107 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
108 , decode = Hasql.int8
114instance DBType Float where
115 typeInformation = TypeInformation
116 { encode = Opaleye.ConstExpr . Opaleye.NumericLit . realToFrac
117 , decode = Hasql.float4
118 , typeName = "float4"
123instance DBType Double where
124 typeInformation = TypeInformation
125 { encode = Opaleye.ConstExpr . Opaleye.NumericLit . realToFrac
126 , decode = Hasql.float8
127 , typeName = "float8"
132instance DBType Scientific where
133 typeInformation = TypeInformation
134 { encode = Opaleye.ConstExpr . Opaleye.NumericLit
135 , decode = Hasql.numeric
136 , typeName = "numeric"
141instance DBType UTCTime where
142 typeInformation = TypeInformation
144 Opaleye.ConstExpr . Opaleye.OtherLit .
145 formatTime defaultTimeLocale "'%FT%T%QZ'"
146 , decode = Hasql.timestamptz
147 , typeName = "timestamptz"
152instance DBType Day where
153 typeInformation = TypeInformation
155 Opaleye.ConstExpr . Opaleye.OtherLit .
156 formatTime defaultTimeLocale "'%F'"
157 , decode = Hasql.date
163instance DBType LocalTime where
164 typeInformation = TypeInformation
166 Opaleye.ConstExpr . Opaleye.OtherLit .
167 formatTime defaultTimeLocale "'%FT%T%Q'"
168 , decode = Hasql.timestamp
169 , typeName = "timestamp"
174instance DBType TimeOfDay where
175 typeInformation = TypeInformation
177 Opaleye.ConstExpr . Opaleye.OtherLit .
178 formatTime defaultTimeLocale "'%T%Q'"
179 , decode = Hasql.time
185instance DBType DiffTime where
186 typeInformation = TypeInformation
188 Opaleye.ConstExpr . Opaleye.OtherLit .
189 formatTime defaultTimeLocale "'%-6Es'"
190 , decode = Hasql.interval
191 , typeName = "interval"
196instance DBType NominalDiffTime where
198 mapTypeInformation @DiffTime realToFrac realToFrac typeInformation
201instance DBType Text where
202 typeInformation = TypeInformation
203 { encode = Opaleye.ConstExpr . Opaleye.StringLit . Text.unpack
204 , decode = Hasql.text
210instance DBType Lazy.Text where
212 mapTypeInformation Text.fromStrict Text.toStrict typeInformation
215instance DBType (CI Text) where
216 typeInformation = mapTypeInformation CI.mk CI.original typeInformation
217 { typeName = "citext"
221instance DBType (CI Lazy.Text) where
222 typeInformation = mapTypeInformation CI.mk CI.original typeInformation
223 { typeName = "citext"
227instance DBType ByteString where
228 typeInformation = TypeInformation
229 { encode = Opaleye.ConstExpr . Opaleye.ByteStringLit
230 , decode = Hasql.bytea
236instance DBType Lazy.ByteString where
238 mapTypeInformation ByteString.fromStrict ByteString.toStrict
242instance DBType UUID where
243 typeInformation = TypeInformation
244 { encode = Opaleye.ConstExpr . Opaleye.StringLit . UUID.toString
245 , decode = Hasql.uuid
251instance DBType Value where
252 typeInformation = TypeInformation
254 Opaleye.ConstExpr . Opaleye.OtherLit .
256 Lazy.unpack . Lazy.decodeUtf8 . Aeson.encode
257 , decode = Hasql.jsonb
263instance (DBType db, Nullabilizes db a) => DBType [a] where
264 typeInformation = listTypeInformation nullabilization typeInformation
267instance (DBType db, Nullabilizes db a) => DBType (NonEmpty a) where
268 typeInformation = nonEmptyTypeInformation nullabilization typeInformation
271instance {-# OVERLAPPING #-} DBType Opaque where
272 typeInformation = error "opaque"
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