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
77instance DBType Char where
78 typeInformation = TypeInformation
79 { encode = Opaleye.ConstExpr . Opaleye.StringLit . pure
85instance DBType Int16 where
86 typeInformation = TypeInformation
87 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
93instance DBType Int32 where
94 typeInformation = TypeInformation
95 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
101instance DBType Int64 where
102 typeInformation = TypeInformation
103 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
104 , decode = Hasql.int8
109instance DBType Float where
110 typeInformation = TypeInformation
111 { encode = Opaleye.ConstExpr . Opaleye.NumericLit . realToFrac
112 , decode = Hasql.float4
113 , typeName = "float4"
117instance DBType Double where
118 typeInformation = TypeInformation
119 { encode = Opaleye.ConstExpr . Opaleye.NumericLit . realToFrac
120 , decode = Hasql.float8
121 , typeName = "float8"
125instance DBType Scientific where
126 typeInformation = TypeInformation
127 { encode = Opaleye.ConstExpr . Opaleye.NumericLit
128 , decode = Hasql.numeric
129 , typeName = "numeric"
133instance DBType UTCTime where
134 typeInformation = TypeInformation
136 Opaleye.ConstExpr . Opaleye.OtherLit .
137 formatTime defaultTimeLocale "'%FT%T%QZ'"
138 , decode = Hasql.timestamptz
139 , typeName = "timestamptz"
143instance DBType Day where
144 typeInformation = TypeInformation
146 Opaleye.ConstExpr . Opaleye.OtherLit .
147 formatTime defaultTimeLocale "'%F'"
148 , decode = Hasql.date
153instance DBType LocalTime where
154 typeInformation = TypeInformation
156 Opaleye.ConstExpr . Opaleye.OtherLit .
157 formatTime defaultTimeLocale "'%FT%T%Q'"
158 , decode = Hasql.timestamp
159 , typeName = "timestamp"
163instance DBType TimeOfDay where
164 typeInformation = TypeInformation
166 Opaleye.ConstExpr . Opaleye.OtherLit .
167 formatTime defaultTimeLocale "'%T%Q'"
168 , decode = Hasql.time
173instance DBType DiffTime where
174 typeInformation = TypeInformation
176 Opaleye.ConstExpr . Opaleye.OtherLit .
177 formatTime defaultTimeLocale "'%-6Es'"
178 , decode = Hasql.interval
179 , typeName = "interval"
183instance DBType NominalDiffTime where
185 mapTypeInformation @DiffTime realToFrac realToFrac typeInformation
188instance DBType Text where
189 typeInformation = TypeInformation
190 { encode = Opaleye.ConstExpr . Opaleye.StringLit . Text.unpack
191 , decode = Hasql.text
196instance DBType Lazy.Text where
198 mapTypeInformation Text.fromStrict Text.toStrict typeInformation
201instance DBType (CI Text) where
202 typeInformation = mapTypeInformation CI.mk CI.original typeInformation
203 { typeName = "citext"
207instance DBType (CI Lazy.Text) where
208 typeInformation = mapTypeInformation CI.mk CI.original typeInformation
209 { typeName = "citext"
213instance DBType ByteString where
214 typeInformation = TypeInformation
215 { encode = Opaleye.ConstExpr . Opaleye.ByteStringLit
216 , decode = Hasql.bytea
221instance DBType Lazy.ByteString where
223 mapTypeInformation ByteString.fromStrict ByteString.toStrict
227instance DBType UUID where
228 typeInformation = TypeInformation
229 { encode = Opaleye.ConstExpr . Opaleye.StringLit . UUID.toString
230 , decode = Hasql.uuid
235instance DBType Value where
236 typeInformation = TypeInformation
238 Opaleye.ConstExpr . Opaleye.OtherLit .
240 Lazy.unpack . Lazy.decodeUtf8 . Aeson.encode
241 , decode = Hasql.jsonb
246instance (DBType db, Nullabilizes db a) => DBType [a] where
247 typeInformation = listTypeInformation nullabilization typeInformation
250instance (DBType db, Nullabilizes db a) => DBType (NonEmpty a) where
251 typeInformation = nonEmptyTypeInformation nullabilization typeInformation
254instance {-# OVERLAPPING #-} DBType Opaque where
255 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