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.Nullability
43import Rel8.Type.Array ( listTypeInformation, nonEmptyTypeInformation )
44import Rel8.Type.Information ( TypeInformation(..), mapTypeInformation )
47import Data.Scientific ( Scientific )
50import Data.Text ( Text )
51import qualified Data.Text as Text
52import qualified Data.Text.Lazy as Lazy ( Text, unpack )
53import qualified Data.Text.Lazy as Text ( fromStrict, toStrict )
54import qualified Data.Text.Lazy.Encoding as Lazy ( decodeUtf8 )
57import Data.Time.Calendar ( Day )
58import Data.Time.Clock ( UTCTime )
59import Data.Time.LocalTime
60 ( CalendarDiffTime( CalendarDiffTime )
64import Data.Time.Format ( formatTime, defaultTimeLocale )
67import Data.UUID ( UUID )
68import qualified Data.UUID as UUID
71-- | Haskell types that can be represented as expressions in a database. There
72-- should be an instance of @DBType@ for all column types in your database
73-- schema (e.g., @int@, @timestamptz@, etc).
75-- Rel8 comes with stock instances for most default types in PostgreSQL, so you
76-- should only need to derive instances of this class for custom database
77-- types, such as types defined in PostgreSQL extensions, or custom domain
79type DBType :: Type -> Constraint
80class NotNull a => DBType a where
81 typeInformation :: TypeInformation a
84-- | Corresponds to @bool@
85instance DBType Bool where
86 typeInformation = TypeInformation
87 { encode = Opaleye.ConstExpr . Opaleye.BoolLit
93-- | Corresponds to @char@
94instance DBType Char where
95 typeInformation = TypeInformation
96 { encode = Opaleye.ConstExpr . Opaleye.StringLit . pure
102-- | Corresponds to @int2@
103instance DBType Int16 where
104 typeInformation = TypeInformation
105 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
106 , decode = Hasql.int2
111-- | Corresponds to @int4@
112instance DBType Int32 where
113 typeInformation = TypeInformation
114 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
115 , decode = Hasql.int4
120-- | Corresponds to @int8@
121instance DBType Int64 where
122 typeInformation = TypeInformation
123 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
124 , decode = Hasql.int8
129-- | Corresponds to @float4@
130instance DBType Float where
131 typeInformation = TypeInformation
132 { encode = Opaleye.ConstExpr . Opaleye.NumericLit . realToFrac
133 , decode = Hasql.float4
134 , typeName = "float4"
138-- | Corresponds to @float8@
139instance DBType Double where
140 typeInformation = TypeInformation
141 { encode = Opaleye.ConstExpr . Opaleye.NumericLit . realToFrac
142 , decode = Hasql.float8
143 , typeName = "float8"
147-- | Corresponds to @numeric@
148instance DBType Scientific where
149 typeInformation = TypeInformation
150 { encode = Opaleye.ConstExpr . Opaleye.NumericLit
151 , decode = Hasql.numeric
152 , typeName = "numeric"
156-- | Corresponds to @timestamptz@
157instance DBType UTCTime where
158 typeInformation = TypeInformation
160 Opaleye.ConstExpr . Opaleye.OtherLit .
161 formatTime defaultTimeLocale "'%FT%T%QZ'"
162 , decode = Hasql.timestamptz
163 , typeName = "timestamptz"
167-- | Corresponds to @date@
168instance DBType Day where
169 typeInformation = TypeInformation
171 Opaleye.ConstExpr . Opaleye.OtherLit .
172 formatTime defaultTimeLocale "'%F'"
173 , decode = Hasql.date
178-- | Corresponds to @timestamp@
179instance DBType LocalTime where
180 typeInformation = TypeInformation
182 Opaleye.ConstExpr . Opaleye.OtherLit .
183 formatTime defaultTimeLocale "'%FT%T%Q'"
184 , decode = Hasql.timestamp
185 , typeName = "timestamp"
189-- | Corresponds to @time@
190instance DBType TimeOfDay where
191 typeInformation = TypeInformation
193 Opaleye.ConstExpr . Opaleye.OtherLit .
194 formatTime defaultTimeLocale "'%T%Q'"
195 , decode = Hasql.time
200-- | Corresponds to @interval@
201instance DBType CalendarDiffTime where
202 typeInformation = TypeInformation
204 Opaleye.ConstExpr . Opaleye.OtherLit .
205 formatTime defaultTimeLocale "'%bmon %0Es'"
206 , decode = CalendarDiffTime 0 . realToFrac <$> Hasql.interval
207 , typeName = "interval"
211-- | Corresponds to @text@
212instance DBType Text where
213 typeInformation = TypeInformation
214 { encode = Opaleye.ConstExpr . Opaleye.StringLit . Text.unpack
215 , decode = Hasql.text
220-- | Corresponds to @text@
221instance DBType Lazy.Text where
223 mapTypeInformation Text.fromStrict Text.toStrict typeInformation
226-- | Corresponds to @citext@
227instance DBType (CI Text) where
228 typeInformation = mapTypeInformation CI.mk CI.original typeInformation
229 { typeName = "citext"
233-- | Corresponds to @citext@
234instance DBType (CI Lazy.Text) where
235 typeInformation = mapTypeInformation CI.mk CI.original typeInformation
236 { typeName = "citext"
240-- | Corresponds to @bytea@
241instance DBType ByteString where
242 typeInformation = TypeInformation
243 { encode = Opaleye.ConstExpr . Opaleye.ByteStringLit
244 , decode = Hasql.bytea
249-- | Corresponds to @bytea@
250instance DBType Lazy.ByteString where
252 mapTypeInformation ByteString.fromStrict ByteString.toStrict
256-- | Corresponds to @uuid@
257instance DBType UUID where
258 typeInformation = TypeInformation
259 { encode = Opaleye.ConstExpr . Opaleye.StringLit . UUID.toString
260 , decode = Hasql.uuid
265-- | Corresponds to @jsonb@
266instance DBType Value where
267 typeInformation = TypeInformation
269 Opaleye.ConstExpr . Opaleye.OtherLit .
271 Lazy.unpack . Lazy.decodeUtf8 . Aeson.encode
272 , decode = Hasql.jsonb
277instance Sql DBType a => DBType [a] where
278 typeInformation = listTypeInformation nullabilization typeInformation
281instance Sql DBType a => DBType (NonEmpty a) where
282 typeInformation = nonEmptyTypeInformation nullabilization 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