1{-# language FlexibleInstances #-}
2{-# language MonoLocalBinds #-}
3{-# language StandaloneKindSignatures #-}
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.Schema.Null ( NotNull, Sql, nullable )
39import Rel8.Type.Array ( listTypeInformation, nonEmptyTypeInformation )
40import Rel8.Type.Information ( TypeInformation(..), mapTypeInformation )
43import Data.Scientific ( Scientific )
46import Data.Text ( Text )
47import qualified Data.Text as Text
48import qualified Data.Text.Lazy as Lazy ( Text, unpack )
49import qualified Data.Text.Lazy as Text ( fromStrict, toStrict )
50import qualified Data.Text.Lazy.Encoding as Lazy ( decodeUtf8 )
53import Data.Time.Calendar ( Day )
54import Data.Time.Clock ( UTCTime )
55import Data.Time.LocalTime
56 ( CalendarDiffTime( CalendarDiffTime )
60import Data.Time.Format ( formatTime, defaultTimeLocale )
63import Data.UUID ( UUID )
64import qualified Data.UUID as UUID
67-- | Haskell types that can be represented as expressions in a database. There
68-- should be an instance of @DBType@ for all column types in your database
69-- schema (e.g., @int@, @timestamptz@, etc).
71-- Rel8 comes with stock instances for most default types in PostgreSQL, so you
72-- should only need to derive instances of this class for custom database
73-- types, such as types defined in PostgreSQL extensions, or custom domain
75type DBType :: Type -> Constraint
76class NotNull a => DBType a where
77 typeInformation :: TypeInformation a
80-- | Corresponds to @bool@
81instance DBType Bool where
82 typeInformation = TypeInformation
83 { encode = Opaleye.ConstExpr . Opaleye.BoolLit
89-- | Corresponds to @char@
90instance DBType Char where
91 typeInformation = TypeInformation
92 { encode = Opaleye.ConstExpr . Opaleye.StringLit . pure
98-- | Corresponds to @int2@
99instance DBType Int16 where
100 typeInformation = TypeInformation
101 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
102 , decode = Hasql.int2
107-- | Corresponds to @int4@
108instance DBType Int32 where
109 typeInformation = TypeInformation
110 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
111 , decode = Hasql.int4
116-- | Corresponds to @int8@
117instance DBType Int64 where
118 typeInformation = TypeInformation
119 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
120 , decode = Hasql.int8
125-- | Corresponds to @float4@
126instance DBType Float where
127 typeInformation = TypeInformation
128 { encode = Opaleye.ConstExpr . Opaleye.NumericLit . realToFrac
129 , decode = Hasql.float4
130 , typeName = "float4"
134-- | Corresponds to @float8@
135instance DBType Double where
136 typeInformation = TypeInformation
137 { encode = Opaleye.ConstExpr . Opaleye.NumericLit . realToFrac
138 , decode = Hasql.float8
139 , typeName = "float8"
143-- | Corresponds to @numeric@
144instance DBType Scientific where
145 typeInformation = TypeInformation
146 { encode = Opaleye.ConstExpr . Opaleye.NumericLit
147 , decode = Hasql.numeric
148 , typeName = "numeric"
152-- | Corresponds to @timestamptz@
153instance DBType UTCTime where
154 typeInformation = TypeInformation
156 Opaleye.ConstExpr . Opaleye.OtherLit .
157 formatTime defaultTimeLocale "'%FT%T%QZ'"
158 , decode = Hasql.timestamptz
159 , typeName = "timestamptz"
163-- | Corresponds to @date@
164instance DBType Day where
165 typeInformation = TypeInformation
167 Opaleye.ConstExpr . Opaleye.OtherLit .
168 formatTime defaultTimeLocale "'%F'"
169 , decode = Hasql.date
174-- | Corresponds to @timestamp@
175instance DBType LocalTime where
176 typeInformation = TypeInformation
178 Opaleye.ConstExpr . Opaleye.OtherLit .
179 formatTime defaultTimeLocale "'%FT%T%Q'"
180 , decode = Hasql.timestamp
181 , typeName = "timestamp"
185-- | Corresponds to @time@
186instance DBType TimeOfDay where
187 typeInformation = TypeInformation
189 Opaleye.ConstExpr . Opaleye.OtherLit .
190 formatTime defaultTimeLocale "'%T%Q'"
191 , decode = Hasql.time
196-- | Corresponds to @interval@
197instance DBType CalendarDiffTime where
198 typeInformation = TypeInformation
200 Opaleye.ConstExpr . Opaleye.OtherLit .
201 formatTime defaultTimeLocale "'%bmon %0Es'"
202 , decode = CalendarDiffTime 0 . realToFrac <$> Hasql.interval
203 , typeName = "interval"
207-- | Corresponds to @text@
208instance DBType Text where
209 typeInformation = TypeInformation
210 { encode = Opaleye.ConstExpr . Opaleye.StringLit . Text.unpack
211 , decode = Hasql.text
216-- | Corresponds to @text@
217instance DBType Lazy.Text where
219 mapTypeInformation Text.fromStrict Text.toStrict typeInformation
222-- | Corresponds to @citext@
223instance DBType (CI Text) where
224 typeInformation = mapTypeInformation CI.mk CI.original typeInformation
225 { typeName = "citext"
229-- | Corresponds to @citext@
230instance DBType (CI Lazy.Text) where
231 typeInformation = mapTypeInformation CI.mk CI.original typeInformation
232 { typeName = "citext"
236-- | Corresponds to @bytea@
237instance DBType ByteString where
238 typeInformation = TypeInformation
239 { encode = Opaleye.ConstExpr . Opaleye.ByteStringLit
240 , decode = Hasql.bytea
245-- | Corresponds to @bytea@
246instance DBType Lazy.ByteString where
248 mapTypeInformation ByteString.fromStrict ByteString.toStrict
252-- | Corresponds to @uuid@
253instance DBType UUID where
254 typeInformation = TypeInformation
255 { encode = Opaleye.ConstExpr . Opaleye.StringLit . UUID.toString
256 , decode = Hasql.uuid
261-- | Corresponds to @jsonb@
262instance DBType Value where
263 typeInformation = TypeInformation
265 Opaleye.ConstExpr . Opaleye.OtherLit .
267 Lazy.unpack . Lazy.decodeUtf8 . Aeson.encode
268 , decode = Hasql.jsonb
273instance Sql DBType a => DBType [a] where
274 typeInformation = listTypeInformation nullable typeInformation
277instance Sql DBType a => DBType (NonEmpty a) where
278 typeInformation = nonEmptyTypeInformation nullable typeInformation
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