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 ( Sql, 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
64-- | Haskell types that can be represented as expressions in a database. There
65-- should be an instance of @DBType@ for all column types in your database
66-- schema (e.g., @int@, @timestamptz@, etc).
68-- Rel8 comes with stock instances for most default types in PostgreSQL, so you
69-- should only need to derive instances of this class for custom database
70-- types, such as types defined in PostgreSQL extensions, or custom domain
73-- [ Creating @DBType@s using @newtype@ ]
75-- Generalized newtype deriving can be used when you want use a @newtype@
76-- around a database type for clarity and accuracy in your Haskell code. A
77-- common example is to @newtype@ row id types:
79-- >>> newtype UserId = UserId { toInt32 :: Int32 } deriving newtype (DBType)
81-- You can now write queries using @UserId@ instead of @Int32@, which may help
82-- avoid making bad joins. However, when SQL is generated, it will be as if you
83-- just used integers (the type distinction does not impact query generation).
84type DBType :: Type -> Constraint
86 typeInformation :: TypeInformation a
89-- | Corresponds to @bool@
90instance DBType Bool where
91 typeInformation = TypeInformation
92 { encode = Opaleye.ConstExpr . Opaleye.BoolLit
99-- | Corresponds to @char@
100instance DBType Char where
101 typeInformation = TypeInformation
102 { encode = Opaleye.ConstExpr . Opaleye.StringLit . pure
103 , decode = Hasql.char
109-- | Corresponds to @int2@
110instance DBType Int16 where
111 typeInformation = TypeInformation
112 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
113 , decode = Hasql.int2
119-- | Corresponds to @int4@
120instance DBType Int32 where
121 typeInformation = TypeInformation
122 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
123 , decode = Hasql.int4
129-- | Corresponds to @int8@
130instance DBType Int64 where
131 typeInformation = TypeInformation
132 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
133 , decode = Hasql.int8
139-- | Corresponds to @float4@
140instance DBType Float where
141 typeInformation = TypeInformation
142 { encode = Opaleye.ConstExpr . Opaleye.NumericLit . realToFrac
143 , decode = Hasql.float4
144 , typeName = "float4"
149-- | Corresponds to @float8@
150instance DBType Double where
151 typeInformation = TypeInformation
152 { encode = Opaleye.ConstExpr . Opaleye.NumericLit . realToFrac
153 , decode = Hasql.float8
154 , typeName = "float8"
159-- | Corresponds to @numeric@
160instance DBType Scientific where
161 typeInformation = TypeInformation
162 { encode = Opaleye.ConstExpr . Opaleye.NumericLit
163 , decode = Hasql.numeric
164 , typeName = "numeric"
169-- | Corresponds to @timestamptz@
170instance DBType UTCTime where
171 typeInformation = TypeInformation
173 Opaleye.ConstExpr . Opaleye.OtherLit .
174 formatTime defaultTimeLocale "'%FT%T%QZ'"
175 , decode = Hasql.timestamptz
176 , typeName = "timestamptz"
181-- | Corresponds to @date@
182instance DBType Day where
183 typeInformation = TypeInformation
185 Opaleye.ConstExpr . Opaleye.OtherLit .
186 formatTime defaultTimeLocale "'%F'"
187 , decode = Hasql.date
193-- | Corresponds to @timestamp@
194instance DBType LocalTime where
195 typeInformation = TypeInformation
197 Opaleye.ConstExpr . Opaleye.OtherLit .
198 formatTime defaultTimeLocale "'%FT%T%Q'"
199 , decode = Hasql.timestamp
200 , typeName = "timestamp"
205-- | Corresponds to @time@
206instance DBType TimeOfDay where
207 typeInformation = TypeInformation
209 Opaleye.ConstExpr . Opaleye.OtherLit .
210 formatTime defaultTimeLocale "'%T%Q'"
211 , decode = Hasql.time
217-- | Corresponds to @interval@
218instance DBType DiffTime where
219 typeInformation = TypeInformation
221 Opaleye.ConstExpr . Opaleye.OtherLit .
222 formatTime defaultTimeLocale "'%-6Es'"
223 , decode = Hasql.interval
224 , typeName = "interval"
229instance DBType NominalDiffTime where
231 mapTypeInformation @DiffTime realToFrac realToFrac typeInformation
234-- | Corresponds to @text@
235instance DBType Text where
236 typeInformation = TypeInformation
237 { encode = Opaleye.ConstExpr . Opaleye.StringLit . Text.unpack
238 , decode = Hasql.text
244-- | Corresponds to @text@
245instance DBType Lazy.Text where
247 mapTypeInformation Text.fromStrict Text.toStrict typeInformation
250-- | Corresponds to @citext@
251instance DBType (CI Text) where
252 typeInformation = mapTypeInformation CI.mk CI.original typeInformation
253 { typeName = "citext"
257-- | Corresponds to @citext@
258instance DBType (CI Lazy.Text) where
259 typeInformation = mapTypeInformation CI.mk CI.original typeInformation
260 { typeName = "citext"
264-- | Corresponds to @bytea@
265instance DBType ByteString where
266 typeInformation = TypeInformation
267 { encode = Opaleye.ConstExpr . Opaleye.ByteStringLit
268 , decode = Hasql.bytea
274-- | Corresponds to @bytea@
275instance DBType Lazy.ByteString where
277 mapTypeInformation ByteString.fromStrict ByteString.toStrict
281-- | Corresponds to @uuid@
282instance DBType UUID where
283 typeInformation = TypeInformation
284 { encode = Opaleye.ConstExpr . Opaleye.StringLit . UUID.toString
285 , decode = Hasql.uuid
291-- | Corresponds to @jsonb@
292instance DBType Value where
293 typeInformation = TypeInformation
295 Opaleye.ConstExpr . Opaleye.OtherLit .
297 Lazy.unpack . Lazy.decodeUtf8 . Aeson.encode
298 , decode = Hasql.jsonb
304instance Sql DBType a => DBType [a] where
305 typeInformation = listTypeInformation nullabilization typeInformation
308instance Sql DBType a => DBType (NonEmpty a) where
309 typeInformation = nonEmptyTypeInformation nullabilization typeInformation
312instance {-# OVERLAPPING #-} DBType Opaque where
313 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