1{-# language FlexibleInstances #-}
2{-# language MultiParamTypeClasses #-}
3{-# language StandaloneKindSignatures #-}
4{-# language TypeApplications #-}
5{-# language UndecidableInstances #-}
6{-# language UndecidableInstances #-}
9 ( DBType (typeInformation)
14import Data.Aeson ( Value )
15import qualified Data.Aeson as Aeson
18import Data.Int ( Int16, Int32, Int64 )
19import Data.List.NonEmpty ( NonEmpty )
20import Data.Kind ( Constraint, Type )
24import Data.ByteString ( ByteString )
25import qualified Data.ByteString.Lazy as Lazy ( ByteString )
26import qualified Data.ByteString.Lazy as ByteString ( fromStrict, toStrict )
29import Data.CaseInsensitive ( CI )
30import qualified Data.CaseInsensitive as CI
33import qualified Hasql.Decoders as Hasql
36import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
37import qualified Opaleye.Internal.HaskellDB.Sql.Default as Opaleye ( quote )
40import Rel8.Opaque ( Opaque )
41import Rel8.Schema.Nullability
43 , HasNullability, nullabilization
46import Rel8.Type.Array ( listTypeInformation, nonEmptyTypeInformation )
47import Rel8.Type.Information ( TypeInformation(..), mapTypeInformation )
50import Data.Scientific ( Scientific )
53import Data.Text ( Text )
54import qualified Data.Text as Text
55import qualified Data.Text.Lazy as Lazy ( Text, unpack )
56import qualified Data.Text.Lazy as Text ( fromStrict, toStrict )
57import qualified Data.Text.Lazy.Encoding as Lazy ( decodeUtf8 )
60import Data.Time.Calendar ( Day )
61import Data.Time.Clock ( DiffTime, NominalDiffTime, UTCTime )
62import Data.Time.LocalTime ( LocalTime, TimeOfDay )
63import Data.Time.Format ( formatTime, defaultTimeLocale )
66import Data.UUID ( UUID )
67import qualified Data.UUID as UUID
70-- | Haskell types that can be represented as expressions in a database. There
71-- should be an instance of @DBType@ for all column types in your database
72-- schema (e.g., @int@, @timestamptz@, etc).
74-- Rel8 comes with stock instances for most default types in PostgreSQL, so you
75-- should only need to derive instances of this class for custom database
76-- types, such as types defined in PostgreSQL extensions, or custom domain
79-- [ Creating @DBType@s using @newtype@ ]
81-- Generalized newtype deriving can be used when you want use a @newtype@
82-- around a database type for clarity and accuracy in your Haskell code. A
83-- common example is to @newtype@ row id types:
85-- >>> newtype UserId = UserId { toInt32 :: Int32 } deriving newtype (DBType)
87-- You can now write queries using @UserId@ instead of @Int32@, which may help
88-- avoid making bad joins. However, when SQL is generated, it will be as if you
89-- just used integers (the type distinction does not impact query generation).
90type DBType :: Type -> Constraint
91class NotNull a => DBType a where
92 typeInformation :: TypeInformation a
95-- | Corresponds to @bool@
96instance DBType Bool where
97 typeInformation = TypeInformation
98 { encode = Opaleye.ConstExpr . Opaleye.BoolLit
105-- | Corresponds to @char@
106instance DBType Char where
107 typeInformation = TypeInformation
108 { encode = Opaleye.ConstExpr . Opaleye.StringLit . pure
109 , decode = Hasql.char
115-- | Corresponds to @int2@
116instance DBType Int16 where
117 typeInformation = TypeInformation
118 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
119 , decode = Hasql.int2
125-- | Corresponds to @int4@
126instance DBType Int32 where
127 typeInformation = TypeInformation
128 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
129 , decode = Hasql.int4
135-- | Corresponds to @int8@
136instance DBType Int64 where
137 typeInformation = TypeInformation
138 { encode = Opaleye.ConstExpr . Opaleye.IntegerLit . toInteger
139 , decode = Hasql.int8
145-- | Corresponds to @float4@
146instance DBType Float where
147 typeInformation = TypeInformation
148 { encode = Opaleye.ConstExpr . Opaleye.NumericLit . realToFrac
149 , decode = Hasql.float4
150 , typeName = "float4"
155-- | Corresponds to @float8@
156instance DBType Double where
157 typeInformation = TypeInformation
158 { encode = Opaleye.ConstExpr . Opaleye.NumericLit . realToFrac
159 , decode = Hasql.float8
160 , typeName = "float8"
165-- | Corresponds to @numeric@
166instance DBType Scientific where
167 typeInformation = TypeInformation
168 { encode = Opaleye.ConstExpr . Opaleye.NumericLit
169 , decode = Hasql.numeric
170 , typeName = "numeric"
175-- | Corresponds to @timestamptz@
176instance DBType UTCTime where
177 typeInformation = TypeInformation
179 Opaleye.ConstExpr . Opaleye.OtherLit .
180 formatTime defaultTimeLocale "'%FT%T%QZ'"
181 , decode = Hasql.timestamptz
182 , typeName = "timestamptz"
187-- | Corresponds to @date@
188instance DBType Day where
189 typeInformation = TypeInformation
191 Opaleye.ConstExpr . Opaleye.OtherLit .
192 formatTime defaultTimeLocale "'%F'"
193 , decode = Hasql.date
199-- | Corresponds to @timestamp@
200instance DBType LocalTime where
201 typeInformation = TypeInformation
203 Opaleye.ConstExpr . Opaleye.OtherLit .
204 formatTime defaultTimeLocale "'%FT%T%Q'"
205 , decode = Hasql.timestamp
206 , typeName = "timestamp"
211-- | Corresponds to @time@
212instance DBType TimeOfDay where
213 typeInformation = TypeInformation
215 Opaleye.ConstExpr . Opaleye.OtherLit .
216 formatTime defaultTimeLocale "'%T%Q'"
217 , decode = Hasql.time
223-- | Corresponds to @interval@
224instance DBType DiffTime where
225 typeInformation = TypeInformation
227 Opaleye.ConstExpr . Opaleye.OtherLit .
228 formatTime defaultTimeLocale "'%-6Es'"
229 , decode = Hasql.interval
230 , typeName = "interval"
235instance DBType NominalDiffTime where
237 mapTypeInformation @DiffTime realToFrac realToFrac typeInformation
240-- | Corresponds to @text@
241instance DBType Text where
242 typeInformation = TypeInformation
243 { encode = Opaleye.ConstExpr . Opaleye.StringLit . Text.unpack
244 , decode = Hasql.text
250-- | Corresponds to @text@
251instance DBType Lazy.Text where
253 mapTypeInformation Text.fromStrict Text.toStrict typeInformation
256-- | Corresponds to @citext@
257instance DBType (CI Text) where
258 typeInformation = mapTypeInformation CI.mk CI.original typeInformation
259 { typeName = "citext"
263-- | Corresponds to @citext@
264instance DBType (CI Lazy.Text) where
265 typeInformation = mapTypeInformation CI.mk CI.original typeInformation
266 { typeName = "citext"
270-- | Corresponds to @bytea@
271instance DBType ByteString where
272 typeInformation = TypeInformation
273 { encode = Opaleye.ConstExpr . Opaleye.ByteStringLit
274 , decode = Hasql.bytea
280-- | Corresponds to @bytea@
281instance DBType Lazy.ByteString where
283 mapTypeInformation ByteString.fromStrict ByteString.toStrict
287-- | Corresponds to @uuid@
288instance DBType UUID where
289 typeInformation = TypeInformation
290 { encode = Opaleye.ConstExpr . Opaleye.StringLit . UUID.toString
291 , decode = Hasql.uuid
297-- | Corresponds to @jsonb@
298instance DBType Value where
299 typeInformation = TypeInformation
301 Opaleye.ConstExpr . Opaleye.OtherLit .
303 Lazy.unpack . Lazy.decodeUtf8 . Aeson.encode
304 , decode = Hasql.jsonb
310instance Sql DBType a => DBType [a] where
311 typeInformation = listTypeInformation nullabilization typeInformation
314instance Sql DBType a => DBType (NonEmpty a) where
315 typeInformation = nonEmptyTypeInformation nullabilization typeInformation
318instance {-# OVERLAPPING #-} DBType Opaque where
319 typeInformation = error "opaque"
322instance {-# INCOHERENT #-} (HasNullability a, DBType (Unnullify a)) =>
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