hpgsql 0.3.0 → 0.4.0
raw patch · 26 files changed
+3634/−2345 lines, 26 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
- Hpgsql.Encoding: instance (GHC.Internal.Generics.Generic a, Hpgsql.Encoding.EnumDecoder (GHC.Internal.Generics.Rep a)) => Hpgsql.Encoding.FromPgField (Hpgsql.Encoding.LowerCasedPgEnum a)
- Hpgsql.Encoding: instance (GHC.Internal.Generics.Generic a, Hpgsql.Encoding.EnumEncoder (GHC.Internal.Generics.Rep a)) => Hpgsql.Encoding.ToPgField (Hpgsql.Encoding.LowerCasedPgEnum a)
- Hpgsql.Encoding: instance (Hpgsql.Encoding.EnumDecoder a, Hpgsql.Encoding.EnumDecoder b) => Hpgsql.Encoding.EnumDecoder (a GHC.Internal.Generics.:+: b)
- Hpgsql.Encoding: instance (Hpgsql.Encoding.EnumEncoder a, Hpgsql.Encoding.EnumEncoder b) => Hpgsql.Encoding.EnumEncoder (a GHC.Internal.Generics.:+: b)
- Hpgsql.Encoding: instance (Hpgsql.Encoding.FromPgField a, Hpgsql.Encoding.FromPgField b) => Hpgsql.Encoding.FromPgRow (a, b)
- Hpgsql.Encoding: instance (Hpgsql.Encoding.FromPgField a, Hpgsql.Encoding.FromPgField b, Hpgsql.Encoding.FromPgField c) => Hpgsql.Encoding.FromPgRow (a, b, c)
- Hpgsql.Encoding: instance (Hpgsql.Encoding.FromPgField a, Hpgsql.Encoding.FromPgField b, Hpgsql.Encoding.FromPgField c, Hpgsql.Encoding.FromPgField d) => Hpgsql.Encoding.FromPgRow (a, b, c, d)
- Hpgsql.Encoding: instance (Hpgsql.Encoding.FromPgField a, Hpgsql.Encoding.FromPgField b, Hpgsql.Encoding.FromPgField c, Hpgsql.Encoding.FromPgField d, Hpgsql.Encoding.FromPgField e) => Hpgsql.Encoding.FromPgRow (a, b, c, d, e)
- Hpgsql.Encoding: instance (Hpgsql.Encoding.FromPgField a, Hpgsql.Encoding.FromPgField b, Hpgsql.Encoding.FromPgField c, Hpgsql.Encoding.FromPgField d, Hpgsql.Encoding.FromPgField e, Hpgsql.Encoding.FromPgField f) => Hpgsql.Encoding.FromPgRow (a, b, c, d, e, f)
- Hpgsql.Encoding: instance (Hpgsql.Encoding.FromPgField a, Hpgsql.Encoding.FromPgField b, Hpgsql.Encoding.FromPgField c, Hpgsql.Encoding.FromPgField d, Hpgsql.Encoding.FromPgField e, Hpgsql.Encoding.FromPgField f, Hpgsql.Encoding.FromPgField g) => Hpgsql.Encoding.FromPgRow (a, b, c, d, e, f, g)
- Hpgsql.Encoding: instance (Hpgsql.Encoding.FromPgField a, Hpgsql.Encoding.FromPgField b, Hpgsql.Encoding.FromPgField c, Hpgsql.Encoding.FromPgField d, Hpgsql.Encoding.FromPgField e, Hpgsql.Encoding.FromPgField f, Hpgsql.Encoding.FromPgField g, Hpgsql.Encoding.FromPgField h) => Hpgsql.Encoding.FromPgRow (a, b, c, d, e, f, g, h)
- Hpgsql.Encoding: instance (Hpgsql.Encoding.FromPgField a, Hpgsql.Encoding.FromPgField b, Hpgsql.Encoding.FromPgField c, Hpgsql.Encoding.FromPgField d, Hpgsql.Encoding.FromPgField e, Hpgsql.Encoding.FromPgField f, Hpgsql.Encoding.FromPgField g, Hpgsql.Encoding.FromPgField h, Hpgsql.Encoding.FromPgField i) => Hpgsql.Encoding.FromPgRow (a, b, c, d, e, f, g, h, i)
- Hpgsql.Encoding: instance (Hpgsql.Encoding.FromPgField a, Hpgsql.Encoding.FromPgField b, Hpgsql.Encoding.FromPgField c, Hpgsql.Encoding.FromPgField d, Hpgsql.Encoding.FromPgField e, Hpgsql.Encoding.FromPgField f, Hpgsql.Encoding.FromPgField g, Hpgsql.Encoding.FromPgField h, Hpgsql.Encoding.FromPgField i, Hpgsql.Encoding.FromPgField j) => Hpgsql.Encoding.FromPgRow (a, b, c, d, e, f, g, h, i, j)
- Hpgsql.Encoding: instance (Hpgsql.Encoding.FromPgField a, Hpgsql.Encoding.FromPgField b, Hpgsql.Encoding.FromPgField c, Hpgsql.Encoding.FromPgField d, Hpgsql.Encoding.FromPgField e, Hpgsql.Encoding.FromPgField f, Hpgsql.Encoding.FromPgField g, Hpgsql.Encoding.FromPgField h, Hpgsql.Encoding.FromPgField i, Hpgsql.Encoding.FromPgField j, Hpgsql.Encoding.FromPgField k) => Hpgsql.Encoding.FromPgRow (a, b, c, d, e, f, g, h, i, j, k)
- Hpgsql.Encoding: instance (Hpgsql.Encoding.FromPgField a, Hpgsql.Encoding.FromPgField b, Hpgsql.Encoding.FromPgField c, Hpgsql.Encoding.FromPgField d, Hpgsql.Encoding.FromPgField e, Hpgsql.Encoding.FromPgField f, Hpgsql.Encoding.FromPgField g, Hpgsql.Encoding.FromPgField h, Hpgsql.Encoding.FromPgField i, Hpgsql.Encoding.FromPgField j, Hpgsql.Encoding.FromPgField k, Hpgsql.Encoding.FromPgField l) => Hpgsql.Encoding.FromPgRow (a, b, c, d, e, f, g, h, i, j, k, l)
- Hpgsql.Encoding: instance (Hpgsql.Encoding.FromPgField a, Hpgsql.Encoding.FromPgField b, Hpgsql.Encoding.FromPgField c, Hpgsql.Encoding.FromPgField d, Hpgsql.Encoding.FromPgField e, Hpgsql.Encoding.FromPgField f, Hpgsql.Encoding.FromPgField g, Hpgsql.Encoding.FromPgField h, Hpgsql.Encoding.FromPgField i, Hpgsql.Encoding.FromPgField j, Hpgsql.Encoding.FromPgField k, Hpgsql.Encoding.FromPgField l, Hpgsql.Encoding.FromPgField m) => Hpgsql.Encoding.FromPgRow (a, b, c, d, e, f, g, h, i, j, k, l, m)
- Hpgsql.Encoding: instance (Hpgsql.Encoding.ProductTypeDecoder a, Hpgsql.Encoding.ProductTypeDecoder b) => Hpgsql.Encoding.ProductTypeDecoder (a GHC.Internal.Generics.:*: b)
- Hpgsql.Encoding: instance (Hpgsql.Encoding.ProductTypeEncoder a, Hpgsql.Encoding.ProductTypeEncoder b) => Hpgsql.Encoding.ProductTypeEncoder (a GHC.Internal.Generics.:*: b)
- Hpgsql.Encoding: instance (Hpgsql.Encoding.ToPgField a, Hpgsql.Encoding.ToPgField b) => Hpgsql.Encoding.ToPgRow (a, b)
- Hpgsql.Encoding: instance (Hpgsql.Encoding.ToPgField a, Hpgsql.Encoding.ToPgField b, Hpgsql.Encoding.ToPgField c) => Hpgsql.Encoding.ToPgRow (a, b, c)
- Hpgsql.Encoding: instance (Hpgsql.Encoding.ToPgField a, Hpgsql.Encoding.ToPgField b, Hpgsql.Encoding.ToPgField c, Hpgsql.Encoding.ToPgField d) => Hpgsql.Encoding.ToPgRow (a, b, c, d)
- Hpgsql.Encoding: instance (Hpgsql.Encoding.ToPgField a, Hpgsql.Encoding.ToPgField b, Hpgsql.Encoding.ToPgField c, Hpgsql.Encoding.ToPgField d, Hpgsql.Encoding.ToPgField e) => Hpgsql.Encoding.ToPgRow (a, b, c, d, e)
- Hpgsql.Encoding: instance (Hpgsql.Encoding.ToPgField a, Hpgsql.Encoding.ToPgField b, Hpgsql.Encoding.ToPgField c, Hpgsql.Encoding.ToPgField d, Hpgsql.Encoding.ToPgField e, Hpgsql.Encoding.ToPgField f) => Hpgsql.Encoding.ToPgRow (a, b, c, d, e, f)
- Hpgsql.Encoding: instance (Hpgsql.Encoding.ToPgField a, Hpgsql.Encoding.ToPgField b, Hpgsql.Encoding.ToPgField c, Hpgsql.Encoding.ToPgField d, Hpgsql.Encoding.ToPgField e, Hpgsql.Encoding.ToPgField f, Hpgsql.Encoding.ToPgField g) => Hpgsql.Encoding.ToPgRow (a, b, c, d, e, f, g)
- Hpgsql.Encoding: instance (Hpgsql.Encoding.ToPgField a, Hpgsql.Encoding.ToPgField b, Hpgsql.Encoding.ToPgField c, Hpgsql.Encoding.ToPgField d, Hpgsql.Encoding.ToPgField e, Hpgsql.Encoding.ToPgField f, Hpgsql.Encoding.ToPgField g, Hpgsql.Encoding.ToPgField h) => Hpgsql.Encoding.ToPgRow (a, b, c, d, e, f, g, h)
- Hpgsql.Encoding: instance (Hpgsql.Encoding.ToPgField a, Hpgsql.Encoding.ToPgField b, Hpgsql.Encoding.ToPgField c, Hpgsql.Encoding.ToPgField d, Hpgsql.Encoding.ToPgField e, Hpgsql.Encoding.ToPgField f, Hpgsql.Encoding.ToPgField g, Hpgsql.Encoding.ToPgField h, Hpgsql.Encoding.ToPgField i) => Hpgsql.Encoding.ToPgRow (a, b, c, d, e, f, g, h, i)
- Hpgsql.Encoding: instance (Hpgsql.Encoding.ToPgField a, Hpgsql.Encoding.ToPgField b, Hpgsql.Encoding.ToPgField c, Hpgsql.Encoding.ToPgField d, Hpgsql.Encoding.ToPgField e, Hpgsql.Encoding.ToPgField f, Hpgsql.Encoding.ToPgField g, Hpgsql.Encoding.ToPgField h, Hpgsql.Encoding.ToPgField i, Hpgsql.Encoding.ToPgField j) => Hpgsql.Encoding.ToPgRow (a, b, c, d, e, f, g, h, i, j)
- Hpgsql.Encoding: instance (Hpgsql.Encoding.ToPgField a, Hpgsql.Encoding.ToPgField b, Hpgsql.Encoding.ToPgField c, Hpgsql.Encoding.ToPgField d, Hpgsql.Encoding.ToPgField e, Hpgsql.Encoding.ToPgField f, Hpgsql.Encoding.ToPgField g, Hpgsql.Encoding.ToPgField h, Hpgsql.Encoding.ToPgField i, Hpgsql.Encoding.ToPgField j, Hpgsql.Encoding.ToPgField k) => Hpgsql.Encoding.ToPgRow (a, b, c, d, e, f, g, h, i, j, k)
- Hpgsql.Encoding: instance (TypeError ...) => GHC.Internal.Base.Monad Hpgsql.Encoding.RowDecoder
- Hpgsql.Encoding: instance Data.Functor.Contravariant.Contravariant Hpgsql.Encoding.FieldEncoder
- Hpgsql.Encoding: instance Data.Functor.Contravariant.Contravariant Hpgsql.Encoding.RowEncoder
- Hpgsql.Encoding: instance GHC.Internal.Base.Applicative Hpgsql.Encoding.RowDecoder
- Hpgsql.Encoding: instance GHC.Internal.Base.Functor Hpgsql.Encoding.FieldDecoder
- Hpgsql.Encoding: instance GHC.Internal.Base.Functor Hpgsql.Encoding.RowDecoder
- Hpgsql.Encoding: instance GHC.Internal.Base.Semigroup (Hpgsql.Encoding.FieldDecoder a)
- Hpgsql.Encoding: instance GHC.Internal.Generics.Generic (Hpgsql.Encoding.RowDecoder a)
- Hpgsql.Encoding: instance GHC.Internal.TypeLits.KnownSymbol ctorName => Hpgsql.Encoding.EnumDecoder (GHC.Internal.Generics.M1 GHC.Internal.Generics.C ('GHC.Internal.Generics.MetaCons ctorName ctorFixity 'GHC.Types.False) GHC.Internal.Generics.U1)
- Hpgsql.Encoding: instance GHC.Internal.TypeLits.KnownSymbol ctorName => Hpgsql.Encoding.EnumEncoder (GHC.Internal.Generics.M1 GHC.Internal.Generics.C ('GHC.Internal.Generics.MetaCons ctorName ctorFixity 'GHC.Types.False) GHC.Internal.Generics.U1)
- Hpgsql.Encoding: instance Hpgsql.Encoding.EnumDecoder f => Hpgsql.Encoding.EnumDecoder (GHC.Internal.Generics.M1 GHC.Internal.Generics.D c f)
- Hpgsql.Encoding: instance Hpgsql.Encoding.EnumEncoder f => Hpgsql.Encoding.EnumEncoder (GHC.Internal.Generics.M1 GHC.Internal.Generics.D c f)
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField ()
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField (Data.CaseInsensitive.Internal.CI Data.Text.Internal.Lazy.Text)
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField (Data.CaseInsensitive.Internal.CI Data.Text.Internal.Text)
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField (Data.CaseInsensitive.Internal.CI GHC.Internal.Base.String)
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField (GHC.Internal.Real.Ratio GHC.Num.Integer.Integer)
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField (Hpgsql.Time.Unbounded Data.Time.Calendar.Days.Day)
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField (Hpgsql.Time.Unbounded Data.Time.Clock.Internal.UTCTime.UTCTime)
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField (Hpgsql.Time.Unbounded Data.Time.LocalTime.Internal.ZonedTime.ZonedTime)
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField Data.Aeson.Types.Internal.Value
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField Data.ByteString.Internal.Type.ByteString
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField Data.ByteString.Lazy.Internal.ByteString
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField Data.Scientific.Scientific
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField Data.Text.Internal.Lazy.Text
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField Data.Text.Internal.Text
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField Data.Time.Calendar.Days.Day
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField Data.Time.Clock.Internal.UTCTime.UTCTime
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField Data.Time.LocalTime.Internal.CalendarDiffTime.CalendarDiffTime
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField Data.Time.LocalTime.Internal.LocalTime.LocalTime
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField Data.Time.LocalTime.Internal.TimeOfDay.TimeOfDay
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField Data.Time.LocalTime.Internal.ZonedTime.ZonedTime
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField Data.UUID.Types.Internal.UUID
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField GHC.Internal.Base.String
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField GHC.Internal.Int.Int16
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField GHC.Internal.Int.Int32
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField GHC.Internal.Int.Int64
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField GHC.Num.Integer.Integer
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField GHC.Types.Bool
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField GHC.Types.Char
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField GHC.Types.Double
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField GHC.Types.Float
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField GHC.Types.Int
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField Hpgsql.TypeInfo.Oid
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField a => Hpgsql.Encoding.FromPgField (Data.Vector.Vector (Data.Vector.Vector a))
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField a => Hpgsql.Encoding.FromPgField (Data.Vector.Vector a)
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField a => Hpgsql.Encoding.FromPgField (GHC.Internal.Maybe.Maybe a)
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField a => Hpgsql.Encoding.FromPgRow (Data.Tuple.Only.Only a)
- Hpgsql.Encoding: instance Hpgsql.Encoding.FromPgField a => Hpgsql.Encoding.ProductTypeDecoder (GHC.Internal.Generics.K1 r a)
- Hpgsql.Encoding: instance Hpgsql.Encoding.ProductTypeDecoder f => Hpgsql.Encoding.ProductTypeDecoder (GHC.Internal.Generics.M1 a c f)
- Hpgsql.Encoding: instance Hpgsql.Encoding.ProductTypeEncoder f => Hpgsql.Encoding.ProductTypeEncoder (GHC.Internal.Generics.M1 i c f)
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField (Data.CaseInsensitive.Internal.CI Data.Text.Internal.Lazy.Text)
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField (Data.CaseInsensitive.Internal.CI Data.Text.Internal.Text)
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField (Data.CaseInsensitive.Internal.CI GHC.Internal.Base.String)
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField (GHC.Internal.Real.Ratio GHC.Num.Integer.Integer)
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField (Hpgsql.Time.Unbounded Data.Time.Calendar.Days.Day)
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField (Hpgsql.Time.Unbounded Data.Time.Clock.Internal.UTCTime.UTCTime)
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField (Hpgsql.Time.Unbounded Data.Time.LocalTime.Internal.ZonedTime.ZonedTime)
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField Data.Aeson.Types.Internal.Value
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField Data.ByteString.Internal.Type.ByteString
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField Data.ByteString.Lazy.Internal.ByteString
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField Data.Scientific.Scientific
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField Data.Text.Internal.Lazy.Text
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField Data.Text.Internal.Text
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField Data.Time.Calendar.Days.Day
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField Data.Time.Clock.Internal.NominalDiffTime.NominalDiffTime
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField Data.Time.Clock.Internal.UTCTime.UTCTime
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField Data.Time.LocalTime.Internal.CalendarDiffTime.CalendarDiffTime
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField Data.Time.LocalTime.Internal.LocalTime.LocalTime
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField Data.Time.LocalTime.Internal.TimeOfDay.TimeOfDay
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField Data.Time.LocalTime.Internal.ZonedTime.ZonedTime
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField Data.UUID.Types.Internal.UUID
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField GHC.Internal.Base.String
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField GHC.Internal.Int.Int16
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField GHC.Internal.Int.Int32
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField GHC.Internal.Int.Int64
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField GHC.Num.Integer.Integer
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField GHC.Types.Bool
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField GHC.Types.Char
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField GHC.Types.Double
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField GHC.Types.Float
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField GHC.Types.Int
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField Hpgsql.TypeInfo.Oid
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField a => Hpgsql.Encoding.ProductTypeEncoder (GHC.Internal.Generics.K1 r a)
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField a => Hpgsql.Encoding.ToPgField (Data.Vector.Vector a)
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField a => Hpgsql.Encoding.ToPgField (GHC.Internal.Maybe.Maybe a)
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgField a => Hpgsql.Encoding.ToPgRow (Data.Tuple.Only.Only a)
- Hpgsql.Encoding: instance Hpgsql.Encoding.ToPgRow ()
- Hpgsql.InternalTypes: BindComplete :: BindComplete
- Hpgsql.InternalTypes: BindCompleteReceived :: BindComplete -> ResponseMsgsReceived
- Hpgsql.InternalTypes: CommandComplete :: Int64 -> CommandComplete
- Hpgsql.InternalTypes: CommandCompleteReceived :: Either3 NoData RowDescription CopyInResponse -> CommandComplete -> ResponseMsgsReceived
- Hpgsql.InternalTypes: ConnectOpts :: !Int -> !Int -> !Bool -> !Int -> ConnectOpts
- Hpgsql.InternalTypes: ConnectionString :: Text -> Word16 -> Text -> Text -> Text -> Text -> ConnectionString
- Hpgsql.InternalTypes: CopyDoneAndSyncSent :: CopyQueryState
- Hpgsql.InternalTypes: CopyFailAndSyncSent :: CopyQueryState
- Hpgsql.InternalTypes: CopyInResponse :: CopyInResponse
- Hpgsql.InternalTypes: CopyQuery :: CopyQueryState -> QueryProtocol
- Hpgsql.InternalTypes: DataRow :: ByteString -> DataRow
- Hpgsql.InternalTypes: EncodingContext :: TypeInfoCache -> EncodingContext
- Hpgsql.InternalTypes: ErrorCode :: ErrorDetail
- Hpgsql.InternalTypes: ErrorColumn :: ErrorDetail
- Hpgsql.InternalTypes: ErrorConstraint :: ErrorDetail
- Hpgsql.InternalTypes: ErrorContext :: ErrorDetail
- Hpgsql.InternalTypes: ErrorDetail :: ErrorDetail
- Hpgsql.InternalTypes: ErrorHint :: ErrorDetail
- Hpgsql.InternalTypes: ErrorHumanReadableMsg :: ErrorDetail
- Hpgsql.InternalTypes: ErrorInternalCommand :: ErrorDetail
- Hpgsql.InternalTypes: ErrorInternalPosition :: ErrorDetail
- Hpgsql.InternalTypes: ErrorPosition :: ErrorDetail
- Hpgsql.InternalTypes: ErrorResponse :: Map ErrorDetail ByteString -> ErrorResponse
- Hpgsql.InternalTypes: ErrorResponseReceived :: Maybe (Either3 NoData RowDescription CopyInResponse) -> ErrorResponse -> ResponseMsgsReceived
- Hpgsql.InternalTypes: ErrorSchema :: ErrorDetail
- Hpgsql.InternalTypes: ErrorSeverity :: ErrorDetail
- Hpgsql.InternalTypes: ErrorSourceFile :: ErrorDetail
- Hpgsql.InternalTypes: ErrorSourceLine :: ErrorDetail
- Hpgsql.InternalTypes: ErrorSourceRoutine :: ErrorDetail
- Hpgsql.InternalTypes: ErrorTable :: ErrorDetail
- Hpgsql.InternalTypes: ErrorType :: ErrorDetail
- Hpgsql.InternalTypes: ExtendedQuery :: QueryProtocol
- Hpgsql.InternalTypes: FragmentOfCommentsOrWhitespace :: !ByteString -> SingleQueryFragment
- Hpgsql.InternalTypes: FragmentOfStaticSql :: !ByteString -> SingleQueryFragment
- Hpgsql.InternalTypes: FragmentWithSemiColon :: SingleQueryFragment
- Hpgsql.InternalTypes: HPgConnection :: !Socket -> !MVar Bool -> !IORef ByteString -> !MVar [(ByteString, STM ())] -> !Mutex -> !ConnectionString -> !AddrInfo -> !MVar EncodingContext -> !MVar (Map Text Text) -> !TVar InternalConnectionState -> !Int32 -> !ByteString -> !ConnectOpts -> HPgConnection
- Hpgsql.InternalTypes: InternalConnectionState :: !Integer -> ![QueryState] -> Bool -> !TQueue NotificationResponse -> Set String -> !TransactionStatus -> InternalConnectionState
- Hpgsql.InternalTypes: IrrecoverableHpgsqlError :: Text -> Maybe SomeException -> !Maybe ByteString -> IrrecoverableHpgsqlError
- Hpgsql.InternalTypes: Left3 :: !a -> Either3 a b c
- Hpgsql.InternalTypes: Middle3 :: !b -> Either3 a b c
- Hpgsql.InternalTypes: Mutex :: TVar (Maybe (WeakThreadId, Int)) -> Mutex
- Hpgsql.InternalTypes: NoData :: NoData
- Hpgsql.InternalTypes: NoMsgsReceived :: ResponseMsgsReceived
- Hpgsql.InternalTypes: NotificationResponse :: !Int32 -> !Text -> !Text -> NotificationResponse
- Hpgsql.InternalTypes: ParseComplete :: ParseComplete
- Hpgsql.InternalTypes: ParseCompleteReceived :: ParseComplete -> ResponseMsgsReceived
- Hpgsql.InternalTypes: Pipeline :: [(SingleQuery, Maybe Int)] -> (HPgConnection -> [QueryId] -> a) -> Pipeline a
- Hpgsql.InternalTypes: PostgresError :: Map ErrorDetail ByteString -> !ByteString -> PostgresError
- Hpgsql.InternalTypes: Query :: ![SingleQueryFragment] -> ![EncodingContext -> (Maybe Oid, BinaryField)] -> !Bool -> Query
- Hpgsql.InternalTypes: QueryArgumentPlaceHolder :: !Int -> SingleQueryFragment
- Hpgsql.InternalTypes: QueryId :: Integer -> QueryId
- Hpgsql.InternalTypes: QueryState :: !QueryId -> !ByteString -> !QueryProtocol -> !Maybe String -> !WeakThreadId -> ResponseMsgsReceived -> QueryState
- Hpgsql.InternalTypes: ReadyForQuery :: TransactionStatus -> ReadyForQuery
- Hpgsql.InternalTypes: ReadyForQueryReceived :: Either ErrorResponse CommandComplete -> ReadyForQuery -> ResponseMsgsReceived
- Hpgsql.InternalTypes: ResetConnectionOpts :: Bool -> Bool -> Bool -> Bool -> Bool -> Bool -> Bool -> Bool -> Bool -> ResetConnectionOpts
- Hpgsql.InternalTypes: RespBindComplete :: BindComplete -> ResponseMsg
- Hpgsql.InternalTypes: RespCommandComplete :: CommandComplete -> ResponseMsg
- Hpgsql.InternalTypes: RespCopyInResponse :: CopyInResponse -> ResponseMsg
- Hpgsql.InternalTypes: RespDataRow :: DataRow -> ResponseMsg
- Hpgsql.InternalTypes: RespErrorResponse :: ErrorResponse -> ResponseMsg
- Hpgsql.InternalTypes: RespNoData :: NoData -> ResponseMsg
- Hpgsql.InternalTypes: RespParseComplete :: ParseComplete -> ResponseMsg
- Hpgsql.InternalTypes: RespReadyForQuery :: ReadyForQuery -> ResponseMsg
- Hpgsql.InternalTypes: RespRowDescription :: RowDescription -> ResponseMsg
- Hpgsql.InternalTypes: Right3 :: !c -> Either3 a b c
- Hpgsql.InternalTypes: RowDescription :: [(Text, Oid)] -> RowDescription
- Hpgsql.InternalTypes: RowDescriptionOrNoDataOrCopyInResponseReceived :: Either3 NoData RowDescription CopyInResponse -> ResponseMsgsReceived
- Hpgsql.InternalTypes: SingleQuery :: !ByteString -> ![EncodingContext -> (Maybe Oid, BinaryField)] -> !Maybe String -> SingleQuery
- Hpgsql.InternalTypes: StillCopying :: CopyQueryState
- Hpgsql.InternalTypes: TransActive :: TransactionStatus
- Hpgsql.InternalTypes: TransIdle :: TransactionStatus
- Hpgsql.InternalTypes: TransInError :: TransactionStatus
- Hpgsql.InternalTypes: TransInTrans :: TransactionStatus
- Hpgsql.InternalTypes: WeakThreadId :: !Weak ThreadId -> !Word64 -> WeakThreadId
- Hpgsql.InternalTypes: [cancelSecretKey] :: HPgConnection -> !ByteString
- Hpgsql.InternalTypes: [cancellationRequestResendIntervalMs] :: ConnectOpts -> !Int
- Hpgsql.InternalTypes: [channelName] :: NotificationResponse -> !Text
- Hpgsql.InternalTypes: [checkTransactionState] :: ResetConnectionOpts -> Bool
- Hpgsql.InternalTypes: [closeAllCursors] :: ResetConnectionOpts -> Bool
- Hpgsql.InternalTypes: [connOpts] :: HPgConnection -> !ConnectOpts
- Hpgsql.InternalTypes: [connPid] :: HPgConnection -> !Int32
- Hpgsql.InternalTypes: [connectedTo] :: HPgConnection -> !AddrInfo
- Hpgsql.InternalTypes: [currentPipeline] :: InternalConnectionState -> ![QueryState]
- Hpgsql.InternalTypes: [database] :: ConnectionString -> Text
- Hpgsql.InternalTypes: [deallocatePreparedStmts] :: ResetConnectionOpts -> Bool
- Hpgsql.InternalTypes: [discardSequences] :: ResetConnectionOpts -> Bool
- Hpgsql.InternalTypes: [discardTempTables] :: ResetConnectionOpts -> Bool
- Hpgsql.InternalTypes: [encodingContext] :: HPgConnection -> !MVar EncodingContext
- Hpgsql.InternalTypes: [failedStatement] :: PostgresError -> !ByteString
- Hpgsql.InternalTypes: [fillTypeInfoCache] :: ConnectOpts -> !Bool
- Hpgsql.InternalTypes: [fullDataRow] :: DataRow -> ByteString
- Hpgsql.InternalTypes: [hostname] :: ConnectionString -> Text
- Hpgsql.InternalTypes: [hpgsqlDetails] :: IrrecoverableHpgsqlError -> Text
- Hpgsql.InternalTypes: [innerException] :: IrrecoverableHpgsqlError -> Maybe SomeException
- Hpgsql.InternalTypes: [internalConnectionState] :: HPgConnection -> !TVar InternalConnectionState
- Hpgsql.InternalTypes: [isPrepared] :: Query -> !Bool
- Hpgsql.InternalTypes: [killedThreadPollIntervalMs] :: ConnectOpts -> !Int
- Hpgsql.InternalTypes: [mustIssueRollbackBeforeNextCommand] :: InternalConnectionState -> Bool
- Hpgsql.InternalTypes: [notifPayload] :: NotificationResponse -> !Text
- Hpgsql.InternalTypes: [notificationsReceived] :: InternalConnectionState -> !TQueue NotificationResponse
- Hpgsql.InternalTypes: [notifierPid] :: NotificationResponse -> !Int32
- Hpgsql.InternalTypes: [numRows] :: CommandComplete -> Int64
- Hpgsql.InternalTypes: [options] :: ConnectionString -> Text
- Hpgsql.InternalTypes: [originalConnStr] :: HPgConnection -> !ConnectionString
- Hpgsql.InternalTypes: [parameterStatusMap] :: HPgConnection -> !MVar (Map Text Text)
- Hpgsql.InternalTypes: [password] :: ConnectionString -> Text
- Hpgsql.InternalTypes: [pgErrorDetails] :: PostgresError -> Map ErrorDetail ByteString
- Hpgsql.InternalTypes: [port] :: ConnectionString -> Word16
- Hpgsql.InternalTypes: [preparedStatementNames] :: InternalConnectionState -> Set String
- Hpgsql.InternalTypes: [preparedStmtHash] :: SingleQuery -> !Maybe String
- Hpgsql.InternalTypes: [queryIdentifier] :: QueryState -> !QueryId
- Hpgsql.InternalTypes: [queryOwner] :: QueryState -> !WeakThreadId
- Hpgsql.InternalTypes: [queryParams] :: SingleQuery -> ![EncodingContext -> (Maybe Oid, BinaryField)]
- Hpgsql.InternalTypes: [queryPrepStmtName] :: QueryState -> !Maybe String
- Hpgsql.InternalTypes: [queryProtocol] :: QueryState -> !QueryProtocol
- Hpgsql.InternalTypes: [queryString] :: SingleQuery -> !ByteString
- Hpgsql.InternalTypes: [queryText] :: QueryState -> !ByteString
- Hpgsql.InternalTypes: [recvBuffer] :: HPgConnection -> !IORef ByteString
- Hpgsql.InternalTypes: [recvChunkSize] :: ConnectOpts -> !Int
- Hpgsql.InternalTypes: [relatedStatement] :: IrrecoverableHpgsqlError -> !Maybe ByteString
- Hpgsql.InternalTypes: [releaseAdvisoryLocks] :: ResetConnectionOpts -> Bool
- Hpgsql.InternalTypes: [resetAll] :: ResetConnectionOpts -> Bool
- Hpgsql.InternalTypes: [resetRole] :: ResetConnectionOpts -> Bool
- Hpgsql.InternalTypes: [responseMsgsState] :: QueryState -> ResponseMsgsReceived
- Hpgsql.InternalTypes: [resultColumnTypes] :: RowDescription -> [(Text, Oid)]
- Hpgsql.InternalTypes: [sendBuffer] :: HPgConnection -> !MVar [(ByteString, STM ())]
- Hpgsql.InternalTypes: [socketClosed] :: HPgConnection -> !MVar Bool
- Hpgsql.InternalTypes: [socketMutex] :: HPgConnection -> !Mutex
- Hpgsql.InternalTypes: [socket] :: HPgConnection -> !Socket
- Hpgsql.InternalTypes: [totalQueriesSent] :: InternalConnectionState -> !Integer
- Hpgsql.InternalTypes: [transactionStatusBeforeCurrentPipeline] :: InternalConnectionState -> !TransactionStatus
- Hpgsql.InternalTypes: [typeInfoCache] :: EncodingContext -> TypeInfoCache
- Hpgsql.InternalTypes: [unlistenAll] :: ResetConnectionOpts -> Bool
- Hpgsql.InternalTypes: [user] :: ConnectionString -> Text
- Hpgsql.InternalTypes: breakQueryIntoStatements :: Query -> NonEmpty SingleQuery
- Hpgsql.InternalTypes: data BindComplete
- Hpgsql.InternalTypes: data ConnectOpts
- Hpgsql.InternalTypes: data ConnectionString
- Hpgsql.InternalTypes: data CopyInResponse
- Hpgsql.InternalTypes: data CopyQueryState
- Hpgsql.InternalTypes: data Either3 a b c
- Hpgsql.InternalTypes: data ErrorDetail
- Hpgsql.InternalTypes: data HPgConnection
- Hpgsql.InternalTypes: data InternalConnectionState
- Hpgsql.InternalTypes: data IrrecoverableHpgsqlError
- Hpgsql.InternalTypes: data NoData
- Hpgsql.InternalTypes: data NotificationResponse
- Hpgsql.InternalTypes: data ParseComplete
- Hpgsql.InternalTypes: data Pipeline a
- Hpgsql.InternalTypes: data PostgresError
- Hpgsql.InternalTypes: data Query
- Hpgsql.InternalTypes: data QueryProtocol
- Hpgsql.InternalTypes: data QueryState
- Hpgsql.InternalTypes: data ResetConnectionOpts
- Hpgsql.InternalTypes: data ResponseMsg
- Hpgsql.InternalTypes: data ResponseMsgsReceived
- Hpgsql.InternalTypes: data SingleQuery
- Hpgsql.InternalTypes: data SingleQueryFragment
- Hpgsql.InternalTypes: data TransactionStatus
- Hpgsql.InternalTypes: data WeakThreadId
- Hpgsql.InternalTypes: instance (GHC.Internal.Show.Show a, GHC.Internal.Show.Show b, GHC.Internal.Show.Show c) => GHC.Internal.Show.Show (Hpgsql.InternalTypes.Either3 a b c)
- Hpgsql.InternalTypes: instance GHC.Classes.Eq Hpgsql.InternalTypes.ConnectionString
- Hpgsql.InternalTypes: instance GHC.Classes.Eq Hpgsql.InternalTypes.CopyQueryState
- Hpgsql.InternalTypes: instance GHC.Classes.Eq Hpgsql.InternalTypes.ErrorDetail
- Hpgsql.InternalTypes: instance GHC.Classes.Eq Hpgsql.InternalTypes.HPgConnection
- Hpgsql.InternalTypes: instance GHC.Classes.Eq Hpgsql.InternalTypes.NotificationResponse
- Hpgsql.InternalTypes: instance GHC.Classes.Eq Hpgsql.InternalTypes.QueryId
- Hpgsql.InternalTypes: instance GHC.Classes.Eq Hpgsql.InternalTypes.QueryProtocol
- Hpgsql.InternalTypes: instance GHC.Classes.Eq Hpgsql.InternalTypes.SingleQueryFragment
- Hpgsql.InternalTypes: instance GHC.Classes.Eq Hpgsql.InternalTypes.WeakThreadId
- Hpgsql.InternalTypes: instance GHC.Classes.Ord Hpgsql.InternalTypes.ErrorDetail
- Hpgsql.InternalTypes: instance GHC.Classes.Ord Hpgsql.InternalTypes.QueryId
- Hpgsql.InternalTypes: instance GHC.Internal.Base.Applicative Hpgsql.InternalTypes.Pipeline
- Hpgsql.InternalTypes: instance GHC.Internal.Base.Functor Hpgsql.InternalTypes.Pipeline
- Hpgsql.InternalTypes: instance GHC.Internal.Base.Semigroup Hpgsql.InternalTypes.Query
- Hpgsql.InternalTypes: instance GHC.Internal.Data.String.IsString Hpgsql.InternalTypes.Query
- Hpgsql.InternalTypes: instance GHC.Internal.Enum.Enum Hpgsql.InternalTypes.QueryId
- Hpgsql.InternalTypes: instance GHC.Internal.Exception.Type.Exception Hpgsql.InternalTypes.IrrecoverableHpgsqlError
- Hpgsql.InternalTypes: instance GHC.Internal.Exception.Type.Exception Hpgsql.InternalTypes.PostgresError
- Hpgsql.InternalTypes: instance GHC.Internal.Num.Num Hpgsql.InternalTypes.QueryId
- Hpgsql.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.InternalTypes.BindComplete
- Hpgsql.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.InternalTypes.CommandComplete
- Hpgsql.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.InternalTypes.ConnectionString
- Hpgsql.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.InternalTypes.CopyInResponse
- Hpgsql.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.InternalTypes.CopyQueryState
- Hpgsql.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.InternalTypes.DataRow
- Hpgsql.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.InternalTypes.ErrorDetail
- Hpgsql.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.InternalTypes.ErrorResponse
- Hpgsql.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.InternalTypes.IrrecoverableHpgsqlError
- Hpgsql.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.InternalTypes.NoData
- Hpgsql.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.InternalTypes.NotificationResponse
- Hpgsql.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.InternalTypes.ParseComplete
- Hpgsql.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.InternalTypes.PostgresError
- Hpgsql.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.InternalTypes.Query
- Hpgsql.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.InternalTypes.QueryId
- Hpgsql.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.InternalTypes.QueryProtocol
- Hpgsql.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.InternalTypes.QueryState
- Hpgsql.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.InternalTypes.ReadyForQuery
- Hpgsql.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.InternalTypes.ResponseMsg
- Hpgsql.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.InternalTypes.ResponseMsgsReceived
- Hpgsql.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.InternalTypes.RowDescription
- Hpgsql.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.InternalTypes.SingleQuery
- Hpgsql.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.InternalTypes.SingleQueryFragment
- Hpgsql.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.InternalTypes.WeakThreadId
- Hpgsql.InternalTypes: mkMutex :: IO Mutex
- Hpgsql.InternalTypes: newtype CommandComplete
- Hpgsql.InternalTypes: newtype DataRow
- Hpgsql.InternalTypes: newtype EncodingContext
- Hpgsql.InternalTypes: newtype ErrorResponse
- Hpgsql.InternalTypes: newtype Mutex
- Hpgsql.InternalTypes: newtype QueryId
- Hpgsql.InternalTypes: newtype ReadyForQuery
- Hpgsql.InternalTypes: newtype RowDescription
- Hpgsql.InternalTypes: queryToByteString :: Query -> ByteString
- Hpgsql.InternalTypes: renumberParamsFrom :: [SingleQueryFragment] -> Int -> (Int, [SingleQueryFragment])
- Hpgsql.InternalTypes: throwIrrecoverableError :: MonadThrow m => Text -> m a
- Hpgsql.Types: instance (Hpgsql.Encoding.FromPgRow a, Hpgsql.Encoding.FromPgRow b) => Hpgsql.Encoding.FromPgRow (a Hpgsql.Types.:. b)
- Hpgsql.Types: instance (Hpgsql.Encoding.ToPgRow a, Hpgsql.Encoding.ToPgRow b) => Hpgsql.Encoding.ToPgRow (a Hpgsql.Types.:. b)
- Hpgsql.Types: instance Data.Aeson.Types.FromJSON.FromJSON a => Hpgsql.Encoding.FromPgField (Hpgsql.Types.Aeson a)
- Hpgsql.Types: instance Data.Aeson.Types.ToJSON.ToJSON a => Hpgsql.Encoding.ToPgField (Hpgsql.Types.Aeson a)
- Hpgsql.Types: instance Hpgsql.Encoding.FromPgField Hpgsql.Types.PgJson
- Hpgsql.Types: instance Hpgsql.Encoding.FromPgField a => Hpgsql.Encoding.FromPgField (Hpgsql.Types.PGArray a)
- Hpgsql.Types: instance Hpgsql.Encoding.ToPgField a => Hpgsql.Encoding.ToPgField (Hpgsql.Types.PGArray a)
+ Hpgsql.Builder: nulTerminatedUtf8String :: String -> LengthAwareBuilder
+ Hpgsql.Encoding: fieldRowDecoder :: FromPgField a => RowDecoder a
+ Hpgsql.Internal.InternalTypes: BindComplete :: BindComplete
+ Hpgsql.Internal.InternalTypes: BindCompleteReceived :: BindComplete -> ResponseMsgsReceived
+ Hpgsql.Internal.InternalTypes: CommandComplete :: Int64 -> CommandComplete
+ Hpgsql.Internal.InternalTypes: CommandCompleteReceived :: Either3 NoData RowDescription CopyInResponse -> CommandComplete -> ResponseMsgsReceived
+ Hpgsql.Internal.InternalTypes: ConnectOpts :: !Int -> !Int -> !Bool -> !Int -> ConnectOpts
+ Hpgsql.Internal.InternalTypes: ConnectionString :: Text -> Word16 -> Text -> Text -> Text -> Text -> ConnectionString
+ Hpgsql.Internal.InternalTypes: CopyDoneAndSyncSent :: CopyQueryState
+ Hpgsql.Internal.InternalTypes: CopyFailAndSyncSent :: CopyQueryState
+ Hpgsql.Internal.InternalTypes: CopyInResponse :: CopyInResponse
+ Hpgsql.Internal.InternalTypes: CopyQuery :: CopyQueryState -> QueryProtocol
+ Hpgsql.Internal.InternalTypes: DataRow :: PinnedByteArray -> DataRow
+ Hpgsql.Internal.InternalTypes: EncodingContext :: TypeInfoCache -> EncodingContext
+ Hpgsql.Internal.InternalTypes: ErrorCode :: ErrorDetail
+ Hpgsql.Internal.InternalTypes: ErrorColumn :: ErrorDetail
+ Hpgsql.Internal.InternalTypes: ErrorConstraint :: ErrorDetail
+ Hpgsql.Internal.InternalTypes: ErrorContext :: ErrorDetail
+ Hpgsql.Internal.InternalTypes: ErrorDetail :: ErrorDetail
+ Hpgsql.Internal.InternalTypes: ErrorHint :: ErrorDetail
+ Hpgsql.Internal.InternalTypes: ErrorHumanReadableMsg :: ErrorDetail
+ Hpgsql.Internal.InternalTypes: ErrorInternalCommand :: ErrorDetail
+ Hpgsql.Internal.InternalTypes: ErrorInternalPosition :: ErrorDetail
+ Hpgsql.Internal.InternalTypes: ErrorPosition :: ErrorDetail
+ Hpgsql.Internal.InternalTypes: ErrorResponse :: Map ErrorDetail ByteString -> ErrorResponse
+ Hpgsql.Internal.InternalTypes: ErrorResponseReceived :: Maybe (Either3 NoData RowDescription CopyInResponse) -> ErrorResponse -> ResponseMsgsReceived
+ Hpgsql.Internal.InternalTypes: ErrorSchema :: ErrorDetail
+ Hpgsql.Internal.InternalTypes: ErrorSeverity :: ErrorDetail
+ Hpgsql.Internal.InternalTypes: ErrorSourceFile :: ErrorDetail
+ Hpgsql.Internal.InternalTypes: ErrorSourceLine :: ErrorDetail
+ Hpgsql.Internal.InternalTypes: ErrorSourceRoutine :: ErrorDetail
+ Hpgsql.Internal.InternalTypes: ErrorTable :: ErrorDetail
+ Hpgsql.Internal.InternalTypes: ErrorType :: ErrorDetail
+ Hpgsql.Internal.InternalTypes: ExtendedQuery :: QueryProtocol
+ Hpgsql.Internal.InternalTypes: FragmentOfCommentsOrWhitespace :: !ByteString -> SingleQueryFragment
+ Hpgsql.Internal.InternalTypes: FragmentOfStaticSql :: !ByteString -> SingleQueryFragment
+ Hpgsql.Internal.InternalTypes: FragmentWithSemiColon :: SingleQueryFragment
+ Hpgsql.Internal.InternalTypes: HPgConnection :: !Socket -> !MVar Bool -> !IORef LazyPinnedByteArray -> !MVar [(ByteString, STM ())] -> !Mutex -> !ConnectionString -> !AddrInfo -> !MVar EncodingContext -> !MVar (Map Text Text) -> !TVar InternalConnectionState -> !Int32 -> !ByteString -> !ConnectOpts -> HPgConnection
+ Hpgsql.Internal.InternalTypes: InternalConnectionState :: !Integer -> ![QueryState] -> Bool -> !TQueue NotificationResponse -> Set String -> !TransactionStatus -> InternalConnectionState
+ Hpgsql.Internal.InternalTypes: IrrecoverableHpgsqlError :: Text -> Maybe SomeException -> !Maybe ByteString -> IrrecoverableHpgsqlError
+ Hpgsql.Internal.InternalTypes: Left3 :: !a -> Either3 a b c
+ Hpgsql.Internal.InternalTypes: Middle3 :: !b -> Either3 a b c
+ Hpgsql.Internal.InternalTypes: Mutex :: TVar (Maybe (WeakThreadId, Int)) -> Mutex
+ Hpgsql.Internal.InternalTypes: NoData :: NoData
+ Hpgsql.Internal.InternalTypes: NoMsgsReceived :: ResponseMsgsReceived
+ Hpgsql.Internal.InternalTypes: NotificationResponse :: !Int32 -> !Text -> !Text -> NotificationResponse
+ Hpgsql.Internal.InternalTypes: ParseComplete :: ParseComplete
+ Hpgsql.Internal.InternalTypes: ParseCompleteReceived :: ParseComplete -> ResponseMsgsReceived
+ Hpgsql.Internal.InternalTypes: Pipeline :: [(SingleQuery, Maybe Int)] -> (HPgConnection -> [QueryId] -> a) -> Pipeline a
+ Hpgsql.Internal.InternalTypes: PostgresError :: Map ErrorDetail ByteString -> !ByteString -> PostgresError
+ Hpgsql.Internal.InternalTypes: Query :: ![SingleQueryFragment] -> ![EncodingContext -> (Maybe Oid, BinaryField)] -> !Bool -> Query
+ Hpgsql.Internal.InternalTypes: QueryArgumentPlaceHolder :: !Int -> SingleQueryFragment
+ Hpgsql.Internal.InternalTypes: QueryId :: Integer -> QueryId
+ Hpgsql.Internal.InternalTypes: QueryState :: !QueryId -> !ByteString -> !QueryProtocol -> !Maybe String -> !WeakThreadId -> ResponseMsgsReceived -> QueryState
+ Hpgsql.Internal.InternalTypes: ReadyForQuery :: TransactionStatus -> ReadyForQuery
+ Hpgsql.Internal.InternalTypes: ReadyForQueryReceived :: Either ErrorResponse CommandComplete -> ReadyForQuery -> ResponseMsgsReceived
+ Hpgsql.Internal.InternalTypes: ResetConnectionOpts :: Bool -> Bool -> Bool -> Bool -> Bool -> Bool -> Bool -> Bool -> Bool -> ResetConnectionOpts
+ Hpgsql.Internal.InternalTypes: RespBindComplete :: BindComplete -> ResponseMsg
+ Hpgsql.Internal.InternalTypes: RespCommandComplete :: CommandComplete -> ResponseMsg
+ Hpgsql.Internal.InternalTypes: RespCopyInResponse :: CopyInResponse -> ResponseMsg
+ Hpgsql.Internal.InternalTypes: RespDataRow :: DataRow -> ResponseMsg
+ Hpgsql.Internal.InternalTypes: RespErrorResponse :: ErrorResponse -> ResponseMsg
+ Hpgsql.Internal.InternalTypes: RespNoData :: NoData -> ResponseMsg
+ Hpgsql.Internal.InternalTypes: RespParseComplete :: ParseComplete -> ResponseMsg
+ Hpgsql.Internal.InternalTypes: RespReadyForQuery :: ReadyForQuery -> ResponseMsg
+ Hpgsql.Internal.InternalTypes: RespRowDescription :: RowDescription -> ResponseMsg
+ Hpgsql.Internal.InternalTypes: Right3 :: !c -> Either3 a b c
+ Hpgsql.Internal.InternalTypes: RowDescription :: [(Text, Oid)] -> RowDescription
+ Hpgsql.Internal.InternalTypes: RowDescriptionOrNoDataOrCopyInResponseReceived :: Either3 NoData RowDescription CopyInResponse -> ResponseMsgsReceived
+ Hpgsql.Internal.InternalTypes: SingleQuery :: !ByteString -> ![EncodingContext -> (Maybe Oid, BinaryField)] -> !Maybe String -> SingleQuery
+ Hpgsql.Internal.InternalTypes: StillCopying :: CopyQueryState
+ Hpgsql.Internal.InternalTypes: TransActive :: TransactionStatus
+ Hpgsql.Internal.InternalTypes: TransIdle :: TransactionStatus
+ Hpgsql.Internal.InternalTypes: TransInError :: TransactionStatus
+ Hpgsql.Internal.InternalTypes: TransInTrans :: TransactionStatus
+ Hpgsql.Internal.InternalTypes: WeakThreadId :: !Weak ThreadId -> !Word64 -> WeakThreadId
+ Hpgsql.Internal.InternalTypes: [cancelSecretKey] :: HPgConnection -> !ByteString
+ Hpgsql.Internal.InternalTypes: [cancellationRequestResendIntervalMs] :: ConnectOpts -> !Int
+ Hpgsql.Internal.InternalTypes: [channelName] :: NotificationResponse -> !Text
+ Hpgsql.Internal.InternalTypes: [checkTransactionState] :: ResetConnectionOpts -> Bool
+ Hpgsql.Internal.InternalTypes: [closeAllCursors] :: ResetConnectionOpts -> Bool
+ Hpgsql.Internal.InternalTypes: [connOpts] :: HPgConnection -> !ConnectOpts
+ Hpgsql.Internal.InternalTypes: [connPid] :: HPgConnection -> !Int32
+ Hpgsql.Internal.InternalTypes: [connectedTo] :: HPgConnection -> !AddrInfo
+ Hpgsql.Internal.InternalTypes: [currentPipeline] :: InternalConnectionState -> ![QueryState]
+ Hpgsql.Internal.InternalTypes: [database] :: ConnectionString -> Text
+ Hpgsql.Internal.InternalTypes: [deallocatePreparedStmts] :: ResetConnectionOpts -> Bool
+ Hpgsql.Internal.InternalTypes: [discardSequences] :: ResetConnectionOpts -> Bool
+ Hpgsql.Internal.InternalTypes: [discardTempTables] :: ResetConnectionOpts -> Bool
+ Hpgsql.Internal.InternalTypes: [encodingContext] :: HPgConnection -> !MVar EncodingContext
+ Hpgsql.Internal.InternalTypes: [failedStatement] :: PostgresError -> !ByteString
+ Hpgsql.Internal.InternalTypes: [fillTypeInfoCache] :: ConnectOpts -> !Bool
+ Hpgsql.Internal.InternalTypes: [fullDataRow] :: DataRow -> PinnedByteArray
+ Hpgsql.Internal.InternalTypes: [hostname] :: ConnectionString -> Text
+ Hpgsql.Internal.InternalTypes: [hpgsqlDetails] :: IrrecoverableHpgsqlError -> Text
+ Hpgsql.Internal.InternalTypes: [innerException] :: IrrecoverableHpgsqlError -> Maybe SomeException
+ Hpgsql.Internal.InternalTypes: [internalConnectionState] :: HPgConnection -> !TVar InternalConnectionState
+ Hpgsql.Internal.InternalTypes: [isPrepared] :: Query -> !Bool
+ Hpgsql.Internal.InternalTypes: [killedThreadPollIntervalMs] :: ConnectOpts -> !Int
+ Hpgsql.Internal.InternalTypes: [mustIssueRollbackBeforeNextCommand] :: InternalConnectionState -> Bool
+ Hpgsql.Internal.InternalTypes: [notifPayload] :: NotificationResponse -> !Text
+ Hpgsql.Internal.InternalTypes: [notificationsReceived] :: InternalConnectionState -> !TQueue NotificationResponse
+ Hpgsql.Internal.InternalTypes: [notifierPid] :: NotificationResponse -> !Int32
+ Hpgsql.Internal.InternalTypes: [numRows] :: CommandComplete -> Int64
+ Hpgsql.Internal.InternalTypes: [options] :: ConnectionString -> Text
+ Hpgsql.Internal.InternalTypes: [originalConnStr] :: HPgConnection -> !ConnectionString
+ Hpgsql.Internal.InternalTypes: [parameterStatusMap] :: HPgConnection -> !MVar (Map Text Text)
+ Hpgsql.Internal.InternalTypes: [password] :: ConnectionString -> Text
+ Hpgsql.Internal.InternalTypes: [pgErrorDetails] :: PostgresError -> Map ErrorDetail ByteString
+ Hpgsql.Internal.InternalTypes: [port] :: ConnectionString -> Word16
+ Hpgsql.Internal.InternalTypes: [preparedStatementNames] :: InternalConnectionState -> Set String
+ Hpgsql.Internal.InternalTypes: [preparedStmtHash] :: SingleQuery -> !Maybe String
+ Hpgsql.Internal.InternalTypes: [queryIdentifier] :: QueryState -> !QueryId
+ Hpgsql.Internal.InternalTypes: [queryOwner] :: QueryState -> !WeakThreadId
+ Hpgsql.Internal.InternalTypes: [queryParams] :: SingleQuery -> ![EncodingContext -> (Maybe Oid, BinaryField)]
+ Hpgsql.Internal.InternalTypes: [queryPrepStmtName] :: QueryState -> !Maybe String
+ Hpgsql.Internal.InternalTypes: [queryProtocol] :: QueryState -> !QueryProtocol
+ Hpgsql.Internal.InternalTypes: [queryString] :: SingleQuery -> !ByteString
+ Hpgsql.Internal.InternalTypes: [queryText] :: QueryState -> !ByteString
+ Hpgsql.Internal.InternalTypes: [recvBuffer] :: HPgConnection -> !IORef LazyPinnedByteArray
+ Hpgsql.Internal.InternalTypes: [recvChunkSize] :: ConnectOpts -> !Int
+ Hpgsql.Internal.InternalTypes: [relatedStatement] :: IrrecoverableHpgsqlError -> !Maybe ByteString
+ Hpgsql.Internal.InternalTypes: [releaseAdvisoryLocks] :: ResetConnectionOpts -> Bool
+ Hpgsql.Internal.InternalTypes: [resetAll] :: ResetConnectionOpts -> Bool
+ Hpgsql.Internal.InternalTypes: [resetRole] :: ResetConnectionOpts -> Bool
+ Hpgsql.Internal.InternalTypes: [responseMsgsState] :: QueryState -> ResponseMsgsReceived
+ Hpgsql.Internal.InternalTypes: [resultColumnTypes] :: RowDescription -> [(Text, Oid)]
+ Hpgsql.Internal.InternalTypes: [sendBuffer] :: HPgConnection -> !MVar [(ByteString, STM ())]
+ Hpgsql.Internal.InternalTypes: [socketClosed] :: HPgConnection -> !MVar Bool
+ Hpgsql.Internal.InternalTypes: [socketMutex] :: HPgConnection -> !Mutex
+ Hpgsql.Internal.InternalTypes: [socket] :: HPgConnection -> !Socket
+ Hpgsql.Internal.InternalTypes: [totalQueriesSent] :: InternalConnectionState -> !Integer
+ Hpgsql.Internal.InternalTypes: [transactionStatusBeforeCurrentPipeline] :: InternalConnectionState -> !TransactionStatus
+ Hpgsql.Internal.InternalTypes: [typeInfoCache] :: EncodingContext -> TypeInfoCache
+ Hpgsql.Internal.InternalTypes: [unlistenAll] :: ResetConnectionOpts -> Bool
+ Hpgsql.Internal.InternalTypes: [user] :: ConnectionString -> Text
+ Hpgsql.Internal.InternalTypes: breakQueryIntoStatements :: Query -> NonEmpty SingleQuery
+ Hpgsql.Internal.InternalTypes: data BindComplete
+ Hpgsql.Internal.InternalTypes: data ConnectOpts
+ Hpgsql.Internal.InternalTypes: data ConnectionString
+ Hpgsql.Internal.InternalTypes: data CopyInResponse
+ Hpgsql.Internal.InternalTypes: data CopyQueryState
+ Hpgsql.Internal.InternalTypes: data Either3 a b c
+ Hpgsql.Internal.InternalTypes: data ErrorDetail
+ Hpgsql.Internal.InternalTypes: data HPgConnection
+ Hpgsql.Internal.InternalTypes: data InternalConnectionState
+ Hpgsql.Internal.InternalTypes: data IrrecoverableHpgsqlError
+ Hpgsql.Internal.InternalTypes: data NoData
+ Hpgsql.Internal.InternalTypes: data NotificationResponse
+ Hpgsql.Internal.InternalTypes: data ParseComplete
+ Hpgsql.Internal.InternalTypes: data Pipeline a
+ Hpgsql.Internal.InternalTypes: data PostgresError
+ Hpgsql.Internal.InternalTypes: data Query
+ Hpgsql.Internal.InternalTypes: data QueryProtocol
+ Hpgsql.Internal.InternalTypes: data QueryState
+ Hpgsql.Internal.InternalTypes: data ResetConnectionOpts
+ Hpgsql.Internal.InternalTypes: data ResponseMsg
+ Hpgsql.Internal.InternalTypes: data ResponseMsgsReceived
+ Hpgsql.Internal.InternalTypes: data SingleQuery
+ Hpgsql.Internal.InternalTypes: data SingleQueryFragment
+ Hpgsql.Internal.InternalTypes: data TransactionStatus
+ Hpgsql.Internal.InternalTypes: data WeakThreadId
+ Hpgsql.Internal.InternalTypes: instance (GHC.Internal.Show.Show a, GHC.Internal.Show.Show b, GHC.Internal.Show.Show c) => GHC.Internal.Show.Show (Hpgsql.Internal.InternalTypes.Either3 a b c)
+ Hpgsql.Internal.InternalTypes: instance GHC.Classes.Eq Hpgsql.Internal.InternalTypes.ConnectionString
+ Hpgsql.Internal.InternalTypes: instance GHC.Classes.Eq Hpgsql.Internal.InternalTypes.CopyQueryState
+ Hpgsql.Internal.InternalTypes: instance GHC.Classes.Eq Hpgsql.Internal.InternalTypes.ErrorDetail
+ Hpgsql.Internal.InternalTypes: instance GHC.Classes.Eq Hpgsql.Internal.InternalTypes.HPgConnection
+ Hpgsql.Internal.InternalTypes: instance GHC.Classes.Eq Hpgsql.Internal.InternalTypes.NotificationResponse
+ Hpgsql.Internal.InternalTypes: instance GHC.Classes.Eq Hpgsql.Internal.InternalTypes.QueryId
+ Hpgsql.Internal.InternalTypes: instance GHC.Classes.Eq Hpgsql.Internal.InternalTypes.QueryProtocol
+ Hpgsql.Internal.InternalTypes: instance GHC.Classes.Eq Hpgsql.Internal.InternalTypes.SingleQueryFragment
+ Hpgsql.Internal.InternalTypes: instance GHC.Classes.Eq Hpgsql.Internal.InternalTypes.WeakThreadId
+ Hpgsql.Internal.InternalTypes: instance GHC.Classes.Ord Hpgsql.Internal.InternalTypes.ErrorDetail
+ Hpgsql.Internal.InternalTypes: instance GHC.Classes.Ord Hpgsql.Internal.InternalTypes.QueryId
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Base.Applicative Hpgsql.Internal.InternalTypes.Pipeline
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Base.Functor Hpgsql.Internal.InternalTypes.Pipeline
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Base.Monoid Hpgsql.Internal.InternalTypes.Query
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Base.Semigroup Hpgsql.Internal.InternalTypes.Query
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Data.String.IsString Hpgsql.Internal.InternalTypes.Query
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Enum.Enum Hpgsql.Internal.InternalTypes.QueryId
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Exception.Type.Exception Hpgsql.Internal.InternalTypes.IrrecoverableHpgsqlError
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Exception.Type.Exception Hpgsql.Internal.InternalTypes.PostgresError
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Num.Num Hpgsql.Internal.InternalTypes.QueryId
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.Internal.InternalTypes.BindComplete
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.Internal.InternalTypes.CommandComplete
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.Internal.InternalTypes.ConnectionString
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.Internal.InternalTypes.CopyInResponse
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.Internal.InternalTypes.CopyQueryState
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.Internal.InternalTypes.DataRow
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.Internal.InternalTypes.ErrorDetail
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.Internal.InternalTypes.ErrorResponse
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.Internal.InternalTypes.IrrecoverableHpgsqlError
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.Internal.InternalTypes.NoData
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.Internal.InternalTypes.NotificationResponse
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.Internal.InternalTypes.ParseComplete
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.Internal.InternalTypes.PostgresError
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.Internal.InternalTypes.Query
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.Internal.InternalTypes.QueryId
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.Internal.InternalTypes.QueryProtocol
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.Internal.InternalTypes.QueryState
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.Internal.InternalTypes.ReadyForQuery
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.Internal.InternalTypes.ResponseMsg
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.Internal.InternalTypes.ResponseMsgsReceived
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.Internal.InternalTypes.RowDescription
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.Internal.InternalTypes.SingleQuery
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.Internal.InternalTypes.SingleQueryFragment
+ Hpgsql.Internal.InternalTypes: instance GHC.Internal.Show.Show Hpgsql.Internal.InternalTypes.WeakThreadId
+ Hpgsql.Internal.InternalTypes: mkMutex :: IO Mutex
+ Hpgsql.Internal.InternalTypes: newtype CommandComplete
+ Hpgsql.Internal.InternalTypes: newtype DataRow
+ Hpgsql.Internal.InternalTypes: newtype EncodingContext
+ Hpgsql.Internal.InternalTypes: newtype ErrorResponse
+ Hpgsql.Internal.InternalTypes: newtype Mutex
+ Hpgsql.Internal.InternalTypes: newtype QueryId
+ Hpgsql.Internal.InternalTypes: newtype ReadyForQuery
+ Hpgsql.Internal.InternalTypes: newtype RowDescription
+ Hpgsql.Internal.InternalTypes: queryToByteString :: Query -> ByteString
+ Hpgsql.Internal.InternalTypes: renumberParamsFrom :: [SingleQueryFragment] -> Int -> (Int, [SingleQueryFragment])
+ Hpgsql.Internal.InternalTypes: throwIrrecoverableError :: MonadThrow m => Text -> m a
+ Hpgsql.Internal.PinnedByteArray: ByteStringIdx :: Int -> ByteStringIdx
+ Hpgsql.Internal.PinnedByteArray: [idx] :: ByteStringIdx -> Int
+ Hpgsql.Internal.PinnedByteArray: append :: LazyPinnedByteArray -> PinnedByteArray -> LazyPinnedByteArray
+ Hpgsql.Internal.PinnedByteArray: copyStrictSlice :: Int -> Int -> LazyPinnedByteArray -> PinnedByteArray
+ Hpgsql.Internal.PinnedByteArray: createPinnedByteArray' :: Int -> (Addr# -> IO (CInt, a)) -> IO (PinnedByteArray, a)
+ Hpgsql.Internal.PinnedByteArray: data LazyPinnedByteArray
+ Hpgsql.Internal.PinnedByteArray: data PinnedByteArray
+ Hpgsql.Internal.PinnedByteArray: decodeDataRow :: ByteStringIdx -> PinnedByteArray -> Either String ByteStringIdx
+ Hpgsql.Internal.PinnedByteArray: decodeInt16BE :: ByteStringIdx -> PinnedByteArray -> Either String Int16
+ Hpgsql.Internal.PinnedByteArray: decodeInt32BE :: ByteStringIdx -> PinnedByteArray -> Either String Int32
+ Hpgsql.Internal.PinnedByteArray: decodeInt64BE :: ByteStringIdx -> PinnedByteArray -> Either String Int64
+ Hpgsql.Internal.PinnedByteArray: decodeWord32BE :: ByteStringIdx -> PinnedByteArray -> Either String Word32
+ Hpgsql.Internal.PinnedByteArray: decodeWord64BE :: ByteStringIdx -> PinnedByteArray -> Either String Word64
+ Hpgsql.Internal.PinnedByteArray: decodeWord8 :: ByteStringIdx -> PinnedByteArray -> Either String Word8
+ Hpgsql.Internal.PinnedByteArray: drop :: Int -> PinnedByteArray -> PinnedByteArray
+ Hpgsql.Internal.PinnedByteArray: emptyLazyPBA :: LazyPinnedByteArray
+ Hpgsql.Internal.PinnedByteArray: emptyPBA :: PinnedByteArray
+ Hpgsql.Internal.PinnedByteArray: encodeDouble :: Double -> ByteString
+ Hpgsql.Internal.PinnedByteArray: encodeFloat :: Float -> ByteString
+ Hpgsql.Internal.PinnedByteArray: encodeInt16BE :: Int16 -> ByteString
+ Hpgsql.Internal.PinnedByteArray: encodeInt32BE :: Int32 -> ByteString
+ Hpgsql.Internal.PinnedByteArray: encodeInt64BE :: Int64 -> ByteString
+ Hpgsql.Internal.PinnedByteArray: encodePgBoolean :: Bool -> ByteString
+ Hpgsql.Internal.PinnedByteArray: fromByteString :: ByteString -> PinnedByteArray
+ Hpgsql.Internal.PinnedByteArray: fromChunks :: [PinnedByteArray] -> LazyPinnedByteArray
+ Hpgsql.Internal.PinnedByteArray: fromStrict :: PinnedByteArray -> LazyPinnedByteArray
+ Hpgsql.Internal.PinnedByteArray: instance GHC.Classes.Eq Hpgsql.Internal.PinnedByteArray.ByteStringIdx
+ Hpgsql.Internal.PinnedByteArray: instance GHC.Classes.Eq Hpgsql.Internal.PinnedByteArray.PinnedByteArray
+ Hpgsql.Internal.PinnedByteArray: instance GHC.Internal.Num.Num Hpgsql.Internal.PinnedByteArray.ByteStringIdx
+ Hpgsql.Internal.PinnedByteArray: instance GHC.Internal.Show.Show Hpgsql.Internal.PinnedByteArray.ByteStringIdx
+ Hpgsql.Internal.PinnedByteArray: instance GHC.Internal.Show.Show Hpgsql.Internal.PinnedByteArray.PinnedByteArray
+ Hpgsql.Internal.PinnedByteArray: lazyLength :: LazyPinnedByteArray -> Int
+ Hpgsql.Internal.PinnedByteArray: length :: PinnedByteArray -> Int
+ Hpgsql.Internal.PinnedByteArray: newtype ByteStringIdx
+ Hpgsql.Internal.PinnedByteArray: splitAt :: Int -> PinnedByteArray -> (PinnedByteArray, PinnedByteArray)
+ Hpgsql.Internal.PinnedByteArray: take :: Int -> PinnedByteArray -> PinnedByteArray
+ Hpgsql.Internal.PinnedByteArray: takePgMessageIdentAndLen :: LazyPinnedByteArray -> Maybe (Char, Int32)
+ Hpgsql.Internal.PinnedByteArray: toByteString :: PinnedByteArray -> ByteString
+ Hpgsql.Internal.PinnedByteArray: toStrict :: LazyPinnedByteArray -> PinnedByteArray
+ Hpgsql.Internal.PinnedByteArray: unsafeToUtf8Text :: ByteStringIdx -> Int -> PinnedByteArray -> Either String Text
+ Hpgsql.TypeInfo: DomainTypeDetails :: Oid -> DomainTypeDetails
+ Hpgsql.TypeInfo: [baseTypeOid] :: DomainTypeDetails -> Oid
+ Hpgsql.TypeInfo: newtype DomainTypeDetails
+ Hpgsql.Types: instance (Hpgsql.Encoding.Internal.FromPgRow a, Hpgsql.Encoding.Internal.FromPgRow b) => Hpgsql.Encoding.Internal.FromPgRow (a Hpgsql.Types.:. b)
+ Hpgsql.Types: instance (Hpgsql.Encoding.Internal.ToPgRow a, Hpgsql.Encoding.Internal.ToPgRow b) => Hpgsql.Encoding.Internal.ToPgRow (a Hpgsql.Types.:. b)
+ Hpgsql.Types: instance Data.Aeson.Types.FromJSON.FromJSON a => Hpgsql.Encoding.Internal.FromPgField (Hpgsql.Types.Aeson a)
+ Hpgsql.Types: instance Data.Aeson.Types.ToJSON.ToJSON a => Hpgsql.Encoding.Internal.ToPgField (Hpgsql.Types.Aeson a)
+ Hpgsql.Types: instance Hpgsql.Encoding.Internal.FromPgField Hpgsql.Types.PgJson
+ Hpgsql.Types: instance Hpgsql.Encoding.Internal.FromPgField a => Hpgsql.Encoding.Internal.FromPgField (Hpgsql.Types.PGArray a)
+ Hpgsql.Types: instance Hpgsql.Encoding.Internal.ToPgField a => Hpgsql.Encoding.Internal.ToPgField (Hpgsql.Types.PGArray a)
- Hpgsql.TypeInfo: DomainType :: TypeDetails
+ Hpgsql.TypeInfo: DomainType :: !DomainTypeDetails -> TypeDetails
Files
- CHANGELOG.md +20/−0
- hpgsql.cabal +4/−3
- src/Hpgsql.hs +1/−1
- src/Hpgsql/Builder.hs +5/−0
- src/Hpgsql/Connection.hs +1/−1
- src/Hpgsql/Encoding.hs +84/−1373
- src/Hpgsql/Encoding/BinarySerializer.hs +0/−175
- src/Hpgsql/Encoding/Internal.hs +2129/−0
- src/Hpgsql/Encoding/RowDecoderMonadic.hs +1/−1
- src/Hpgsql/Internal.hs +136/−86
- src/Hpgsql/Internal/InternalTypes.hs +503/−0
- src/Hpgsql/Internal/PinnedByteArray.hs +376/−0
- src/Hpgsql/InternalTypes.hs +0/−499
- src/Hpgsql/LanguageHaskell/ParseHaskellExpression.hs +16/−6
- src/Hpgsql/Locking.hs +1/−1
- src/Hpgsql/Msgs.hs +24/−26
- src/Hpgsql/Networking.hs +47/−14
- src/Hpgsql/Notification.hs +1/−1
- src/Hpgsql/ParsingInternal.hs +3/−1
- src/Hpgsql/Query.hs +2/−2
- src/Hpgsql/QueryInternal.hs +5/−4
- src/Hpgsql/ScramSHA256.hs +7/−1
- src/Hpgsql/SimpleParser.hs +177/−111
- src/Hpgsql/Transaction.hs +1/−1
- src/Hpgsql/TypeInfo.hs +12/−7
- src/Hpgsql/Types.hs +78/−31
CHANGELOG.md view
@@ -1,3 +1,23 @@+## v0.4.0++This is a performance improvement and bugfixing release, with very few features added. Users are highly encouraged to upgrade.++- `vALUES` was encoding the list in reverse order, and that has been fixed. Shouldn't have affected order-insensitive queries, but is embarrassing.+- Fix a connection hanging if postgres killed the connection abruptly, when either sending or receiving to/from the socket. Now a proper exception is thrown.+- Fix a parameter interpolation bug that caused an infinite loop at run time, before queries were even sent to postgres.+- Fix COPY deadlocking when trying to run syntactically invalid COPY statements.+- Fix wrong scale when encoding `numeric` values as per the postgres `scale(numeric)` function.+- Fix stale `transactionStatus` immediately after `putCopyError`.+- Add missing client nonce check in ScramSHA256 authentication.+- Throw informative exception instead of deadlocking os cross-query contamination when attempting to use documented-to-be-undefined-behaviour code patterns, that is: attempting to resume a Stream from a previous query _while_ another query is running will now throw an exception.+- Some (likely rare) cases of the SQL quasi quoter not throwing a good type error or RecordWildCards producing partial objects when uside inside `#{}` fixed.+- Support non-ASCII characters in roles' passwords.+- Added a `Monoid Query` instance.+- Domain types in both arrays and custom types can now be decoded, and `DomainType` now has more information in it.+- In terms of raw row decoding performance, we now beat Npgsql and are close to tokio-postgres in one benchmark. Read more below.++No breaking API changes in non-Internal modules in this version, but we now recommend using `fieldRowDecoder` instead of `singleField fieldDecoder` where you can. They're equivalent when compiling with -O1 due to rewrite rules, but the former is more performant when compiling with no optimizations.+ ## v0.3.0 - Support GHC 9.12 - Support OverloadedRecordDot inside the `sql` quasiquoter
hpgsql.cabal view
@@ -1,7 +1,7 @@ cabal-version: 2.0 name: hpgsql-version: 0.3.0+version: 0.4.0 synopsis: Pure Haskell PostgreSQL driver (no libpq) description: hpgsql is a pure Haskell implementation of a PostgreSQL driver category: Database@@ -33,9 +33,10 @@ Hpgsql.Copy Hpgsql.Encoding Hpgsql.Encoding.RowDecoderMonadic- Hpgsql.InternalTypes+ Hpgsql.Internal.InternalTypes Hpgsql.Notification Hpgsql.ParsingInternal+ Hpgsql.Internal.PinnedByteArray Hpgsql.Pipeline Hpgsql.Query Hpgsql.Time@@ -44,7 +45,7 @@ Hpgsql.Types other-modules: Hpgsql.Base- Hpgsql.Encoding.BinarySerializer+ Hpgsql.Encoding.Internal Hpgsql.Internal Hpgsql.LanguageHaskell.FromThExtension Hpgsql.LanguageHaskell.GhcParserOpts
src/Hpgsql.hs view
@@ -112,4 +112,4 @@ where import Hpgsql.Internal-import Hpgsql.InternalTypes (ErrorDetail (..), HPgConnection, IrrecoverableHpgsqlError (..), PostgresError (..))+import Hpgsql.Internal.InternalTypes (ErrorDetail (..), HPgConnection, IrrecoverableHpgsqlError (..), PostgresError (..))
src/Hpgsql/Builder.hs view
@@ -12,6 +12,8 @@ import qualified Data.ByteString.Builder.Extra as Builder import qualified Data.ByteString.Lazy as LBS import Data.Int (Int16, Int32, Int64)+import qualified Data.Text as Text+import Data.Text.Encoding (encodeUtf8) import Data.Word (Word8) data BinaryField = SqlNull | NotNull !ByteString@@ -52,6 +54,9 @@ string7 :: String -> LengthAwareBuilder string7 s = LengthAwareBuilder (fromIntegral $ length s) (Builder.string7 s)++nulTerminatedUtf8String :: String -> LengthAwareBuilder+nulTerminatedUtf8String s = let bs = encodeUtf8 (Text.pack s) in LengthAwareBuilder (fromIntegral $ BS.length bs) (Builder.byteString bs) <> word8 0 word8 :: Word8 -> LengthAwareBuilder word8 s = LengthAwareBuilder 1 (Builder.word8 s)
src/Hpgsql/Connection.hs view
@@ -49,7 +49,7 @@ import qualified Data.Text as Text import Data.Text.Encoding (encodeUtf8) import Hpgsql.Internal (closeForcefully, closeGracefully, connect, connectOpts, connectionIsClosed, defaultConnectOpts, getBackendPid, getParameterStatus, refreshTypeInfoCache, resetConnectionState, resetTypeInfoCache, withConnection, withConnectionOpts)-import Hpgsql.InternalTypes (ConnectOpts (..), ConnectionString (..), ResetConnectionOpts (..))+import Hpgsql.Internal.InternalTypes (ConnectOpts (..), ConnectionString (..), ResetConnectionOpts (..)) import Network.URI ( URI (..), URIAuth (..),
src/Hpgsql/Encoding.hs view
@@ -1,1373 +1,84 @@-{-# LANGUAGE UndecidableInstances #-}---- |------ = Encoding and decoding fields and rows------ This module contains a collection of functions, classes, and instances that can--- help you build encoders and decoders for your Haskell types.------ Here's an example:------ > data Person = Person { name :: Text, born :: Day, heightMeters :: Double }--- > deriving stock Generic--- > deriving anyclass FromPgRow--- >--- > persons :: [Person] <- query conn "SELECT * FROM persons"------ Note that Hpgsql's `RowDecoder` does not have a `Monad` instance because that allows it to--- type check query results and field counts only once per query. If you need--- to write a row decoder that is monadic (because decoding can change depending on the values--- of fields), check "Hpgsql.Encoding.RowDecoderMonadic".-module Hpgsql.Encoding- ( -- * Decoding- FromPgField (..),- FieldDecoder (..), -- TODO: Can we export ctor?- FieldInfo (..),- FromPgRow (..),- RowDecoder (..), -- TODO: Can we export ctor?- singleField,- nullableField,- genericFromPgRow,-- -- * Encoding-- -- | Keep in mind that Haskell's `Int` is an `Int64` on most- -- hardware, which is a mismatch for the commonly used 32-bit- -- `integer` PostgreSQL type.- -- This is not a problem when decoding because Hpgsql can decode- -- `integer` into Haskell's `Int`, but when encoding PostgreSQL- -- will understandably not accept a larger type.- ToPgField (..),- FieldEncoder (..),- ToPgRow (..),- RowEncoder (..),- EncodingContext (..),- genericToPgRow,-- -- * PostgreSQL enums- LowerCasedPgEnum (..),- genericEnumFieldDecoder,- genericEnumFieldEncoder,-- -- * PostgreSQL composite types- compositeTypeDecoder,- compositeTypeEncoder,-- -- * Driving PostgreSQL type inference- typeFieldDecoder,- typeFieldEncoder,- typeOidWithName,- typeMustBeNamed,-- -- * Others- rawBytesFieldDecoder,- untypedFieldEncoder,- toPgVectorField,- arrayField,- )-where--import Control.Monad (replicateM, unless, when)-import qualified Data.Aeson as Aeson-import Data.ByteString (ByteString)-import qualified Data.ByteString as BS-import qualified Data.ByteString.Char8 as BSC-import qualified Data.ByteString.Lazy as LBS-import Data.CaseInsensitive (CI)-import qualified Data.CaseInsensitive as CI-import Data.Coerce (coerce)-import Data.Fixed (divMod')-import Data.Functor.Contravariant (Contravariant (..))-import Data.Int (Int16, Int32, Int64)-import qualified Data.List as List-import Data.Map.Strict (Map)-import qualified Data.Map.Strict as Map-import Data.Maybe (fromMaybe)-import Data.Monoid (Sum (..))-import Data.Proxy (Proxy (..))-import Data.Ratio (Ratio)-import Data.Scientific (Scientific (..), floatingOrInteger, scientific)-import Data.Text (Text)-import qualified Data.Text as Text-import Data.Text.Encoding (decodeUtf8, encodeUtf8)-import qualified Data.Text.Lazy as LT-import qualified Data.Text.Lazy.Encoding as LT-import Data.Time (CalendarDiffDays (..), CalendarDiffTime (..), Day, LocalTime (..), NominalDiffTime, TimeOfDay, UTCTime (..), ZonedTime, diffDays, diffTimeToPicoseconds, fromGregorian, picosecondsToDiffTime, secondsToNominalDiffTime, timeOfDayToTime, timeToTimeOfDay, utc, utcToZonedTime, zonedTimeToUTC)-import Data.Time.Calendar.Julian (addJulianDurationClip, fromJulian)-import Data.Tuple.Only (Only (..))-import Data.UUID.Types (UUID)-import qualified Data.UUID.Types as UUID-import Data.Vector (Vector)-import qualified Data.Vector as Vector-import GHC.Float (castWord32ToFloat, castWord64ToDouble, expt, float2Double)-import GHC.Generics (C, D, Generic (..), K1 (..), M1 (..), Meta (MetaCons), U1 (..), (:*:) (..), (:+:) (..))-import GHC.TypeLits (KnownSymbol, TypeError, symbolVal)-import qualified GHC.TypeLits as TypeLits-import Hpgsql.Builder (BinaryField (..))-import qualified Hpgsql.Builder as Builder-import qualified Hpgsql.Encoding.BinarySerializer as BinSer-import qualified Hpgsql.SimpleParser as Parser-import Hpgsql.Time (Unbounded (..))-import Hpgsql.TypeInfo (EncodingContext (..), Oid (..), TypeDetails (..), TypeInfo (..), boolOid, byteaOid, charOid, dateOid, float4Oid, float8Oid, int2Oid, int4Oid, int8Oid, intervalOid, jsonOid, jsonbOid, lookupTypeByName, lookupTypeByOid, nameOid, numericOid, oidOid, textOid, timeOid, timestampOid, timestamptzOid, uuidOid, varcharOid, voidOid)--data FieldInfo = FieldInfo- { fieldTypeOid :: !Oid,- -- | The column name from the query's result, if available.- fieldName :: !(Maybe Text),- -- | The EncodingContext as of the moment the query ran.- encodingContext :: !EncodingContext- }---- | A decoder for a single field/column.-data FieldDecoder a = FieldDecoder- { fieldValueDecoder :: FieldInfo -> Maybe ByteString -> Either String a,- allowedPgTypes :: FieldInfo -> Bool- }- deriving stock (Functor)---- | `f1 <> f2` produces a `FieldDecoder` that tries `f1` first, and if that fails it tries `f2`.-instance Semigroup (FieldDecoder a) where- dec1 <> dec2 =- FieldDecoder- { fieldValueDecoder = \cInfo ->- let f1 = dec1.fieldValueDecoder cInfo- f2 = dec2.fieldValueDecoder cInfo- in \mbs ->- let cand1 = if dec1.allowedPgTypes cInfo then f1 mbs else Left "Not first parser"- cand2 = if dec2.allowedPgTypes cInfo then f2 mbs else Left "Not second parser"- in cand1 <> cand2,- allowedPgTypes = \cInfo -> dec1.allowedPgTypes cInfo || dec2.allowedPgTypes cInfo- }--data RowDecoder a = RowDecoder- { fullRowDecoder :: [FieldInfo] -> Parser.Parser a,- -- | Returns the same colInfos with a boolean indicating if- -- the expected types match for each colInfo.- rowColumnsTypeCheck :: [FieldInfo] -> [(FieldInfo, Bool)],- numExpectedColumns :: !Int- }- deriving stock (Functor, Generic)--instance Applicative RowDecoder where- pure v = RowDecoder (const $ pure v) (map (,True)) 0- {-# INLINE (<*>) #-} -- This is crucial for performance. It makes our CPS Parser truly compile to CPS row decoders.- RowDecoder p1 tc1 nc1 <*> RowDecoder p2 tc2 nc2 = RowDecoder (\colTypes -> let (cols1, cols2) = List.splitAt nc1 colTypes in p1 cols1 <*> p2 cols2) (\colTypes -> let (cols1, cols2) = List.splitAt nc1 colTypes in tc1 cols1 ++ tc2 cols2) (nc1 + nc2)--instance (TypeError (TypeLits.Text "RowDecoder does not have a Monad instance in Hpgsql because Hpgsql type-checks the result types of queries before having access to even the first data row. Use the Applicative class to write your instances or use the Monadic decoding variants.")) => Monad RowDecoder where- (>>=) = error "inaccessible bind in Monad RowDecoder instance"--singleField :: FieldDecoder a -> RowDecoder a-singleField (FieldDecoder {..}) =- RowDecoder- { fullRowDecoder = \case- [singleColInfo] ->- let decode = fieldValueDecoder singleColInfo- in do- lenNextCol <- fromIntegral <$> Parser.takeInt32BE- nextColBs <-- if lenNextCol >= 0- then- Just <$> Parser.take lenNextCol- else pure Nothing- case decode nextColBs of- Right v -> pure v- Left err -> fail err- _ -> error "singleField expected a single column OID but got 0 or >1",- rowColumnsTypeCheck = \case- [singleColInfo] -> [(singleColInfo, allowedPgTypes singleColInfo)]- _ -> error "singleField's rowColumnsTypeCheck expected a single column OID but got 0 or >1",- numExpectedColumns = 1- }--class FromPgField a where- fieldDecoder :: FieldDecoder a--class FromPgRow a where- rowDecoder :: RowDecoder a- default rowDecoder :: (Generic a, ProductTypeDecoder (Rep a)) => RowDecoder a- rowDecoder = genericFromPgRow---- | Allows you to create a @FieldDecoder@ for composite types.--- For a type such as:------ > CREATE TYPE int_and_bool AS (numfield INT, boolfield BOOL);------ You can define a Haskell type as such:------ > data IntAndBool = IntAndBool Int Bool--- >--- > instance FromPgField IntAndBool where--- > fieldDecoder = compositeTypeDecoder rowDecoder <&> \(i, b) -> IntAndBool i b-compositeTypeDecoder :: forall a. RowDecoder a -> FieldDecoder a-compositeTypeDecoder (RowDecoder {..}) =- FieldDecoder- { fieldValueDecoder = \compositeTypeOid -> \case- Nothing -> Left "Got NULL in composite type but it was not allowed"- Just bs -> case Parser.parseOnly (parserForRecord compositeTypeOid.encodingContext <* Parser.endOfInput) bs of- Parser.ParseOk v -> Right v- Parser.ParseFail err -> Left err,- allowedPgTypes = const True -- There's no way to enforce a custom type's OID. We only check if it's structurally the same in the parser (same subtypes in same order)- }- where- parserForRecord :: EncodingContext -> Parser.Parser a- parserForRecord encodingContext = do- -- From https://github.com/postgres/postgres/blob/50ba65e73325cf55fedb3e1f14673d816726923b/src/backend/utils/adt/rowtypes.c#L687- -- we can see a composite type's binary representation consists of: number of columns (Int32) + for_each_column { OID (Int32) + size_or_minus_1 (Int32) + Bytes }- numCols <- fromIntegral <$> Parser.takeInt32BE- unless (numCols == numExpectedColumns) $ fail $ "Composite type has " ++ show numCols ++ " attributes but parser expected " ++ show numExpectedColumns- let mkColInfo oid = FieldInfo oid Nothing encodingContext- cols <- replicateM numCols $ do- !oid <- Oid . fromIntegral <$> Parser.takeInt32BE- (sizeBs, !size) <- Parser.match $ fromIntegral <$> Parser.takeInt32BE- !bs <- Parser.take (max 0 size)- pure (oid, sizeBs <> bs)- let typecheckedCols = rowColumnsTypeCheck (map (mkColInfo . fst) cols)- unless (all snd typecheckedCols) $ fail $ "Parser for composite found type OIDs " ++ show (map fst cols) ++ " but expected different"- case Parser.parseOnly (fullRowDecoder (map (mkColInfo . fst) cols) <* Parser.endOfInput) (mconcat $ map snd cols) of- Parser.ParseOk v -> pure v- Parser.ParseFail err -> error $ "Error decoding composite type: " ++ show err---- | Allows you to create a @FieldEncoder@ for composite types.--- For a type such as:------ > CREATE TYPE int_and_bool AS (numfield INT, boolfield BOOL);------ You can define a Haskell type as such:------ > data IntAndBool = IntAndBool Int Bool--- >--- > instance ToPgField IntAndBool where--- > fieldEncoder = typeFieldEncoder (typeOidWithName "int_and_bool")--- > $ compositeTypeEncoder $ contramap (\(IntAndBool i b) -> (fromIntegral i :: Int32, b)) rowEncoder-compositeTypeEncoder :: forall a. RowEncoder a -> FieldEncoder a-compositeTypeEncoder rowEnc =- FieldEncoder- { toTypeOid = \_ -> Nothing,- toPgField = \encCtx -> \a ->- let fields = map (\f -> f encCtx) (rowEnc.toPgParams a)- numCols = Builder.int32BE (fromIntegral $ length fields)- encodeField (mOid, bf) =- let Oid oid = fromMaybe (Oid 0) mOid- in Builder.int32BE oid <> Builder.binaryField bf- in NotNull (Builder.toStrictByteString (numCols <> foldMap encodeField fields))- }--instance (FromPgField a) => FromPgRow (Only a) where- rowDecoder = Only <$> singleField fieldDecoder--instance (FromPgField a, FromPgField b) => FromPgRow (a, b) where- rowDecoder = (,) <$> singleField fieldDecoder <*> singleField fieldDecoder--instance (FromPgField a, FromPgField b, FromPgField c) => FromPgRow (a, b, c) where- rowDecoder = (,,) <$> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder--instance (FromPgField a, FromPgField b, FromPgField c, FromPgField d) => FromPgRow (a, b, c, d) where- rowDecoder = (,,,) <$> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder--instance (FromPgField a, FromPgField b, FromPgField c, FromPgField d, FromPgField e) => FromPgRow (a, b, c, d, e) where- rowDecoder = (,,,,) <$> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder--instance (FromPgField a, FromPgField b, FromPgField c, FromPgField d, FromPgField e, FromPgField f) => FromPgRow (a, b, c, d, e, f) where- rowDecoder = (,,,,,) <$> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder--instance (FromPgField a, FromPgField b, FromPgField c, FromPgField d, FromPgField e, FromPgField f, FromPgField g) => FromPgRow (a, b, c, d, e, f, g) where- rowDecoder = (,,,,,,) <$> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder--instance (FromPgField a, FromPgField b, FromPgField c, FromPgField d, FromPgField e, FromPgField f, FromPgField g, FromPgField h) => FromPgRow (a, b, c, d, e, f, g, h) where- rowDecoder = (,,,,,,,) <$> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder--instance (FromPgField a, FromPgField b, FromPgField c, FromPgField d, FromPgField e, FromPgField f, FromPgField g, FromPgField h, FromPgField i) => FromPgRow (a, b, c, d, e, f, g, h, i) where- rowDecoder = (,,,,,,,,) <$> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder--instance (FromPgField a, FromPgField b, FromPgField c, FromPgField d, FromPgField e, FromPgField f, FromPgField g, FromPgField h, FromPgField i, FromPgField j) => FromPgRow (a, b, c, d, e, f, g, h, i, j) where- rowDecoder = (,,,,,,,,,) <$> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder--instance (FromPgField a, FromPgField b, FromPgField c, FromPgField d, FromPgField e, FromPgField f, FromPgField g, FromPgField h, FromPgField i, FromPgField j, FromPgField k) => FromPgRow (a, b, c, d, e, f, g, h, i, j, k) where- rowDecoder = (,,,,,,,,,,) <$> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder--instance (FromPgField a, FromPgField b, FromPgField c, FromPgField d, FromPgField e, FromPgField f, FromPgField g, FromPgField h, FromPgField i, FromPgField j, FromPgField k, FromPgField l) => FromPgRow (a, b, c, d, e, f, g, h, i, j, k, l) where- rowDecoder = (,,,,,,,,,,,) <$> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder--instance (FromPgField a, FromPgField b, FromPgField c, FromPgField d, FromPgField e, FromPgField f, FromPgField g, FromPgField h, FromPgField i, FromPgField j, FromPgField k, FromPgField l, FromPgField m) => FromPgRow (a, b, c, d, e, f, g, h, i, j, k, l, m) where- rowDecoder = (,,,,,,,,,,,,) <$> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder <*> singleField fieldDecoder--data FieldEncoder a = FieldEncoder- { toTypeOid :: !(EncodingContext -> Maybe Oid),- toPgField :: !(EncodingContext -> a -> BinaryField)- }--instance Contravariant FieldEncoder where- contramap f fEnc = FieldEncoder {toTypeOid = fEnc.toTypeOid, toPgField = \encCtx -> let toF = fEnc.toPgField encCtx in \v -> toF (f v)}--class ToPgField a where- fieldEncoder :: FieldEncoder a---- | Allows you to specify a type for a FieldEncoder. This can be useful to avoid--- letting postgres infer types itself, which can cause errors. For example:------ > data MyEnum = Val1 | Val2 | Val3--- > myEnumFieldDecoderWithTypeInfoCheck :: FieldEncoder MyEnum--- > myEnumFieldDecoderWithTypeInfoCheck =--- > let convert = \case--- > Val1 -> "val1" :: Text--- > Val2 -> "val2"--- > Val3 -> "val3"--- > in typeFieldEncoder--- > (typeOidWithName "my_enum")--- > $ contramap convert fieldEncoder------ This will work unless you use non-default flags in your connection options.-typeFieldEncoder :: (EncodingContext -> Maybe Oid) -> FieldEncoder a -> FieldEncoder a-typeFieldEncoder ttoid enc = enc {toTypeOid = ttoid}--typeOidWithName :: Text -> (EncodingContext -> Maybe Oid)-typeOidWithName typName = \encCtx -> typeOid <$> lookupTypeByName typName encCtx.typeInfoCache--instance ToPgField Int where- fieldEncoder =- FieldEncoder- { toTypeOid = \_ -> Just haskellIntOid,- toPgField = \_ -> binaryIntEncoder- }--instance ToPgField Int16 where- fieldEncoder =- FieldEncoder- { toTypeOid = \_ -> Just int2Oid,- toPgField = \_ -> \n -> NotNull $ BinSer.encodeInt16BE n- }--instance ToPgField Int32 where- fieldEncoder =- FieldEncoder- { toTypeOid = \_ -> Just int4Oid,- toPgField = \_ -> \n -> NotNull $ BinSer.encodeInt32BE n- }--instance ToPgField Int64 where- fieldEncoder =- FieldEncoder- { toTypeOid = \_ -> Just int8Oid,- toPgField = \_ -> \n -> NotNull $ BinSer.encodeInt64BE n- }--instance ToPgField Integer where- fieldEncoder =- let fe = fieldEncoder @Scientific- in FieldEncoder- { toTypeOid = \_ -> Just numericOid,- toPgField = \encCtx -> \n -> fe.toPgField encCtx (fromIntegral n)- }--instance ToPgField (Ratio Integer) where- fieldEncoder =- let fe = fieldEncoder @Scientific- in FieldEncoder- { toTypeOid = \_ -> Just numericOid,- toPgField = \encCtx -> \r -> fe.toPgField encCtx (fromRational r)- }--instance ToPgField Oid where- fieldEncoder =- FieldEncoder- { toTypeOid = \_ -> Just oidOid,- toPgField = \_ -> \n -> NotNull $ BinSer.encodeInt32BE $ fromIntegral n- }--instance ToPgField Scientific where- fieldEncoder =- FieldEncoder- { toTypeOid = \_ -> Just numericOid,- toPgField = \_ -> \n ->- let sign = BinSer.encodeInt16BE $ if n >= 0 then 0 else 0x4000- -- The number is coeff * 10^exp, but we want it in base-10000 so we convert it to- -- new_coeff * 10^new_exp with new_exp a multiple of 4- base10000Expon = 4 * (base10Exponent n `div` 4)- base10000Coeff = coefficient n * expt 10 (base10Exponent n - base10000Expon)- ndigits, weight :: Int16- digits :: ByteString- (ndigits, weight, digits) = calculateDigits 0 0 (abs base10000Coeff) ""- dscale = BinSer.encodeInt16BE (abs $ fromIntegral base10000Expon) -- More than necessary, but safe?- in NotNull $ BinSer.encodeInt16BE ndigits <> BinSer.encodeInt16BE (weight - 1 + fromIntegral (base10000Expon `div` 4)) <> sign <> dscale <> digits- }- where- calculateDigits :: Int16 -> Int16 -> Integer -> BS.ByteString -> (Int16, Int16, BS.ByteString)- calculateDigits !ndigitsSoFar !weightSoFar 0 !encodedDigits = (ndigitsSoFar, weightSoFar, encodedDigits)- calculateDigits !ndigitsSoFar !weightSoFar !val !encodedDigits =- let (quotient, fromIntegral -> (rest :: Int16)) = val `divMod` 10000- in calculateDigits- (ndigitsSoFar + 1)- (weightSoFar + 1)- quotient- (BinSer.encodeInt16BE rest <> encodedDigits)--instance ToPgField Float where- fieldEncoder =- FieldEncoder- { toTypeOid = \_ -> Just float4Oid,- toPgField = \_ -> \n -> NotNull $ BinSer.encodeFloat n- }--instance ToPgField Double where- fieldEncoder =- FieldEncoder- { toTypeOid = \_ -> Just float8Oid,- toPgField = \_ -> \n -> NotNull $ BinSer.encodeDouble n- }--instance ToPgField Bool where- fieldEncoder =- FieldEncoder- { toTypeOid = \_ -> Just boolOid,- toPgField = \_ n -> NotNull $ BinSer.encodePgBoolean n- }--instance ToPgField Day where- -- PG Dates are Int32 number of days relative to 2000-01-01- -- https://github.com/postgres/postgres/blob/master/src/include/datatype/timestamp.h#L235- fieldEncoder =- FieldEncoder- { toTypeOid = \_ -> Just dateOid,- -- TODO: Catch integer overflow and do what?- toPgField = \_ d -> NotNull $ BinSer.encodeInt32BE $ fromIntegral $ diffDays d (fromGregorian 2000 1 1)- }--instance ToPgField (Unbounded Day) where- fieldEncoder =- let fe = fieldEncoder @Day- in FieldEncoder- { toTypeOid = fe.toTypeOid,- toPgField = \encCtx -> \case- NegInfinity -> NotNull $ BinSer.encodeInt32BE minBound- Finite v -> fe.toPgField encCtx v- PosInfinity -> NotNull $ BinSer.encodeInt32BE maxBound- }--instance ToPgField CalendarDiffTime where- fieldEncoder =- FieldEncoder- { toTypeOid = \_ -> Just intervalOid,- toPgField = \_ CalendarDiffTime {..} ->- let (days :: Int32, timeUnderOneDay) = ctTime `divMod'` 86_400- in NotNull $ BinSer.encodeInt64BE (round $ timeUnderOneDay * 1_000_000) <> BinSer.encodeInt32BE days <> BinSer.encodeInt32BE (fromIntegral ctMonths)- }--instance ToPgField NominalDiffTime where- fieldEncoder =- FieldEncoder- { toTypeOid = \_ -> Just intervalOid,- toPgField = \_ ndt ->- NotNull $ BinSer.encodeInt64BE (round $ ndt * 1_000_000) <> BinSer.encodeInt32BE 0 <> BinSer.encodeInt32BE 0- }--instance ToPgField UTCTime where- fieldEncoder =- FieldEncoder- { toTypeOid = \_ -> Just timestamptzOid,- -- TODO: Catch integer overflow and do what?- toPgField = \_ (UTCTime parsedDate timeinday) ->- let day :: Int64 = fromInteger $ parsedDate `diffDays` fromJulian 1999 12 19- totalusecs :: Int64 = 86_400_000_000 * day + fromInteger (diffTimeToPicoseconds timeinday `div` 1_000_000)- in NotNull $ BinSer.encodeInt64BE totalusecs- }--instance ToPgField (Unbounded UTCTime) where- fieldEncoder =- let fe = fieldEncoder @UTCTime- in FieldEncoder- { toTypeOid = fe.toTypeOid,- toPgField = \encCtx -> \case- NegInfinity -> NotNull $ BinSer.encodeInt64BE minBound- Finite v -> fe.toPgField encCtx v- PosInfinity -> NotNull $ BinSer.encodeInt64BE maxBound- }--instance ToPgField ZonedTime where- fieldEncoder =- let fe = fieldEncoder @UTCTime- in FieldEncoder- { toTypeOid = \_ -> Just timestamptzOid,- toPgField = \encCtx -> fe.toPgField encCtx . zonedTimeToUTC- }--instance ToPgField (Unbounded ZonedTime) where- fieldEncoder =- let fe = fieldEncoder @ZonedTime- in FieldEncoder- { toTypeOid = fe.toTypeOid,- toPgField = \encCtx -> \case- NegInfinity -> NotNull $ BinSer.encodeInt64BE minBound- Finite v -> fe.toPgField encCtx v- PosInfinity -> NotNull $ BinSer.encodeInt64BE maxBound- }--instance ToPgField LocalTime where- fieldEncoder =- FieldEncoder- { toTypeOid = \_ -> Just timestampOid,- toPgField = \_ (LocalTime localDay localTimeOfDay) ->- let day :: Int64 = fromInteger $ localDay `diffDays` fromJulian 1999 12 19- totalusecs :: Int64 = 86_400_000_000 * day + fromInteger (diffTimeToPicoseconds (timeOfDayToTime localTimeOfDay) `div` 1_000_000)- in NotNull $ BinSer.encodeInt64BE totalusecs- }--instance ToPgField TimeOfDay where- fieldEncoder =- FieldEncoder- { toTypeOid = \_ -> Just timeOid,- toPgField = \_ tod ->- let usecs :: Int64 = fromInteger $ diffTimeToPicoseconds (timeOfDayToTime tod) `div` 1_000_000- in NotNull $ BinSer.encodeInt64BE usecs- }--instance ToPgField Char where- fieldEncoder =- let fe = fieldEncoder @Text- in FieldEncoder- { toTypeOid = \_ -> Just textOid,- toPgField = \encCtx -> let !toTextField = fe.toPgField encCtx in \t -> toTextField $ Text.singleton t- }--instance ToPgField ByteString where- fieldEncoder =- FieldEncoder- { toTypeOid = \_ -> Just byteaOid,- toPgField = \_ -> \bs -> NotNull bs- }--instance ToPgField LBS.ByteString where- fieldEncoder =- let fe = fieldEncoder @ByteString- in FieldEncoder- { toTypeOid = \_ -> Just byteaOid,- toPgField = \encCtx -> fe.toPgField encCtx . LBS.toStrict- }--instance ToPgField Text where- fieldEncoder =- FieldEncoder- { toTypeOid = \_ -> Just textOid,- toPgField = \_ -> \t ->- let bs = encodeUtf8 t- in NotNull bs- }--instance ToPgField LT.Text where- fieldEncoder =- FieldEncoder- { toTypeOid = \_ -> Just textOid,- toPgField = \_ -> \t ->- let bs = LBS.toStrict $ LT.encodeUtf8 t- in NotNull bs- }--instance ToPgField String where- fieldEncoder =- let fe = fieldEncoder @Text- in FieldEncoder- { toTypeOid = \_ -> Just textOid,- toPgField = \encCtx -> fe.toPgField encCtx . Text.pack- }---- From https://hackage.haskell.org/package/case-insensitive-1.2.1.0/docs/Data-CaseInsensitive.html,--- "Note that the FoldCase instance for ByteStrings is only guaranteed to be correct for ISO-8859-1 encoded strings!".--- So we don't have those instances.---- | This instance does not work if you have fillTypeInfoCache disabled (that would be a non-default--- connection option).-instance ToPgField (CI Text) where- fieldEncoder = typeFieldEncoder (typeOidWithName "citext") $ contramap CI.original fieldEncoder---- | This instance does not work if you have fillTypeInfoCache disabled (that would be a non-default--- connection option).-instance ToPgField (CI LT.Text) where- fieldEncoder = typeFieldEncoder (typeOidWithName "citext") $ contramap CI.original fieldEncoder---- | This instance does not work if you have fillTypeInfoCache disabled (that would be a non-default--- connection option).-instance ToPgField (CI String) where- fieldEncoder = typeFieldEncoder (typeOidWithName "citext") $ contramap CI.original fieldEncoder--instance ToPgField UUID where- fieldEncoder =- FieldEncoder- { toTypeOid = \_ -> Just uuidOid,- toPgField = \_ -> NotNull . LBS.toStrict . UUID.toByteString- }--instance ToPgField Aeson.Value where- fieldEncoder =- FieldEncoder- { toTypeOid = \_ -> Just jsonbOid,- toPgField = \_ -> \v ->- let bs = BS.cons 1 (LBS.toStrict $ Aeson.encode v)- in NotNull bs- }--instance (ToPgField a) => ToPgField (Maybe a) where- fieldEncoder =- let fe = fieldEncoder @a- in FieldEncoder- { toTypeOid = fe.toTypeOid,- toPgField = \encCtx -> \case- Nothing -> SqlNull- Just n -> fe.toPgField encCtx n- }--instance (ToPgField a) => ToPgField (Vector a) where- fieldEncoder =- let fe = fieldEncoder @a- in FieldEncoder- { toTypeOid = \encodingContext -> do- -- Maybe monad- elOid <- fe.toTypeOid encodingContext- arrayTypInfo <- lookupTypeByOid elOid encodingContext.typeInfoCache- arrayTypInfo.oidOfArrayType,- toPgField = toPgVectorField- }--data RowEncoder a = RowEncoder- { toPgParams :: !(a -> [EncodingContext -> (Maybe Oid, BinaryField)]),- toTypeOids :: !(Proxy a -> [EncodingContext -> Maybe Oid]),- -- | This produces bytes for Binary COPY FROM STDIN rows, which can increase performance- -- and reduce memory usage comparing to deriving these bytes from `toPgParams`.- -- The produced bytes should not contain the total number of fields in the- -- beginning.- toBinaryCopyBytes :: !(EncodingContext -> a -> Builder.Builder)- }--instance Contravariant RowEncoder where- contramap f rec = RowEncoder (\v -> rec.toPgParams (f v)) (\_ -> rec.toTypeOids Proxy) (\encCtx -> let !toBytes = rec.toBinaryCopyBytes encCtx in \v -> toBytes (f v))---- | These are from `Divisible`, but we don't currently pull in the extra dependency that has that.-divide :: (a -> (b, c)) -> RowEncoder b -> RowEncoder c -> RowEncoder a-divide d re1 re2 =- RowEncoder- { toPgParams = \a -> let (b, c) = d a in re1.toPgParams b ++ re2.toPgParams c,- toTypeOids = \_ -> re1.toTypeOids Proxy ++ re2.toTypeOids Proxy,- toBinaryCopyBytes = \encCtx ->- let !toBytes1 = re1.toBinaryCopyBytes encCtx- !toBytes2 = re2.toBinaryCopyBytes encCtx- in \a -> let (b, c) = d a in toBytes1 b <> toBytes2 c- }--class ToPgRow a where- rowEncoder :: RowEncoder a- default rowEncoder :: (Generic a, ProductTypeEncoder (Rep a)) => RowEncoder a- rowEncoder = genericToPgRow--instance ToPgRow () where- rowEncoder = RowEncoder (\_ -> []) (\_ -> []) (\_ -> \_ -> mempty)--singleFieldRowEncoder :: forall a. (ToPgField a) => RowEncoder a-singleFieldRowEncoder =- let fe = fieldEncoder @a- in RowEncoder- { toPgParams = \a -> [\encodingContext -> (fe.toTypeOid encodingContext, fe.toPgField encodingContext a)],- toTypeOids = \_ -> [fe.toTypeOid],- toBinaryCopyBytes = \encCtx -> let !enc = fe.toPgField encCtx in \a -> Builder.binaryField $ enc a- }--instance (ToPgField a) => ToPgRow (Only a) where- rowEncoder = contramap fromOnly singleFieldRowEncoder--instance (ToPgField a, ToPgField b) => ToPgRow (a, b) where- rowEncoder = divide id singleFieldRowEncoder singleFieldRowEncoder--instance (ToPgField a, ToPgField b, ToPgField c) => ToPgRow (a, b, c) where- rowEncoder = divide (\(a, b, c) -> ((a, b), c)) rowEncoder singleFieldRowEncoder--instance (ToPgField a, ToPgField b, ToPgField c, ToPgField d) => ToPgRow (a, b, c, d) where- rowEncoder = divide (\(a, b, c, d) -> ((a, b), (c, d))) rowEncoder rowEncoder--instance (ToPgField a, ToPgField b, ToPgField c, ToPgField d, ToPgField e) => ToPgRow (a, b, c, d, e) where- rowEncoder = divide (\(a, b, c, d, e) -> ((a, b, c), (d, e))) rowEncoder rowEncoder--instance (ToPgField a, ToPgField b, ToPgField c, ToPgField d, ToPgField e, ToPgField f) => ToPgRow (a, b, c, d, e, f) where- rowEncoder = divide (\(a, b, c, d, e, f) -> ((a, b, c), (d, e, f))) rowEncoder rowEncoder--instance (ToPgField a, ToPgField b, ToPgField c, ToPgField d, ToPgField e, ToPgField f, ToPgField g) => ToPgRow (a, b, c, d, e, f, g) where- rowEncoder = divide (\(a, b, c, d, e, f, g) -> ((a, b, c), (d, e, f, g))) rowEncoder rowEncoder--instance (ToPgField a, ToPgField b, ToPgField c, ToPgField d, ToPgField e, ToPgField f, ToPgField g, ToPgField h) => ToPgRow (a, b, c, d, e, f, g, h) where- rowEncoder = divide (\(a, b, c, d, e, f, g, h) -> ((a, b, c, d), (e, f, g, h))) rowEncoder rowEncoder--instance (ToPgField a, ToPgField b, ToPgField c, ToPgField d, ToPgField e, ToPgField f, ToPgField g, ToPgField h, ToPgField i) => ToPgRow (a, b, c, d, e, f, g, h, i) where- rowEncoder = divide (\(a, b, c, d, e, f, g, h, i) -> ((a, b, c, d), (e, f, g, h, i))) rowEncoder rowEncoder--instance (ToPgField a, ToPgField b, ToPgField c, ToPgField d, ToPgField e, ToPgField f, ToPgField g, ToPgField h, ToPgField i, ToPgField j) => ToPgRow (a, b, c, d, e, f, g, h, i, j) where- rowEncoder = divide (\(a, b, c, d, e, f, g, h, i, j) -> ((a, b, c, d, e), (f, g, h, i, j))) rowEncoder rowEncoder--instance (ToPgField a, ToPgField b, ToPgField c, ToPgField d, ToPgField e, ToPgField f, ToPgField g, ToPgField h, ToPgField i, ToPgField j, ToPgField k) => ToPgRow (a, b, c, d, e, f, g, h, i, j, k) where- rowEncoder = divide (\(a, b, c, d, e, f, g, h, i, j, k) -> ((a, b, c, d, e, f), (g, h, i, j, k))) rowEncoder rowEncoder---- instance (ToPgField a) => ToPgRow [a] where--- rowEncoder = RowEncoder {--- toPgParams = \xs -> concatMap toPgParams xs--- , toTypeOids = \_ -> concatMap (\)--- } $ \cols -> map (\v encodingContext -> let typOid = toTypeOid (Proxy @a) encodingContext in (typOid, toPgField encodingContext v)) cols---- | The OID for `Data.Int`, which is machine dependent.-haskellIntOid :: Oid---- | All pg type OIDs that fit into Haskell's `Data.Int`, whose size is machine dependent.-haskellIntOids :: [Oid]-(haskellIntOid, haskellIntOids)- | (fromIntegral (maxBound @Int) :: Integer) > fromIntegral (maxBound @Int32) = (int8Oid, [int2Oid, int4Oid, int8Oid])- | (fromIntegral (maxBound @Int) :: Integer) > fromIntegral (maxBound @Int16) = (int4Oid, [int2Oid, int4Oid])- | otherwise = (int2Oid, [int2Oid])---- | Big-Endian binary encoder for Haskell's `Data.Int`, which is machine-dependent.-binaryIntEncoder :: Int -> BinaryField-binaryIntEncoder- | haskellIntOid == int8Oid = NotNull . BinSer.encodeInt64BE . fromIntegral- | haskellIntOid == int4Oid = NotNull . BinSer.encodeInt32BE . fromIntegral- | otherwise = NotNull . BinSer.encodeInt16BE . fromIntegral---- | Big-Endian binary decoder for Haskell's various IntXX types.-binaryIntDecoder :: forall a. (Integral a, Bounded a) => Oid -> ByteString -> Either String a-binaryIntDecoder typOid = \bs ->- if doesFit- then intDecoder bs- else Left $ "Chosen integral type does not fit every value for PG type with OID " ++ show typOid- where- maxBoundPgType :: Integer- intDecoder :: ByteString -> Either String a- (maxBoundPgType, intDecoder)- | typOid == int8Oid = (fromIntegral $ maxBound @Int64, fmap fromIntegral . BinSer.decodeInt64BE 0)- | typOid == int4Oid = (fromIntegral $ maxBound @Int32, fmap fromIntegral . BinSer.decodeInt32BE 0)- | typOid == int2Oid = (fromIntegral $ maxBound @Int16, fmap fromIntegral . BinSer.decodeInt16BE 0)- | otherwise = error "Bug in Hpgsql. Decoding binary integral type not an int2, int4 or int8"- doesFit = maxBoundPgType <= fromIntegral (maxBound @a)--binaryFloat4Decoder :: ByteString -> Float-binaryFloat4Decoder = castWord32ToFloat . either error id . BinSer.decodeWord32BE--binaryFloat8Decoder :: ByteString -> Double-binaryFloat8Decoder = castWord64ToDouble . either error id . BinSer.decodeWord64BE--parsePgType :: [Oid] -> (Maybe ByteString -> Either String a) -> FieldDecoder a-parsePgType !requiredTypeOids !fieldValueDecoder =- FieldDecoder- { fieldValueDecoder = \_oid -> fieldValueDecoder,- allowedPgTypes = (`elem` requiredTypeOids) . fieldTypeOid- }--instance FromPgField () where- fieldDecoder =- FieldDecoder- { fieldValueDecoder = \_oid -> \case- Just "" -> Right ()- Just bs -> Left $ "Invalid value '" ++ show bs ++ "' for postgres void type"- Nothing -> Left "Cannot decode SQL null as the Haskell () type. Use a `Maybe ()`",- allowedPgTypes = (== voidOid) . fieldTypeOid- }--instance FromPgField Int where- fieldDecoder =- FieldDecoder- { fieldValueDecoder = \FieldInfo {fieldTypeOid = oid} ->- let !decode = binaryIntDecoder oid- in \case- Just bs -> decode bs- Nothing -> Left "Cannot decode SQL null as the Haskell Int type. Use a `Maybe Int`",- allowedPgTypes = (`elem` haskellIntOids) . fieldTypeOid- }--instance FromPgField Int16 where- fieldDecoder =- FieldDecoder- { fieldValueDecoder = \FieldInfo {fieldTypeOid = oid} ->- let !decode = binaryIntDecoder oid- in \case- Just bs -> decode bs- Nothing -> Left "Cannot decode SQL null as the Haskell Int16 type. Use a `Maybe Int16`",- allowedPgTypes = (== int2Oid) . fieldTypeOid- }--instance FromPgField Int32 where- fieldDecoder =- FieldDecoder- { fieldValueDecoder = \FieldInfo {fieldTypeOid = oid} ->- let !decode = binaryIntDecoder oid- in \case- Just bs -> decode bs- Nothing -> Left "Cannot decode SQL null as the Haskell Int32 type. Use a `Maybe Int32`",- allowedPgTypes = (`elem` [int2Oid, int4Oid]) . fieldTypeOid- }--instance FromPgField Int64 where- fieldDecoder =- FieldDecoder- { fieldValueDecoder = \FieldInfo {fieldTypeOid = oid} ->- let !decode = binaryIntDecoder oid- in \case- Just bs -> decode bs- Nothing -> Left "Cannot decode SQL null as the Haskell Int64 type. Use a `Maybe Int64`",- allowedPgTypes = (`elem` [int2Oid, int4Oid, int8Oid]) . fieldTypeOid- }--instance FromPgField Integer where- fieldDecoder =- FieldDecoder- { fieldValueDecoder = \FieldInfo {fieldTypeOid = oid} ->- let !decodeInt = binaryIntDecoder @Int64 oid- in \case- Just bs- | oid /= numericOid -> fromIntegral <$> decodeInt bs- | otherwise -> case Parser.parseOnly (scientificDecoder True <* Parser.endOfInput) bs of- Parser.ParseOk sci -> case floatingOrInteger @Double @Integer sci of- Right i -> Right i- Left _ -> Left "Internal error in Hpgsql. Scientific to Integer conversion failed"- Parser.ParseFail err -> Left err- Nothing -> Left "Cannot decode SQL null as the Haskell Integer type. Use a `Maybe Integer`",- allowedPgTypes = (`elem` [int8Oid, numericOid, int4Oid, int2Oid]) . fieldTypeOid- }--instance FromPgField Oid where- fieldDecoder =- FieldDecoder- { fieldValueDecoder = \_ -> \case- -- Oids are just int4- Just bs -> Oid <$> binaryIntDecoder int4Oid bs- Nothing -> Left "Cannot decode SQL null as the Haskell Oid type. Use a `Maybe Oid`",- allowedPgTypes = (== oidOid) . fieldTypeOid- }--instance FromPgField Float where- fieldDecoder = parsePgType [float4Oid] $ \case- Just bs -> Right $ binaryFloat4Decoder bs- Nothing -> Left "Cannot decode SQL null as the Haskell Float type. Use a `Maybe Float`"--instance FromPgField Double where- fieldDecoder =- FieldDecoder- { fieldValueDecoder = \FieldInfo {fieldTypeOid = oid} ->- let !decoder- | oid == float8Oid = binaryFloat8Decoder- | otherwise = float2Double . binaryFloat4Decoder- in \case- Just bs -> Right $ decoder bs- Nothing -> Left "Cannot decode SQL null as the Haskell Double type. Use a `Maybe Double`",- allowedPgTypes = (`elem` [float8Oid, float4Oid]) . fieldTypeOid- }---- | Allows you to specify a type (and other checks, possibly) for a `FieldDecoder`.--- This can be useful to ensure you're not accidentally decoding a different type.------ > data MyEnum = Val1 | Val2 | Val3--- > myEnumFieldDecoderWithTypeInfoCheck :: FieldDecoder MyEnum--- > myEnumFieldDecoderWithTypeInfoCheck =--- > let convert = \case--- > "val1" -> Val1--- > "val2" -> Val2--- > "val3" -> Val3--- > _ -> error "Invalid value for MyEnum"--- > in typeFieldDecoder--- > (typeMustBeNamed "my_enum")--- > $ convert <$> rawBytesFieldDecoder------ This will work unless you use non-default flags in your connection options.-typeFieldDecoder :: (FieldInfo -> Bool) -> FieldDecoder a -> FieldDecoder a-typeFieldDecoder fieldCheck dec = dec {allowedPgTypes = fieldCheck}--typeMustBeNamed :: Text -> (FieldInfo -> Bool)-typeMustBeNamed typName = \fieldInfo ->- (typeName <$> lookupTypeByOid fieldInfo.fieldTypeOid fieldInfo.encodingContext.typeInfoCache) == Just typName--scientificDecoder :: Bool -> Parser.Parser Scientific-scientificDecoder mustBeInteger = do- ndigits <- Parser.takeInt16BE- weight <- Parser.takeInt16BE- sign <- Parser.takeInt16BE -- 0x0000 is positive, 0x4000 is negative, 0xC000 is NAN, 0xD000 is Positive Infinity, 0xF000 is Negative Infinity- unless (sign == 0x0000 || sign == 0x4000) $ fail "NaN, positive or negative infinities cannot be decoded into Integer or Scientific"- !dscale <- Parser.takeInt16BE- when (mustBeInteger && dscale /= 0) $ fail "Decoding into `Integer` requires explicit casting with `numeric(X,0)` to force integral values"- valueAbs <- parseAndMult ndigits (fromIntegral weight * 4) 0- pure $ (if sign == 0x0000 then 1 else (-1)) * valueAbs- where- parseAndMult :: Int16 -> Int -> Scientific -> Parser.Parser Scientific- parseAndMult 0 _ !val = pure val- parseAndMult !ndigitsLeft !currexpon !val = do- !digit <- fromIntegral <$> Parser.takeInt16BE- parseAndMult (ndigitsLeft - 1) (currexpon - 4) (val + scientific digit currexpon)--instance FromPgField Scientific where- -- See https://github.com/postgres/postgres/blob/799959dc7cf0e2462601bea8d07b6edec3fa0c4f/src/backend/utils/adt/numeric.c#L1163- fieldDecoder =- FieldDecoder- { fieldValueDecoder = \FieldInfo {fieldTypeOid = oid} ->- let !decodeInt = binaryIntDecoder @Int64 oid- in \case- Just bs ->- -- TODO: There is loss converting from Float/Double to Scientific, but it might be quite small, so should we accept- -- float4Oid and float8Oid here?- if oid == numericOid- then case Parser.parseOnly (scientificDecoder False <* Parser.endOfInput) bs of- Parser.ParseOk sci -> Right sci- Parser.ParseFail err -> Left err- else flip scientific 0 . fromIntegral <$> decodeInt bs- Nothing -> Left "Cannot decode SQL null as the Haskell Scientific type. Use a `Maybe Scientific`",- allowedPgTypes = (`elem` [numericOid, int2Oid, int4Oid, int8Oid]) . fieldTypeOid- }--instance FromPgField (Ratio Integer) where- fieldDecoder = toRational <$> fieldDecoder @Scientific--binaryTrue :: ByteString-binaryTrue = BinSer.encodePgBoolean True--instance FromPgField Bool where- fieldDecoder = parsePgType [boolOid] $ \case- Just bs -> Right $ bs == binaryTrue- Nothing -> Left "Cannot decode SQL null as the Haskell Bool type. Use a `Maybe Bool`"--instance FromPgField Char where- fieldDecoder =- let textParser = fieldValueDecoder (fieldDecoder @Text)- in FieldDecoder- { fieldValueDecoder = \colInfo@FieldInfo {fieldTypeOid = oid} ->- let !decodeText = textParser colInfo- in \mbs -> case mbs of- Just bs ->- if oid == charOid- -- TODO: Postgres has values of type "char" in the pg_type.typcategory table.- -- We should test this instance works with those, and we haven't yet.- then Right $ BSC.head bs- else case decodeText mbs of- Left err -> Left err- Right t -> if Text.length t > 1 then Left "Cannot parse text with more than one character into a Haskell Char type." else Right (Text.head t)- Nothing -> Left "Cannot decode SQL null as the Haskell Char type. Use a `Maybe Char`",- -- TODO: All the varchar types?- allowedPgTypes = (`elem` [charOid, textOid]) . fieldTypeOid- }--instance FromPgField ByteString where- fieldDecoder = parsePgType [byteaOid] $ \case- Just bs -> Right bs- Nothing -> Left "Cannot decode SQL null as the Haskell ByteString type. Use a `Maybe ByteString`"--instance FromPgField LBS.ByteString where- fieldDecoder = parsePgType [byteaOid] $ \case- Just bs -> Right $ LBS.fromStrict bs- Nothing -> Left "Cannot decode SQL null as the Haskell ByteString type. Use a `Maybe ByteString`"--instance FromPgField Text where- fieldDecoder = parsePgType [textOid, varcharOid, nameOid] $ \case- Just bs -> Right $ decodeUtf8 bs- -- TODO: Use some faster unsafeDecodeUtf8 function?- Nothing -> Left "Cannot decode SQL null as the Haskell Text type. Use a `Maybe Text`"--instance FromPgField LT.Text where- fieldDecoder = parsePgType [textOid, varcharOid, nameOid] $ \case- Just bs -> Right $ LT.fromStrict $ decodeUtf8 bs- -- TODO: Use some faster unsafeDecodeUtf8 function?- Nothing -> Left "Cannot decode SQL null as the Haskell Text type. Use a `Maybe Text`"--instance FromPgField String where- fieldDecoder = parsePgType [textOid, varcharOid, nameOid] $ \case- -- connection option).- Just bs -> Right $ Text.unpack $ decodeUtf8 bs- -- TODO: Use some faster unsafeDecodeUtf8 function?- Nothing -> Left "Cannot decode SQL null as the Haskell String type. Use a `Maybe String`"---- | This instance does not work if you have fillTypeInfoCache disabled (that would be a non-default--- connection option).-instance FromPgField (CI Text) where- fieldDecoder = typeFieldDecoder (typeMustBeNamed "citext") $ CI.mk <$> fieldDecoder---- | This instance does not work if you have fillTypeInfoCache disabled (that would be a non-default--- connection option).-instance FromPgField (CI LT.Text) where- fieldDecoder = typeFieldDecoder (typeMustBeNamed "citext") $ CI.mk <$> fieldDecoder---- | This instance does not work if you have fillTypeInfoCache disabled (that would be a non-default--- connection option).-instance FromPgField (CI String) where- fieldDecoder = typeFieldDecoder (typeMustBeNamed "citext") $ CI.mk <$> fieldDecoder--instance FromPgField UTCTime where- fieldDecoder = parsePgType [timestamptzOid] $ \case- Just bs -> do- -- See https://github.com/postgres/postgres/blob/50cb7505b3010736b9a7922e903931534785f3aa/src/backend/utils/adt/timestamp.c#L1909- totalusecs <- BinSer.decodeInt64BE 0 bs- let (day, timeusecs) = totalusecs `divMod` 86_400_000_000 -- USECS per day- parsedDate = addJulianDurationClip (CalendarDiffDays 0 (fromIntegral day)) $ fromJulian 1999 12 19- Right $ UTCTime parsedDate (picosecondsToDiffTime $ fromIntegral timeusecs * 1_000_000)- Nothing -> Left "Cannot decode SQL null as the Haskell UTCTime type. Use a `Maybe UTCTime`"--instance FromPgField (Unbounded UTCTime) where- fieldDecoder = parsePgType [timestamptzOid] $ \case- Just bs -> do- -- See https://github.com/postgres/postgres/blob/50cb7505b3010736b9a7922e903931534785f3aa/src/backend/utils/adt/timestamp.c#L1909- totalusecs <- BinSer.decodeInt64BE 0 bs- Right $- if totalusecs == minBound- then NegInfinity- else- if totalusecs == maxBound- then PosInfinity- else- let (day, timeusecs) = totalusecs `divMod` 86_400_000_000 -- USECS per day- parsedDate = addJulianDurationClip (CalendarDiffDays 0 (fromIntegral day)) $ fromJulian 1999 12 19- in Finite $ UTCTime parsedDate (picosecondsToDiffTime $ fromIntegral timeusecs * 1_000_000)- Nothing -> Left "Cannot decode SQL null as the Haskell (Unbounded UTCTime) type. Use a `Maybe (Unbounded UTCTime)`"--instance FromPgField ZonedTime where- fieldDecoder = parsePgType [timestamptzOid] $ \case- Just bs -> do- -- See https://github.com/postgres/postgres/blob/50cb7505b3010736b9a7922e903931534785f3aa/src/backend/utils/adt/timestamp.c#L1909- totalusecs <- BinSer.decodeInt64BE 0 bs- let (day, timeusecs) = totalusecs `divMod` 86_400_000_000 -- USECS per day- parsedDate = addJulianDurationClip (CalendarDiffDays 0 (fromIntegral day)) $ fromJulian 1999 12 19- Right $ utcToZonedTime utc $ UTCTime parsedDate (picosecondsToDiffTime $ fromIntegral timeusecs * 1_000_000)- Nothing -> Left "Cannot decode SQL null as the Haskell ZonedTime type. Use a `Maybe ZonedTime`"--instance FromPgField (Unbounded ZonedTime) where- fieldDecoder = parsePgType [timestamptzOid] $ \case- Just bs -> do- -- See https://github.com/postgres/postgres/blob/50cb7505b3010736b9a7922e903931534785f3aa/src/backend/utils/adt/timestamp.c#L1909- totalusecs <- BinSer.decodeInt64BE 0 bs- Right $- if totalusecs == minBound- then NegInfinity- else- if totalusecs == maxBound- then PosInfinity- else- let (day, timeusecs) = totalusecs `divMod` 86_400_000_000 -- USECS per day- parsedDate = addJulianDurationClip (CalendarDiffDays 0 (fromIntegral day)) $ fromJulian 1999 12 19- in Finite $ utcToZonedTime utc $ UTCTime parsedDate (picosecondsToDiffTime $ fromIntegral timeusecs * 1_000_000)- Nothing -> Left "Cannot decode SQL null as the Haskell ZonedTime type. Use a `Maybe ZonedTime`"--instance FromPgField LocalTime where- fieldDecoder = parsePgType [timestampOid] $ \case- Just bs -> do- totalusecs <- BinSer.decodeInt64BE 0 bs- let (day, timeusecs) = totalusecs `divMod` 86_400_000_000 -- USECS per day- parsedDate = addJulianDurationClip (CalendarDiffDays 0 (fromIntegral day)) $ fromJulian 1999 12 19- Right $ LocalTime parsedDate (timeToTimeOfDay $ picosecondsToDiffTime $ fromIntegral timeusecs * 1_000_000)- Nothing -> Left "Cannot decode SQL null as the Haskell LocalTime type. Use a `Maybe LocalTime`"--instance FromPgField TimeOfDay where- fieldDecoder = parsePgType [timeOid] $ \case- Just bs -> do- usecs <- BinSer.decodeInt64BE 0 bs- Right $ timeToTimeOfDay $ picosecondsToDiffTime $ fromIntegral usecs * 1_000_000- Nothing -> Left "Cannot decode SQL null as the Haskell TimeOfDay type. Use a `Maybe TimeOfDay`"--instance FromPgField Day where- fieldDecoder = parsePgType [dateOid] $ \case- Just bs -> do- -- There is a very specific conversion function for these, which I poorly translated to Haskell- -- https://github.com/postgres/postgres/blob/799959dc7cf0e2462601bea8d07b6edec3fa0c4f/src/backend/utils/adt/datetime.c#L321- -- But I found a simpler way to do this. Let's see if it works in our property based tests- jd <- BinSer.decodeInt32BE 0 bs- Right $ addJulianDurationClip (CalendarDiffDays 0 (fromIntegral jd - 13)) $ fromJulian 2000 01 01- Nothing -> Left "Cannot decode SQL null as the Haskell Day type. Use a `Maybe Day`"--instance FromPgField (Unbounded Day) where- fieldDecoder = parsePgType [dateOid] $ \case- Just bs -> do- -- There is a very specific conversion function for these, which I poorly translated to Haskell- -- https://github.com/postgres/postgres/blob/799959dc7cf0e2462601bea8d07b6edec3fa0c4f/src/backend/utils/adt/datetime.c#L321- -- But I found a simpler way to do this. Let's see if it works in our property based tests- jd <- BinSer.decodeInt32BE 0 bs- Right $- if jd == minBound- then NegInfinity- else- if jd == maxBound- then PosInfinity- else- Finite $ addJulianDurationClip (CalendarDiffDays 0 (fromIntegral jd - 13)) $ fromJulian 2000 01 01- Nothing -> Left "Cannot decode SQL null as the Haskell (Unbounded Day) type. Use a `Maybe (Unbounded Day)`"--instance FromPgField CalendarDiffTime where- fieldDecoder = parsePgType [intervalOid] $ \case- Just bs -> do- nMicrosecs <- BinSer.decodeInt64BE 0 bs- nDays <- BinSer.decodeInt32BE 8 bs- nMonths <- BinSer.decodeInt32BE 12 bs- Right $ CalendarDiffTime {ctMonths = fromIntegral nMonths, ctTime = secondsToNominalDiffTime (fromIntegral nDays * 86400) + realToFrac (picosecondsToDiffTime (fromIntegral nMicrosecs * 1_000_000))}- Nothing -> Left "Cannot decode SQL null as the Haskell CalendarDiffTime type. Use a `Maybe CalendarDiffTime`"--instance FromPgField UUID where- fieldDecoder = parsePgType [uuidOid] $ \case- Just bs -> case UUID.fromByteString (LBS.fromStrict bs) of- Just uuid -> Right uuid- Nothing -> Left "Bug in Hpgsql: UUID field could not be decoded"- Nothing -> Left "Cannot decode SQL null as the Haskell UUID type. Use a `Maybe UUID`"--instance FromPgField Aeson.Value where- fieldDecoder =- FieldDecoder- { fieldValueDecoder =- \FieldInfo {fieldTypeOid} ->- let- -- jsonb has a byte prepended to the contents and json does not- !fixJsonb = if fieldTypeOid == jsonbOid then BS.drop 1 else Prelude.id- in- \case- Just bs -> case Aeson.decodeStrict $ fixJsonb bs of- Just d -> Right d- Nothing -> Left "Bug in Hpgsql. Postgres produced a json or jsonb value that Aeson does not consider valid."- Nothing -> Left "Cannot decode SQL null as the Haskell Aeson.Value type. Use a `Maybe Aeson.Value` if you want SQL nulls",- allowedPgTypes = (`elem` [jsonOid, jsonbOid]) . fieldTypeOid- }---- | A FieldDecoder that accepts and decodes SQL NULLs into `Nothing` values--- for a given decoder.-nullableField :: FieldDecoder a -> FieldDecoder (Maybe a)-nullableField FieldDecoder {..} =- FieldDecoder- { fieldValueDecoder = \oid ->- let !origFieldValueParser = fieldValueDecoder oid- in \case- Nothing -> Right Nothing- justBs -> Just <$> origFieldValueParser justBs,- allowedPgTypes- }--instance (FromPgField a) => FromPgField (Maybe a) where- fieldDecoder = nullableField fieldDecoder--allowOnlyArrayTypes :: FieldInfo -> Bool-allowOnlyArrayTypes fieldInfo =- -- TODO: We could check the elemTypeOid too, but maybe later- case lookupTypeByOid fieldInfo.fieldTypeOid fieldInfo.encodingContext.typeInfoCache of- Just (TypeInfo {typeDetails = ArrayType _}) -> True- Nothing -> True -- Assume user knows what they're doing- Just _ -> False -- Definitely not an array--instance forall a. (FromPgField a) => FromPgField (Vector a) where- fieldDecoder = arrayField Vector.replicateM fieldDecoder--instance {-# OVERLAPPING #-} forall a. (FromPgField a) => FromPgField (Vector (Vector a)) where- -- From https://github.com/postgres/postgres/blob/5941946d0934b9eccb0d5bfebd40b155249a0130/src/backend/utils/adt/arrayfuncs.c#L1548- fieldDecoder =- FieldDecoder- { fieldValueDecoder = \colInfo ->- let !arrayFieldDecoder = arrayParser colInfo.encodingContext <* Parser.endOfInput- in \case- Nothing -> Left "Cannot decode SQL null as the Haskell Vector type. Use a `Maybe (Vector (Vector a))`"- Just bs -> case Parser.parseOnly arrayFieldDecoder bs of- Parser.ParseOk v -> Right v- Parser.ParseFail err -> Left err,- allowedPgTypes = allowOnlyArrayTypes- }- where- !elementParser = fieldDecoder @a- arrayParser :: EncodingContext -> Parser.Parser (Vector (Vector a))- arrayParser encodingContext = do- !ndim <- Parser.takeInt32BE- !_hasNull <- Parser.takeInt32BE- !elementTypeOid :: Oid <- Oid . fromIntegral <$> Parser.takeInt32BE- let !elementColInfo = FieldInfo elementTypeOid Nothing encodingContext- when (ndim /= 2) $ fail $ "TODO: No support for " ++ show ndim ++ "-dimensional arrays in Hpgsql. Got array with ndim=" ++ show ndim- unless (elementParser.allowedPgTypes elementColInfo) $ fail $ "Array contains elements of type OID " ++ show elementTypeOid ++ " but decoder does not handle that type"- numRows <- do- !dim_i :: Int <- fromIntegral <$> Parser.takeInt32BE- !_lb_i <- Parser.takeInt32BE- pure dim_i- lengthEachRow <- do- !dim_i :: Int <- fromIntegral <$> Parser.takeInt32BE- !_lb_i <- Parser.takeInt32BE- pure dim_i-- Vector.replicateM numRows $ do- Vector.replicateM lengthEachRow $- do- size :: Int <- fromIntegral <$> Parser.takeInt32BE- elementBs <- if size == (-1) then pure Nothing else Just <$> Parser.take size- case elementParser.fieldValueDecoder elementColInfo elementBs of- Left err -> fail $ "Error parsing array element: " ++ show err- Right el -> pure el---- | Derives `FromPgRow` generically.-genericFromPgRow :: forall a. (Generic a, ProductTypeDecoder (Rep a)) => RowDecoder a-genericFromPgRow = to <$> genRowDecoder @(Rep a)--class ProductTypeDecoder f where- genRowDecoder :: RowDecoder (f a)--instance (ProductTypeDecoder a, ProductTypeDecoder b) => ProductTypeDecoder (a :*: b) where- {-# INLINE genRowDecoder #-}- genRowDecoder = (:*:) <$> genRowDecoder <*> genRowDecoder--instance (ProductTypeDecoder f) => ProductTypeDecoder (M1 a c f) where- {-# INLINE genRowDecoder #-}- genRowDecoder = M1 <$> genRowDecoder--instance (FromPgField a) => ProductTypeDecoder (K1 r a) where- {-# INLINE genRowDecoder #-}- -- coercing instead of fmap reduces memory usage, apparently- -- by reducing (unnecessary) closures in the final row decoder,- -- as per looking at GHC Core- genRowDecoder = coerce $ singleField $ fieldDecoder @a--genericToPgRow :: forall a. (Generic a, ProductTypeEncoder (Rep a)) => RowEncoder a-genericToPgRow = contramap from genRowEncoder--class ProductTypeEncoder f where- genRowEncoder :: RowEncoder (f a)--instance (ProductTypeEncoder a, ProductTypeEncoder b) => ProductTypeEncoder (a :*: b) where- genRowEncoder = divide (\(a :*: b) -> (a, b)) genRowEncoder genRowEncoder--instance (ProductTypeEncoder f) => ProductTypeEncoder (M1 i c f) where- genRowEncoder = contramap unM1 genRowEncoder--instance (ToPgField a) => ProductTypeEncoder (K1 r a) where- genRowEncoder = contramap unK1 singleFieldRowEncoder---- | For the very common case of a Haskell enum matching a custom postgres enum type--- that has its values all as lower case strings, this newtype can help you derive--- instances as such:------ > data Mood = Sad | Ok | Happy--- > deriving stock (Generic)--- > deriving (FromPgField, ToPgField) via (LowerCasedPgEnum Mood)------ And this would match the Postgres equivalent:------ > CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');------ If you run into PostgreSQL type inference problems with this, you can--- write instances manually with 'genericEnumFieldDecoder', 'genericEnumFieldEncoder',--- 'typeFieldEncoder', and 'typeFieldDecoder'.-newtype LowerCasedPgEnum a = LowerCasedPgEnum a--instance (Generic a, EnumDecoder (Rep a)) => FromPgField (LowerCasedPgEnum a) where- fieldDecoder = LowerCasedPgEnum <$> genericEnumFieldDecoder LT.toLower--instance (Generic a, EnumEncoder (Rep a)) => ToPgField (LowerCasedPgEnum a) where- fieldEncoder = untypedFieldEncoder $ \_encCtx -> \(LowerCasedPgEnum v) -> NotNull $ genericEnumFieldEncoder Text.toLower v---- | One of the functions behind 'LowerCasedPgEnum', but you can decide--- how to map your type's constructor names arbitrarily, which can be--- useful if you're not using lowercase values in your postgres enums.-genericEnumFieldDecoder ::- forall a.- (Generic a, EnumDecoder (Rep a)) =>- -- | A function that takes in the Haskell constructor name and returns the textual representation of the enum in postgres- (LT.Text -> LT.Text) ->- FieldDecoder a-genericEnumFieldDecoder nameTransform = fromMaybe (error $ "Invalid enum value. Not one of " ++ show (Map.keys allValuesMap)) . flip Map.lookup allValuesMap <$> rawBytesFieldDecoder- where- -- TODO: Vector of pointers to ByteStrings for a bit more memory locality? Does it make a perf difference?- allValuesMap = Map.mapKeys (LBS.toStrict . LT.encodeUtf8 . nameTransform) $ fmap to genEnumDecoder--class EnumDecoder f where- -- | Returns the textual representation and constructed object for every possible- -- value of the enum.- genEnumDecoder :: Map LT.Text (f a)--instance (EnumDecoder a, EnumDecoder b) => EnumDecoder (a :+: b) where- genEnumDecoder = (L1 <$> genEnumDecoder) `Map.union` (R1 <$> genEnumDecoder)--instance (EnumDecoder f) => EnumDecoder (M1 D c f) where- genEnumDecoder = M1 <$> genEnumDecoder---- U1 is "Unit"-type, that is: no value in the constructor, AKA "pure enum".-instance (KnownSymbol ctorName) => EnumDecoder (M1 C ('MetaCons ctorName ctorFixity 'False) U1) where- genEnumDecoder = Map.singleton (LT.pack $ symbolVal (Proxy @ctorName)) (M1 U1)---- | One of the functions behind 'LowerCasedPgEnum', but you can decide--- how to map your type's constructor names arbitrarily, which can be--- useful if you're not using lowercase values in your postgres enums.-genericEnumFieldEncoder ::- forall a.- (Generic a, EnumEncoder (Rep a)) =>- -- | A function that takes in the Haskell constructor name and returns the textual representation of the enum in postgres- (Text -> Text) ->- a ->- ByteString-genericEnumFieldEncoder nameTransform = encodeUtf8 . nameTransform . genEnumEncoder . from--class EnumEncoder f where- -- | Returns the textual representation of an enum value's constructor.- genEnumEncoder :: f a -> Text--instance (EnumEncoder a, EnumEncoder b) => EnumEncoder (a :+: b) where- genEnumEncoder (L1 x) = genEnumEncoder x- genEnumEncoder (R1 x) = genEnumEncoder x--instance (EnumEncoder f) => EnumEncoder (M1 D c f) where- genEnumEncoder (M1 x) = genEnumEncoder x---- U1 is "Unit"-type, that is: no value in the constructor, AKA "pure enum".-instance (KnownSymbol ctorName) => EnumEncoder (M1 C ('MetaCons ctorName ctorFixity 'False) U1) where- genEnumEncoder _ = Text.pack $ symbolVal (Proxy @ctorName)---- | Returns a `FieldEncoder` that is sent without a type OID in queries.--- This means postgres will try to infer the type of these arguments.--- Check `typedFieldEncoder` if you're interested in encoding your custom types,--- you probably don't need this.-untypedFieldEncoder :: (EncodingContext -> a -> BinaryField) -> FieldEncoder a-untypedFieldEncoder enc = FieldEncoder {toTypeOid = \_ -> Nothing, toPgField = enc}---- | A decoder that accepts any PG type and returns the object's--- postgres' binary representation as a ByteString.-rawBytesFieldDecoder :: FieldDecoder ByteString-rawBytesFieldDecoder =- FieldDecoder- { fieldValueDecoder = \_oid -> \case- Nothing -> Left "Cannot decode SQL null as the `rawBytesFieldDecoder`."- Just bs -> Right bs,- allowedPgTypes = const True- }---- | Returns a field-encoding function for a vector-like Foldable (e.g. Lists and Vector itself).-toPgVectorField :: forall f a. (Foldable f, ToPgField a) => EncodingContext -> f a -> BinaryField-toPgVectorField encCtx =- let fe = fieldEncoder @a- encodeElement el = Builder.binaryField $ fe.toPgField encCtx el- Oid elemOid = fromMaybe (Oid 0) (fe.toTypeOid encCtx)- in \vec ->- let ndim = Builder.int32BE 1- -- Postgres seems to build the "has_nulls" flag itself in the ReadArrayBinary function at https://github.com/postgres/postgres/blob/aa7f9493a02f5981c09b924323f0e7a58a32f2ed/src/backend/utils/adt/arrayfuncs.c#L1429, so we can just set it to 0- hasNull = Builder.byteString $ BinSer.encodeInt32BE 0- -- hasNull = Builder.byteString $ BinSer.encodeInt32BE (if Vector.any (\e -> toPgField e == Nothing) vec then 1 else 0)- elemOidBs = Builder.byteString $ BinSer.encodeInt32BE elemOid- lb1 = Builder.byteString $ BinSer.encodeInt32BE 1- (Sum len, encodedElements) = foldMap (\el -> (Sum 1, encodeElement el)) vec- dim1 = Builder.byteString $ BinSer.encodeInt32BE len- fullBs = ndim <> hasNull <> elemOidBs <> dim1 <> lb1 <> encodedElements- in NotNull (Builder.toStrictByteString fullBs)---- | A FieldDecoder that accepts and decodes Postgres arrays.-arrayField :: forall a f. (Monoid (f a)) => (forall m. (Monad m) => Int -> m a -> m (f a)) -> FieldDecoder a -> FieldDecoder (f a)-arrayField !replicateFunction !elementParser =- -- From https://github.com/postgres/postgres/blob/5941946d0934b9eccb0d5bfebd40b155249a0130/src/backend/utils/adt/arrayfuncs.c#L1548- FieldDecoder- { fieldValueDecoder = \colInfo ->- let !arrayFieldDecoder = arrayParser colInfo.encodingContext <* Parser.endOfInput- in \case- Nothing -> Left "Cannot decode SQL null as the Haskell Vector type. Use a `Maybe (Vector a)`"- Just bs -> case Parser.parseOnly arrayFieldDecoder bs of- Parser.ParseOk v -> Right v- Parser.ParseFail err -> Left err,- allowedPgTypes = allowOnlyArrayTypes- }- where- arrayParser :: EncodingContext -> Parser.Parser (f a)- arrayParser encodingContext = do- !ndim <- Parser.takeInt32BE- !_hasNull <- Parser.takeInt32BE- !elementTypeOid :: Oid <- Oid . fromIntegral <$> Parser.takeInt32BE- let !elementColInfo = FieldInfo elementTypeOid Nothing encodingContext- when (ndim > 1) $ fail $ "TODO: No support for multi-dimensional arrays in Hpgsql. Got array with ndim=" ++ show ndim- if ndim == 0- then pure mempty- else do- !dim_i :: Int <- fromIntegral <$> Parser.takeInt32BE- !_lb_i <- Parser.takeInt32BE- unless (elementParser.allowedPgTypes elementColInfo) $ fail $ "Array contains elements of type OID " ++ show elementTypeOid ++ " but decoder does not handle that type"- replicateFunction dim_i $ do- size :: Int <- fromIntegral <$> Parser.takeInt32BE- elementBs <- if size == (-1) then pure Nothing else Just <$> Parser.take size- case elementParser.fieldValueDecoder elementColInfo elementBs of- Left err -> fail $ "Error parsing array element: " ++ show err- Right el -> pure el+-- |+--+-- = Encoding and decoding fields and rows+--+-- This module contains a collection of functions, classes, and instances that can+-- help you build encoders and decoders for your Haskell types.+--+-- Here's an example:+--+-- > data Person = Person { name :: Text, born :: Day, heightMeters :: Double }+-- > deriving stock Generic+-- > deriving anyclass FromPgRow+-- >+-- > persons :: [Person] <- query conn "SELECT * FROM persons"+--+-- Note that Hpgsql's `RowDecoder` does not have a `Monad` instance because that allows it to+-- type check query results and field counts only once per query instead of paying that price+-- for every field of every row. If you need to write a row decoder that is monadic, check "Hpgsql.Encoding.RowDecoderMonadic".+--+-- = Manually derived row decoders+--+-- You can use `fieldRowDecoder` for each field in manually derived row decoders.+-- For example:+--+-- > data Car = Car { model :: Text, year :: Maybe Int, inGoodCondition :: Bool }+-- >+-- > instance FromPgRow Car where+-- > rowDecoder = Car <$> fieldRowDecoder+-- > <*> fieldRowDecoder+-- > <*> fieldRowDecoder+--+-- In our benchmarks hand written FromPgRow instances aren't any more performant than Generically derived ones.+--+-- Noteworthy is you should not use `singleField fieldDecoder` when you can use `fieldRowDecoder` instead. Hpgsql has rewrite rules to make this replacement when compiling with -O1, but under -O0 the latter is much faster.+module Hpgsql.Encoding+ ( -- * Decoding+ FromPgField (fieldDecoder, fieldRowDecoder), -- Do not export other methods so we can change them+ FieldDecoder (..),+ FieldInfo (..),+ FromPgRow (..),+ RowDecoder (..), -- TODO: We should consider not exporting everything to give us freedom+ singleField,+ nullableField,+ genericFromPgRow,++ -- * Encoding++ -- | Keep in mind that Haskell's `Int` is an `Int64` on most+ -- hardware, which is a mismatch for the commonly used 32-bit+ -- `integer` PostgreSQL type.+ -- This is not a problem when decoding because Hpgsql can decode+ -- `integer` into Haskell's `Int`, but when encoding PostgreSQL+ -- will understandably not accept a larger type.+ ToPgField (..),+ FieldEncoder (..),+ ToPgRow (..),+ RowEncoder (..),+ EncodingContext (..),+ genericToPgRow,++ -- * PostgreSQL enums+ LowerCasedPgEnum (..),+ genericEnumFieldDecoder,+ genericEnumFieldEncoder,++ -- * PostgreSQL composite types+ compositeTypeDecoder,+ compositeTypeEncoder,++ -- * Driving PostgreSQL type inference+ typeFieldDecoder,+ typeFieldEncoder,+ typeOidWithName,+ typeMustBeNamed,++ -- * Others+ rawBytesFieldDecoder,+ untypedFieldEncoder,+ toPgVectorField,+ arrayField,+ )+where++import Hpgsql.Encoding.Internal
− src/Hpgsql/Encoding/BinarySerializer.hs
@@ -1,175 +0,0 @@-{-# LANGUAGE BinaryLiterals #-}-{-# LANGUAGE CPP #-}---- |--- A replacement for libraries like cereal or binary.--- In our tests, this is ~6.0% faster than cereal, and it also--- (or by virtue of) allocates ~13% less memory in some of our benchmarks.--- And it also means one fewer dependency.--- The caveat is that this module makes unaligned memory access. For the target--- CPU architectures of this library, this should be fine.-module Hpgsql.Encoding.BinarySerializer- ( ByteStringIdx (..),- decodeInt16BE,- decodeInt32BE,- decodeInt64BE,- decodeWord32BE,- decodeWord64BE,- encodeInt32BE,- encodeDouble,- encodeFloat,- encodeInt64BE,- encodeInt16BE,- encodePgBoolean,- decodeDataRow,- )-where--import Data.ByteString (ByteString)-import qualified Data.ByteString.Internal as InternalBS-import Data.Int (Int16, Int32, Int64)-import Prelude hiding (encodeFloat)-#if WORDS_BIGENDIAN-import Data.Word (Word16, Word32, Word64)-#else-import Data.Word (Word16, Word32, Word64, byteSwap16, byteSwap32, byteSwap64, Word8)-#endif-import Data.Bits (Bits (unsafeShiftR))-import Data.Coerce (coerce)-import Foreign (Storable (..), (.&.))-import Foreign.ForeignPtr (withForeignPtr)-import GHC.Float (castDoubleToWord64, castFloatToWord32)-import System.IO.Unsafe (unsafeDupablePerformIO)--fromBigEndian32 :: Word32 -> Word32-#if WORDS_BIGENDIAN-fromBigEndian32 = Prelude.id-#else-fromBigEndian32 = byteSwap32-#endif--fromBigEndian64 :: Word64 -> Word64-#if WORDS_BIGENDIAN-fromBigEndian64 = Prelude.id-#else-fromBigEndian64 = byteSwap64-#endif--fromBigEndian16 :: Word16 -> Word16-#if WORDS_BIGENDIAN-fromBigEndian16 = Prelude.id-#else-fromBigEndian16 = byteSwap16-#endif--data CoolWordDec a where- CWord8 :: CoolWordDec Word8- CWord16 :: CoolWordDec Word16- CWord32 :: CoolWordDec Word32- CWord64 :: CoolWordDec Word64--{-# INLINE decodeWord #-}-decodeWord :: CoolWordDec a -> ByteStringIdx -> ByteString -> (a -> a) -> Either String a-decodeWord wdec idx (InternalBS.BS bytesPtr len) endianConvert =- case wdec of- CWord8 -> if len < 1 + idx.idx then Left "Less than enough bytes to decode" else Right $ endianConvert $ unsafeDupablePerformIO $ withForeignPtr bytesPtr $ \ptr -> peekByteOff (coerce ptr) idx.idx- CWord16 -> if len < 2 + idx.idx then Left "Less than enough bytes to decode" else Right $ endianConvert $ unsafeDupablePerformIO $ withForeignPtr bytesPtr $ \ptr -> peekByteOff (coerce ptr) idx.idx- CWord32 -> if len < 4 + idx.idx then Left "Less than enough bytes to decode" else Right $ endianConvert $ unsafeDupablePerformIO $ withForeignPtr bytesPtr $ \ptr -> peekByteOff (coerce ptr) idx.idx- CWord64 -> if len < 8 + idx.idx then Left "Less than enough bytes to decode" else Right $ endianConvert $ unsafeDupablePerformIO $ withForeignPtr bytesPtr $ \ptr -> peekByteOff (coerce ptr) idx.idx--{-# INLINE unsafeEncodeWord #-}-unsafeEncodeWord :: (Storable a) => a -> (a -> a) -> Int -> ByteString-unsafeEncodeWord n endianConvert len =- InternalBS.unsafeCreate len $ \bufferPtr ->- poke (coerce bufferPtr) $ endianConvert n--newtype ByteStringIdx = ByteStringIdx {idx :: Int}- deriving newtype (Num)--{-# INLINE decodeInt16BE #-}-decodeInt16BE :: ByteStringIdx -> ByteString -> Either String Int16-decodeInt16BE idx bs = fromIntegral <$> decodeWord CWord16 idx bs fromBigEndian16--{-# INLINE encodeInt16BE #-}-encodeInt16BE :: Int16 -> ByteString-encodeInt16BE n = unsafeEncodeWord (fromIntegral n) fromBigEndian16 2--{-# INLINE decodeWord8 #-}-decodeWord8 :: ByteStringIdx -> ByteString -> Either String Word8-decodeWord8 idx bs = decodeWord CWord8 idx bs Prelude.id--{-# INLINE decodeWord32BE #-}-decodeWord32BE :: ByteString -> Either String Word32-decodeWord32BE bs = decodeWord CWord32 0 bs fromBigEndian32--{-# INLINE decodeWord64BE #-}-decodeWord64BE :: ByteString -> Either String Word64-decodeWord64BE bs = decodeWord CWord64 0 bs fromBigEndian64--{-# INLINE decodeInt32BE #-}-decodeInt32BE :: ByteStringIdx -> ByteString -> Either String Int32-decodeInt32BE idx bs = fromIntegral <$> decodeWord CWord32 idx bs fromBigEndian32--{-# INLINE encodeInt32BE #-}-encodeInt32BE :: Int32 -> ByteString-encodeInt32BE n = unsafeEncodeWord (fromIntegral n) fromBigEndian32 4--{-# INLINE decodeInt64BE #-}-decodeInt64BE :: ByteStringIdx -> ByteString -> Either String Int64-decodeInt64BE idx bs = fromIntegral <$> decodeWord CWord64 idx bs fromBigEndian64--{-# INLINE encodeInt64BE #-}-encodeInt64BE :: Int64 -> ByteString-encodeInt64BE n = unsafeEncodeWord (fromIntegral n) fromBigEndian64 8--{-# INLINE encodeFloat #-}-encodeFloat :: Float -> ByteString-encodeFloat n = unsafeEncodeWord (castFloatToWord32 n) fromBigEndian32 4--{-# INLINE encodeDouble #-}-encodeDouble :: Double -> ByteString-encodeDouble n = unsafeEncodeWord (castDoubleToWord64 n) fromBigEndian64 8--{-# INLINE encodePgBoolean #-}-encodePgBoolean :: Bool -> ByteString-encodePgBoolean v = if v then "\SOH" else "\NUL"--{-# INLINE decodeDataRow #-}---- | A super specialized decoder to decode a postgres DataRow message--- more quickly than a naive implementation.--- Returns the index into the left-unparsed contents of the supplied bytestring.-decodeDataRow :: ByteStringIdx -> ByteString -> Either String ByteStringIdx-decodeDataRow idx bs@(InternalBS.BS _bytesPtr len) =- -- We have a fast path when rows are at least 8 bytes long (should be the case- -- for all but 0-column query results or bytestring chunks "cut in the middle of the message")- -- by playing with bitwise operations.- -- Whether this is worth keeping is sort of questionable. It's complex- -- (even if I think it's safe and well tested) and reduces runtime of one of- -- our benchmarks by 2% compared to not having it.- case decodeWord CWord64 idx bs fromBigEndian64 of- Right (w64 :: Word64) ->- -- After fromBigEndian64, the Word64 has bytes in big-endian order:- -- byte 0 (msg type) in MSB, bytes 1-4 (length) next, bytes 5-6 (col count), byte 7 in LSB.- let msgIdentByte64 = w64 .&. 0b11111111_00000000_00000000_00000000_00000000_00000000_00000000_00000000- lenFullMsg = flip unsafeShiftR 24 $ w64 .&. 0b00000000_11111111_11111111_11111111_11111111_00000000_00000000_00000000- letterD :: Word64 = 0b01000100_00000000_00000000_00000000_00000000_00000000_00000000_00000000- in if msgIdentByte64 == letterD- then- toResult (fromIntegral lenFullMsg)- else Left "Not a DataRow (Word64 bits decoding path)"- Left _ ->- -- It is possible the DataRow has length less than 8 bytes, so- -- we still have to try to parse that.- if len >= 5 + idx.idx- then do- msgIdentChar <- decodeWord8 idx bs- lenFullMsg <- decodeInt32BE (1 + idx) bs- if msgIdentChar == 68 -- Letter 'D'- then toResult (fromIntegral lenFullMsg)- else Left "Not a DataRow"- else Left "Less than enough bytes to decode a DataRow"- where- toResult lenFullMsg- | len >= 1 + lenFullMsg + idx.idx = Right $ ByteStringIdx $ 1 + lenFullMsg + idx.idx- | otherwise = Left "Less than enough bytes to decode a full DataRow"
+ src/Hpgsql/Encoding/Internal.hs view
@@ -0,0 +1,2129 @@+-- For the disabled warning, see Note [singleField fieldDecoder rewrite rules]+{-# LANGUAGE UndecidableInstances #-}+{-# OPTIONS_GHC -Wno-inline-rule-shadowing #-}++module Hpgsql.Encoding.Internal+ ( -- * Decoding+ FromPgField (..),+ FieldDecoder (..),+ FieldInfo (..),+ FromPgRow (..),+ RowDecoder (..),+ singleField,+ nullableField,+ genericFromPgRow,++ -- * Encoding+ ToPgField (..),+ FieldEncoder (..),+ ToPgRow (..),+ RowEncoder (..),+ EncodingContext (..),+ genericToPgRow,++ -- * PostgreSQL enums+ LowerCasedPgEnum (..),+ genericEnumFieldDecoder,+ genericEnumFieldEncoder,++ -- * PostgreSQL composite types+ compositeTypeDecoder,+ compositeTypeEncoder,++ -- * Driving PostgreSQL type inference+ typeFieldDecoder,+ typeFieldEncoder,+ typeOidWithName,+ typeMustBeNamed,++ -- * Others+ parsePgType,+ rawBytesFieldDecoder,+ rootBaseTypeOf,+ untypedFieldEncoder,+ toPgVectorField,+ arrayField,+ arrayFieldRowDec,+ )+where++import Control.Monad (replicateM, unless, when)+import qualified Data.Aeson as Aeson+import Data.ByteString (ByteString)+import qualified Data.ByteString as BS+import qualified Data.ByteString.Lazy as LBS+import Data.CaseInsensitive (CI)+import qualified Data.CaseInsensitive as CI+import Data.Char (chr)+import Data.Coerce (coerce)+import Data.Fixed (divMod')+import Data.Functor.Contravariant (Contravariant (..))+import Data.Int (Int16, Int32, Int64)+import qualified Data.List as List+import Data.Map.Strict (Map)+import qualified Data.Map.Strict as Map+import Data.Maybe (fromMaybe)+import Data.Monoid (Sum (..))+import Data.Proxy (Proxy (..))+import Data.Ratio (Ratio)+import Data.Scientific (Scientific (..), floatingOrInteger, scientific)+import Data.Text (Text)+import qualified Data.Text as Text+import Data.Text.Encoding (encodeUtf8)+import qualified Data.Text.Lazy as LT+import qualified Data.Text.Lazy.Encoding as LT+import Data.Time (CalendarDiffDays (..), CalendarDiffTime (..), Day, LocalTime (..), NominalDiffTime, TimeOfDay, UTCTime (..), ZonedTime, diffDays, diffTimeToPicoseconds, fromGregorian, picosecondsToDiffTime, secondsToNominalDiffTime, timeOfDayToTime, timeToTimeOfDay, utc, utcToZonedTime, zonedTimeToUTC)+import Data.Time.Calendar.Julian (addJulianDurationClip, fromJulian)+import Data.Tuple.Only (Only (..))+import Data.UUID.Types (UUID)+import qualified Data.UUID.Types as UUID+import Data.Vector (Vector)+import qualified Data.Vector as Vector+import GHC.Float (castWord32ToFloat, castWord64ToDouble, expt, float2Double)+import GHC.Generics (C, D, Generic (..), K1 (..), M1 (..), Meta (MetaCons), U1 (..), (:*:) (..), (:+:) (..))+import GHC.TypeLits (KnownSymbol, TypeError, symbolVal)+import qualified GHC.TypeLits as TypeLits+import Hpgsql.Builder (BinaryField (..))+import qualified Hpgsql.Builder as Builder+import Hpgsql.Internal.PinnedByteArray (PinnedByteArray)+import qualified Hpgsql.Internal.PinnedByteArray as PBA+import qualified Hpgsql.SimpleParser as Parser+import Hpgsql.Time (Unbounded (..))+import Hpgsql.TypeInfo (DomainTypeDetails (..), EncodingContext (..), Oid (..), TypeDetails (..), TypeInfo (..), boolOid, bpcharOid, byteaOid, charOid, dateOid, float4Oid, float8Oid, int2Oid, int4Oid, int8Oid, intervalOid, jsonOid, jsonbOid, lookupTypeByName, lookupTypeByOid, nameOid, numericOid, oidOid, textOid, timeOid, timestampOid, timestamptzOid, uuidOid, varcharOid, voidOid)++data FieldInfo = FieldInfo+ { fieldTypeOid :: !Oid,+ -- | The column name from the query's result, if available.+ fieldName :: !(Maybe Text),+ -- | The EncodingContext as of the moment the query ran.+ encodingContext :: !EncodingContext+ }++-- | A decoder for a single field/column.+data FieldDecoder a = FieldDecoder+ { fieldValueDecoder :: FieldInfo -> Maybe ByteString -> Either String a,+ allowedPgTypes :: FieldInfo -> Bool+ }+ deriving stock (Functor)++-- | `f1 <> f2` produces a `FieldDecoder` that tries `f1` first, and if that fails it tries `f2`.+instance Semigroup (FieldDecoder a) where+ dec1 <> dec2 =+ FieldDecoder+ { fieldValueDecoder = \cInfo ->+ let f1 = dec1.fieldValueDecoder cInfo+ f2 = dec2.fieldValueDecoder cInfo+ in \mbs ->+ let cand1 = if dec1.allowedPgTypes cInfo then f1 mbs else Left "Not first parser"+ cand2 = if dec2.allowedPgTypes cInfo then f2 mbs else Left "Not second parser"+ in cand1 <> cand2,+ allowedPgTypes = \cInfo -> dec1.allowedPgTypes cInfo || dec2.allowedPgTypes cInfo+ }++data RowDecoder a = RowDecoder+ { fullRowDecoder :: [FieldInfo] -> Parser.Parser a,+ -- | Returns the same colInfos with a boolean indicating if+ -- the expected types match for each colInfo.+ rowColumnsTypeCheck :: [FieldInfo] -> [(FieldInfo, Bool)],+ numExpectedColumns :: !Int+ }+ deriving stock (Functor, Generic)++instance Applicative RowDecoder where+ pure v = RowDecoder (const $ pure v) (map (,True)) 0+ {-# INLINE (<*>) #-} -- This is crucial for performance. It makes our CPS Parser truly compile to CPS row decoders.+ RowDecoder p1 tc1 nc1 <*> RowDecoder p2 tc2 nc2 = RowDecoder (\colTypes -> let (cols1, cols2) = List.splitAt nc1 colTypes in p1 cols1 <*> p2 cols2) (\colTypes -> let (cols1, cols2) = List.splitAt nc1 colTypes in tc1 cols1 ++ tc2 cols2) (nc1 + nc2)++instance (TypeError (TypeLits.Text "RowDecoder does not have a Monad instance in Hpgsql because Hpgsql type-checks the result types of queries before having access to even the first data row. Use the Applicative class to write your instances or use the Monadic decoding variants.")) => Monad RowDecoder where+ (>>=) = error "inaccessible bind in Monad RowDecoder instance"++-- Note [singleField fieldDecoder rewrite rules]+-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+--+-- There are strictly speaking three ways to derive a single-field/column+-- row decoder in hpgsql: `singleField fieldDecoder`, `fieldRowDecoder`, and+-- `inlinedFieldRowDecoder`.+--+-- The last two are sensible: one is more aggressive with inlining and produces faster row decoders+-- at the cost of compilation times and binary sizes, the other produces row decoders that call out+-- to functions when decoding each field, hence being smaller but slower.+--+-- But what about the first? It forces the allocation of `ByteString` values from our Pinned Byte Arrays,+-- and is hence the slower of all three, except that it doesn't produce row decoders any smaller+-- than `fieldRowDecoder`. It is strictly worse than that.+--+-- Since `singleField fieldDecoder` might be used by users of hpgsql, however, we can't just remove it.+-- So we introduce rewrite rules to rewrite those to `fieldRowDecoder` instead.+-- These rewrite rules require the implementations of each Field Decoder to be separated and not+-- inlinable, or else GHC inlines `fieldDecoder` too early and these rules don't fire.+--+-- So there will be some phase annotations in some places and this requires a delicate choice+-- of both INLINE and NOINLINE pragmas to work properly. To know if something's broken, the+-- benchmarks with "Generically derived" and "singleField fieldDecoder" row decoders both+-- should allocate the same amount of memory.++-- Per-type rewrite rules for this Note live immediately above each type's+-- FieldDecoder/RowDecoder definitions further down this file. This rule right below+-- is still useful and triggers at call sites where the type is not known at+-- compile time.+{-# RULES+"singleField fieldDecoder" singleField fieldDecoder = fieldRowDecoder+"singleField (nullableField fieldDecoder)" singleField (nullableField fieldDecoder) = fieldRowDecoder+ #-}++{-# INLINE [1] singleField #-}++-- | Builds a single-field row decoder. Prefer to use `fieldRowDecoder`+-- if you can because that's much faster.+singleField :: FieldDecoder a -> RowDecoder a+singleField fdec =+ let !typeCheck = fdec.allowedPgTypes+ in RowDecoder+ { fullRowDecoder = \case+ [singleColInfo] ->+ let decode = fdec.fieldValueDecoder singleColInfo+ in do+ lenNextCol <- fromIntegral <$> Parser.takeInt32BE+ if lenNextCol >= 0+ then do+ -- Some forcing to avoid thunks keeping references to our+ -- PinnedByteArray buffers in case user-defined types+ -- don't have strict fields+ nextColBs <- Parser.take lenNextCol+ case decode (Just (PBA.toByteString nextColBs)) of+ Right !v -> pure v+ Left err -> fail err+ else case decode Nothing of+ Right !v -> pure v+ Left err -> fail err+ _ -> fail "singleField expected a single column OID but got 0 or >1",+ rowColumnsTypeCheck = \case+ [singleColInfo] -> [(singleColInfo, typeCheck singleColInfo)]+ _ -> error "singleField's rowColumnsTypeCheck expected a single column OID but got 0 or >1",+ numExpectedColumns = 1+ }++class FromPgField a where+ {-# MINIMAL fieldDecoder #-}+ fieldDecoder :: FieldDecoder a++ -- | When possible, a specialized field-length+field-value parser that is+ -- much faster than shelling out to the `FieldDecoder a` instance, because+ -- that requires intantiating a `ByteString` while this operates on top of+ -- the `PinnedByteArray` in our buffers directly.+ --+ -- Any implementation of this _must_ return a `Nothing` for a SQL NULL value,+ -- regardless of what `FieldDecoder` would do with a SQL NULL.+ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder :: FieldInfo -> Parser.Parser (Maybe a)+ specializedFieldDecoder singleColInfo = do+ len <- Parser.takeInt32BE+ if len == (-1)+ then pure Nothing+ else do+ bs <- Parser.take (fromIntegral len)+ case fieldDecoder.fieldValueDecoder singleColInfo (Just (PBA.toByteString bs)) of+ Left err -> fail err+ Right v -> pure (Just v)++ -- | Semantically equivalent to `singleField fieldDecoder`, but for+ -- most types it can provide a much faster `RowDecoder`.+ {-# NOINLINE fieldRowDecoder #-}+ fieldRowDecoder :: RowDecoder a+ fieldRowDecoder = inlinedFieldRowDecoder++ -- | Semantically equivalent to `singleField fieldDecoder`, but might+ -- have every field decoder's code completely inlined into the row+ -- decoder's code. This is not faster than just `fieldRowDecoder`,+ -- so it's not exposed for now. More investigation is necessary.+ {-# INLINE inlinedFieldRowDecoder #-}+ inlinedFieldRowDecoder :: RowDecoder a+ inlinedFieldRowDecoder =+ let !typeCheck = (fieldDecoder @a).allowedPgTypes+ in RowDecoder+ { fullRowDecoder = \case+ [singleColInfo] ->+ let !valueForNull = case (fieldDecoder @a).fieldValueDecoder singleColInfo Nothing of+ Left err -> fail err+ Right v -> pure v+ in do+ -- Some forcing to avoid thunks keeping references to our+ -- PinnedByteArray buffers in case user-defined types+ -- don't have strict fields+ mv <- specializedFieldDecoder singleColInfo+ case mv of+ Nothing -> valueForNull+ Just !v -> pure v+ _ -> fail "inlinedFieldRowDecoder expected a single column OID but got 0 or >1",+ rowColumnsTypeCheck = \case+ [singleColInfo] -> [(singleColInfo, typeCheck singleColInfo)]+ _ -> error "singleField's rowColumnsTypeCheck expected a single column OID but got 0 or >1",+ numExpectedColumns = 1+ }++class FromPgRow a where+ rowDecoder :: RowDecoder a+ default rowDecoder :: (Generic a, ProductTypeDecoder (Rep a)) => RowDecoder a+ rowDecoder = genericFromPgRow++-- | Allows you to create a @FieldDecoder@ for composite types.+-- For a type such as:+--+-- > CREATE TYPE int_and_bool AS (numfield INT, boolfield BOOL);+--+-- You can define a Haskell type as such:+--+-- > data IntAndBool = IntAndBool Int Bool+-- >+-- > instance FromPgField IntAndBool where+-- > fieldDecoder = compositeTypeDecoder rowDecoder <&> \(i, b) -> IntAndBool i b+compositeTypeDecoder :: forall a. RowDecoder a -> FieldDecoder a+compositeTypeDecoder (RowDecoder {..}) =+ FieldDecoder+ { fieldValueDecoder = \compositeTypeOid ->+ let !prs = parserForRecord compositeTypeOid.encodingContext <* Parser.endOfInput+ in \case+ Nothing -> Left "Got NULL in composite type but it was not allowed"+ Just bs ->+ case Parser.parseOnly prs (PBA.fromByteString bs) of+ Parser.ParseOk v _ -> Right v+ Parser.ParseFail err -> Left err,+ allowedPgTypes = const True -- There's no way to enforce a custom type's OID. We only check if it's structurally the same in the parser (same subtypes in same order)+ }+ where+ parserForRecord :: EncodingContext -> Parser.Parser a+ parserForRecord encodingContext = do+ -- From https://github.com/postgres/postgres/blob/50ba65e73325cf55fedb3e1f14673d816726923b/src/backend/utils/adt/rowtypes.c#L687+ -- we can see a composite type's binary representation consists of: number of columns (Int32) + for_each_column { OID (Int32) + size_or_minus_1 (Int32) + Bytes }+ numCols <- fromIntegral <$> Parser.takeInt32BE+ unless (numCols == numExpectedColumns) $ fail $ "Composite type has " ++ show numCols ++ " attributes but parser expected " ++ show numExpectedColumns+ let mkColInfo oid = FieldInfo oid Nothing encodingContext+ cols <- replicateM numCols $ do+ !oid <- Oid . fromIntegral <$> Parser.takeInt32BE+ !size <- fromIntegral <$> Parser.peekInt32BE+ sizeAndValueBs <- Parser.take (4 + max 0 size)+ pure (oid, sizeAndValueBs)+ let typecheckedCols = rowColumnsTypeCheck (map (mkColInfo . fst) cols)+ unless (all snd typecheckedCols) $ fail $ "Parser for composite type found type OIDs " ++ show (map fst cols) ++ " but expected different"+ case Parser.parseOnly (fullRowDecoder (map (mkColInfo . fst) cols) <* Parser.endOfInput) (PBA.toStrict $ PBA.fromChunks $ map snd cols) of+ Parser.ParseOk v _ -> pure v+ Parser.ParseFail err -> fail $ "Error decoding composite type: " ++ show err++-- | Allows you to create a @FieldEncoder@ for composite types.+-- For a type such as:+--+-- > CREATE TYPE int_and_bool AS (numfield INT, boolfield BOOL);+--+-- You can define a Haskell type as such:+--+-- > data IntAndBool = IntAndBool Int Bool+-- >+-- > instance ToPgField IntAndBool where+-- > fieldEncoder = typeFieldEncoder (typeOidWithName "int_and_bool")+-- > $ compositeTypeEncoder $ contramap (\(IntAndBool i b) -> (fromIntegral i :: Int32, b)) rowEncoder+compositeTypeEncoder :: forall a. RowEncoder a -> FieldEncoder a+compositeTypeEncoder rowEnc =+ FieldEncoder+ { toTypeOid = \_ -> Nothing,+ toPgField = \encCtx -> \a ->+ let fields = map (\f -> f encCtx) (rowEnc.toPgParams a)+ numCols = Builder.int32BE (fromIntegral $ length fields)+ encodeField (mOid, bf) =+ let Oid oid = fromMaybe (Oid 0) mOid+ in Builder.int32BE oid <> Builder.binaryField bf+ in NotNull (Builder.toStrictByteString (numCols <> foldMap encodeField fields))+ }++instance (FromPgField a) => FromPgRow (Only a) where+ rowDecoder = Only <$> fieldRowDecoder++instance (FromPgField a, FromPgField b) => FromPgRow (a, b) where+ rowDecoder = (,) <$> fieldRowDecoder <*> fieldRowDecoder++instance (FromPgField a, FromPgField b, FromPgField c) => FromPgRow (a, b, c) where+ rowDecoder = (,,) <$> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder++instance (FromPgField a, FromPgField b, FromPgField c, FromPgField d) => FromPgRow (a, b, c, d) where+ rowDecoder = (,,,) <$> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder++instance (FromPgField a, FromPgField b, FromPgField c, FromPgField d, FromPgField e) => FromPgRow (a, b, c, d, e) where+ rowDecoder = (,,,,) <$> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder++instance (FromPgField a, FromPgField b, FromPgField c, FromPgField d, FromPgField e, FromPgField f) => FromPgRow (a, b, c, d, e, f) where+ rowDecoder = (,,,,,) <$> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder++instance (FromPgField a, FromPgField b, FromPgField c, FromPgField d, FromPgField e, FromPgField f, FromPgField g) => FromPgRow (a, b, c, d, e, f, g) where+ rowDecoder = (,,,,,,) <$> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder++instance (FromPgField a, FromPgField b, FromPgField c, FromPgField d, FromPgField e, FromPgField f, FromPgField g, FromPgField h) => FromPgRow (a, b, c, d, e, f, g, h) where+ rowDecoder = (,,,,,,,) <$> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder++instance (FromPgField a, FromPgField b, FromPgField c, FromPgField d, FromPgField e, FromPgField f, FromPgField g, FromPgField h, FromPgField i) => FromPgRow (a, b, c, d, e, f, g, h, i) where+ rowDecoder = (,,,,,,,,) <$> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder++instance (FromPgField a, FromPgField b, FromPgField c, FromPgField d, FromPgField e, FromPgField f, FromPgField g, FromPgField h, FromPgField i, FromPgField j) => FromPgRow (a, b, c, d, e, f, g, h, i, j) where+ rowDecoder = (,,,,,,,,,) <$> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder++instance (FromPgField a, FromPgField b, FromPgField c, FromPgField d, FromPgField e, FromPgField f, FromPgField g, FromPgField h, FromPgField i, FromPgField j, FromPgField k) => FromPgRow (a, b, c, d, e, f, g, h, i, j, k) where+ rowDecoder = (,,,,,,,,,,) <$> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder++instance (FromPgField a, FromPgField b, FromPgField c, FromPgField d, FromPgField e, FromPgField f, FromPgField g, FromPgField h, FromPgField i, FromPgField j, FromPgField k, FromPgField l) => FromPgRow (a, b, c, d, e, f, g, h, i, j, k, l) where+ rowDecoder = (,,,,,,,,,,,) <$> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder++instance (FromPgField a, FromPgField b, FromPgField c, FromPgField d, FromPgField e, FromPgField f, FromPgField g, FromPgField h, FromPgField i, FromPgField j, FromPgField k, FromPgField l, FromPgField m) => FromPgRow (a, b, c, d, e, f, g, h, i, j, k, l, m) where+ rowDecoder = (,,,,,,,,,,,,) <$> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder <*> fieldRowDecoder++data FieldEncoder a = FieldEncoder+ { toTypeOid :: !(EncodingContext -> Maybe Oid),+ toPgField :: !(EncodingContext -> a -> BinaryField)+ }++instance Contravariant FieldEncoder where+ contramap f fEnc = FieldEncoder {toTypeOid = fEnc.toTypeOid, toPgField = \encCtx -> let toF = fEnc.toPgField encCtx in \v -> toF (f v)}++class ToPgField a where+ fieldEncoder :: FieldEncoder a++-- | Allows you to specify a type for a FieldEncoder. This can be useful to avoid+-- letting postgres infer types itself, which can cause errors. For example:+--+-- > data MyEnum = Val1 | Val2 | Val3+-- > myEnumFieldDecoderWithTypeInfoCheck :: FieldEncoder MyEnum+-- > myEnumFieldDecoderWithTypeInfoCheck =+-- > let convert = \case+-- > Val1 -> "val1" :: Text+-- > Val2 -> "val2"+-- > Val3 -> "val3"+-- > in typeFieldEncoder+-- > (typeOidWithName "my_enum")+-- > $ contramap convert fieldEncoder+--+-- This will work unless you use non-default flags in your connection options.+typeFieldEncoder :: (EncodingContext -> Maybe Oid) -> FieldEncoder a -> FieldEncoder a+typeFieldEncoder ttoid enc = enc {toTypeOid = ttoid}++typeOidWithName :: Text -> (EncodingContext -> Maybe Oid)+typeOidWithName typName = \encCtx -> typeOid <$> lookupTypeByName typName encCtx.typeInfoCache++instance ToPgField Int where+ fieldEncoder =+ FieldEncoder+ { toTypeOid = \_ -> Just haskellIntOid,+ toPgField = \_ -> binaryIntEncoder+ }++instance ToPgField Int16 where+ fieldEncoder =+ FieldEncoder+ { toTypeOid = \_ -> Just int2Oid,+ toPgField = \_ -> \n -> NotNull $ PBA.encodeInt16BE n+ }++instance ToPgField Int32 where+ fieldEncoder =+ FieldEncoder+ { toTypeOid = \_ -> Just int4Oid,+ toPgField = \_ -> \n -> NotNull $ PBA.encodeInt32BE n+ }++instance ToPgField Int64 where+ fieldEncoder =+ FieldEncoder+ { toTypeOid = \_ -> Just int8Oid,+ toPgField = \_ -> \n -> NotNull $ PBA.encodeInt64BE n+ }++instance ToPgField Integer where+ fieldEncoder =+ let fe = fieldEncoder @Scientific+ in FieldEncoder+ { toTypeOid = \_ -> Just numericOid,+ toPgField = \encCtx -> \n -> fe.toPgField encCtx (fromIntegral n)+ }++instance ToPgField (Ratio Integer) where+ fieldEncoder =+ let fe = fieldEncoder @Scientific+ in FieldEncoder+ { toTypeOid = \_ -> Just numericOid,+ toPgField = \encCtx -> \r -> fe.toPgField encCtx (fromRational r)+ }++instance ToPgField Oid where+ fieldEncoder =+ FieldEncoder+ { toTypeOid = \_ -> Just oidOid,+ toPgField = \_ -> \n -> NotNull $ PBA.encodeInt32BE $ fromIntegral n+ }++instance ToPgField Scientific where+ fieldEncoder =+ FieldEncoder+ { toTypeOid = \_ -> Just numericOid,+ toPgField = \_ -> \n ->+ let sign = PBA.encodeInt16BE $ if n >= 0 then 0 else 0x4000+ -- The number is coeff * 10^exp, but we want it in base-10000 so we convert it to+ -- new_coeff * 10^new_exp with new_exp a multiple of 4+ base10000Expon = 4 * (base10Exponent n `div` 4)+ base10000Coeff = coefficient n * expt 10 (base10Exponent n - base10000Expon)+ ndigits, weight :: Int16+ digits :: ByteString+ (ndigits, weight, digits) = calculateDigits 0 0 (abs base10000Coeff) ""+ dscale = PBA.encodeInt16BE (fromIntegral (max 0 (-base10000Expon))) -- Digits after the decimal point; 0 for integral values (base10000Expon >= 0). May be more than strictly necessary when negative, but that's safe.+ in NotNull $ PBA.encodeInt16BE ndigits <> PBA.encodeInt16BE (weight - 1 + fromIntegral (base10000Expon `div` 4)) <> sign <> dscale <> digits+ }+ where+ calculateDigits :: Int16 -> Int16 -> Integer -> BS.ByteString -> (Int16, Int16, BS.ByteString)+ calculateDigits !ndigitsSoFar !weightSoFar 0 !encodedDigits = (ndigitsSoFar, weightSoFar, encodedDigits)+ calculateDigits !ndigitsSoFar !weightSoFar !val !encodedDigits =+ let (quotient, fromIntegral -> (rest :: Int16)) = val `divMod` 10000+ in calculateDigits+ (ndigitsSoFar + 1)+ (weightSoFar + 1)+ quotient+ (PBA.encodeInt16BE rest <> encodedDigits)++instance ToPgField Float where+ fieldEncoder =+ FieldEncoder+ { toTypeOid = \_ -> Just float4Oid,+ toPgField = \_ -> \n -> NotNull $ PBA.encodeFloat n+ }++instance ToPgField Double where+ fieldEncoder =+ FieldEncoder+ { toTypeOid = \_ -> Just float8Oid,+ toPgField = \_ -> \n -> NotNull $ PBA.encodeDouble n+ }++instance ToPgField Bool where+ fieldEncoder =+ FieldEncoder+ { toTypeOid = \_ -> Just boolOid,+ toPgField = \_ n -> NotNull $ PBA.encodePgBoolean n+ }++instance ToPgField Day where+ -- PG Dates are Int32 number of days relative to 2000-01-01+ -- https://github.com/postgres/postgres/blob/master/src/include/datatype/timestamp.h#L235+ fieldEncoder =+ FieldEncoder+ { toTypeOid = \_ -> Just dateOid,+ -- TODO: Catch integer overflow and do what?+ toPgField = \_ d -> NotNull $ PBA.encodeInt32BE $ fromIntegral $ diffDays d (fromGregorian 2000 1 1)+ }++instance ToPgField (Unbounded Day) where+ fieldEncoder =+ let fe = fieldEncoder @Day+ in FieldEncoder+ { toTypeOid = fe.toTypeOid,+ toPgField = \encCtx -> \case+ NegInfinity -> NotNull $ PBA.encodeInt32BE minBound+ Finite v -> fe.toPgField encCtx v+ PosInfinity -> NotNull $ PBA.encodeInt32BE maxBound+ }++instance ToPgField CalendarDiffTime where+ fieldEncoder =+ FieldEncoder+ { toTypeOid = \_ -> Just intervalOid,+ toPgField = \_ CalendarDiffTime {..} ->+ let (days :: Int32, timeUnderOneDay) = ctTime `divMod'` 86_400+ in NotNull $ PBA.encodeInt64BE (round $ timeUnderOneDay * 1_000_000) <> PBA.encodeInt32BE days <> PBA.encodeInt32BE (fromIntegral ctMonths)+ }++instance ToPgField NominalDiffTime where+ fieldEncoder =+ FieldEncoder+ { toTypeOid = \_ -> Just intervalOid,+ toPgField = \_ ndt ->+ NotNull $ PBA.encodeInt64BE (round $ ndt * 1_000_000) <> PBA.encodeInt32BE 0 <> PBA.encodeInt32BE 0+ }++instance ToPgField UTCTime where+ fieldEncoder =+ FieldEncoder+ { toTypeOid = \_ -> Just timestamptzOid,+ -- TODO: Catch integer overflow and do what?+ toPgField = \_ (UTCTime parsedDate timeinday) ->+ let day :: Int64 = fromInteger $ parsedDate `diffDays` fromJulian 1999 12 19+ totalusecs :: Int64 = 86_400_000_000 * day + fromInteger (diffTimeToPicoseconds timeinday `div` 1_000_000)+ in NotNull $ PBA.encodeInt64BE totalusecs+ }++instance ToPgField (Unbounded UTCTime) where+ fieldEncoder =+ let fe = fieldEncoder @UTCTime+ in FieldEncoder+ { toTypeOid = fe.toTypeOid,+ toPgField = \encCtx -> \case+ NegInfinity -> NotNull $ PBA.encodeInt64BE minBound+ Finite v -> fe.toPgField encCtx v+ PosInfinity -> NotNull $ PBA.encodeInt64BE maxBound+ }++instance ToPgField ZonedTime where+ fieldEncoder =+ let fe = fieldEncoder @UTCTime+ in FieldEncoder+ { toTypeOid = \_ -> Just timestamptzOid,+ toPgField = \encCtx -> fe.toPgField encCtx . zonedTimeToUTC+ }++instance ToPgField (Unbounded ZonedTime) where+ fieldEncoder =+ let fe = fieldEncoder @ZonedTime+ in FieldEncoder+ { toTypeOid = fe.toTypeOid,+ toPgField = \encCtx -> \case+ NegInfinity -> NotNull $ PBA.encodeInt64BE minBound+ Finite v -> fe.toPgField encCtx v+ PosInfinity -> NotNull $ PBA.encodeInt64BE maxBound+ }++instance ToPgField LocalTime where+ fieldEncoder =+ FieldEncoder+ { toTypeOid = \_ -> Just timestampOid,+ toPgField = \_ (LocalTime localDay localTimeOfDay) ->+ let day :: Int64 = fromInteger $ localDay `diffDays` fromJulian 1999 12 19+ totalusecs :: Int64 = 86_400_000_000 * day + fromInteger (diffTimeToPicoseconds (timeOfDayToTime localTimeOfDay) `div` 1_000_000)+ in NotNull $ PBA.encodeInt64BE totalusecs+ }++instance ToPgField TimeOfDay where+ fieldEncoder =+ FieldEncoder+ { toTypeOid = \_ -> Just timeOid,+ toPgField = \_ tod ->+ let usecs :: Int64 = fromInteger $ diffTimeToPicoseconds (timeOfDayToTime tod) `div` 1_000_000+ in NotNull $ PBA.encodeInt64BE usecs+ }++instance ToPgField Char where+ fieldEncoder =+ let fe = fieldEncoder @Text+ in FieldEncoder+ { toTypeOid = \_ -> Just textOid,+ toPgField = \encCtx -> let !toTextField = fe.toPgField encCtx in \t -> toTextField $ Text.singleton t+ }++instance ToPgField ByteString where+ fieldEncoder =+ FieldEncoder+ { toTypeOid = \_ -> Just byteaOid,+ toPgField = \_ -> \bs -> NotNull bs+ }++instance ToPgField LBS.ByteString where+ fieldEncoder =+ let fe = fieldEncoder @ByteString+ in FieldEncoder+ { toTypeOid = \_ -> Just byteaOid,+ toPgField = \encCtx -> fe.toPgField encCtx . LBS.toStrict+ }++instance ToPgField Text where+ fieldEncoder =+ FieldEncoder+ { toTypeOid = \_ -> Just textOid,+ toPgField = \_ -> \t ->+ let bs = encodeUtf8 t+ in NotNull bs+ }++instance ToPgField LT.Text where+ fieldEncoder =+ FieldEncoder+ { toTypeOid = \_ -> Just textOid,+ toPgField = \_ -> \t ->+ let bs = LBS.toStrict $ LT.encodeUtf8 t+ in NotNull bs+ }++instance ToPgField String where+ fieldEncoder =+ let fe = fieldEncoder @Text+ in FieldEncoder+ { toTypeOid = \_ -> Just textOid,+ toPgField = \encCtx -> fe.toPgField encCtx . Text.pack+ }++-- From https://hackage.haskell.org/package/case-insensitive-1.2.1.0/docs/Data-CaseInsensitive.html,+-- "Note that the FoldCase instance for ByteStrings is only guaranteed to be correct for ISO-8859-1 encoded strings!".+-- So we don't have those instances.++-- | This instance does not work if you have fillTypeInfoCache disabled (that would be a non-default+-- connection option).+instance ToPgField (CI Text) where+ fieldEncoder = typeFieldEncoder (typeOidWithName "citext") $ contramap CI.original fieldEncoder++-- | This instance does not work if you have fillTypeInfoCache disabled (that would be a non-default+-- connection option).+instance ToPgField (CI LT.Text) where+ fieldEncoder = typeFieldEncoder (typeOidWithName "citext") $ contramap CI.original fieldEncoder++-- | This instance does not work if you have fillTypeInfoCache disabled (that would be a non-default+-- connection option).+instance ToPgField (CI String) where+ fieldEncoder = typeFieldEncoder (typeOidWithName "citext") $ contramap CI.original fieldEncoder++instance ToPgField UUID where+ fieldEncoder =+ FieldEncoder+ { toTypeOid = \_ -> Just uuidOid,+ toPgField = \_ -> NotNull . LBS.toStrict . UUID.toByteString+ }++instance ToPgField Aeson.Value where+ fieldEncoder =+ FieldEncoder+ { toTypeOid = \_ -> Just jsonbOid,+ toPgField = \_ -> \v ->+ let bs = BS.cons 1 (LBS.toStrict $ Aeson.encode v)+ in NotNull bs+ }++instance (ToPgField a) => ToPgField (Maybe a) where+ fieldEncoder =+ let fe = fieldEncoder @a+ in FieldEncoder+ { toTypeOid = fe.toTypeOid,+ toPgField = \encCtx -> \case+ Nothing -> SqlNull+ Just n -> fe.toPgField encCtx n+ }++instance (ToPgField a) => ToPgField (Vector a) where+ fieldEncoder =+ let fe = fieldEncoder @a+ in FieldEncoder+ { toTypeOid = \encodingContext -> do+ -- Maybe monad+ elOid <- fe.toTypeOid encodingContext+ arrayTypInfo <- lookupTypeByOid elOid encodingContext.typeInfoCache+ arrayTypInfo.oidOfArrayType,+ toPgField = toPgVectorField+ }++data RowEncoder a = RowEncoder+ { toPgParams :: !(a -> [EncodingContext -> (Maybe Oid, BinaryField)]),+ toTypeOids :: !(Proxy a -> [EncodingContext -> Maybe Oid]),+ -- | This produces bytes for Binary COPY FROM STDIN rows, which can increase performance+ -- and reduce memory usage comparing to deriving these bytes from `toPgParams`.+ -- The produced bytes should not contain the total number of fields in the+ -- beginning.+ toBinaryCopyBytes :: !(EncodingContext -> a -> Builder.Builder)+ }++instance Contravariant RowEncoder where+ contramap f rec = RowEncoder (\v -> rec.toPgParams (f v)) (\_ -> rec.toTypeOids Proxy) (\encCtx -> let !toBytes = rec.toBinaryCopyBytes encCtx in \v -> toBytes (f v))++-- | These are from `Divisible`, but we don't currently pull in the extra dependency that has that.+divide :: (a -> (b, c)) -> RowEncoder b -> RowEncoder c -> RowEncoder a+divide d re1 re2 =+ RowEncoder+ { toPgParams = \a -> let (b, c) = d a in re1.toPgParams b ++ re2.toPgParams c,+ toTypeOids = \_ -> re1.toTypeOids Proxy ++ re2.toTypeOids Proxy,+ toBinaryCopyBytes = \encCtx ->+ let !toBytes1 = re1.toBinaryCopyBytes encCtx+ !toBytes2 = re2.toBinaryCopyBytes encCtx+ in \a -> let (b, c) = d a in toBytes1 b <> toBytes2 c+ }++class ToPgRow a where+ rowEncoder :: RowEncoder a+ default rowEncoder :: (Generic a, ProductTypeEncoder (Rep a)) => RowEncoder a+ rowEncoder = genericToPgRow++instance ToPgRow () where+ rowEncoder = RowEncoder (\_ -> []) (\_ -> []) (\_ -> \_ -> mempty)++singleFieldRowEncoder :: forall a. (ToPgField a) => RowEncoder a+singleFieldRowEncoder =+ let fe = fieldEncoder @a+ in RowEncoder+ { toPgParams = \a -> [\encodingContext -> (fe.toTypeOid encodingContext, fe.toPgField encodingContext a)],+ toTypeOids = \_ -> [fe.toTypeOid],+ toBinaryCopyBytes = \encCtx -> let !enc = fe.toPgField encCtx in \a -> Builder.binaryField $ enc a+ }++instance (ToPgField a) => ToPgRow (Only a) where+ rowEncoder = contramap fromOnly singleFieldRowEncoder++instance (ToPgField a, ToPgField b) => ToPgRow (a, b) where+ rowEncoder = divide id singleFieldRowEncoder singleFieldRowEncoder++instance (ToPgField a, ToPgField b, ToPgField c) => ToPgRow (a, b, c) where+ rowEncoder = divide (\(a, b, c) -> ((a, b), c)) rowEncoder singleFieldRowEncoder++instance (ToPgField a, ToPgField b, ToPgField c, ToPgField d) => ToPgRow (a, b, c, d) where+ rowEncoder = divide (\(a, b, c, d) -> ((a, b), (c, d))) rowEncoder rowEncoder++instance (ToPgField a, ToPgField b, ToPgField c, ToPgField d, ToPgField e) => ToPgRow (a, b, c, d, e) where+ rowEncoder = divide (\(a, b, c, d, e) -> ((a, b, c), (d, e))) rowEncoder rowEncoder++instance (ToPgField a, ToPgField b, ToPgField c, ToPgField d, ToPgField e, ToPgField f) => ToPgRow (a, b, c, d, e, f) where+ rowEncoder = divide (\(a, b, c, d, e, f) -> ((a, b, c), (d, e, f))) rowEncoder rowEncoder++instance (ToPgField a, ToPgField b, ToPgField c, ToPgField d, ToPgField e, ToPgField f, ToPgField g) => ToPgRow (a, b, c, d, e, f, g) where+ rowEncoder = divide (\(a, b, c, d, e, f, g) -> ((a, b, c), (d, e, f, g))) rowEncoder rowEncoder++instance (ToPgField a, ToPgField b, ToPgField c, ToPgField d, ToPgField e, ToPgField f, ToPgField g, ToPgField h) => ToPgRow (a, b, c, d, e, f, g, h) where+ rowEncoder = divide (\(a, b, c, d, e, f, g, h) -> ((a, b, c, d), (e, f, g, h))) rowEncoder rowEncoder++instance (ToPgField a, ToPgField b, ToPgField c, ToPgField d, ToPgField e, ToPgField f, ToPgField g, ToPgField h, ToPgField i) => ToPgRow (a, b, c, d, e, f, g, h, i) where+ rowEncoder = divide (\(a, b, c, d, e, f, g, h, i) -> ((a, b, c, d), (e, f, g, h, i))) rowEncoder rowEncoder++instance (ToPgField a, ToPgField b, ToPgField c, ToPgField d, ToPgField e, ToPgField f, ToPgField g, ToPgField h, ToPgField i, ToPgField j) => ToPgRow (a, b, c, d, e, f, g, h, i, j) where+ rowEncoder = divide (\(a, b, c, d, e, f, g, h, i, j) -> ((a, b, c, d, e), (f, g, h, i, j))) rowEncoder rowEncoder++instance (ToPgField a, ToPgField b, ToPgField c, ToPgField d, ToPgField e, ToPgField f, ToPgField g, ToPgField h, ToPgField i, ToPgField j, ToPgField k) => ToPgRow (a, b, c, d, e, f, g, h, i, j, k) where+ rowEncoder = divide (\(a, b, c, d, e, f, g, h, i, j, k) -> ((a, b, c, d, e, f), (g, h, i, j, k))) rowEncoder rowEncoder++-- | The OID for `Data.Int`, which is machine dependent.+haskellIntOid :: Oid++-- | All pg type OIDs that fit into Haskell's `Data.Int`, whose size is machine dependent.+haskellIntOids :: [Oid]+(haskellIntOid, haskellIntOids)+ | (fromIntegral (maxBound @Int) :: Integer) > fromIntegral (maxBound @Int32) = (int8Oid, [int2Oid, int4Oid, int8Oid])+ | (fromIntegral (maxBound @Int) :: Integer) > fromIntegral (maxBound @Int16) = (int4Oid, [int2Oid, int4Oid])+ | otherwise = (int2Oid, [int2Oid])++-- | Big-Endian binary encoder for Haskell's `Data.Int`, which is machine-dependent.+binaryIntEncoder :: Int -> BinaryField+binaryIntEncoder+ | haskellIntOid == int8Oid = NotNull . PBA.encodeInt64BE . fromIntegral+ | haskellIntOid == int4Oid = NotNull . PBA.encodeInt32BE . fromIntegral+ | otherwise = NotNull . PBA.encodeInt16BE . fromIntegral++-- | Big-Endian binary decoder for Haskell's various IntXX types and domain types derived from those.+binaryIntDecoder :: forall a. (Integral a, Bounded a) => FieldInfo -> PinnedByteArray -> Either String a+binaryIntDecoder finfo =+ let typOid = rootBaseTypeOf finfo+ in case maxBoundAndDecoder typOid of+ Right intDecoder ->+ intDecoder+ Left err ->+ const $ Left err+ where+ maxBoundAndDecoder :: Oid -> Either String (PinnedByteArray -> Either String a)+ maxBoundAndDecoder typOid+ | typOid == int8Oid = if fits (Proxy @Int64) then Right (fmap fromIntegral . PBA.decodeInt64BE 0) else Left "Postgres int8 does not fit into target numeric Haskell type"+ | typOid == int4Oid = if fits (Proxy @Int32) then Right (fmap fromIntegral . PBA.decodeInt32BE 0) else Left "Postgres int4 does not fit into target numeric Haskell type"+ | typOid == int2Oid = if fits (Proxy @Int16) then Right (fmap fromIntegral . PBA.decodeInt16BE 0) else Left "Postgres int2 does not fit into target numeric Haskell type"+ | otherwise = Left "Probable bug in Hpgsql. Decoding binary integral type not an int2, int4 or int8, nor a domain type derived from those"+ fits :: forall b. (Integral b, Bounded b) => Proxy b -> Bool+ fits _ = fromIntegral @b @Integer (maxBound @b) <= fromIntegral (maxBound @a)++binaryFloat4Decoder :: PinnedByteArray -> Either String Float+binaryFloat4Decoder = fmap castWord32ToFloat . PBA.decodeWord32BE 0++binaryFloat8Decoder :: PinnedByteArray -> Either String Double+binaryFloat8Decoder = fmap castWord64ToDouble . PBA.decodeWord64BE 0++-- | The OID of the base type for a field. For most types this is just the OID of+-- the type of the field itself, but for Domain types this is the first non-Domain+-- type from which the field's type is ultimately derived.+-- This allows us to support decoding of domain types inside arrays and composite types+-- because it returns the OID that determines how values are encoded.+rootBaseTypeOf :: FieldInfo -> Oid+rootBaseTypeOf finfo = go finfo.fieldTypeOid+ where+ tyCache = finfo.encodingContext.typeInfoCache+ go toid =+ case lookupTypeByOid toid tyCache of+ Nothing -> toid+ Just tinfo -> case tinfo.typeDetails of+ DomainType (DomainTypeDetails baseTypeOid) -> go baseTypeOid+ _ -> toid++{-# INLINE parsePgType #-}+parsePgType :: String -> [Oid] -> (FieldInfo -> Maybe ByteString -> Either String a) -> FieldDecoder a+parsePgType !_typeName !requiredTypeOids !fieldValueDecoder = parsePgTypeFull isSuppliedTypeOrADomain fieldValueDecoder+ where+ -- \| Domain types of the supplied "allowedPgTypes" types are always allowed, as they're+ -- strictly a subset of their base types.+ isSuppliedTypeOrADomain :: FieldInfo -> Bool+ isSuppliedTypeOrADomain finfo = rootBaseTypeOf finfo `elem` requiredTypeOids++{-# INLINE parsePgTypeFull #-}+parsePgTypeFull :: (FieldInfo -> Bool) -> (FieldInfo -> Maybe ByteString -> Either String a) -> FieldDecoder a+parsePgTypeFull !allowedPgTypes !fieldValueDecoder =+ FieldDecoder+ { fieldValueDecoder = fieldValueDecoder,+ allowedPgTypes+ }++instance FromPgField () where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = parsePgType "()" [voidOid] $ const $ \case+ Nothing -> Left "Cannot decode SQL null as the Haskell () type. Use a `Maybe ()`"+ Just bs ->+ if BS.length bs == 0+ then Right ()+ else+ Left "Invalid value for postgres void type"++{-# RULES+"singleField intFieldDecoder" singleField intFieldDecoder = fieldRowDecoder+"singleField (nullableField intFieldDecoder)" singleField (nullableField intFieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE intFieldDecoder #-} -- See Note [singleField fieldDecoder rewrite rules]+intFieldDecoder :: FieldDecoder Int+intFieldDecoder = parsePgType "Int" haskellIntOids $ \finfo ->+ let !decode = binaryIntDecoder finfo+ in \case+ Nothing -> Left "Cannot decode SQL null as the Haskell Int type. Use a `Maybe Int`"+ Just bs -> decode (PBA.fromByteString bs)++instance FromPgField Int where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = intFieldDecoder++ -- Interestingly, the notConst field decoder is a tiny little bit+ -- faster than the constFieldDecoder+ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder = \finfo ->+ let rootTypeOid = rootBaseTypeOf finfo+ in if rootTypeOid == int4Oid+ then fmap fromIntegral <$> Parser.takeInt32BEWithFieldLength+ else+ if rootTypeOid == int8Oid+ then fmap fromIntegral <$> Parser.takeInt64BEWithFieldLength+ else fmap fromIntegral <$> Parser.takeInt16BEWithFieldLength++{-# RULES+"singleField int16FieldDecoder" singleField int16FieldDecoder = fieldRowDecoder+"singleField (nullableField int16FieldDecoder)" singleField (nullableField int16FieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE int16FieldDecoder #-} -- See Note [singleField fieldDecoder rewrite rules]+int16FieldDecoder :: FieldDecoder Int16+int16FieldDecoder =+ parsePgType "Int16" [int2Oid] $ \finfo ->+ let !decode = binaryIntDecoder finfo+ in \case+ Nothing -> Left "Cannot decode SQL null as the Haskell Int16 type. Use a `Maybe Int16`"+ Just bs -> decode (PBA.fromByteString bs)++instance FromPgField Int16 where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = int16FieldDecoder+ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder = const Parser.takeInt16BEWithFieldLength++{-# RULES+"singleField int32FieldDecoder" singleField int32FieldDecoder = fieldRowDecoder+"singleField (nullableField int32FieldDecoder)" singleField (nullableField int32FieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE int32FieldDecoder #-} -- See Note [singleField fieldDecoder rewrite rules]+int32FieldDecoder :: FieldDecoder Int32+int32FieldDecoder = parsePgType "Int32" [int2Oid, int4Oid] $ \finfo ->+ let !decode = binaryIntDecoder finfo+ in \case+ Nothing -> Left "Cannot decode SQL null as the Haskell Int32 type. Use a `Maybe Int32`"+ Just bs -> decode (PBA.fromByteString bs)++instance FromPgField Int32 where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = int32FieldDecoder+ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder = const $ do+ fieldLen <- Parser.takeInt32BE+ case fieldLen of+ 4 -> Just <$> Parser.takeInt32BE+ (-1) -> pure Nothing+ 2 -> Just . fromIntegral <$> Parser.takeInt16BE+ _ -> fail "Trying to decode PG int4 but it's not 2 or 4 bytes long"++{-# RULES+"singleField int64FieldDecoder" singleField int64FieldDecoder = fieldRowDecoder+"singleField (nullableField int64FieldDecoder)" singleField (nullableField int64FieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE int64FieldDecoder #-} -- See Note [singleField fieldDecoder rewrite rules]+int64FieldDecoder :: FieldDecoder Int64+int64FieldDecoder = parsePgType "Int64" [int2Oid, int4Oid, int8Oid] $ \finfo ->+ let !decode = binaryIntDecoder finfo+ in \case+ Nothing -> Left "Cannot decode SQL null as the Haskell Int64 type. Use a `Maybe Int64`"+ Just bs -> decode (PBA.fromByteString bs)++-- | Parses smaller integer types too.+{-# INLINE int64ConstFieldDecoder #-}+int64ConstFieldDecoder :: FieldInfo -> Parser.Parser (Maybe Int64)+int64ConstFieldDecoder finfo = do+ let rootTypeOid = rootBaseTypeOf finfo+ if rootTypeOid == int4Oid+ then fmap fromIntegral <$> Parser.takeInt32BEWithFieldLength+ else+ if rootTypeOid == int8Oid+ then Parser.takeInt64BEWithFieldLength+ else fmap fromIntegral <$> Parser.takeInt16BEWithFieldLength++instance FromPgField Int64 where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = int64FieldDecoder++ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder = int64ConstFieldDecoder++{-# RULES+"singleField integerFieldDecoder" singleField integerFieldDecoder = fieldRowDecoder+"singleField (nullableField integerFieldDecoder)" singleField (nullableField integerFieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE integerFieldDecoder #-} -- See Note [singleField fieldDecoder rewrite rules]+integerFieldDecoder :: FieldDecoder Integer+integerFieldDecoder = parsePgType "Integer" [int8Oid, numericOid, int4Oid, int2Oid] $ \finfo ->+ let !decodeInt = binaryIntDecoder @Int64 finfo+ !rootTypeOid = rootBaseTypeOf finfo+ in \case+ Nothing -> Left "Cannot decode SQL null as the Haskell Integer type. Use a `Maybe Integer`"+ Just bs ->+ let !pbaBs = PBA.fromByteString bs+ in if rootTypeOid /= numericOid+ then fromIntegral <$> decodeInt pbaBs+ else case Parser.parseOnly (scientificDecoder True <* Parser.endOfInput) pbaBs of+ Parser.ParseOk sci _ -> case floatingOrInteger @Double @Integer sci of+ Right i -> Right i+ Left _ -> Left "Internal error in Hpgsql. Scientific to Integer conversion failed"+ Parser.ParseFail err -> Left err++instance FromPgField Integer where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = integerFieldDecoder++ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder = \finfo -> do+ let !decodeInt = binaryIntDecoder @Int64 finfo+ !rootTypeOid = rootBaseTypeOf finfo+ len <- fromIntegral <$> Parser.takeInt32BE+ case len of+ (-1) -> pure Nothing+ _ -> do+ pbaBs <- Parser.take len+ if rootTypeOid == numericOid+ then case Parser.parseOnly (scientificDecoder True <* Parser.endOfInput) pbaBs of+ Parser.ParseOk sci _ -> case floatingOrInteger @Double @Integer sci of+ Right i -> pure $ Just i+ Left _ -> fail "Internal error in Hpgsql. Scientific to Integer conversion failed"+ Parser.ParseFail err -> fail err+ else case decodeInt pbaBs of+ -- This case is for ints sized less than or equal to 64 bits+ Right v -> pure $ Just (fromIntegral v)+ Left err -> fail err++{-# RULES+"singleField oidFieldDecoder" singleField oidFieldDecoder = fieldRowDecoder+"singleField (nullableField oidFieldDecoder)" singleField (nullableField oidFieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE oidFieldDecoder #-} -- See Note [singleField fieldDecoder rewrite rules]+oidFieldDecoder :: FieldDecoder Oid+oidFieldDecoder = parsePgType "Oid" [oidOid] $ const $ \case+ Nothing -> Left "Cannot decode SQL null as the Haskell Oid type. Use a `Maybe Oid`"+ -- Oids are just int4+ Just bs -> Oid <$> PBA.decodeInt32BE 0 (PBA.fromByteString bs)++instance FromPgField Oid where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = oidFieldDecoder++ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder = const $ fmap Oid <$> Parser.takeInt32BEWithFieldLength++{-# RULES+"singleField floatFieldDecoder" singleField floatFieldDecoder = fieldRowDecoder+"singleField (nullableField floatFieldDecoder)" singleField (nullableField floatFieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE floatFieldDecoder #-} -- See Note [singleField fieldDecoder rewrite rules]+floatFieldDecoder :: FieldDecoder Float+floatFieldDecoder = parsePgType "Float" [float4Oid] $ const $ \case+ Nothing -> Left "Cannot decode SQL null as the Haskell Float type. Use a `Maybe Float`"+ Just bs -> binaryFloat4Decoder (PBA.fromByteString bs)++instance FromPgField Float where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = floatFieldDecoder++ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder = const Parser.takeFloatBEWithFieldLength++{-# RULES+"singleField doubleFieldDecoder" singleField doubleFieldDecoder = fieldRowDecoder+"singleField (nullableField doubleFieldDecoder)" singleField (nullableField doubleFieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE doubleFieldDecoder #-} -- See Note [singleField fieldDecoder rewrite rules]+doubleFieldDecoder :: FieldDecoder Double+doubleFieldDecoder = parsePgType "Double" [float8Oid, float4Oid] $ \finfo ->+ let !decoder+ | rootBaseTypeOf finfo == float8Oid = binaryFloat8Decoder+ | otherwise = fmap float2Double . binaryFloat4Decoder+ in \case+ Nothing -> Left "Cannot decode SQL null as the Haskell Double type. Use a `Maybe Double`"+ Just bs -> decoder (PBA.fromByteString bs)++{-# INLINE doubleRowDecoder #-}+doubleRowDecoder :: Parser.Parser (Maybe Double)+doubleRowDecoder = do+ len <- Parser.takeInt32BE+ case len of+ 8 -> Just <$> Parser.takeDoubleBE+ 4 -> Just . float2Double <$> Parser.takeFloatBE+ _ -> pure Nothing++instance FromPgField Double where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = doubleFieldDecoder++ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder = const doubleRowDecoder++-- | Allows you to specify a type (and other checks, possibly) for a `FieldDecoder`.+-- This can be useful to ensure you're not accidentally decoding a different type.+--+-- > data MyEnum = Val1 | Val2 | Val3+-- > myEnumFieldDecoderWithTypeInfoCheck :: FieldDecoder MyEnum+-- > myEnumFieldDecoderWithTypeInfoCheck =+-- > let convert = \case+-- > "val1" -> Val1+-- > "val2" -> Val2+-- > "val3" -> Val3+-- > _ -> error "Invalid value for MyEnum"+-- > in typeFieldDecoder+-- > (typeMustBeNamed "my_enum")+-- > $ convert <$> rawBytesFieldDecoder+--+-- This will work unless you use non-default flags in your connection options.+typeFieldDecoder :: (FieldInfo -> Bool) -> FieldDecoder a -> FieldDecoder a+typeFieldDecoder fieldCheck dec = dec {allowedPgTypes = fieldCheck}++typeMustBeNamed :: Text -> (FieldInfo -> Bool)+typeMustBeNamed typName = \fieldInfo ->+ (typeName <$> lookupTypeByOid fieldInfo.fieldTypeOid fieldInfo.encodingContext.typeInfoCache) == Just typName++{-# INLINE scientificDecoder #-}+scientificDecoder :: Bool -> Parser.Parser Scientific+scientificDecoder mustBeInteger = do+ ndigits <- Parser.takeInt16BE+ weight <- Parser.takeInt16BE+ sign <- Parser.takeInt16BE -- 0x0000 is positive, 0x4000 is negative, 0xC000 is NAN, 0xD000 is Positive Infinity, 0xF000 is Negative Infinity+ unless (sign == 0x0000 || sign == 0x4000) $ fail "NaN, positive or negative infinities cannot be decoded into Integer or Scientific"+ !dscale <- Parser.takeInt16BE+ when (mustBeInteger && dscale /= 0) $ fail "Decoding into `Integer` requires explicit casting with `numeric(X,0)` to force integral values"+ valueAbs <- parseAndMult ndigits (fromIntegral weight * 4) 0+ pure $ (if sign == 0x0000 then 1 else (-1)) * valueAbs+ where+ parseAndMult :: Int16 -> Int -> Scientific -> Parser.Parser Scientific+ parseAndMult 0 _ !val = pure val+ parseAndMult !ndigitsLeft !currexpon !val = do+ !digit <- fromIntegral <$> Parser.takeInt16BE+ parseAndMult (ndigitsLeft - 1) (currexpon - 4) (val + scientific digit currexpon)++{-# INLINE numericRowParser #-}+numericRowParser :: Parser.Parser (Maybe Scientific)+numericRowParser = do+ fieldLen <- Parser.takeInt32BE+ case fieldLen of+ (-1) -> pure Nothing+ _ -> Just <$> scientificDecoder False++{-# RULES+"singleField scientificFieldDecoder" singleField scientificFieldDecoder = fieldRowDecoder+"singleField (nullableField scientificFieldDecoder)" singleField (nullableField scientificFieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE scientificFieldDecoder #-} -- See Note [singleField fieldDecoder rewrite rules]+scientificFieldDecoder :: FieldDecoder Scientific+scientificFieldDecoder = parsePgType "Scientific" [numericOid, int2Oid, int4Oid, int8Oid] $ \finfo ->+ let !rootTypeOid = rootBaseTypeOf finfo+ !intdec = binaryIntDecoder @Int64 finfo+ in \case+ Nothing -> Left "Cannot decode SQL null as the Haskell Scientific type. Use a `Maybe Scientific`"+ Just bs ->+ let !pbaBs = PBA.fromByteString bs+ in if rootTypeOid /= numericOid+ then flip scientific 0 . fromIntegral <$> intdec pbaBs+ else+ -- TODO: There is loss converting from Float/Double to Scientific, but it might be quite small, so should we accept+ -- float4Oid and float8Oid here?+ case Parser.parseOnly (scientificDecoder False <* Parser.endOfInput) pbaBs of+ Parser.ParseOk sci _ -> Right sci+ Parser.ParseFail err -> Left err++instance FromPgField Scientific where+ -- See https://github.com/postgres/postgres/blob/799959dc7cf0e2462601bea8d07b6edec3fa0c4f/src/backend/utils/adt/numeric.c#L1163+ {-# INLINE fieldDecoder #-}+ fieldDecoder = scientificFieldDecoder++ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder singleColInfo =+ if rootBaseTypeOf singleColInfo == numericOid+ then numericRowParser+ else fmap (flip scientific 0 . fromIntegral) <$> int64ConstFieldDecoder singleColInfo++{-# RULES+"singleField ratioIntegerFieldDecoder" singleField ratioIntegerFieldDecoder = fieldRowDecoder+"singleField (nullableField ratioIntegerFieldDecoder)" singleField (nullableField ratioIntegerFieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE ratioIntegerFieldDecoder #-} -- See Note [singleField fieldDecoder rewrite rules]+ratioIntegerFieldDecoder :: FieldDecoder Rational+ratioIntegerFieldDecoder = toRational <$> fieldDecoder @Scientific++instance FromPgField (Ratio Integer) where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = ratioIntegerFieldDecoder++ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder = \finfo -> let sciDec = specializedFieldDecoder finfo in fmap (toRational :: Scientific -> Rational) <$> sciDec++binaryTrue :: PinnedByteArray+binaryTrue = PBA.fromByteString $ PBA.encodePgBoolean True++{-# RULES+"singleField boolFieldDecoder" singleField boolFieldDecoder = fieldRowDecoder+"singleField (nullableField boolFieldDecoder)" singleField (nullableField boolFieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE boolFieldDecoder #-} -- See Note [singleField fieldDecoder rewrite rules]+boolFieldDecoder :: FieldDecoder Bool+boolFieldDecoder = parsePgType "Bool" [boolOid] $ const $ \case+ Nothing -> Left "Cannot decode SQL null as the Haskell Bool type. Use a `Maybe Bool`"+ Just bs -> Right $ PBA.fromByteString bs == binaryTrue++{-# INLINE boolRowDecoder #-}+boolRowDecoder :: Parser.Parser (Maybe Bool)+boolRowDecoder =+ fmap (== 1) <$> Parser.takeWord8WithFieldLength++instance FromPgField Bool where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = boolFieldDecoder++ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder = const boolRowDecoder++{-# RULES+"singleField charFieldDecoder" singleField charFieldDecoder = fieldRowDecoder+"singleField (nullableField charFieldDecoder)" singleField (nullableField charFieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE charFieldDecoder #-} -- See Note [singleField fieldDecoder rewrite rules]+charFieldDecoder :: FieldDecoder Char+charFieldDecoder = parsePgType "Char" [charOid, textOid, varcharOid, nameOid, bpcharOid] $ \finfo ->+ -- The Postgres "char" type is just a byte, so we should consider+ -- not decoding it into `Char`, but rather just into Word8.+ -- This would be a breaking change, however.+ if rootBaseTypeOf finfo == charOid+ then \case+ Nothing -> Left "Cannot decode SQL null as the Haskell Char type. Use a `Maybe Char`"+ Just bs -> Right $ chr (fromIntegral (BS.head bs))+ else+ let !decodeText = (fieldDecoder @Text).fieldValueDecoder finfo+ in \mbs -> case decodeText mbs of+ Left err -> Left err+ Right t -> case Text.length t of+ 1 -> Right (Text.head t)+ 0 -> Left "Cannot parse text with zero characters into a Haskell Char type."+ _ -> Left "Cannot parse text with more than one character into a Haskell Char type."++instance FromPgField Char where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = charFieldDecoder++ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder finfo =+ if rootBaseTypeOf finfo == charOid+ then fmap (chr . fromIntegral) <$> Parser.takeWord8WithFieldLength+ else do+ mt <- specializedFieldDecoder @Text finfo+ case mt of+ Nothing -> pure Nothing+ Just t ->+ case Text.length t of+ 1 -> pure $ Just (Text.head t)+ 0 -> fail "Cannot parse text with zero characters into a Haskell Char type."+ _ -> fail "Cannot parse text with more than one character into a Haskell Char type."++instance FromPgField ByteString where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = parsePgType "byteString" [byteaOid] $ const $ \case+ Nothing -> Left "Cannot decode SQL null as the Haskell byteString type. Use a `Maybe byteString`"+ Just bs -> Right bs++instance FromPgField LBS.ByteString where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = parsePgType "ByteString" [byteaOid] $ const $ \case+ Nothing -> Left "Cannot decode SQL null as the Haskell ByteString type. Use a `Maybe ByteString`"+ Just bs -> Right $ LBS.fromStrict bs++{-# RULES+"singleField textFieldDecoder" singleField textFieldDecoder = fieldRowDecoder+"singleField (nullableField textFieldDecoder)" singleField (nullableField textFieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE textFieldDecoder #-} -- See Note [singleField fieldDecoder rewrite rules]+textFieldDecoder :: FieldDecoder Text+textFieldDecoder = parsePgType "Text" [textOid, varcharOid, nameOid, bpcharOid] $ const $ \case+ Nothing -> Left "Cannot decode SQL null as the Haskell Text type. Use a `Maybe Text`"+ Just bs -> PBA.unsafeToUtf8Text 0 (BS.length bs) (PBA.fromByteString bs)++{-# INLINE textDecoder #-}+textDecoder :: Parser.Parser (Maybe Text)+textDecoder = do+ len <- Parser.takeInt32BE+ if len >= 0+ then Just <$> Parser.takeUtf8Text (fromIntegral len)+ else pure Nothing++instance FromPgField Text where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = textFieldDecoder++ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder = const textDecoder++{-# RULES+"singleField lazyTextFieldDecoder" singleField lazyTextFieldDecoder = fieldRowDecoder+"singleField (nullableField lazyTextFieldDecoder)" singleField (nullableField lazyTextFieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE lazyTextFieldDecoder #-} -- See Note [singleField fieldDecoder rewrite rules]+lazyTextFieldDecoder :: FieldDecoder LT.Text+lazyTextFieldDecoder = LT.fromStrict <$> textFieldDecoder++{-# INLINE lazyTextDecoder #-}+lazyTextDecoder :: Parser.Parser (Maybe LT.Text)+lazyTextDecoder = fmap LT.fromStrict <$> textDecoder++instance FromPgField LT.Text where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = lazyTextFieldDecoder++ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder = const lazyTextDecoder++{-# RULES+"singleField stringFieldDecoder" singleField stringFieldDecoder = fieldRowDecoder+"singleField (nullableField stringFieldDecoder)" singleField (nullableField stringFieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE stringFieldDecoder #-} -- See Note [singleField fieldDecoder rewrite rules]+stringFieldDecoder :: FieldDecoder String+stringFieldDecoder = parsePgType "String" [textOid, varcharOid, nameOid] $ const $ \case+ Nothing -> Left "Cannot decode SQL null as the Haskell String type. Use a `Maybe String`"+ Just bs -> Text.unpack <$> PBA.unsafeToUtf8Text 0 (BS.length bs) (PBA.fromByteString bs)++{-# INLINE stringDecoder #-}+stringDecoder :: Parser.Parser (Maybe String)+stringDecoder = fmap Text.unpack <$> textDecoder++instance FromPgField String where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = stringFieldDecoder++ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder = const stringDecoder++{-# RULES+"singleField ciTextFieldDecoder" singleField ciTextFieldDecoder = fieldRowDecoder+"singleField (nullableField ciTextFieldDecoder)" singleField (nullableField ciTextFieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE ciTextFieldDecoder #-} -- See Note [singleField fieldDecoder rewrite rules]+ciTextFieldDecoder :: FieldDecoder (CI Text)+ciTextFieldDecoder = typeFieldDecoder (typeMustBeNamed "citext") $ CI.mk <$> fieldDecoder++-- | This instance does not work if you have fillTypeInfoCache disabled (that would be a non-default+-- connection option).+instance FromPgField (CI Text) where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = ciTextFieldDecoder+ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder =+ const $ fmap CI.mk <$> textDecoder++{-# RULES+"singleField ciLazyTextFieldDecoder" singleField ciLazyTextFieldDecoder = fieldRowDecoder+"singleField (nullableField ciLazyTextFieldDecoder)" singleField (nullableField ciLazyTextFieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE ciLazyTextFieldDecoder #-} -- See Note [singleField fieldDecoder rewrite rules]+ciLazyTextFieldDecoder :: FieldDecoder (CI LT.Text)+ciLazyTextFieldDecoder = typeFieldDecoder (typeMustBeNamed "citext") $ CI.mk <$> lazyTextFieldDecoder++-- | This instance does not work if you have fillTypeInfoCache disabled (that would be a non-default+-- connection option).+instance FromPgField (CI LT.Text) where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = ciLazyTextFieldDecoder+ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder =+ const $ fmap CI.mk <$> lazyTextDecoder++{-# RULES+"singleField ciStringFieldDecoder" singleField ciStringFieldDecoder = fieldRowDecoder+"singleField (nullableField ciStringFieldDecoder)" singleField (nullableField ciStringFieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE ciStringFieldDecoder #-} -- See Note [singleField fieldDecoder rewrite rules]+ciStringFieldDecoder :: FieldDecoder (CI String)+ciStringFieldDecoder = typeFieldDecoder (typeMustBeNamed "citext") $ CI.mk <$> stringFieldDecoder++-- | This instance does not work if you have fillTypeInfoCache disabled (that would be a non-default+-- connection option).+instance FromPgField (CI String) where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = ciStringFieldDecoder+ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder =+ const $ fmap CI.mk <$> stringDecoder++{-# RULES+"singleField utcTimeFieldDecoder" singleField utcTimeFieldDecoder = fieldRowDecoder+"singleField (nullableField utcTimeFieldDecoder)" singleField (nullableField utcTimeFieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE utcTimeFieldDecoder #-} -- See Note [singleField fieldDecoder rewrite rules]+utcTimeFieldDecoder :: FieldDecoder UTCTime+utcTimeFieldDecoder = parsePgType "UTCTime" [timestamptzOid] $ const $ \case+ Nothing -> Left "Cannot decode SQL null as the Haskell UTCTime type. Use a `Maybe UTCTime`"+ Just bs -> do+ -- See https://github.com/postgres/postgres/blob/50cb7505b3010736b9a7922e903931534785f3aa/src/backend/utils/adt/timestamp.c#L1909+ totalusecs <- PBA.decodeInt64BE 0 (PBA.fromByteString bs)+ let (day, timeusecs) = totalusecs `divMod` 86_400_000_000 -- USECS per day+ parsedDate = addJulianDurationClip (CalendarDiffDays 0 (fromIntegral day)) $ fromJulian 1999 12 19+ Right $ UTCTime parsedDate (picosecondsToDiffTime $ fromIntegral timeusecs * 1_000_000)++{-# INLINE utcTimeRowDecoder #-}+utcTimeRowDecoder :: Parser.Parser (Maybe UTCTime)+utcTimeRowDecoder = do+ len <- Parser.takeInt32BE+ case len of+ 8 -> do+ totalusecs <- Parser.takeInt64BE+ let (day, timeusecs) = totalusecs `divMod` 86_400_000_000 -- USECS per day+ parsedDate = addJulianDurationClip (CalendarDiffDays 0 (fromIntegral day)) $ fromJulian 1999 12 19+ pure $ Just $ UTCTime parsedDate (picosecondsToDiffTime $ fromIntegral timeusecs * 1_000_000)+ _ -> pure Nothing++instance FromPgField UTCTime where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = utcTimeFieldDecoder++ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder = const utcTimeRowDecoder++{-# RULES+"singleField unboundedUtcTimeFieldDecoder" singleField unboundedUtcTimeFieldDecoder = fieldRowDecoder+"singleField (nullableField unboundedUtcTimeFieldDecoder)" singleField (nullableField unboundedUtcTimeFieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE unboundedUtcTimeFieldDecoder #-} -- See Note [singleField fieldDecoder rewrite rules]+unboundedUtcTimeFieldDecoder :: FieldDecoder (Unbounded UTCTime)+unboundedUtcTimeFieldDecoder = parsePgType "Unbounded UTCTime" [timestamptzOid] $ const $ \case+ Nothing -> Left "Cannot decode SQL null as the Haskell Unbounded UTCTime type. Use a `Maybe (Unbounded UTCTime)`"+ Just bs -> do+ -- See https://github.com/postgres/postgres/blob/50cb7505b3010736b9a7922e903931534785f3aa/src/backend/utils/adt/timestamp.c#L1909+ totalusecs <- PBA.decodeInt64BE 0 (PBA.fromByteString bs)+ Right $+ if totalusecs == minBound+ then NegInfinity+ else+ if totalusecs == maxBound+ then PosInfinity+ else+ let (day, timeusecs) = totalusecs `divMod` 86_400_000_000 -- USECS per day+ parsedDate = addJulianDurationClip (CalendarDiffDays 0 (fromIntegral day)) $ fromJulian 1999 12 19+ in Finite $ UTCTime parsedDate (picosecondsToDiffTime $ fromIntegral timeusecs * 1_000_000)++{-# INLINE unboundedUtcTimeRowDecoder #-}+unboundedUtcTimeRowDecoder :: Parser.Parser (Maybe (Unbounded UTCTime))+unboundedUtcTimeRowDecoder = do+ len <- Parser.takeInt32BE+ case len of+ 8 -> do+ totalusecs <- Parser.takeInt64BE+ pure $+ Just $+ if totalusecs == minBound+ then NegInfinity+ else+ if totalusecs == maxBound+ then PosInfinity+ else+ let (day, timeusecs) = totalusecs `divMod` 86_400_000_000 -- USECS per day+ parsedDate = addJulianDurationClip (CalendarDiffDays 0 (fromIntegral day)) $ fromJulian 1999 12 19+ in Finite $ UTCTime parsedDate (picosecondsToDiffTime $ fromIntegral timeusecs * 1_000_000)+ _ -> pure Nothing++instance FromPgField (Unbounded UTCTime) where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = unboundedUtcTimeFieldDecoder++ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder = const unboundedUtcTimeRowDecoder++{-# RULES+"singleField zonedTimeFieldDecoder" singleField zonedTimeFieldDecoder = fieldRowDecoder+"singleField (nullableField zonedTimeFieldDecoder)" singleField (nullableField zonedTimeFieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE zonedTimeFieldDecoder #-} -- See Note [singleField fieldDecoder rewrite rules]+zonedTimeFieldDecoder :: FieldDecoder ZonedTime+zonedTimeFieldDecoder = parsePgType "ZonedTime" [timestamptzOid] $ const $ \case+ Nothing -> Left "Cannot decode SQL null as the Haskell ZonedTime type. Use a `Maybe ZonedTime`"+ Just bs -> do+ -- See https://github.com/postgres/postgres/blob/50cb7505b3010736b9a7922e903931534785f3aa/src/backend/utils/adt/timestamp.c#L1909+ totalusecs <- PBA.decodeInt64BE 0 (PBA.fromByteString bs)+ let (day, timeusecs) = totalusecs `divMod` 86_400_000_000 -- USECS per day+ parsedDate = addJulianDurationClip (CalendarDiffDays 0 (fromIntegral day)) $ fromJulian 1999 12 19+ Right $ utcToZonedTime utc $ UTCTime parsedDate (picosecondsToDiffTime $ fromIntegral timeusecs * 1_000_000)++{-# INLINE zonedTimeRowDecoder #-}+zonedTimeRowDecoder :: Parser.Parser (Maybe ZonedTime)+zonedTimeRowDecoder = do+ len <- Parser.takeInt32BE+ case len of+ 8 -> do+ totalusecs <- Parser.takeInt64BE+ let (day, timeusecs) = totalusecs `divMod` 86_400_000_000 -- USECS per day+ parsedDate = addJulianDurationClip (CalendarDiffDays 0 (fromIntegral day)) $ fromJulian 1999 12 19+ pure $ Just $ utcToZonedTime utc $ UTCTime parsedDate (picosecondsToDiffTime $ fromIntegral timeusecs * 1_000_000)+ _ -> pure Nothing++instance FromPgField ZonedTime where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = zonedTimeFieldDecoder++ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder = const zonedTimeRowDecoder++{-# RULES+"singleField unboundedZonedTimeFieldDecoder" singleField unboundedZonedTimeFieldDecoder = fieldRowDecoder+"singleField (nullableField unboundedZonedTimeFieldDecoder)" singleField (nullableField unboundedZonedTimeFieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE unboundedZonedTimeFieldDecoder #-} -- See Note [singleField fieldDecoder rewrite rules]+unboundedZonedTimeFieldDecoder :: FieldDecoder (Unbounded ZonedTime)+unboundedZonedTimeFieldDecoder = parsePgType "Unbounded ZonedTime" [timestamptzOid] $ const $ \case+ Nothing -> Left "Cannot decode SQL null as the Haskell Unbounded ZonedTime type. Use a `Maybe (Unbounded ZonedTime)`"+ Just bs -> do+ -- See https://github.com/postgres/postgres/blob/50cb7505b3010736b9a7922e903931534785f3aa/src/backend/utils/adt/timestamp.c#L1909+ totalusecs <- PBA.decodeInt64BE 0 (PBA.fromByteString bs)+ Right $+ if totalusecs == minBound+ then NegInfinity+ else+ if totalusecs == maxBound+ then PosInfinity+ else+ let (day, timeusecs) = totalusecs `divMod` 86_400_000_000 -- USECS per day+ parsedDate = addJulianDurationClip (CalendarDiffDays 0 (fromIntegral day)) $ fromJulian 1999 12 19+ in Finite $ utcToZonedTime utc $ UTCTime parsedDate (picosecondsToDiffTime $ fromIntegral timeusecs * 1_000_000)++{-# INLINE unboundedZonedTimeRowDecoder #-}+unboundedZonedTimeRowDecoder :: Parser.Parser (Maybe (Unbounded ZonedTime))+unboundedZonedTimeRowDecoder = do+ len <- Parser.takeInt32BE+ case len of+ 8 -> do+ totalusecs <- Parser.takeInt64BE+ pure $+ Just $+ if totalusecs == minBound+ then NegInfinity+ else+ if totalusecs == maxBound+ then PosInfinity+ else+ let (day, timeusecs) = totalusecs `divMod` 86_400_000_000 -- USECS per day+ parsedDate = addJulianDurationClip (CalendarDiffDays 0 (fromIntegral day)) $ fromJulian 1999 12 19+ in Finite $ utcToZonedTime utc $ UTCTime parsedDate (picosecondsToDiffTime $ fromIntegral timeusecs * 1_000_000)+ _ -> pure Nothing++instance FromPgField (Unbounded ZonedTime) where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = unboundedZonedTimeFieldDecoder++ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder = const unboundedZonedTimeRowDecoder++{-# RULES+"singleField localTimeFieldDecoder" singleField localTimeFieldDecoder = fieldRowDecoder+"singleField (nullableField localTimeFieldDecoder)" singleField (nullableField localTimeFieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE localTimeFieldDecoder #-} -- See Note [singleField fieldDecoder rewrite rules]+localTimeFieldDecoder :: FieldDecoder LocalTime+localTimeFieldDecoder = parsePgType "LocalTime" [timestampOid] $ const $ \case+ Nothing -> Left "Cannot decode SQL null as the Haskell LocalTime type. Use a `Maybe LocalTime`"+ Just bs -> do+ totalusecs <- PBA.decodeInt64BE 0 (PBA.fromByteString bs)+ let (day, timeusecs) = totalusecs `divMod` 86_400_000_000 -- USECS per day+ parsedDate = addJulianDurationClip (CalendarDiffDays 0 (fromIntegral day)) $ fromJulian 1999 12 19+ Right $ LocalTime parsedDate (timeToTimeOfDay $ picosecondsToDiffTime $ fromIntegral timeusecs * 1_000_000)++{-# INLINE localTimeRowDecoder #-}+localTimeRowDecoder :: Parser.Parser (Maybe LocalTime)+localTimeRowDecoder = do+ len <- Parser.takeInt32BE+ case len of+ 8 -> do+ totalusecs <- Parser.takeInt64BE+ let (day, timeusecs) = totalusecs `divMod` 86_400_000_000 -- USECS per day+ parsedDate = addJulianDurationClip (CalendarDiffDays 0 (fromIntegral day)) $ fromJulian 1999 12 19+ pure $ Just $ LocalTime parsedDate (timeToTimeOfDay $ picosecondsToDiffTime $ fromIntegral timeusecs * 1_000_000)+ _ -> pure Nothing++instance FromPgField LocalTime where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = localTimeFieldDecoder++ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder = const localTimeRowDecoder++{-# RULES+"singleField timeOfDayFieldDecoder" singleField timeOfDayFieldDecoder = fieldRowDecoder+"singleField (nullableField timeOfDayFieldDecoder)" singleField (nullableField timeOfDayFieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE timeOfDayFieldDecoder #-} -- See Note [singleField fieldDecoder rewrite rules]+timeOfDayFieldDecoder :: FieldDecoder TimeOfDay+timeOfDayFieldDecoder = parsePgType "TimeOfDay" [timeOid] $ const $ \case+ Nothing -> Left "Cannot decode SQL null as the Haskell TimeOfDay type. Use a `Maybe TimeOfDay`"+ Just bs -> do+ usecs <- PBA.decodeInt64BE 0 (PBA.fromByteString bs)+ Right $ timeToTimeOfDay $ picosecondsToDiffTime $ fromIntegral usecs * 1_000_000++{-# INLINE timeOfDayRowDecoder #-}+timeOfDayRowDecoder :: Parser.Parser (Maybe TimeOfDay)+timeOfDayRowDecoder = do+ len <- Parser.takeInt32BE+ case len of+ 8 -> do+ usecs <- Parser.takeInt64BE+ pure $ Just $ timeToTimeOfDay $ picosecondsToDiffTime $ fromIntegral usecs * 1_000_000+ _ -> pure Nothing++instance FromPgField TimeOfDay where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = timeOfDayFieldDecoder++ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder = const timeOfDayRowDecoder++{-# RULES+"singleField dayFieldDecoder" singleField dayFieldDecoder = fieldRowDecoder+"singleField (nullableField dayFieldDecoder)" singleField (nullableField dayFieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE dayFieldDecoder #-} -- See Note [singleField fieldDecoder rewrite rules]+dayFieldDecoder :: FieldDecoder Day+dayFieldDecoder = parsePgType "Day" [dateOid] $ const $ \case+ Nothing -> Left "Cannot decode SQL null as the Haskell Day type. Use a `Maybe Day`"+ Just bs -> do+ -- There is a very specific conversion function for these, which I poorly translated to Haskell+ -- https://github.com/postgres/postgres/blob/799959dc7cf0e2462601bea8d07b6edec3fa0c4f/src/backend/utils/adt/datetime.c#L321+ -- But I found a simpler way to do this. Let's see if it works in our property based tests+ jd <- PBA.decodeInt32BE 0 (PBA.fromByteString bs)+ Right $ addJulianDurationClip (CalendarDiffDays 0 (fromIntegral jd - 13)) $ fromJulian 2000 01 01++{-# INLINE dayRowDecoder #-}+dayRowDecoder :: Parser.Parser (Maybe Day)+dayRowDecoder =+ let int32ToDay (i32 :: Int32) = let jd = fromIntegral i32 :: Integer in addJulianDurationClip (CalendarDiffDays 0 (jd - 13)) $ fromJulian 2000 01 01+ in fmap int32ToDay <$> Parser.takeInt32BEWithFieldLength++instance FromPgField Day where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = dayFieldDecoder++ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder = const dayRowDecoder++{-# RULES+"singleField unboundedDayFieldDecoder" singleField unboundedDayFieldDecoder = fieldRowDecoder+"singleField (nullableField unboundedDayFieldDecoder)" singleField (nullableField unboundedDayFieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE unboundedDayFieldDecoder #-}+unboundedDayFieldDecoder :: FieldDecoder (Unbounded Day)+unboundedDayFieldDecoder = parsePgType "Unbounded Day" [dateOid] $ const $ \case+ Nothing -> Left "Cannot decode SQL null as the Haskell Unbounded Day type. Use a `Maybe (Unbounded Day)`"+ Just bs -> do+ -- There is a very specific conversion function for these, which I poorly translated to Haskell+ -- https://github.com/postgres/postgres/blob/799959dc7cf0e2462601bea8d07b6edec3fa0c4f/src/backend/utils/adt/datetime.c#L321+ -- But I found a simpler way to do this. Let's see if it works in our property based tests+ jd <- PBA.decodeInt32BE 0 (PBA.fromByteString bs)+ Right $+ if jd == minBound+ then NegInfinity+ else+ if jd == maxBound+ then PosInfinity+ else+ Finite $ addJulianDurationClip (CalendarDiffDays 0 (fromIntegral jd - 13)) $ fromJulian 2000 01 01++instance FromPgField (Unbounded Day) where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = unboundedDayFieldDecoder++{-# RULES+"singleField calendarDiffTimeFieldDecoder" singleField calendarDiffTimeFieldDecoder = fieldRowDecoder+"singleField (nullableField calendarDiffTimeFieldDecoder)" singleField (nullableField calendarDiffTimeFieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE calendarDiffTimeFieldDecoder #-}+calendarDiffTimeFieldDecoder :: FieldDecoder CalendarDiffTime+calendarDiffTimeFieldDecoder = parsePgType "CalendarDiffTime " [intervalOid] $ const $ \case+ Nothing -> Left "Cannot decode SQL null as the Haskell CalendarDiffTime type. Use a `Maybe CalendarDiffTime `"+ Just bs -> do+ let !pbaBs = PBA.fromByteString bs+ nMicrosecs <- PBA.decodeInt64BE 0 pbaBs+ nDays <- PBA.decodeInt32BE 8 pbaBs+ nMonths <- PBA.decodeInt32BE 12 pbaBs+ Right $ CalendarDiffTime {ctMonths = fromIntegral nMonths, ctTime = secondsToNominalDiffTime (fromIntegral nDays * 86400) + realToFrac (picosecondsToDiffTime (fromIntegral nMicrosecs * 1_000_000))}++instance FromPgField CalendarDiffTime where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = calendarDiffTimeFieldDecoder++{-# RULES+"singleField uuidFieldDecoder" singleField uuidFieldDecoder = fieldRowDecoder+"singleField (nullableField uuidFieldDecoder)" singleField (nullableField uuidFieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE uuidFieldDecoder #-}+uuidFieldDecoder :: FieldDecoder UUID+uuidFieldDecoder = parsePgType "UUID" [uuidOid] $ const $ \case+ Nothing -> Left "Cannot decode SQL null as the Haskell UUID type. Use a `Maybe UUID`"+ Just bs -> case UUID.fromByteString (LBS.fromStrict bs) of+ Just uuid -> Right uuid+ Nothing -> Left "Bug in Hpgsql: UUID field could not be decoded"++instance FromPgField UUID where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = uuidFieldDecoder+ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder = const $ do+ len <- Parser.takeInt32BE+ case len of+ (-1) -> pure Nothing+ _ ->+ fmap Just $+ UUID.fromWords64+ <$> Parser.takeWord64BE+ <*> Parser.takeWord64BE++{-# RULES+"singleField aesonFieldDecoder" singleField aesonFieldDecoder = fieldRowDecoder+"singleField (nullableField aesonFieldDecoder)" singleField (nullableField aesonFieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE aesonFieldDecoder #-} -- See Note [singleField fieldDecoder rewrite rules]+aesonFieldDecoder :: FieldDecoder Aeson.Value+aesonFieldDecoder = parsePgType "Aeson.Value" [jsonOid, jsonbOid] $ \finfo ->+ let+ -- jsonb has a byte prepended to the contents and json does not+ !fixJsonb = if rootBaseTypeOf finfo == jsonbOid then BS.drop 1 else Prelude.id+ in+ \case+ Nothing -> Left "Cannot decode SQL null as the Haskell Aeson.Value type. Use a `Maybe Aeson.Value` if you want SQL nulls"+ Just bs -> case Aeson.decodeStrict $ fixJsonb bs of+ Just d -> Right d+ Nothing -> Left "Bug in Hpgsql. Postgres produced a json or jsonb value that Aeson does not consider valid."++instance FromPgField Aeson.Value where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = aesonFieldDecoder+ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder finfo = do+ len <- fromIntegral <$> Parser.takeInt32BE+ if len == (-1)+ then pure Nothing+ else do+ bs <-+ PBA.toByteString+ <$> if rootBaseTypeOf finfo == jsonbOid+ then Parser.skip 1 >> Parser.take (len - 1)+ else Parser.take len+ case Aeson.decodeStrict bs of+ Just d -> pure (Just d)+ Nothing -> fail "Bug in Hpgsql. Postgres produced a json or jsonb value that Aeson does not consider valid."++{-# INLINE [1] nullableField #-}++-- | A FieldDecoder that accepts and decodes SQL NULLs into `Nothing` values+-- for a given decoder.+nullableField :: FieldDecoder a -> FieldDecoder (Maybe a)+nullableField FieldDecoder {..} =+ FieldDecoder+ { fieldValueDecoder = \oid ->+ let origFieldValueParser = fieldValueDecoder oid+ in \case+ Nothing -> Right Nothing+ Just bs -> Just <$> origFieldValueParser (Just bs),+ allowedPgTypes+ }++instance (FromPgField a) => FromPgField (Maybe a) where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = nullableField fieldDecoder++ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder finfo = do+ mv <- specializedFieldDecoder @a finfo+ case mv of+ Nothing -> pure Nothing+ -- Some forcing to avoid thunks keeping references to our+ -- PinnedByteArray buffers+ jv@(Just !_) -> pure $ Just jv++allowOnlyArrayTypes :: FieldInfo -> Bool+allowOnlyArrayTypes fieldInfo =+ -- TODO: We could check the elemTypeOid too, but maybe later+ case lookupTypeByOid fieldInfo.fieldTypeOid fieldInfo.encodingContext.typeInfoCache of+ Just (TypeInfo {typeDetails = ArrayType _}) -> True+ Nothing -> True -- Assume user knows what they're doing+ Just _ -> False -- Definitely not an array++{-# RULES+"singleField arrayFieldDecoder" singleField arrayFieldDecoder = fieldRowDecoder+"singleField (nullableField arrayFieldDecoder)" singleField (nullableField arrayFieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE arrayFieldDecoder #-} -- See Note [singleField fieldDecoder rewrite rules]+arrayFieldDecoder :: (FromPgField a) => FieldDecoder (Vector a)+arrayFieldDecoder = fst $ arrayFieldRowDec Vector.replicateM++instance forall a. (FromPgField a) => FromPgField (Vector a) where+ {-# INLINE fieldDecoder #-}+ fieldDecoder = arrayFieldDecoder+ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder = \finfo -> do+ len <- Parser.takeInt32BE+ case len of+ (-1) -> pure Nothing+ _ -> Just <$> snd (arrayFieldRowDec Vector.replicateM) finfo++instance {-# INCOHERENT #-} forall a. (FromPgField a) => FromPgField (Vector (Vector a)) where+ -- From https://github.com/postgres/postgres/blob/5941946d0934b9eccb0d5bfebd40b155249a0130/src/backend/utils/adt/arrayfuncs.c#L1548+ fieldDecoder =+ FieldDecoder+ { fieldValueDecoder = \colInfo ->+ let !arrfdec = arrayParser colInfo.encodingContext <* Parser.endOfInput+ in \case+ Nothing -> Left "Cannot decode SQL null as the Haskell (Vector (Vector a)) type. Use a `Maybe (Vector (Vector a))`"+ Just bs -> case Parser.parseOnly arrfdec (PBA.fromByteString bs) of+ Parser.ParseOk v _ -> Right v+ Parser.ParseFail err -> Left err,+ allowedPgTypes = allowOnlyArrayTypes+ }+ where+ fdec = fieldDecoder @a+ handleNulls p = \finfo -> do+ mv <- p+ case mv of+ Nothing -> case fdec.fieldValueDecoder finfo Nothing of+ Left err -> fail $ "Array element is NULL: " ++ err+ Right v -> pure v+ Just v -> pure v+ fieldDec = \finfo -> handleNulls (specializedFieldDecoder finfo) finfo+ arrayParser :: EncodingContext -> Parser.Parser (Vector (Vector a))+ arrayParser encodingContext = do+ !ndim <- Parser.takeInt32BE+ !_hasNull <- Parser.takeInt32BE+ !elementTypeOid :: Oid <- Oid . fromIntegral <$> Parser.takeInt32BE+ let !elementColInfo = FieldInfo elementTypeOid Nothing encodingContext+ when (ndim /= 2) $ fail $ "TODO: No support for " ++ show ndim ++ "-dimensional arrays in Hpgsql. Got array with ndim=" ++ show ndim+ unless (fdec.allowedPgTypes elementColInfo) $ fail $ "Array contains elements of type OID " ++ show elementTypeOid ++ " but decoder does not handle that type"+ numRows <- do+ !dim_i :: Int <- fromIntegral <$> Parser.takeInt32BE+ !_lb_i <- Parser.takeInt32BE+ pure dim_i+ lengthEachRow <- do+ !dim_i :: Int <- fromIntegral <$> Parser.takeInt32BE+ !_lb_i <- Parser.takeInt32BE+ pure dim_i++ Vector.replicateM numRows $ do+ Vector.replicateM lengthEachRow (fieldDec elementColInfo)++{-# INLINE genericFromPgRow #-}++-- | Derives `FromPgRow` generically.+genericFromPgRow :: forall a. (Generic a, ProductTypeDecoder (Rep a)) => RowDecoder a+genericFromPgRow = to <$> genRowDecoder @(Rep a)++class ProductTypeDecoder f where+ genRowDecoder :: RowDecoder (f a)++instance (ProductTypeDecoder a, ProductTypeDecoder b) => ProductTypeDecoder (a :*: b) where+ {-# INLINE genRowDecoder #-}+ genRowDecoder = (:*:) <$> genRowDecoder <*> genRowDecoder++instance (ProductTypeDecoder f) => ProductTypeDecoder (M1 a c f) where+ {-# INLINE genRowDecoder #-}+ genRowDecoder = M1 <$> genRowDecoder++instance (FromPgField a) => ProductTypeDecoder (K1 r a) where+ {-# INLINE genRowDecoder #-}+ -- coercing instead of fmap reduces memory usage, apparently+ -- by reducing (unnecessary) closures in the final row decoder,+ -- as per looking at GHC Core+ genRowDecoder = coerce $ fieldRowDecoder @a++genericToPgRow :: forall a. (Generic a, ProductTypeEncoder (Rep a)) => RowEncoder a+genericToPgRow = contramap from genRowEncoder++class ProductTypeEncoder f where+ genRowEncoder :: RowEncoder (f a)++instance (ProductTypeEncoder a, ProductTypeEncoder b) => ProductTypeEncoder (a :*: b) where+ genRowEncoder = divide (\(a :*: b) -> (a, b)) genRowEncoder genRowEncoder++instance (ProductTypeEncoder f) => ProductTypeEncoder (M1 i c f) where+ genRowEncoder = contramap unM1 genRowEncoder++instance (ToPgField a) => ProductTypeEncoder (K1 r a) where+ genRowEncoder = contramap unK1 singleFieldRowEncoder++-- | For the very common case of a Haskell enum matching a custom postgres enum type+-- that has its values all as lower case strings, this newtype can help you derive+-- instances as such:+--+-- > data Mood = Sad | Ok | Happy+-- > deriving stock (Generic)+-- > deriving (FromPgField, ToPgField) via (LowerCasedPgEnum Mood)+--+-- And this would match the Postgres equivalent:+--+-- > CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');+--+-- If you run into PostgreSQL type inference problems with this, you can+-- write instances manually with 'genericEnumFieldDecoder', 'genericEnumFieldEncoder',+-- 'typeFieldEncoder', and 'typeFieldDecoder'.+newtype LowerCasedPgEnum a = LowerCasedPgEnum a++instance (Generic a, EnumDecoder (Rep a)) => FromPgField (LowerCasedPgEnum a) where+ fieldDecoder = LowerCasedPgEnum <$> genericEnumFieldDecoder LT.toLower+ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder = const $ do+ enumAsText <- lazyTextDecoder+ case enumAsText of+ Nothing -> pure Nothing+ Just e -> case textToEnum e of+ Left err -> fail err+ Right v -> pure $ Just (LowerCasedPgEnum v)+ where+ textToEnum = enumFieldMapper @a LT.toLower++instance (Generic a, EnumEncoder (Rep a)) => ToPgField (LowerCasedPgEnum a) where+ fieldEncoder = untypedFieldEncoder $ \_encCtx -> \(LowerCasedPgEnum v) -> NotNull $ genericEnumFieldEncoder Text.toLower v++-- | One of the functions behind 'LowerCasedPgEnum', but you can decide+-- how to map your type's constructor names arbitrarily, which can be+-- useful if you're not using lowercase values in your postgres enums.+genericEnumFieldDecoder ::+ forall a.+ (Generic a, EnumDecoder (Rep a)) =>+ -- | A function that takes in the Haskell constructor name and returns the textual representation of the enum in postgres+ (LT.Text -> LT.Text) ->+ FieldDecoder a+genericEnumFieldDecoder nameTransform = parsePgTypeFull (const True) $ const $ \case+ Nothing -> Left "Cannot decode SQL null with the Enum decoder. Use a `Maybe` if you want SQL nulls"+ Just bs -> transform (LT.decodeUtf8 $ BS.fromStrict bs)+ where+ transform = enumFieldMapper nameTransform++-- | Returns a function that maps an enum value coming from Postgres+-- into the enum while respecting the Haskell-constructor mapping function.+enumFieldMapper ::+ forall a.+ (Generic a, EnumDecoder (Rep a)) =>+ -- | A function that takes in the Haskell constructor name and returns the textual representation of the enum in postgres+ (LT.Text -> LT.Text) ->+ (LT.Text -> Either String a)+enumFieldMapper nameTransform = \enumVal -> case Map.lookup enumVal allValuesMap of+ Nothing -> Left $ "Invalid enum value. Not one of " ++ show (Map.keys allValuesMap)+ Just v -> Right v+ where+ -- TODO: Vector of pointers to ByteStrings for a bit more memory locality? Does it make a perf difference?+ allValuesMap = Map.mapKeys nameTransform $ fmap to genEnumDecoder++class EnumDecoder f where+ -- | Returns the textual representation and constructed object for every possible+ -- value of the enum.+ genEnumDecoder :: Map LT.Text (f a)++instance (EnumDecoder a, EnumDecoder b) => EnumDecoder (a :+: b) where+ genEnumDecoder = (L1 <$> genEnumDecoder) `Map.union` (R1 <$> genEnumDecoder)++instance (EnumDecoder f) => EnumDecoder (M1 D c f) where+ genEnumDecoder = M1 <$> genEnumDecoder++-- U1 is "Unit"-type, that is: no value in the constructor, AKA "pure enum".+instance (KnownSymbol ctorName) => EnumDecoder (M1 C ('MetaCons ctorName ctorFixity 'False) U1) where+ genEnumDecoder = Map.singleton (LT.pack $ symbolVal (Proxy @ctorName)) (M1 U1)++-- | One of the functions behind 'LowerCasedPgEnum', but you can decide+-- how to map your type's constructor names arbitrarily, which can be+-- useful if you're not using lowercase values in your postgres enums.+genericEnumFieldEncoder ::+ forall a.+ (Generic a, EnumEncoder (Rep a)) =>+ -- | A function that takes in the Haskell constructor name and returns the textual representation of the enum in postgres+ (Text -> Text) ->+ a ->+ ByteString+genericEnumFieldEncoder nameTransform = encodeUtf8 . nameTransform . genEnumEncoder . from++class EnumEncoder f where+ -- | Returns the textual representation of an enum value's constructor.+ genEnumEncoder :: f a -> Text++instance (EnumEncoder a, EnumEncoder b) => EnumEncoder (a :+: b) where+ genEnumEncoder (L1 x) = genEnumEncoder x+ genEnumEncoder (R1 x) = genEnumEncoder x++instance (EnumEncoder f) => EnumEncoder (M1 D c f) where+ genEnumEncoder (M1 x) = genEnumEncoder x++-- U1 is "Unit"-type, that is: no value in the constructor, AKA "pure enum".+instance (KnownSymbol ctorName) => EnumEncoder (M1 C ('MetaCons ctorName ctorFixity 'False) U1) where+ genEnumEncoder _ = Text.pack $ symbolVal (Proxy @ctorName)++-- | Returns a `FieldEncoder` that is sent without a type OID in queries.+-- This means postgres will try to infer the type of these arguments.+-- Check `typedFieldEncoder` if you're interested in encoding your custom types,+-- you probably don't need this.+untypedFieldEncoder :: (EncodingContext -> a -> BinaryField) -> FieldEncoder a+untypedFieldEncoder enc = FieldEncoder {toTypeOid = \_ -> Nothing, toPgField = enc}++-- | A decoder that accepts any PG type and returns the object's+-- postgres' binary representation as a ByteString.+rawBytesFieldDecoder :: FieldDecoder ByteString+rawBytesFieldDecoder =+ FieldDecoder+ { fieldValueDecoder = \_oid -> \case+ Nothing -> Left "Cannot decode SQL null as the `rawBytesFieldDecoder`."+ Just bs -> Right bs,+ allowedPgTypes = const True+ }++-- | Returns a field-encoding function for a vector-like Foldable (e.g. Lists and Vector itself).+toPgVectorField :: forall f a. (Foldable f, ToPgField a) => EncodingContext -> f a -> BinaryField+toPgVectorField encCtx =+ let fe = fieldEncoder @a+ encodeElement el = Builder.binaryField $ fe.toPgField encCtx el+ Oid elemOid = fromMaybe (Oid 0) (fe.toTypeOid encCtx)+ in \vec ->+ let ndim = Builder.int32BE 1+ -- Postgres seems to build the "has_nulls" flag itself in the ReadArrayBinary function at https://github.com/postgres/postgres/blob/aa7f9493a02f5981c09b924323f0e7a58a32f2ed/src/backend/utils/adt/arrayfuncs.c#L1429, so we can just set it to 0+ hasNull = Builder.byteString $ PBA.encodeInt32BE 0+ -- hasNull = Builder.byteString $ PBA.encodeInt32BE (if Vector.any (\e -> toPgField e == Nothing) vec then 1 else 0)+ elemOidBs = Builder.byteString $ PBA.encodeInt32BE elemOid+ lb1 = Builder.byteString $ PBA.encodeInt32BE 1+ (Sum len, encodedElements) = foldMap (\el -> (Sum 1, encodeElement el)) vec+ dim1 = Builder.byteString $ PBA.encodeInt32BE len+ fullBs = ndim <> hasNull <> elemOidBs <> dim1 <> lb1 <> encodedElements+ in NotNull (Builder.toStrictByteString fullBs)++-- | A FieldDecoder that accepts and decodes Postgres arrays.+arrayField :: forall a f. (Monoid (f a)) => (forall m. (Monad m) => Int -> m a -> m (f a)) -> FieldDecoder a -> FieldDecoder (f a)+arrayField !replicateFunction !elementParser =+ -- From https://github.com/postgres/postgres/blob/5941946d0934b9eccb0d5bfebd40b155249a0130/src/backend/utils/adt/arrayfuncs.c#L1548+ FieldDecoder+ { fieldValueDecoder = \colInfo ->+ let !fdec = arrayParser colInfo.encodingContext <* Parser.endOfInput+ in \case+ Nothing -> Left "Cannot decode SQL null as the Haskell Vector type. Use a `Maybe (Vector a)`"+ Just bs -> case Parser.parseOnly fdec (PBA.fromByteString bs) of+ Parser.ParseOk v _ -> Right v+ Parser.ParseFail err -> Left err,+ allowedPgTypes = allowOnlyArrayTypes+ }+ where+ arrayParser :: EncodingContext -> Parser.Parser (f a)+ arrayParser encodingContext = do+ !ndim <- Parser.takeInt32BE+ !_hasNull <- Parser.takeInt32BE+ !elementTypeOid :: Oid <- Oid . fromIntegral <$> Parser.takeInt32BE+ let !elementColInfo = FieldInfo elementTypeOid Nothing encodingContext+ when (ndim > 1) $ fail $ "TODO: No support for multi-dimensional arrays in Hpgsql. Got array with ndim=" ++ show ndim+ if ndim == 0+ then pure mempty+ else do+ !dim_i :: Int <- fromIntegral <$> Parser.takeInt32BE+ !_lb_i <- Parser.takeInt32BE+ unless (elementParser.allowedPgTypes elementColInfo) $ fail $ "Array contains elements of type OID " ++ show elementTypeOid ++ " but decoder does not handle that type"+ replicateFunction dim_i $ do+ size :: Int <- fromIntegral <$> Parser.takeInt32BE+ if size == (-1)+ then case elementParser.fieldValueDecoder elementColInfo Nothing of+ Left err -> fail err+ Right v -> pure v+ else do+ elementBs <- Parser.take size+ -- We need to convert to ByteString here or we need to change the API in breaking fashion..+ case elementParser.fieldValueDecoder elementColInfo (Just (PBA.toByteString elementBs)) of+ Left err -> fail $ "Error parsing array element: " ++ show err+ Right el -> pure el++-- | Returns a `fieldDecoder` and a specialized parser that both accept and decodes Postgres arrays.+{-# INLINE arrayFieldRowDec #-}+arrayFieldRowDec :: forall a f. (FromPgField a, Monoid (f a)) => (forall m. (Monad m) => Int -> m a -> m (f a)) -> (FieldDecoder (f a), FieldInfo -> Parser.Parser (f a))+arrayFieldRowDec !replicateFunction =+ -- From https://github.com/postgres/postgres/blob/5941946d0934b9eccb0d5bfebd40b155249a0130/src/backend/utils/adt/arrayfuncs.c#L1548+ ( FieldDecoder+ { fieldValueDecoder = \colInfo ->+ let !dec = arrayParser colInfo.encodingContext <* Parser.endOfInput+ in \case+ Nothing -> Left "Cannot decode SQL null as the Haskell Vector type. Use a `Maybe (Vector a)`"+ Just bs -> case Parser.parseOnly dec (PBA.fromByteString bs) of+ -- Some forcing to avoid thunks keeping references to our+ -- PinnedByteArray buffers+ Parser.ParseOk !v _ -> Right v+ Parser.ParseFail err -> Left err,+ allowedPgTypes = allowOnlyArrayTypes+ },+ \finfo -> arrayParser finfo.encodingContext+ )+ where+ fdec = fieldDecoder @a+ handleNulls p = \finfo -> do+ mv <- p+ case mv of+ -- Some forcing to avoid thunks keeping references to our+ -- PinnedByteArray buffers+ Nothing -> case fdec.fieldValueDecoder finfo Nothing of+ Right !v -> pure v+ Left err -> fail $ "Array element is NULL: " ++ err+ Just !v -> pure v+ fieldDec = \finfo -> handleNulls (specializedFieldDecoder finfo) finfo+ arrayParser :: EncodingContext -> Parser.Parser (f a)+ arrayParser encodingContext = do+ !ndim <- Parser.takeInt32BE+ !_hasNull <- Parser.takeInt32BE+ !elementTypeOid :: Oid <- Oid . fromIntegral <$> Parser.takeInt32BE+ let !elementColInfo = FieldInfo elementTypeOid Nothing encodingContext+ when (ndim > 1) $ fail $ "TODO: No support for multi-dimensional arrays in Hpgsql. Got array with ndim=" ++ show ndim+ if ndim == 0+ then pure mempty+ else do+ !dim_i :: Int <- fromIntegral <$> Parser.takeInt32BE+ !_lb_i <- Parser.takeInt32BE+ unless (fdec.allowedPgTypes elementColInfo) $ fail $ "Array contains elements of type OID " ++ show elementTypeOid ++ " but decoder does not handle that type"+ let p = fieldDec elementColInfo+ replicateFunction dim_i p
src/Hpgsql/Encoding/RowDecoderMonadic.hs view
@@ -8,7 +8,7 @@ import Control.Monad (unless) import Data.Bifunctor (first) import qualified Data.List as List-import Hpgsql.Encoding (FieldInfo, RowDecoder (..))+import Hpgsql.Encoding.Internal (FieldInfo, RowDecoder (..)) import qualified Hpgsql.SimpleParser as Parser -- | Unlike @Hpgsql.Encoding.RowDecoder@, this has a @Monad@ instance.
src/Hpgsql/Internal.hs view
@@ -115,8 +115,6 @@ import Control.Exception.Safe (Exception (..), MonadThrow, SomeException, bracket, bracketOnError, finally, handleJust, mask, mask_, onException, throw, toException, tryJust) import Control.Monad (forM, forM_, join, replicateM, unless, void, when) import Data.ByteString (ByteString)-import qualified Data.ByteString as BS-import Data.ByteString.Internal (w2c) import qualified Data.ByteString.Lazy as LBS import Data.Data (Proxy (..)) import Data.Either (isLeft, isRight)@@ -136,18 +134,19 @@ import GHC.Conc (ThreadStatus (..), threadStatus) import Hpgsql.Base import qualified Hpgsql.Builder as Builder-import Hpgsql.Encoding (FieldInfo (..), FromPgRow (..), RowDecoder (..), RowEncoder (..), ToPgRow (..))-import qualified Hpgsql.Encoding.BinarySerializer as BinSer+import Hpgsql.Encoding.Internal (FieldInfo (..), FromPgRow (..), RowDecoder (..), RowEncoder (..), ToPgRow (..)) import Hpgsql.Encoding.RowDecoderMonadic (ConversionState (..), RowDecoderMonadic (..))-import Hpgsql.InternalTypes (BindComplete (..), CommandComplete (..), ConnectOpts (..), ConnectionString (..), CopyInResponse (..), CopyQueryState (..), DataRow (..), Either3 (..), EncodingContext (..), ErrorDetail (..), ErrorResponse (..), HPgConnection (..), InternalConnectionState (..), IrrecoverableHpgsqlError (..), NoData (..), NotificationResponse (..), ParseComplete (..), Pipeline (..), PostgresError (..), Query (..), QueryId (..), QueryProtocol (..), QueryState (..), ReadyForQuery (..), ResetConnectionOpts (..), ResponseMsg (..), ResponseMsgsReceived (..), RowDescription (..), SingleQuery (..), TransactionStatus (..), WeakThreadId (..), mkMutex, queryToByteString, throwIrrecoverableError)+import Hpgsql.Internal.InternalTypes (BindComplete (..), CommandComplete (..), ConnectOpts (..), ConnectionString (..), CopyInResponse (..), CopyQueryState (..), DataRow (..), Either3 (..), EncodingContext (..), ErrorDetail (..), ErrorResponse (..), HPgConnection (..), InternalConnectionState (..), IrrecoverableHpgsqlError (..), NoData (..), NotificationResponse (..), ParseComplete (..), Pipeline (..), PostgresError (..), Query (..), QueryId (..), QueryProtocol (..), QueryState (..), ReadyForQuery (..), ResetConnectionOpts (..), ResponseMsg (..), ResponseMsgsReceived (..), RowDescription (..), SingleQuery (..), TransactionStatus (..), WeakThreadId (..), mkMutex, queryToByteString, throwIrrecoverableError)+import Hpgsql.Internal.PinnedByteArray (LazyPinnedByteArray, PinnedByteArray, takePgMessageIdentAndLen)+import qualified Hpgsql.Internal.PinnedByteArray as PBA import Hpgsql.Locking (getMyWeakThreadId, withMutex) import Hpgsql.Msgs (AuthenticationMethod (..), AuthenticationResponse (..), BackendKeyData (..), Bind (..), CancelRequest (..), CopyData (..), CopyDone (..), Describe (..), Execute (..), FromPgMessage (..), NoticeResponse (..), ParameterStatus (..), Parse (..), PasswordMessage (..), PgMsgParser (..), SASLInitialResponse (..), SASLResponse (..), StartupMessage (..), Sync (..), Terminate (..), ToPgMessage (..), parsePgMessage) import qualified Hpgsql.Msgs as Msgs-import Hpgsql.Networking (recvNonBlocking, sendNonBlocking, socketWaitRead, socketWaitWrite)+import Hpgsql.Networking (RecvResult (..), SendResult (..), recvNonBlocking, sendNonBlocking, socketWaitRead, socketWaitWrite) import Hpgsql.Query (breakQueryIntoStatements) import qualified Hpgsql.ScramSHA256 as ScramSHA256 import qualified Hpgsql.SimpleParser as Parser-import Hpgsql.TypeInfo (ArrayTypeDetails (..), TypeDetails (..), TypeInfo (..), buildTypeInfoCache, builtinPgTypesMap)+import Hpgsql.TypeInfo (ArrayTypeDetails (..), DomainTypeDetails (..), TypeDetails (..), TypeInfo (..), buildTypeInfoCache, builtinPgTypesMap) import Network.Socket (AddrInfo (..)) import qualified Network.Socket as Socket import qualified Network.Socket.ByteString as SocketBS@@ -247,7 +246,7 @@ False -> throwIrrecoverableError "Socket is not marked as non-blocking, which is not supported by hpgsql. You might be running on an unsupported platform" True -> pure () socketIsClosed <- newMVar False- recvBuffer <- newIORef mempty+ recvBuffer <- newIORef PBA.emptyLazyPBA sendBuffer <- newMVar mempty socketMutex <- mkMutex encodingContext <- newMVar (EncodingContext builtinPgTypesMap)@@ -390,17 +389,17 @@ refreshTypeInfoCache conn = -- https://www.postgresql.org/docs/current/system-catalog-initial-data.html#SYSTEM-CATALOG-OID-ASSIGNMENT -- says "OIDs assigned during normal database operation are constrained to be 16384 or higher. This ensures that the range 10000—16383 is free for OIDs assigned automatically by genbki.pl or during initdb. These automatically-assigned OIDs are not considered stable, and may change from one installation to another."- let fetchPipeline = pipeline "select oid, typname, typarray, typelem, typcategory, typtype from pg_catalog.pg_type WHERE oid >= 16384"+ let fetchPipeline = pipeline "select oid, typname, typarray, typelem, typcategory, typtype, typbasetype from pg_catalog.pg_type WHERE oid >= 16384" in fillTypeInfoCache <$> fetchPipeline where fillTypeInfoCache queryResultsIO = do queryResults <- queryResultsIO- let customTypes = buildTypeInfoCache $ map (\(oid, typname, typarray, typelem, typcategory, typtype) -> TypeInfo oid typname (if typarray == 0 then Nothing else Just typarray) (toTypeDetails typelem typcategory typtype)) queryResults+ let customTypes = buildTypeInfoCache $ map (\(oid, typname, typarray, typelem, typcategory, typtype, typbasetype) -> TypeInfo oid typname (if typarray == 0 then Nothing else Just typarray) (toTypeDetails typelem typcategory typtype typbasetype)) queryResults modifyMVar_ conn.encodingContext $ \_ -> pure $ EncodingContext $ customTypes <> builtinPgTypesMap- toTypeDetails typelem typcategory typtype = case (typcategory, typtype) of+ toTypeDetails typelem typcategory typtype typbasetype = case (typcategory, typtype) of ('A', _) -> ArrayType (ArrayTypeDetails typelem) (_, 'c') -> CompositeType- (_, 'd') -> DomainType+ (_, 'd') -> DomainType (DomainTypeDetails typbasetype) (_, 'e') -> EnumType (_, 'p') -> PseudoType (_, 'r') -> RangeType@@ -507,7 +506,7 @@ Left (msgIdentChar, mPgError) -> throw IrrecoverableHpgsqlError {hpgsqlDetails = "Could not parse postgres message with ident char " <> Text.pack (show msgIdentChar) <> ". This is an internal error in Hpgsql. Please report it.", innerException = toException <$> mPgError, relatedStatement = Nothing} data ReceiveWhat a b where- ReceiveDataRows :: ReceiveWhat DataRow (ByteString, Int)+ ReceiveDataRows :: ReceiveWhat DataRow (PinnedByteArray, Int) ReceiveArbitraryMsg :: PgMsgParser a -> (Either (Char, Maybe PostgresError) a -> STM b) -> ReceiveWhat a b -- | Masks asynchronous exceptions in between the moment the message is extracted from@@ -533,13 +532,12 @@ -- So we append to the buffer up until it has been fully fetched, -- and then extract it from the buffer in one piece. (initialBuf, initialBufLen) <- receiveUntilBufferHasAtLeast 5- let charAndLength = LBS.take 5 initialBuf- let (w2c -> msgIdentChar, lenbs) = fromMaybe (error "impossible") $ LBS.uncons charAndLength- lenLeftToFetch :: Int64 = fromIntegral $ either error id (BinSer.decodeInt32BE 0 $ LBS.toStrict lenbs) - 4+ let (msgIdentChar, lenPlus4) = fromMaybe (error "impossible") $ takePgMessageIdentAndLen initialBuf+ let lenLeftToFetch :: Int = fromIntegral $ lenPlus4 - 4 fullMessageLen = 5 + lenLeftToFetch- (nowBuf, _nowBufLen) <- if initialBufLen >= fullMessageLen then pure (initialBuf, initialBufLen) else receiveUntilBufferHasAtLeast fullMessageLen- let fullMsg = LBS.take fullMessageLen nowBuf- receivedNoticeOrParameterSoTryAgain <- go msgIdentChar fullMsg fullMessageLen nowBuf+ (nowBuf, nowBufLen) <- if initialBufLen >= fullMessageLen then pure (initialBuf, initialBufLen) else receiveUntilBufferHasAtLeast fullMessageLen+ let fullMsg = PBA.copyStrictSlice 0 fullMessageLen nowBuf+ receivedNoticeOrParameterSoTryAgain <- go msgIdentChar fullMsg nowBuf nowBufLen case receivedNoticeOrParameterSoTryAgain of Nothing -> receiveNextMsgGeneric conn receiveWhat Just res -> pure res@@ -552,8 +550,9 @@ -- the recvBuffer, then we _must_ remove that message from recvBuffer. -- Ideally we'd have non-retriable STM at the type-level here. Maybe later. -- Make sure to do very little work inside `go`!- go msgIdentChar fullMsg fullMessageLen nowBuf = mask_ $ modifyIORefIO recvBuffer $ do- let bufferWithoutMsg = LBS.drop fullMessageLen nowBuf+ go msgIdentChar fullMsgPBA nowBuf nowBufLen = mask_ $ modifyIORefIO recvBuffer $ do+ let bufferWithoutMsg = PBA.fromStrict $ PBA.copyStrictSlice (PBA.length fullMsgPBA) (nowBufLen - PBA.length fullMsgPBA) nowBuf+ fullMsg = LBS.fromStrict $ PBA.toByteString fullMsgPBA handleUnexpectedMsg onNotAnyReasonableMsg = -- This could be a Notification, NOTICE or a ParameterStatus message, since these -- can be received _at any time_ according to the docs.@@ -584,13 +583,13 @@ case receiveWhat of ReceiveDataRows -> -- Parse as many DataRows as we can to do as much work as we can per buffer "churn"- let fullBuf = LBS.toStrict nowBuf- in case Parser.parseOnly Parser.parseManyRows fullBuf of- Parser.ParseOk (unconsumedBufferBegin, nRowsParsed) | nRowsParsed > 0 -> do- let (msgs, unconsumedBuffer) = BS.splitAt unconsumedBufferBegin.idx fullBuf- debugPrint $ "Received " ++ show nRowsParsed ++ " messages with total length " ++ show (BS.length msgs)- pure (LBS.fromStrict unconsumedBuffer, Just (msgs, nRowsParsed))- _ -> handleUnexpectedMsg $ const $ pure ("", 0) -- No error when we stop receiving DataRows, only emptiness+ let strictNowBuf = PBA.toStrict nowBuf+ in case Parser.parseOnly Parser.parseManyRows strictNowBuf of+ Parser.ParseOk nRowsParsed unconsumedBufferBegin | nRowsParsed > 0 -> do+ let (msgs, unconsumedBuffer) = PBA.splitAt unconsumedBufferBegin.idx strictNowBuf+ debugPrint $ "Received " ++ show nRowsParsed ++ " messages with total length " ++ show (PBA.length msgs)+ pure (PBA.fromStrict unconsumedBuffer, Just (msgs, nRowsParsed))+ _ -> handleUnexpectedMsg $ const $ pure (PBA.emptyPBA, 0) -- No error when we stop receiving DataRows, only emptiness ReceiveArbitraryMsg parser f -> case parsePgMessage msgIdentChar fullMsg parser of Just msg -> do@@ -601,10 +600,10 @@ -- \| Appends into the internal buffer by reading from the socket -- until the buffer has at least N bytes. -- Returns the current buffer and its length.- receiveUntilBufferHasAtLeast :: Int64 -> IO (LBS.ByteString, Int64)+ receiveUntilBufferHasAtLeast :: Int -> IO (LazyPinnedByteArray, Int) receiveUntilBufferHasAtLeast minBytesNecessary = do currentBuffer <- readIORef recvBuffer- let nBytesInBuffer = LBS.length currentBuffer+ let nBytesInBuffer = PBA.lazyLength currentBuffer if nBytesInBuffer >= minBytesNecessary then pure (currentBuffer, nBytesInBuffer) else do@@ -612,8 +611,12 @@ -- or an exception is thrown when receiving. mask $ \restore -> rethrowAsIrrecoverable $ do restore $ socketWaitRead socket- someBytes <- timeDebugNonBlockingOperation "recv" $ recvNonBlocking socket (max conn.connOpts.recvChunkSize $ fromIntegral $ minBytesNecessary - nBytesInBuffer)- atomicWriteIORef recvBuffer (currentBuffer <> LBS.fromStrict someBytes)+ recvRes <- timeDebugNonBlockingOperation "recv" $ recvNonBlocking socket (max conn.connOpts.recvChunkSize $ fromIntegral $ minBytesNecessary - nBytesInBuffer)+ case recvRes of+ RecvSuccess someBytes -> atomicWriteIORef recvBuffer (currentBuffer `PBA.append` someBytes)+ RecvRemotePeerClosed ->+ throwIrrecoverableError "recv: Remote peer has closed the connection"+ RecvTryAgain -> pure () receiveUntilBufferHasAtLeast minBytesNecessary sendCancellationRequest :: HPgConnection -> IO ()@@ -697,7 +700,7 @@ let go = do others <- modifyMVar conn.sendBuffer $ \case [] -> pure ([], [])- ((!msgs, afterSentTxn) : xs) ->+ buff@((!msgs, afterSentTxn) : xs) -> if LBS.null msgs then do -- debugPrint "Finished sending msgs"@@ -705,11 +708,15 @@ pure (xs, xs) else do restore $ socketWaitWrite socket- n <- timeDebugNonBlockingOperation "sendNonBlocking" $ sendNonBlocking socket msgs- -- debugPrint $ "Sent " ++ show n ++ ". Left: " ++ show (LBS.length (LBS.drop n msgs))- let !remaining = LBS.drop n msgs- fin = (remaining, afterSentTxn) : xs- pure (fin, fin)+ sendRes <- timeDebugNonBlockingOperation "sendNonBlocking" $ sendNonBlocking socket msgs+ case sendRes of+ SendSuccess n -> do+ -- debugPrint $ "Sent " ++ show n ++ ". Left: " ++ show (LBS.length (LBS.drop n msgs))+ let !remaining = LBS.drop n msgs+ fin = (remaining, afterSentTxn) : xs+ pure (fin, fin)+ SendTryAgain -> pure (buff, buff) -- `man 2 select` says "even if a file descriptor indicates as writable, a large write may still block"+ SendRemotePeerClosed -> throwIrrecoverableError "send: Remote peer has closed the connection" -- debugPrint $ show $ stop || null others unless (null others) go@@ -730,7 +737,7 @@ withControlMsgsLock conn -- Check last received message and take lock before receiving next message- (const getQueryStateIfFirstOrThrow)+ (getQueryStateIfSafeToReceiveMsg qryId thisThreadId) (const $ pure ()) $ \qryState -> do case responseMsgsState qryState of@@ -767,42 +774,12 @@ Middle3 msg -> RespCommandComplete msg Left3 msg -> RespErrorResponse msg receiveReadyForQueryAtomically = receiveNextMsgWithMaskedContinuation conn (msgParser @ReadyForQuery) $ \rq -> updateQueryStateIfFirstOrThrow $ RespReadyForQuery rq- getQueryStateIfFirstOrThrow :: STM QueryState- getQueryStateIfFirstOrThrow = updateConnStateTxn conn $ \sttv -> do- st <- STM.readTVar sttv- case currentPipeline st of- [] -> throwIrrecoverableError $ "QueryId " <> Text.pack (show qryId) <> " does not exist because the pipeline is empty. This is most likely a bug in Hpgsql, but just in case, are you trying to consume a pipeline that no longer exists?"- queries- | all ((> qryId) . queryIdentifier) queries -> throwIrrecoverableError $ "Bug in Hpgsql: trying to receive outstanding messages for a pipeline that has already been fully consumed. Information about this pipeline no longer available in internal state.: " <> Text.pack (show (qryId, queries))- | any ((/= thisThreadId) . queryOwner) queries -> throwIrrecoverableError "Hpgsql does not support consuming different SQL statements' results of the same pipeline from different threads. Behaviour is undefined if you try that."- (splitQueries -> (earlierQueries, thisQuery))- | any queryInError earlierQueries -> throwIrrecoverableError "Another query in the same pipeline threw an error"- | not (all queryComplete earlierQueries) -> throwIrrecoverableError "Are you trying to consume a statement's results before consuming the results of previous statements of the same pipeline? Hpgsql does not support that. It is also possible a previous statement in the pipeline threw an irrecoverable error, and you still tried to consume another statement's results, which is also not supported."- | otherwise -> pure thisQuery - splitQueries :: [QueryState] -> ([QueryState], QueryState)- splitQueries qs = case List.span ((< qryId) . queryIdentifier) qs of- (_, []) -> error $ "Could not find query with right Id: " ++ show (qs, qryId)- (as, firstQuery : _others)- | queryIdentifier firstQuery == qryId -> (as, firstQuery)- | otherwise -> error $ "Could not find query right Id (part 2): " ++ show (qs, qryId)-- queryComplete :: QueryState -> Bool- queryComplete QueryState {responseMsgsState} = case responseMsgsState of- CommandCompleteReceived _ _ -> True- _ -> False-- queryInError :: QueryState -> Bool- queryInError QueryState {responseMsgsState} = case responseMsgsState of- ErrorResponseReceived _ _ -> True- ReadyForQueryReceived (Left ErrorResponse {}) _ -> True- _ -> False- updateQueryStateIfFirstOrThrow :: ResponseMsg -> STM (Maybe ResponseMsg, ResponseMsgsReceived) updateQueryStateIfFirstOrThrow respMsg = updateConnStateTxn conn $ \sttv -> do -- IMPORTANT: No `STM.retry` here as this is called unmasked and must terminate promptly! st <- STM.readTVar sttv- firstPendingQry <- getQueryStateIfFirstOrThrow+ firstPendingQry <- getQueryStateIfSafeToReceiveMsg qryId thisThreadId sttv (newState, newPrepStmtNames) <- case (respMsg, firstPendingQry.responseMsgsState) of (RespParseComplete msg, NoMsgsReceived) -> -- Add the parsed statement name to internal state if there is one@@ -844,9 +821,46 @@ } pure (Just respMsg, newState) +-- | Given a `QueryId` and this thread's `WeakThreadId`, returns the Query Id's `QueryState`+-- if that is the active query in the pipeline.+-- If there's no active pipeline, or if the desired Query Id does not exist in the active pipeline,+-- or if the active pipeline is owned by a different thread, or if previous queries in the pipeline+-- have erred or haven't all completed, this will throw an exception.+-- Basically, this throws unless it's time to receive _some_ message related to the supplied Query ID.+getQueryStateIfSafeToReceiveMsg :: QueryId -> WeakThreadId -> TVar InternalConnectionState -> STM QueryState+getQueryStateIfSafeToReceiveMsg qryId thisThreadId sttv = do+ st <- STM.readTVar sttv+ case currentPipeline st of+ [] -> throwIrrecoverableError $ "QueryId " <> Text.pack (show qryId) <> " does not exist because the pipeline is empty. This is most likely a bug in Hpgsql, but just in case, are you trying to consume a pipeline that no longer exists?"+ queries+ | all ((> qryId) . queryIdentifier) queries -> throwIrrecoverableError $ "Trying to receive outstanding messages for a pipeline that has already been fully consumed. Information about this pipeline no longer available in internal state.: " <> Text.pack (show (qryId, queries))+ | any ((/= thisThreadId) . queryOwner) queries -> throwIrrecoverableError "Hpgsql does not support consuming different SQL statements' results of the same pipeline from different threads. Behaviour is undefined if you try that."+ (splitQueries -> (earlierQueries, thisQuery))+ | any queryInError earlierQueries -> throwIrrecoverableError "Another query in the same pipeline threw an error"+ | not (all queryComplete earlierQueries) -> throwIrrecoverableError "Are you trying to consume a statement's results before consuming the results of previous statements of the same pipeline? Hpgsql does not support that. It is also possible a previous statement in the pipeline threw an irrecoverable error, and you still tried to consume another statement's results, which is also not supported."+ | otherwise -> pure thisQuery+ where+ splitQueries :: [QueryState] -> ([QueryState], QueryState)+ splitQueries qs = case List.span ((< qryId) . queryIdentifier) qs of+ (_, []) -> error $ "Could not find query with right Id: " ++ show (qs, qryId)+ (as, firstQuery : _others)+ | queryIdentifier firstQuery == qryId -> (as, firstQuery)+ | otherwise -> error $ "Could not find query right Id (part 2): " ++ show (qs, qryId)++queryComplete :: QueryState -> Bool+queryComplete QueryState {responseMsgsState} = case responseMsgsState of+ CommandCompleteReceived _ _ -> True+ _ -> False++queryInError :: QueryState -> Bool+queryInError QueryState {responseMsgsState} = case responseMsgsState of+ ErrorResponseReceived _ _ -> True+ ReadyForQueryReceived (Left ErrorResponse {}) _ -> True+ _ -> False+ -- | A sequence of all the bytes of one or more DataRow messages and the total -- number of DataRow messages.-newtype DataRows = DataRows (ByteString, Int)+newtype DataRows = DataRows (PinnedByteArray, Int) -- | After sending one or more queries to the backend, run this function for each query to fetch that query's results. -- You must call the returned IO function and consume the returned Stream completely until you get to the@@ -893,6 +907,21 @@ let allOtherRows = S.unfold ( \() -> do+ -- Without this check, consuming a Stream would be essentially an unchecked and+ -- direct window into the socket, meaning users might advance a Stream, run another+ -- query, put the connection back into the pool, etc., and then try to advance+ -- that same first Stream again, maybe fetching rows from another query or deadlocking+ -- (because there are no DataRows to be received).+ -- While we document Streams must be linearly consumed or else undefined behaviour,+ -- this check proved to have no performance cost in our benchmarks. It might have a cost+ -- when receiving e.g. very large text values because there's little multiple-rows batching+ -- in that case, but still it's a very healthy check to have.+ -- Streams are still not thread-safe, and there's still a check-then-receive race condition.+ -- But that is so adversarial (and we have documented Streams must be consumed by a single thread)+ -- that it's not worth worrying about.+ qryState <- STM.atomically $ getQueryStateIfSafeToReceiveMsg qryId thisThreadId conn.internalConnectionState+ when (queryComplete qryState || queryInError qryState) $ throwIrrecoverableError "Cannot receive rows of a query that has already failed or completed"+ mRow@(_, nRows) <- receiveNextMsgGeneric conn ReceiveDataRows if nRows > 0 then pure $ Right (DataRows mRow :> ())@@ -1244,14 +1273,14 @@ -- you should never consume it more than once, for it _will_ give you wrong results if you do that. -- This is normal behaviour for IO Streams, but it's worth emphasizing. pipelineSWith :: RowDecoder a -> Query -> Pipeline (IO (Stream (Of a) IO ()))-pipelineSWith rowparser@(RowDecoder _ _ expectedColFmts) (lastAndInitNE . breakQueryIntoStatements -> (firstQueriesToSend, lastQueryToSend)) =+pipelineSWith rowDec (lastAndInitNE . breakQueryIntoStatements -> (firstQueriesToSend, lastQueryToSend)) = Pipeline- (map (,Nothing) firstQueriesToSend ++ [(lastQueryToSend, Just expectedColFmts)])+ (map (,Nothing) firstQueriesToSend ++ [(lastQueryToSend, Just rowDec.numExpectedColumns)]) ( \conn qryIds -> do case lastAndInit qryIds of (firstQueries, mLastQry) -> do forM_ firstQueries $ consumeResultsIgnoreRows conn- pure $ consumeStreamingResults (ApplicativeRowDecoder rowparser) conn (fromMaybe (error "pipelineS internal bug: no mLastQry") mLastQry)+ pure $ consumeStreamingResults (ApplicativeRowDecoder rowDec) conn (fromMaybe (error "pipelineS internal bug: no mLastQry") mLastQry) ) -- | Prefer to use 'pipelineS' and 'pipelineSWith', because 'RowDecoder' can typecheck PostgreSQL results@@ -1428,7 +1457,7 @@ S.mapM ( \(DataRows (rowColumnData, nRows)) -> case Parser.parseOnly (replicateM nRows rowparser <* Parser.endOfInput) rowColumnData of- Parser.ParseOk rows -> pure rows+ Parser.ParseOk rows _ -> pure rows Parser.ParseFail err -> throwIrrecoverableErrorWithStatement qText $ "Failed parsing a row: " <> Text.pack (show err) ) rowsStream@@ -1526,15 +1555,32 @@ SomeMessage $ Bind {paramsValuesInOrder = map snd paramOidsAndValues, resultColumnFmts = 0, preparedStmtHash = Nothing}, -- We don't send Msgs.Describe because we expect CopyInResponse in place of NoData SomeMessage Execute,- SomeMessage Msgs.Flush -- This might not be necessary for COPY, but possibly useful if the user calls this with not-a-COPY statement so we get errors earlier?+ SomeMessage Sync ] ) (pure ()) $ \(qryId :| _) -> do- void $ receiveOutstandingResponseMsgsAtomically thisThreadId conn qryId- void $ receiveOutstandingResponseMsgsAtomically thisThreadId conn qryId- void $ receiveOutstandingResponseMsgsAtomically thisThreadId conn qryId- copyFn qryId+ -- Keep receiving until we're either in copy-in mode (CopyInResponse) or Parse/Bind/+ -- Execute failed and we've drained all the way to ReadyForQuery+ let receiveUntilCopyReadyOrError = do+ (_, st) <- receiveOutstandingResponseMsgsAtomically thisThreadId conn qryId+ case st of+ RowDescriptionOrNoDataOrCopyInResponseReceived (Right3 CopyInResponse) -> pure Nothing+ RowDescriptionOrNoDataOrCopyInResponseReceived _notCopyInResponse -> throwIrrecoverableErrorWithStatement queryString "Got a response not for COPY statements from Postgres. Are you sure this is a valid COPY statement?"+ ReadyForQueryReceived (Left err) _ -> pure $ Just err+ ReadyForQueryReceived (Right _) _ -> pure Nothing+ ParseCompleteReceived _ -> receiveUntilCopyReadyOrError+ BindCompleteReceived _ -> receiveUntilCopyReadyOrError+ ErrorResponseReceived _ _ -> receiveUntilCopyReadyOrError -- We want to receive ReadyForQuery before continuing+ _ -> throwIrrecoverableErrorWithStatement queryString "Received unknown message from postgres after sending COPY statement"+ mErr <- receiveUntilCopyReadyOrError+ case mErr of+ Just err -> throwPostgresError queryString err+ Nothing ->+ -- We expect copyFn to send rows, Sync, and wait until ReadyForQuery.+ -- This is an internal method, and this expectation is part of+ -- our contract.+ copyFn qryId -- | Copies rows into a table with the binary COPY protocol. -- This must be a "COPY table FROM STDIN WITH (FORMAT BINARY)"-like statement.@@ -1619,6 +1665,8 @@ putCopyData :: HPgConnection -> ByteString -> IO () putCopyData conn t = nonAtomicSendMsg conn $ CopyData (Builder.byteString t) +-- | Aborts an ongoing COPY operation with an abort message. This cannot be used+-- with `withCopy` and `withCopy_` yet. putCopyError :: HPgConnection -> String -> IO () putCopyError conn causeForFailure = withControlMsgsLock conn (const $ pure ()) (const $ pure ()) $ const $ do mCopyState <-@@ -1635,6 +1683,8 @@ [qs@QueryState {queryProtocol = CopyQuery StillCopying}] -> STM.writeTVar (internalConnectionState conn) $ st {currentPipeline = [qs {queryProtocol = CopyQuery CopyFailAndSyncSent}]} _ -> throwIrrecoverableError "Impossible: when marking CopyFail state was invalid" atomicallySendControlMsgs_ conn ([SomeMessage $ Msgs.CopyFail causeForFailure, SomeMessage Sync], markCopyFailSent)+ -- Drain the backend's next messages, which should be ErrorResponse and ReadyForQuery+ cancelActiveStatement conn True Just copyState -> throwIrrecoverableError $ "Current COPY command is not in a cancellable state. Its current state is " <> Text.pack (show copyState) Nothing ->@@ -1646,13 +1696,13 @@ qryId <- STM.atomically $ do st <- STM.readTVar conn.internalConnectionState case st.currentPipeline of- [] -> throwIrrecoverableErrorWithStatement "Ending a COPY statement" "No active COPY statement when running copyEnd"+ [] -> throwIrrecoverableErrorWithStatement "Call to copyEnd" "No active COPY statement when running copyEnd" [qs] -> case qs.queryProtocol of- ExtendedQuery -> throwIrrecoverableErrorWithStatement "Ending a COPY statement" "No active COPY statement when running copyEnd, but rather there was a regular query"- CopyQuery StillCopying -> if qs.queryOwner == thisThreadId then pure qs.queryIdentifier else throwIrrecoverableErrorWithStatement "Ending a COPY statement" "Active COPY statement was issued by a different thread, and Hpgsql does not support multiple threads running/ending the same COPY statement"- CopyQuery CopyDoneAndSyncSent -> throwIrrecoverableErrorWithStatement "Ending a COPY statement" "Active COPY statement was already finished"- CopyQuery CopyFailAndSyncSent -> throwIrrecoverableErrorWithStatement "Ending a COPY statement" "Active COPY statement had previously failed"- _ -> throwIrrecoverableErrorWithStatement "Ending a COPY statement" "Active pipeline with other statements running when a copyEnd was attempted"+ ExtendedQuery -> throwIrrecoverableErrorWithStatement "Call to copyEnd" "No active COPY statement when running copyEnd, but rather there was a regular query"+ CopyQuery StillCopying -> if qs.queryOwner == thisThreadId then pure qs.queryIdentifier else throwIrrecoverableErrorWithStatement "Call to copyEnd" "Active COPY statement was issued by a different thread, and Hpgsql does not support multiple threads running/ending the same COPY statement"+ CopyQuery CopyDoneAndSyncSent -> throwIrrecoverableErrorWithStatement "Call to copyEnd" "Active COPY statement was already finished"+ CopyQuery CopyFailAndSyncSent -> throwIrrecoverableErrorWithStatement "Call to copyEnd" "Active COPY statement had previously failed"+ _ -> throwIrrecoverableErrorWithStatement "Call to copyEnd" "Active pipeline with other statements running when a copyEnd was attempted" copyEndInternal conn qryId
+ src/Hpgsql/Internal/InternalTypes.hs view
@@ -0,0 +1,503 @@+module Hpgsql.Internal.InternalTypes+ ( -- * Simple types+ ConnectionString (..),+ ConnectOpts (..),+ PostgresError (..),+ IrrecoverableHpgsqlError (..),+ ErrorDetail (..),+ NotificationResponse (..),+ ResetConnectionOpts (..),+ TransactionStatus (..),+ EncodingContext (..), -- re-exported from Hpgsql.TypeInfo+ throwIrrecoverableError,++ -- * Query types (moved from Hpgsql.Query)+ SingleQueryFragment (..),+ Query (..),+ SingleQuery (..),+ queryToByteString,+ breakQueryIntoStatements,+ renumberParamsFrom,++ -- * Msgs types (moved to avoid cycles)+ ParseComplete (..),+ BindComplete (..),+ NoData (..),+ RowDescription (..),+ CopyInResponse (..),+ ErrorResponse (..),+ CommandComplete (..),+ DataRow (..),+ ReadyForQuery (..),++ -- * Internal connection state types (moved to avoid cycles)+ InternalConnectionState (..),+ WeakThreadId (..),+ QueryState (..),+ QueryId (..),+ QueryProtocol (..),+ CopyQueryState (..),+ ResponseMsgsReceived (..),+ ResponseMsg (..),+ Either3 (..),++ -- * Connection and Pipeline types+ HPgConnection (..),+ Mutex (..),+ Pipeline (..),+ mkMutex,+ )+where++import Control.Concurrent (ThreadId)+import Control.Concurrent.MVar (MVar)+import Control.Concurrent.STM (STM, TQueue, TVar)+import Control.Exception.Safe (Exception (..), MonadThrow, SomeException, throw)+import Data.ByteString (ByteString)+import qualified Data.ByteString.Lazy as LBS+import Data.Int (Int32, Int64)+import qualified Data.List as List+import Data.List.NonEmpty (NonEmpty (..))+import qualified Data.List.NonEmpty as NE+import Data.Map.Strict (Map)+import Data.String (IsString (..))+import Data.Text (Text)+import qualified Data.Text as Text+import Data.Text.Encoding (encodeUtf8)+#if MIN_VERSION_base(4,19,0)+import Data.Word (Word16, Word64)+#else+import Data.Word (Word16)++#endif+import qualified Control.Concurrent.STM as STM+import Data.Hashable (hash)+import Data.IORef (IORef)+import Data.Set (Set)+import Hpgsql.Base (lastTwoAndInit, maximumOnOrDef, minimumOnOrDef)+import Hpgsql.Builder (BinaryField)+import Hpgsql.Internal.PinnedByteArray (LazyPinnedByteArray, PinnedByteArray)+import Hpgsql.ParsingInternal (BlockOrNotBlock (..), ParsingOpts (..), parseSql)+import Hpgsql.TransactionStatusInternal (TransactionStatus (..))+import Hpgsql.TypeInfo (EncodingContext (..), Oid (..))+import Network.Socket (AddrInfo, Socket)+import System.Mem.Weak (Weak)++-- | A fragment of a single SQL statement, which is either static SQL or+-- a placeholder for a query argument.+data SingleQueryFragment+ = FragmentOfStaticSql !ByteString+ | FragmentWithSemiColon+ | FragmentOfCommentsOrWhitespace !ByteString+ | -- | The number/index of the query argument, starting from 1 in a single statement+ QueryArgumentPlaceHolder !Int+ deriving stock (Eq, Show)++-- | `Query` is some SQL with values of query arguments inside and also whether it's a prepared statement.+-- It can be concatenated to other `Query` objects freely, and concatenating chains of Queries with+-- at least one prepared statement among them makes the final `Query` object a prepared statement, too.+-- You can build this with the `sql` and `sqlPrep` quasiquoters, the former for unprepared statements,+-- and you can use `preparedStatement` and `nonPreparedStatement` to convert between them.+data Query = Query+ { -- The query parameters and query fragments continue to go up in number across different+ -- statements, e.g. "SELECT $1; SELECT $2; SELECT $3; ...".+ queryString :: ![SingleQueryFragment],+ queryParams :: ![EncodingContext -> (Maybe Oid, BinaryField)],+ isPrepared :: !Bool+ }++instance Show Query where+ show = show . NE.toList . breakQueryIntoStatements++queryToByteString :: Query -> ByteString+queryToByteString = mconcat . map (\SingleQuery {queryString} -> queryString) . NE.toList . breakQueryIntoStatements++instance Semigroup Query where+ q1 <> q2 =+ let maxArgQ1 =+ maximumOnOrDef+ 0+ q1.queryString+ ( \case+ QueryArgumentPlaceHolder n -> Just n+ _ -> Nothing+ )+ (_, remappedQ2) =+ renumberParamsFrom+ q2.queryString+ (maxArgQ1 + 1)+ in -- Semigroup concatenation must be associative, and for isPrepared+ -- both || and && are associative, but what would the user expect?+ -- For typical query concatenation, I *think* of some examples:+ -- - [sqlPrep|SELECT ^{columnsQry} FROM some_table ^{whereConditions}|]+ -- - [sqlPrep|INSERT INTO x ^{vALUES rows}|]+ -- In both cases above, we want `isPrepared = True` to be "infectious", so+ -- we want to use ||+ -- Are there counter examples? Yes, if users build "smaller" embeddable Queries+ -- with `sqlPrep`, e.g. if `columnsQry` or `whereConditions` were built with `sqlPrep`.+ -- That feels unlikely.. so we go with ||+ Query {queryString = q1.queryString <> remappedQ2, queryParams = q1.queryParams <> q2.queryParams, isPrepared = q1.isPrepared || q2.isPrepared}++instance Monoid Query where+ mempty = Query {queryString = [], queryParams = [], isPrepared = False}++-- | A single statement, not multiple, with dollar-numbered query arguments+-- starting from $1.+data SingleQuery = SingleQuery {queryString :: !ByteString, queryParams :: ![EncodingContext -> (Maybe Oid, BinaryField)], preparedStmtHash :: !(Maybe String)}++mkSingleQuery :: ByteString -> [EncodingContext -> (Maybe Oid, BinaryField)] -> Bool -> SingleQuery+mkSingleQuery queryString queryParams isPrepared = SingleQuery {queryString, queryParams, preparedStmtHash = if isPrepared then Just (show $ hash queryString) else Nothing}++instance Show SingleQuery where+ -- Careful not exposing query arguments+ show (SingleQuery {queryString}) = show queryString++instance IsString Query where+ fromString s =+ let blocks = parseSql AcceptOnlyDollarNumberedArgs (Text.pack s)+ queryFrags =+ map+ ( \case+ StaticSql t -> FragmentOfStaticSql $ encodeUtf8 t+ SemiColon -> FragmentWithSemiColon+ CommentsOrWhitespace t -> FragmentOfCommentsOrWhitespace $ encodeUtf8 t+ DollarNumberedArg _ ->+ error "Dollar-numbered query arguments are not supported in string literals. Use the sql quasiquoter or mkQuery instead."+ QuestionMarkArg ->+ error "Question mark query arguments are not supported in string literals. Use the sql quasiquoter or mkQuery instead."+ QuasiQuoterExpression _ _ ->+ error "Bug in Hpgsql: parseSql AcceptOnlyDollarNumberedArgs returned quasiquoter expression."+ )+ blocks+ in Query {queryString = queryFrags, queryParams = [], isPrepared = False}++-- | For internal usage only. Takes a `Query` and breaks it up into+-- individual SQL statements that can be sent to a postgres backend.+breakQueryIntoStatements :: Query -> NonEmpty SingleQuery+breakQueryIntoStatements Query {queryString = fullQueryString, queryParams = allQueryParams, isPrepared} =+ -- If a user insists in trying to run an empty query, or if+ -- they mistakenly concatenate empty statements, we let them+ toEmptyQueryIfNecessary $+ map toSingleQuery $+ -- If a query string is "SELECT 1; -- comments and empty space", we put+ -- all comments and whitespace together with that last "real" SQL statement+ fixLastEmptyStatement $+ go fullQueryString allQueryParams+ where+ toEmptyQueryIfNecessary [] = NE.singleton $ mkSingleQuery "" allQueryParams isPrepared+ toEmptyQueryIfNecessary (x : xs) = x :| xs+ toSingleQuery (blks, prms) = let queryString = mconcat $ map fragToBytestring blks in mkSingleQuery queryString prms isPrepared+ allWhitespaceOrComments =+ all+ ( \case+ FragmentOfCommentsOrWhitespace _ -> True+ _ -> False+ )+ fixLastEmptyStatement :: [([SingleQueryFragment], [EncodingContext -> (Maybe Oid, BinaryField)])] -> [([SingleQueryFragment], [EncodingContext -> (Maybe Oid, BinaryField)])]+ fixLastEmptyStatement indivStmts = case lastTwoAndInit indivStmts of+ (_, Nothing) -> indivStmts -- Only 0 or 1 statements found+ (firstStmts, Just (secLst, lst))+ | allWhitespaceOrComments (fst lst) -> firstStmts ++ [secLst <> lst]+ | otherwise -> indivStmts+ isLastFragmentOfAStatement = \case+ FragmentWithSemiColon -> True+ _ -> False+ fragToBytestring = \case+ QueryArgumentPlaceHolder n -> "$" <> intToBs n+ FragmentOfStaticSql t -> t+ FragmentWithSemiColon -> ";"+ FragmentOfCommentsOrWhitespace t -> t+ go :: [SingleQueryFragment] -> [EncodingContext -> (Maybe Oid, BinaryField)] -> [([SingleQueryFragment], [EncodingContext -> (Maybe Oid, BinaryField)])]+ go [] [] = []+ go [] _ = error $ "Hpgsql error: empty query fragment list but outstanding query params. Number of query arguments is " ++ show (length allQueryParams) ++ " and query fragments are " ++ show fullQueryString+ go frags params =+ let (stmtFrags, nextFrags) = case List.break isLastFragmentOfAStatement frags of+ (firstStmts, []) -> (firstStmts, [])+ (firstStmts, semiColon : next) -> (firstStmts ++ [semiColon], next)+ (maxArgNum, thisQueryFrags) = renumberParamsFrom stmtFrags 1+ (thisQueryParams, nextParams) = List.splitAt maxArgNum params+ in (thisQueryFrags, thisQueryParams) : go nextFrags nextParams++intToBs :: Int -> ByteString+intToBs = encodeUtf8 . Text.pack . show++-- | Returns new fragments remapped with `renumberFrom` as the smallest query argument number,+-- and also returns the maximum (new) query argument number, or 0 if there were no query arguments.+renumberParamsFrom :: [SingleQueryFragment] -> Int -> (Int, [SingleQueryFragment])+renumberParamsFrom frags renumberFrom =+ List.mapAccumR+ ( \(!maxSoFar) -> \case+ QueryArgumentPlaceHolder n -> let newNum = n - smallestArgNum + renumberFrom in (max newNum maxSoFar, QueryArgumentPlaceHolder newNum)+ x -> (maxSoFar, x)+ )+ 0+ frags+ where+ smallestArgNum =+ minimumOnOrDef+ 1+ frags+ ( \case+ QueryArgumentPlaceHolder n -> Just n+ _ -> Nothing+ )++data ConnectionString = ConnectionString+ { hostname :: Text,+ port :: Word16,+ user :: Text,+ password :: Text,+ database :: Text,+ options :: Text+ }+ deriving stock (Eq)++instance Show ConnectionString where+ show _ = "ConnectionString"++data ConnectOpts = ConnectOpts+ { -- | How long in ms Hpgsql will sleep before re-checking if active queries have been orphaned+ -- from their issuing threads having died. The default is 500ms, and this is only relevant+ -- if you plan on concurrently issuing queries on a single connection, and even then only+ -- if you expect your threads to be killed by asynchronous exceptions frequently enough,+ -- and you want resume using the connection and cannot wait ~500ms until Hpgsql realizes+ -- it's fine to do so.+ -- You probably don't need to worry about this or tune it.+ killedThreadPollIntervalMs :: !Int,+ -- | How long in ms Hpgsql will wait before re-sending a cancellation request+ -- while draining orphaned queries (queries from dead threads). The default is 500ms,+ -- and this is only relevant if you plan on interrupting your queries with+ -- asynchronous exceptions, either by use of concurrency primitives or functions like+ -- `timeout`, and continue using the connection for new queries after that.+ -- It is not recommend setting this below 100ms, because orphaned query draining+ -- alternates with resending cancellation requests, so if this is too low it is possible+ -- that draining never finishes, leading to a form of livelock.+ cancellationRequestResendIntervalMs :: !Int,+ -- | Immediately after connecting, run a query to fetch all types+ -- from the `pg_type` table. This makes them available in FromPgField+ -- instances.+ -- The default is True. You should only set it to False if you really+ -- know what you're doing, because class instances of custom types+ -- can stop working.+ fillTypeInfoCache :: !Bool,+ -- | The minimum amount of bytes to ask for when receiving from the socket.+ -- Note that Hpgsql's internal buffer may grow beyond this to accommodate+ -- larger result rows.+ -- The default is 16000.+ recvChunkSize :: !Int+ }++data ErrorDetail+ = ErrorSeverity+ | ErrorCode+ | ErrorHumanReadableMsg+ | ErrorDetail+ | ErrorHint+ | ErrorPosition+ | ErrorInternalPosition+ | ErrorInternalCommand+ | ErrorContext+ | ErrorSchema+ | ErrorTable+ | ErrorColumn+ | ErrorType+ | ErrorConstraint+ | ErrorSourceFile+ | ErrorSourceLine+ | ErrorSourceRoutine+ deriving stock (Eq, Ord, Show)++data NotificationResponse = NotificationResponse {notifierPid :: !Int32, channelName :: !Text, notifPayload :: !Text}+ deriving stock (Eq, Show)++data ResetConnectionOpts = ResetConnectionOpts+ { -- | Runs `CLOSE ALL` to close all cursors. Defaults to True.+ closeAllCursors :: Bool,+ -- | Runs `RESET ALL` on the connection. Defaults to True.+ resetAll :: Bool,+ -- | Runs `SET SESSION AUTHORIZATION DEFAULT` on the connection. Defaults to True.+ resetRole :: Bool,+ -- | Runs `DEALLOCATE ALL; DISCARD PLANS;` to deallocate all prepared statements and cached query plans. Defaults to False to reap more benefits from prepared statements.+ deallocatePreparedStmts :: Bool,+ -- | Runs `UNLISTEN *` on the connection and clears the internal queue of notifications. Defaults to True.+ unlistenAll :: Bool,+ -- | Runs `SELECT pg_advisory_unlock_all()` to release all session-level advisory locks. Defaults to True.+ releaseAdvisoryLocks :: Bool,+ -- | Runs `DISCARD TEMP` to drop temporary tables created in this session. Defaults to True.+ discardTempTables :: Bool,+ -- | Runs `DISCARD SEQUENCES` to discard all cached sequence-related state. Defaults to True.+ discardSequences :: Bool,+ -- | Throws an exception if there is an open transaction or if there's a transaction in error state. Defaults to True.+ checkTransactionState :: Bool+ }++-- | An error coming from PostgreSQL. You can safely handle this and continue using the connection.+data PostgresError = PostgresError {pgErrorDetails :: Map ErrorDetail LBS.ByteString, failedStatement :: !ByteString}+ deriving stock (Show)++instance Exception PostgresError++-- | If you receive this exception, don't run any further SQL statements or use it for anything. Just close the connection with `closeForcefully` and discard it.+data IrrecoverableHpgsqlError = IrrecoverableHpgsqlError {hpgsqlDetails :: Text, innerException :: Maybe SomeException, relatedStatement :: !(Maybe ByteString)}+ deriving stock (Show)++instance Exception IrrecoverableHpgsqlError++throwIrrecoverableError :: (MonadThrow m) => Text -> m a+throwIrrecoverableError errMsg = throw $ IrrecoverableHpgsqlError {hpgsqlDetails = errMsg, innerException = Nothing, relatedStatement = Nothing}++-- ------------------------------------------------------------------+-- Msgs types (moved from Hpgsql.Msgs to avoid cycles)+-- ------------------------------------------------------------------++data ParseComplete = ParseComplete+ deriving stock (Show)++data BindComplete = BindComplete+ deriving stock (Show)++data NoData = NoData+ deriving stock (Show)++-- | Column names and type OIDs.+newtype RowDescription = RowDescription {resultColumnTypes :: [(Text, Oid)]}+ deriving stock (Show)++data CopyInResponse = CopyInResponse+ deriving stock (Show)++newtype ErrorResponse = ErrorResponse (Map ErrorDetail LBS.ByteString)+ deriving stock (Show)++newtype CommandComplete = CommandComplete {numRows :: Int64}+ deriving stock (Show)++-- | A DataRow with its leading identifying character ('D'), the 32bits self-length,+-- the 2 bytes for the number of fields and the fields' lengths and values themselves.+newtype DataRow = DataRow {fullDataRow :: PinnedByteArray}++instance Show DataRow where+ show _ = "DataRow"++newtype ReadyForQuery+ = ReadyForQuery TransactionStatus+ deriving stock (Show)++-- ------------------------------------------------------------------+-- Internal connection state types (moved from Hpgsql)+-- ------------------------------------------------------------------++data Either3 a b c = Left3 !a | Middle3 !b | Right3 !c+ deriving stock (Show)++-- | An Integer avoids any headaches from wrap-around when comparing query ids.+-- An Int64 should be fine, but the cost of this is negligible.+newtype QueryId = QueryId Integer+ deriving newtype (Enum, Eq, Num, Ord, Show)++data CopyQueryState = StillCopying | CopyDoneAndSyncSent | CopyFailAndSyncSent+ deriving stock (Eq, Show)++data QueryProtocol+ = CopyQuery CopyQueryState+ | ExtendedQuery+ deriving stock (Eq, Show)++-- | From the docs, "in GHC, if you have a ThreadId, you essentially have a pointer to the thread itself. This means the thread itself can't be garbage collected until you drop the ThreadId. This misfeature will hopefully be corrected at a later date.".+-- And as per https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Control-Concurrent.html#v:mkWeakThreadId even BlockedIndefinitely exceptions aren't delivered if we held a ThreadId directly, so we only keep a WeakThreadId.+#if MIN_VERSION_base(4,19,0)+data WeakThreadId = WeakThreadId !(Weak ThreadId) !Word64+#else+-- fromThreadId is not available in GHC 9.6 and below, so+-- we rely on the String obtained from showThreadId instead+data WeakThreadId = WeakThreadId !(Weak ThreadId) !String+#endif++instance Eq WeakThreadId where+ WeakThreadId _ t1 == WeakThreadId _ t2 = t1 == t2++#if MIN_VERSION_base(4,19,0)+instance Show WeakThreadId where+ show (WeakThreadId _ threadIdentifier) = "WeakThreadId " ++ show threadIdentifier+#else+instance Show WeakThreadId where+ show (WeakThreadId _ threadIdentifier) = "WeakThreadId " ++ threadIdentifier+#endif++data ResponseMsgsReceived = NoMsgsReceived | ParseCompleteReceived ParseComplete | BindCompleteReceived BindComplete | RowDescriptionOrNoDataOrCopyInResponseReceived (Either3 NoData RowDescription CopyInResponse) | ErrorResponseReceived (Maybe (Either3 NoData RowDescription CopyInResponse)) ErrorResponse | CommandCompleteReceived (Either3 NoData RowDescription CopyInResponse) CommandComplete | ReadyForQueryReceived (Either ErrorResponse CommandComplete) ReadyForQuery+ deriving stock (Show)++data ResponseMsg = RespParseComplete ParseComplete | RespBindComplete BindComplete | RespNoData NoData | RespRowDescription RowDescription | RespCopyInResponse CopyInResponse | RespErrorResponse ErrorResponse | RespCommandComplete CommandComplete | RespDataRow DataRow | RespReadyForQuery ReadyForQuery+ deriving stock (Show)++data QueryState = QueryState+ { queryIdentifier :: !QueryId,+ queryText :: !ByteString,+ queryProtocol :: !QueryProtocol,+ queryPrepStmtName :: !(Maybe String),+ queryOwner :: !WeakThreadId,+ -- | Storing every single "control" (i.e. not `DataRow`) message received for each query+ -- means we can continue to drain its results from another thread at anytime, which is+ -- necessary to continue using the connection in the presence of asynchronous exceptions.+ responseMsgsState :: ResponseMsgsReceived+ }+ deriving stock (Show)++data InternalConnectionState = InternalConnectionState+ { totalQueriesSent :: !Integer,+ -- | We support only one pipeline sent to the backend at a time.+ currentPipeline :: ![QueryState],+ -- | In cases like when an asynchronous exception interrupts a `withTransaction`+ -- section, we want a "ROLLBACK" to run _before_ any next commands, to preserve+ -- the invariant that `withTransaction` ensures the started transaction is no more+ -- regardless of what happens.+ -- When such a thing happens, this field is True.+ mustIssueRollbackBeforeNextCommand :: Bool,+ -- | How did we end up with a TQueue inside an object that+ -- is already a TVar? This is unnecessary and could live out of+ -- InternalConnectionState.+ notificationsReceived :: !(TQueue NotificationResponse),+ preparedStatementNames :: Set String,+ transactionStatusBeforeCurrentPipeline :: !TransactionStatus+ }++-- ------------------------------------------------------------------+-- Connection and Pipeline types+-- ------------------------------------------------------------------++data HPgConnection = HPgConnection+ { socket :: !Socket,+ socketClosed :: !(MVar Bool),+ recvBuffer :: !(IORef LazyPinnedByteArray),+ sendBuffer :: !(MVar [(LBS.ByteString, STM ())]),+ socketMutex :: !Mutex,+ originalConnStr :: !ConnectionString,+ connectedTo :: !AddrInfo,+ encodingContext :: !(MVar EncodingContext),+ parameterStatusMap :: !(MVar (Map Text Text)),+ internalConnectionState :: !(TVar InternalConnectionState),+ connPid :: !Int32,+ cancelSecretKey :: !ByteString,+ connOpts :: !ConnectOpts+ }++instance Eq HPgConnection where+ conn1 == conn2 = conn1.socket == conn2.socket++-- | A reentrant mutex, i.e. one that can be re-acquired by the same+-- thread without deadlocking.+newtype Mutex = Mutex (TVar (Maybe (WeakThreadId, Int)))++mkMutex :: IO Mutex+mkMutex = Mutex <$> STM.newTVarIO Nothing++data Pipeline a = Pipeline [(SingleQuery, Maybe Int)] (HPgConnection -> [QueryId] -> a)+ deriving stock (Functor)++instance Applicative Pipeline where+ pure x = Pipeline [] (\_ _ -> x)+ Pipeline queries runFunc <*> Pipeline moreQueries run2 = Pipeline (queries ++ moreQueries) $ \conn qryIds ->+ let (firstQueries, lastQueries) = List.splitAt (length queries) qryIds+ f = runFunc conn firstQueries+ g = run2 conn lastQueries+ in f g
+ src/Hpgsql/Internal/PinnedByteArray.hs view
@@ -0,0 +1,376 @@+{-# LANGUAGE BinaryLiterals #-}+{-# LANGUAGE CPP #-}+{-# LANGUAGE MagicHash #-}+{-# LANGUAGE UnboxedTuples #-}+{-# LANGUAGE UnliftedFFITypes #-}++-- |+--+-- = Pinned byte arrays+--+-- This is an internal module so the interface can change very often.+-- The author still commits to following Hackage's PVP when changes are made+-- here (and in any other Internal module), but don't expect this to be a stable+-- interface.+--+-- = Why this module?+--+-- Why create our own `PinnedByteArray` type instead of just using `ByteString`?+-- `ByteString` uses a `ForeignPtr` under the hood, so peeking into its bytes for+-- decoding means our field decoders must use `withForeignPtr`, which runs in IO.+-- We don't want IO infecting our decoding code at all, so this required+-- `unsafePerformIO` (or rather its variant `unsafeDupablePerformIO`), which calls+-- `lazy` internally.+--+-- As per the documentation, "The 'lazy' function restrains strictness analysis a little",+-- and also "After strictness analysis has run, calls to 'lazy' are inlined to be the+-- identity function".+--+-- What this meant in practice is that when inspecting row decoders for a type,+-- I could see boxing of intermediary values that was completely unnecessary.+-- When decoding a postgres int4 into a Haskell Int, for example, the+-- GHC Core clearly showed a Word32 being boxed/allocated when all we needed+-- was a short-lived Word32#.+--+-- That's when pinned byte arrays, backed up by `ByteArray#`, come to the rescue:+-- those aren't moved by the GC so they can be decoded without being in IO.+--+-- After I wrote all of this, I realized _maybe_ I could've just moved `withForeignPtr`+-- higher immediately above the call to parsing buffer chunks into rows, then pass down the+-- `Ptr Word8` in a newtype instead of doing this.+-- But not only did this thought come late, `PinnedByteArray` feels right and easier to use+-- as there's no concerns about scoping decoding, and our buffers are short-lived so heap+-- fragmentation is not a concern.+module Hpgsql.Internal.PinnedByteArray+ ( PinnedByteArray,+ LazyPinnedByteArray,+ append,+ copyStrictSlice,+ createPinnedByteArray',+ drop,+ emptyLazyPBA,+ emptyPBA,+ fromByteString,+ fromChunks,+ fromStrict,+ lazyLength,+ length,+ splitAt,+ take,+ takePgMessageIdentAndLen,+ toByteString,+ toStrict,+ unsafeToUtf8Text,++ -- * Binary (de)serializer+ ByteStringIdx (..),+ decodeInt16BE,+ decodeInt32BE,+ decodeInt64BE,+ decodeWord8,+ decodeWord32BE,+ decodeWord64BE,+ encodeInt32BE,+ encodeDouble,+ encodeFloat,+ encodeInt64BE,+ encodeInt16BE,+ encodePgBoolean,+ decodeDataRow,+ )+where++import Control.Monad (when)+import Data.ByteString.Internal (ByteString (..))+import qualified Data.ByteString.Internal as InternalBS+import Data.Int (Int16, Int32, Int64)+import Foreign.C (CInt (..))+import Foreign.Marshal.Utils (copyBytes)+import Foreign.Ptr (plusPtr)+import GHC.Base (Addr#, ByteArray#, Char (..), IO (..), Int (..), MutableByteArray#, RealWorld, byteArrayContents#, compareByteArrays#, indexWord8ArrayAsChar#, indexWord8ArrayAsWord32#, mutableByteArrayContents#, newPinnedByteArray#, unIO, unsafeFreezeByteArray#, (+#))+import GHC.Exts (indexWord8Array#, indexWord8ArrayAsWord16#, indexWord8ArrayAsWord64#)+import GHC.Ptr (Ptr (..))+import System.IO.Unsafe (unsafeDupablePerformIO)+import Prelude hiding (drop, encodeFloat, length, null, splitAt, take)+#if WORDS_BIGENDIAN+import Data.Word (Word16, Word64)+#else+import Data.Word (Word16, Word64, byteSwap16, byteSwap64, Word8, byteSwap32)+#endif+#if MIN_VERSION_base(4,19,0)+import Data.Array.Byte (ByteArray (..))+#else+import Data.Text.Array (Array (..))+#endif+import Data.Bits (Bits (unsafeShiftR))+import Data.Coerce (coerce)+import Data.Text.Internal (Text (..))+import Foreign (Storable (..), withForeignPtr, (.&.))+import GHC.Float (castDoubleToWord64, castFloatToWord32)+import GHC.Word (Word16 (..), Word32 (..), Word64 (..), Word8 (..))++data PinnedByteArray = PinnedByteArray+ { _start :: !Int,+ _len :: !Int,+ _array :: !ByteArray#+ }++instance Eq PinnedByteArray where+ PinnedByteArray (I# s1) l1@(I# len) arr1# == PinnedByteArray (I# s2) l2 arr2# =+ l1 == l2 && case compareByteArrays# arr1# s1 arr2# s2 len of+ 0# -> True+ _ -> False++instance Show PinnedByteArray where+ show _ = "<PinnedByteArray>"++data LazyPinnedByteArray+ = -- | PinnedByteArrays are in reverse order for efficient appending+ LazyPinnedByteArray !Int ![PinnedByteArray]++append :: LazyPinnedByteArray -> PinnedByteArray -> LazyPinnedByteArray+append (LazyPinnedByteArray l1 pbs1) pba = LazyPinnedByteArray (l1 + length pba) (pba : pbs1 {- Reverse order -})++fromChunks :: [PinnedByteArray] -> LazyPinnedByteArray+fromChunks pbas =+ let len = sum $ map length pbas+ in LazyPinnedByteArray len (reverse pbas)++{-# NOINLINE emptyPBA #-}+emptyPBA :: PinnedByteArray+emptyPBA = unsafeDupablePerformIO $ createPinnedByteArray 0 (\_ -> pure 0)++emptyLazyPBA :: LazyPinnedByteArray+emptyLazyPBA = fromStrict emptyPBA++createPinnedByteArray :: Int -> (Addr# -> IO CInt) -> IO PinnedByteArray+createPinnedByteArray size f = fst <$> createPinnedByteArray' size withUnit+ where+ withUnit addr = do+ len <- f addr+ pure (len, ())++createPinnedByteArray' :: Int -> (Addr# -> IO (CInt, a)) -> IO (PinnedByteArray, a)+createPinnedByteArray' (I# size#) f = IO $ \s0 ->+ let !(# newRW, mutArr# :: MutableByteArray# RealWorld #) = newPinnedByteArray# size# s0+ !(# newRW', (lenCopied, ret) #) = unIO (f (mutableByteArrayContents# mutArr#)) newRW+ !(# finalRW, frozenArr# #) = unsafeFreezeByteArray# mutArr# newRW'+ in (# finalRW, (PinnedByteArray 0 (fromIntegral lenCopied) frozenArr#, ret) #)++fromByteString :: ByteString -> PinnedByteArray+fromByteString (BS fptr len) = unsafeDupablePerformIO $ createPinnedByteArray len $ \dst -> withForeignPtr fptr $ \src -> do+ copyBytes (Ptr dst) src len+ pure $ fromIntegral len++toByteString :: PinnedByteArray -> ByteString+toByteString (PinnedByteArray start len src) = unsafeDupablePerformIO $ InternalBS.create len $ \dst ->+ copyBytes dst (Ptr (byteArrayContents# src) `plusPtr` start) len++{-# INLINE unsafeToUtf8Text #-}++-- | Assuming the pinned byte array contains valid UTF8 text, creates+-- returns an instance of `Text` with the same contents (but does make a copy).+unsafeToUtf8Text :: ByteStringIdx -> Int -> PinnedByteArray -> Either String Text+unsafeToUtf8Text idx desiredLen pba@(PinnedByteArray {}) = let !(PinnedByteArray start arrLen arr#) = copyStrictSlice idx.idx desiredLen (fromStrict pba) in if arrLen /= desiredLen then Left "Insufficient bytes in buffer in unsafeToUtf8Text" else Right $ Text (ByteArray arr#) start arrLen++takePgMessageIdentAndLen :: LazyPinnedByteArray -> Maybe (Char, Int32)+takePgMessageIdentAndLen lpba@(LazyPinnedByteArray len _) =+ if len >= 5+ then+ let !(PinnedByteArray (I# start) _ arr#) = copyStrictSlice 0 5 lpba+ in Just (C# (indexWord8ArrayAsChar# arr# start), fromIntegral $ fromBigEndian32 $ W32# (indexWord8ArrayAsWord32# arr# (start +# 1#)))+ else Nothing++-- | Drops the next `n` bytes.+drop :: Int -> PinnedByteArray -> PinnedByteArray+drop (max 0 -> n) (PinnedByteArray start len arr#) =+ if n >= len+ then emptyPBA+ else+ PinnedByteArray (start + n) (len - n) arr#++-- | Takes the first `n` bytes.+take :: Int -> PinnedByteArray -> PinnedByteArray+take (max 0 -> n) (PinnedByteArray start len arr#) =+ PinnedByteArray start (min n len) arr#++fromStrict :: PinnedByteArray -> LazyPinnedByteArray+fromStrict pba@(PinnedByteArray _ len _) = LazyPinnedByteArray len [pba]++-- | Copies chunks into a single contiguous 'PinnedByteArray'. Avoids the copy+-- when there's already just a single chunk.+toStrict :: LazyPinnedByteArray -> PinnedByteArray+toStrict (LazyPinnedByteArray _ [pba]) = pba+toStrict lpba@(LazyPinnedByteArray totalLen _) = copyStrictSlice 0 totalLen lpba++-- | Creates a strict PBA from a Lazy one, but just with the first `n`+-- bytes after the first `skip` (or less if they're not all there).+-- The returned `PinnedByteArray` is _always_ pointing to a new copy of+-- the bytes in the supplied `LazyPinnedByteArray` - no reference is kept+-- to the supplied bytes.+copyStrictSlice :: Int -> Int -> LazyPinnedByteArray -> PinnedByteArray+copyStrictSlice (max 0 -> skip) (max 0 -> n') (LazyPinnedByteArray totalLen' (reverse -> chunks)) =+ let n = min n' totalLen'+ in unsafeDupablePerformIO $ createPinnedByteArray n $ \dst -> do+ let go !copied !_ !_ [] = pure copied+ go !copied !_ 0 !_ = pure copied+ go !offset !skipLeft !nLeft (PinnedByteArray start l arr# : rest) = do+ let toSkipSrc = min l skipLeft+ toCopy = min nLeft (l - toSkipSrc)+ when (toCopy > 0 && toSkipSrc < l) $ copyBytes (Ptr dst `plusPtr` offset) (Ptr (byteArrayContents# arr#) `plusPtr` (start + toSkipSrc)) toCopy+ when (toCopy < 0) $ error "toCopy < 0 should be impossible"+ go (offset + toCopy) (skipLeft - toSkipSrc) (nLeft - toCopy) rest+ fromIntegral <$> go 0 skip n chunks++splitAt :: Int -> PinnedByteArray -> (PinnedByteArray, PinnedByteArray)+splitAt n pba = (take n pba, drop n pba)++length :: PinnedByteArray -> Int+length (PinnedByteArray _ len _) = len++lazyLength :: LazyPinnedByteArray -> Int+lazyLength (LazyPinnedByteArray len _) = len++-- * Binary (de)serializer++-- A replacement for libraries like cereal or binary.+-- In our tests, this is ~6.0% faster than cereal, and it also+-- (or by virtue of) allocates ~13% less memory in some of our benchmarks.+-- And it also means one fewer dependency.+-- The caveat is that this module makes unaligned memory access. For the target+-- CPU architectures of this library, this should be fine.++fromBigEndian32 :: Word32 -> Word32+#if WORDS_BIGENDIAN+fromBigEndian32 = Prelude.id+#else+fromBigEndian32 = byteSwap32+#endif++fromBigEndian64 :: Word64 -> Word64+#if WORDS_BIGENDIAN+fromBigEndian64 = Prelude.id+#else+fromBigEndian64 = byteSwap64+#endif++fromBigEndian16 :: Word16 -> Word16+#if WORDS_BIGENDIAN+fromBigEndian16 = Prelude.id+#else+fromBigEndian16 = byteSwap16+#endif++data WordDecodingAll a where+ CWord8 :: WordDecodingAll Word8+ CWord16 :: WordDecodingAll Word16+ CWord32 :: WordDecodingAll Word32+ CWord64 :: WordDecodingAll Word64++{-# INLINE decodeWord #-}+decodeWord :: WordDecodingAll a -> ByteStringIdx -> PinnedByteArray -> (a -> a) -> Either String a+decodeWord wdec (ByteStringIdx boxedIdx@(I# idx)) (PinnedByteArray (I# start) len byArrSharp) endianConvert =+ case wdec of+ CWord8 -> if len < 1 + boxedIdx then Left "Less than enough bytes to decode" else Right $ endianConvert $ W8# (indexWord8Array# byArrSharp (idx +# start))+ CWord16 -> if len < 2 + boxedIdx then Left "Less than enough bytes to decode" else Right $ endianConvert $ W16# (indexWord8ArrayAsWord16# byArrSharp (idx +# start))+ CWord32 -> if len < 4 + boxedIdx then Left "Less than enough bytes to decode" else Right $ endianConvert $ W32# (indexWord8ArrayAsWord32# byArrSharp (idx +# start))+ CWord64 -> if len < 8 + boxedIdx then Left "Less than enough bytes to decode" else Right $ endianConvert $ W64# (indexWord8ArrayAsWord64# byArrSharp (idx +# start))++{-# INLINE unsafeEncodeWord #-}+unsafeEncodeWord :: (Storable a) => a -> (a -> a) -> Int -> ByteString+unsafeEncodeWord n endianConvert len =+ InternalBS.unsafeCreate len $ \bufferPtr ->+ poke (coerce bufferPtr) $ endianConvert n++newtype ByteStringIdx = ByteStringIdx {idx :: Int}+ deriving newtype (Eq, Num, Show)++{-# INLINE decodeInt16BE #-}+decodeInt16BE :: ByteStringIdx -> PinnedByteArray -> Either String Int16+decodeInt16BE idx bs = fromIntegral <$> decodeWord CWord16 idx bs fromBigEndian16++{-# INLINE encodeInt16BE #-}+encodeInt16BE :: Int16 -> ByteString+encodeInt16BE n = unsafeEncodeWord (fromIntegral n) fromBigEndian16 2++{-# INLINE decodeWord8 #-}+decodeWord8 :: ByteStringIdx -> PinnedByteArray -> Either String Word8+decodeWord8 idx bs = decodeWord CWord8 idx bs Prelude.id++{-# INLINE decodeWord32BE #-}+decodeWord32BE :: ByteStringIdx -> PinnedByteArray -> Either String Word32+decodeWord32BE idx bs = decodeWord CWord32 idx bs fromBigEndian32++{-# INLINE decodeWord64BE #-}+decodeWord64BE :: ByteStringIdx -> PinnedByteArray -> Either String Word64+decodeWord64BE idx bs = decodeWord CWord64 idx bs fromBigEndian64++{-# INLINE decodeInt32BE #-}+decodeInt32BE :: ByteStringIdx -> PinnedByteArray -> Either String Int32+decodeInt32BE idx bs = fromIntegral <$> decodeWord CWord32 idx bs fromBigEndian32++{-# INLINE encodeInt32BE #-}+encodeInt32BE :: Int32 -> ByteString+encodeInt32BE n = unsafeEncodeWord (fromIntegral n) fromBigEndian32 4++{-# INLINE decodeInt64BE #-}+decodeInt64BE :: ByteStringIdx -> PinnedByteArray -> Either String Int64+decodeInt64BE idx bs = fromIntegral <$> decodeWord CWord64 idx bs fromBigEndian64++{-# INLINE encodeInt64BE #-}+encodeInt64BE :: Int64 -> ByteString+encodeInt64BE n = unsafeEncodeWord (fromIntegral n) fromBigEndian64 8++{-# INLINE encodeFloat #-}+encodeFloat :: Float -> ByteString+encodeFloat n = unsafeEncodeWord (castFloatToWord32 n) fromBigEndian32 4++{-# INLINE encodeDouble #-}+encodeDouble :: Double -> ByteString+encodeDouble n = unsafeEncodeWord (castDoubleToWord64 n) fromBigEndian64 8++-- TODO: Encode field length together with value for small types.+-- This can also be a performance boost by having fewer bytestrings?+{-# INLINE encodePgBoolean #-}+encodePgBoolean :: Bool -> ByteString+encodePgBoolean v = if v then "\SOH" else "\NUL"++{-# INLINE decodeDataRow #-}++-- | A super specialized decoder to decode a postgres DataRow message+-- more quickly than a naive implementation.+-- Returns the index into the left-unparsed contents of the supplied bytestring.+decodeDataRow :: ByteStringIdx -> PinnedByteArray -> Either String ByteStringIdx+decodeDataRow idx sbs@(PinnedByteArray _ len _) =+ -- We have a fast path when rows are at least 8 bytes long (should be the case+ -- for all but 0-column query results or bytestring chunks "cut in the middle of the message")+ -- by playing with bitwise operations.+ -- Whether this is worth keeping is sort of questionable. It's complex+ -- (even if I think it's safe and well tested) and reduces runtime of one of+ -- our benchmarks by 2% compared to not having it.+ case decodeWord CWord64 idx sbs fromBigEndian64 of+ Right (w64 :: Word64) ->+ -- After fromBigEndian64, the Word64 has bytes in big-endian order:+ -- byte 0 (msg type) in MSB, bytes 1-4 (length) next, bytes 5-6 (col count), byte 7 in LSB.+ let msgIdentByte64 = w64 .&. 0b11111111_00000000_00000000_00000000_00000000_00000000_00000000_00000000+ lenFullMsg = flip unsafeShiftR 24 $ w64 .&. 0b00000000_11111111_11111111_11111111_11111111_00000000_00000000_00000000+ letterD :: Word64 = 0b01000100_00000000_00000000_00000000_00000000_00000000_00000000_00000000+ in if msgIdentByte64 == letterD+ then+ toResult (fromIntegral lenFullMsg)+ else Left "Not a DataRow (Word64 bits decoding path)"+ Left _ ->+ -- It is possible the DataRow has length less than 8 bytes, so+ -- we still have to try to parse that.+ if len >= 5 + idx.idx+ then do+ msgIdentChar <- decodeWord8 idx sbs+ lenFullMsg <- decodeInt32BE (1 + idx) sbs+ if msgIdentChar == 68 -- Letter 'D'+ then toResult (fromIntegral lenFullMsg)+ else Left "Not a DataRow"+ else Left "Less than enough bytes to decode a DataRow"+ where+ toResult lenFullMsg+ | len >= 1 + lenFullMsg + idx.idx = Right $ ByteStringIdx $ 1 + lenFullMsg + idx.idx+ | otherwise = Left "Less than enough bytes to decode a full DataRow"
− src/Hpgsql/InternalTypes.hs
@@ -1,499 +0,0 @@-module Hpgsql.InternalTypes- ( -- * Simple types- ConnectionString (..),- ConnectOpts (..),- PostgresError (..),- IrrecoverableHpgsqlError (..),- ErrorDetail (..),- NotificationResponse (..),- ResetConnectionOpts (..),- TransactionStatus (..),- EncodingContext (..), -- re-exported from Hpgsql.TypeInfo- throwIrrecoverableError,-- -- * Query types (moved from Hpgsql.Query)- SingleQueryFragment (..),- Query (..),- SingleQuery (..),- queryToByteString,- breakQueryIntoStatements,- renumberParamsFrom,-- -- * Msgs types (moved to avoid cycles)- ParseComplete (..),- BindComplete (..),- NoData (..),- RowDescription (..),- CopyInResponse (..),- ErrorResponse (..),- CommandComplete (..),- DataRow (..),- ReadyForQuery (..),-- -- * Internal connection state types (moved to avoid cycles)- InternalConnectionState (..),- WeakThreadId (..),- QueryState (..),- QueryId (..),- QueryProtocol (..),- CopyQueryState (..),- ResponseMsgsReceived (..),- ResponseMsg (..),- Either3 (..),-- -- * Connection and Pipeline types- HPgConnection (..),- Mutex (..),- Pipeline (..),- mkMutex,- )-where--import Control.Concurrent (ThreadId)-import Control.Concurrent.MVar (MVar)-import Control.Concurrent.STM (STM, TQueue, TVar)-import Control.Exception.Safe (Exception (..), MonadThrow, SomeException, throw)-import Data.ByteString (ByteString)-import qualified Data.ByteString.Lazy as LBS-import Data.Int (Int32, Int64)-import qualified Data.List as List-import Data.List.NonEmpty (NonEmpty (..))-import qualified Data.List.NonEmpty as NE-import Data.Map.Strict (Map)-import Data.String (IsString (..))-import Data.Text (Text)-import qualified Data.Text as Text-import Data.Text.Encoding (encodeUtf8)-#if MIN_VERSION_base(4,19,0)-import Data.Word (Word16, Word64)-#else-import Data.Word (Word16)--#endif-import qualified Control.Concurrent.STM as STM-import Data.Hashable (hash)-import Data.IORef (IORef)-import Data.Set (Set)-import Hpgsql.Base (lastTwoAndInit, maximumOnOrDef, minimumOnOrDef)-import Hpgsql.Builder (BinaryField)-import Hpgsql.ParsingInternal (BlockOrNotBlock (..), ParsingOpts (..), parseSql)-import Hpgsql.TransactionStatusInternal (TransactionStatus (..))-import Hpgsql.TypeInfo (EncodingContext (..), Oid (..))-import Network.Socket (AddrInfo, Socket)-import System.Mem.Weak (Weak)---- | A fragment of a single SQL statement, which is either static SQL or--- a placeholder for a query argument.-data SingleQueryFragment- = FragmentOfStaticSql !ByteString- | FragmentWithSemiColon- | FragmentOfCommentsOrWhitespace !ByteString- | -- | The number/index of the query argument, starting from 1 in a single statement- QueryArgumentPlaceHolder !Int- deriving stock (Eq, Show)---- | `Query` is some SQL with values of query arguments inside and also whether it's a prepared statement.--- It can be concatenated to other `Query` objects freely, and concatenating chains of Queries with--- at least one prepared statement among them makes the final `Query` object a prepared statement, too.--- You can build this with the `sql` and `sqlPrep` quasiquoters, the former for unprepared statements,--- and you can use `preparedStatement` and `nonPreparedStatement` to convert between them.-data Query = Query- { -- The query parameters and query fragments continue to go up in number across different- -- statements, e.g. "SELECT $1; SELECT $2; SELECT $3; ...".- queryString :: ![SingleQueryFragment],- queryParams :: ![EncodingContext -> (Maybe Oid, BinaryField)],- isPrepared :: !Bool- }--instance Show Query where- show = show . NE.toList . breakQueryIntoStatements--queryToByteString :: Query -> ByteString-queryToByteString = mconcat . map (\SingleQuery {queryString} -> queryString) . NE.toList . breakQueryIntoStatements--instance Semigroup Query where- q1 <> q2 =- let maxArgQ1 =- maximumOnOrDef- 0- q1.queryString- ( \case- QueryArgumentPlaceHolder n -> Just n- _ -> Nothing- )- (_, remappedQ2) =- renumberParamsFrom- q2.queryString- (maxArgQ1 + 1)- in -- Semigroup concatenation must be associative, and for isPrepared- -- both || and && are associative, but what would the user expect?- -- For typical query concatenation, I *think* of some examples:- -- - [sqlPrep|SELECT ^{columnsQry} FROM some_table ^{whereConditions}|]- -- - [sqlPrep|INSERT INTO x ^{vALUES rows}|]- -- In both cases above, we want `isPrepared = True` to be "infectious", so- -- we want to use ||- -- Are there counter examples? Yes, if users build "smaller" embeddable Queries- -- with `sqlPrep`, e.g. if `columnsQry` or `whereConditions` were built with `sqlPrep`.- -- That feels unlikely.. so we go with ||- Query {queryString = q1.queryString <> remappedQ2, queryParams = q1.queryParams <> q2.queryParams, isPrepared = q1.isPrepared || q2.isPrepared}---- | A single statement, not multiple, with dollar-numbered query arguments--- starting from $1.-data SingleQuery = SingleQuery {queryString :: !ByteString, queryParams :: ![EncodingContext -> (Maybe Oid, BinaryField)], preparedStmtHash :: !(Maybe String)}--mkSingleQuery :: ByteString -> [EncodingContext -> (Maybe Oid, BinaryField)] -> Bool -> SingleQuery-mkSingleQuery queryString queryParams isPrepared = SingleQuery {queryString, queryParams, preparedStmtHash = if isPrepared then Just (show $ hash queryString) else Nothing}--instance Show SingleQuery where- -- Careful not exposing query arguments- show (SingleQuery {queryString}) = show queryString--instance IsString Query where- fromString s =- let blocks = parseSql AcceptOnlyDollarNumberedArgs (Text.pack s)- queryFrags =- map- ( \case- StaticSql t -> FragmentOfStaticSql $ encodeUtf8 t- SemiColon -> FragmentWithSemiColon- CommentsOrWhitespace t -> FragmentOfCommentsOrWhitespace $ encodeUtf8 t- DollarNumberedArg _ ->- error "Dollar-numbered query arguments are not supported in string literals. Use the sql quasiquoter or mkQuery instead."- QuestionMarkArg ->- error "Question mark query arguments are not supported in string literals. Use the sql quasiquoter or mkQuery instead."- QuasiQuoterExpression _ _ ->- error "Bug in Hpgsql: parseSql AcceptOnlyDollarNumberedArgs returned quasiquoter expression."- )- blocks- in Query {queryString = queryFrags, queryParams = [], isPrepared = False}---- | For internal usage only. Takes a `Query` and breaks it up into--- individual SQL statements that can be sent to a postgres backend.-breakQueryIntoStatements :: Query -> NonEmpty SingleQuery-breakQueryIntoStatements qry@Query {queryString = fullQueryString, queryParams = allQueryParams, isPrepared} =- -- If a user insists in trying to run an empty query, or if- -- they mistakenly concatenate empty statements, we let them- toEmptyQueryIfNecessary $- map toSingleQuery $- -- If a query string is "SELECT 1; -- comments and empty space", we put- -- all comments and whitespace together with that last "real" SQL statement- fixLastEmptyStatement $- go fullQueryString allQueryParams- where- toEmptyQueryIfNecessary [] = NE.singleton $ mkSingleQuery "" allQueryParams isPrepared- toEmptyQueryIfNecessary (x : xs) = x :| xs- toSingleQuery (blks, prms) = let queryString = mconcat $ map fragToBytestring blks in mkSingleQuery queryString prms isPrepared- allWhitespaceOrComments =- all- ( \case- FragmentOfCommentsOrWhitespace _ -> True- _ -> False- )- fixLastEmptyStatement :: [([SingleQueryFragment], [EncodingContext -> (Maybe Oid, BinaryField)])] -> [([SingleQueryFragment], [EncodingContext -> (Maybe Oid, BinaryField)])]- fixLastEmptyStatement indivStmts = case lastTwoAndInit indivStmts of- (_, Nothing) -> indivStmts -- Only 0 or 1 statements found- (firstStmts, Just (secLst, lst))- | allWhitespaceOrComments (fst lst) -> firstStmts ++ [secLst <> lst]- | otherwise -> indivStmts- isLastFragmentOfAStatement = \case- FragmentWithSemiColon -> True- _ -> False- fragToBytestring = \case- QueryArgumentPlaceHolder n -> "$" <> intToBs n- FragmentOfStaticSql t -> t- FragmentWithSemiColon -> ";"- FragmentOfCommentsOrWhitespace t -> t- go :: [SingleQueryFragment] -> [EncodingContext -> (Maybe Oid, BinaryField)] -> [([SingleQueryFragment], [EncodingContext -> (Maybe Oid, BinaryField)])]- go [] [] = []- go [] _ = error $ "Hpgsql error: empty query fragment list but outstanding query params. Number of query arguments is " ++ show (length allQueryParams) ++ " and query is " ++ show qry- go frags params =- let (stmtFrags, nextFrags) = case List.break isLastFragmentOfAStatement frags of- (firstStmts, []) -> (firstStmts, [])- (firstStmts, semiColon : next) -> (firstStmts ++ [semiColon], next)- (maxArgNum, thisQueryFrags) = renumberParamsFrom stmtFrags 1- (thisQueryParams, nextParams) = List.splitAt maxArgNum params- in (thisQueryFrags, thisQueryParams) : go nextFrags nextParams--intToBs :: Int -> ByteString-intToBs = encodeUtf8 . Text.pack . show---- | Returns new fragments remapped with `renumberFrom` as the smallest query argument number,--- and also returns the maximum (new) query argument number, or 0 if there were no query arguments.-renumberParamsFrom :: [SingleQueryFragment] -> Int -> (Int, [SingleQueryFragment])-renumberParamsFrom frags renumberFrom =- List.mapAccumR- ( \(!maxSoFar) -> \case- QueryArgumentPlaceHolder n -> let newNum = n - smallestArgNum + renumberFrom in (max newNum maxSoFar, QueryArgumentPlaceHolder newNum)- x -> (maxSoFar, x)- )- 0- frags- where- smallestArgNum =- minimumOnOrDef- 1- frags- ( \case- QueryArgumentPlaceHolder n -> Just n- _ -> Nothing- )--data ConnectionString = ConnectionString- { hostname :: Text,- port :: Word16,- user :: Text,- password :: Text,- database :: Text,- options :: Text- }- deriving stock (Eq)--instance Show ConnectionString where- show _ = "ConnectionString"--data ConnectOpts = ConnectOpts- { -- | How long in ms Hpgsql will sleep before re-checking if active queries have been orphaned- -- from their issuing threads having died. The default is 500ms, and this is only relevant- -- if you plan on concurrently issuing queries on a single connection, and even then only- -- if you expect your threads to be killed by asynchronous exceptions frequently enough,- -- and you want resume using the connection and cannot wait ~500ms until Hpgsql realizes- -- it's fine to do so.- -- You probably don't need to worry about this or tune it.- killedThreadPollIntervalMs :: !Int,- -- | How long in ms Hpgsql will wait before re-sending a cancellation request- -- while draining orphaned queries (queries from dead threads). The default is 500ms,- -- and this is only relevant if you plan on interrupting your queries with- -- asynchronous exceptions, either by use of concurrency primitives or functions like- -- `timeout`, and continue using the connection for new queries after that.- -- It is not recommend setting this below 100ms, because orphaned query draining- -- alternates with resending cancellation requests, so if this is too low it is possible- -- that draining never finishes, leading to a form of livelock.- cancellationRequestResendIntervalMs :: !Int,- -- | Immediately after connecting, run a query to fetch all types- -- from the `pg_type` table. This makes them available in FromPgField- -- instances.- -- The default is True. You should only set it to False if you really- -- know what you're doing, because class instances of custom types- -- can stop working.- fillTypeInfoCache :: !Bool,- -- | The minimum amount of bytes to ask for when receiving from the socket.- -- Note that Hpgsql's internal buffer may grow beyond this to accommodate- -- larger result rows.- -- The default is 16000.- recvChunkSize :: !Int- }--data ErrorDetail- = ErrorSeverity- | ErrorCode- | ErrorHumanReadableMsg- | ErrorDetail- | ErrorHint- | ErrorPosition- | ErrorInternalPosition- | ErrorInternalCommand- | ErrorContext- | ErrorSchema- | ErrorTable- | ErrorColumn- | ErrorType- | ErrorConstraint- | ErrorSourceFile- | ErrorSourceLine- | ErrorSourceRoutine- deriving stock (Eq, Ord, Show)--data NotificationResponse = NotificationResponse {notifierPid :: !Int32, channelName :: !Text, notifPayload :: !Text}- deriving stock (Eq, Show)--data ResetConnectionOpts = ResetConnectionOpts- { -- | Runs `CLOSE ALL` to close all cursors. Defaults to True.- closeAllCursors :: Bool,- -- | Runs `RESET ALL` on the connection. Defaults to True.- resetAll :: Bool,- -- | Runs `SET SESSION AUTHORIZATION DEFAULT` on the connection. Defaults to True.- resetRole :: Bool,- -- | Runs `DEALLOCATE ALL; DISCARD PLANS;` to deallocate all prepared statements and cached query plans. Defaults to False to reap more benefits from prepared statements.- deallocatePreparedStmts :: Bool,- -- | Runs `UNLISTEN *` on the connection and clears the internal queue of notifications. Defaults to True.- unlistenAll :: Bool,- -- | Runs `SELECT pg_advisory_unlock_all()` to release all session-level advisory locks. Defaults to True.- releaseAdvisoryLocks :: Bool,- -- | Runs `DISCARD TEMP` to drop temporary tables created in this session. Defaults to True.- discardTempTables :: Bool,- -- | Runs `DISCARD SEQUENCES` to discard all cached sequence-related state. Defaults to True.- discardSequences :: Bool,- -- | Throws an exception if there is an open transaction or if there's a transaction in error state. Defaults to True.- checkTransactionState :: Bool- }---- | An error coming from PostgreSQL. You can safely handle this and continue using the connection.-data PostgresError = PostgresError {pgErrorDetails :: Map ErrorDetail LBS.ByteString, failedStatement :: !ByteString}- deriving stock (Show)--instance Exception PostgresError---- | If you receive this exception, don't run any further SQL statements or use it for anything. Just close the connection with `closeForcefully` and discard it.-data IrrecoverableHpgsqlError = IrrecoverableHpgsqlError {hpgsqlDetails :: Text, innerException :: Maybe SomeException, relatedStatement :: !(Maybe ByteString)}- deriving stock (Show)--instance Exception IrrecoverableHpgsqlError--throwIrrecoverableError :: (MonadThrow m) => Text -> m a-throwIrrecoverableError errMsg = throw $ IrrecoverableHpgsqlError {hpgsqlDetails = errMsg, innerException = Nothing, relatedStatement = Nothing}---- --------------------------------------------------------------------- Msgs types (moved from Hpgsql.Msgs to avoid cycles)--- --------------------------------------------------------------------data ParseComplete = ParseComplete- deriving stock (Show)--data BindComplete = BindComplete- deriving stock (Show)--data NoData = NoData- deriving stock (Show)---- | Column names and type OIDs.-newtype RowDescription = RowDescription {resultColumnTypes :: [(Text, Oid)]}- deriving stock (Show)--data CopyInResponse = CopyInResponse- deriving stock (Show)--newtype ErrorResponse = ErrorResponse (Map ErrorDetail LBS.ByteString)- deriving stock (Show)--newtype CommandComplete = CommandComplete {numRows :: Int64}- deriving stock (Show)---- | A DataRow with its leading identifying character ('D'), the 32bits self-length,--- the 2 bytes for the number of fields and the fields' lengths and values themselves.-newtype DataRow = DataRow {fullDataRow :: ByteString}--instance Show DataRow where- show _ = "DataRow"--newtype ReadyForQuery- = ReadyForQuery TransactionStatus- deriving stock (Show)---- --------------------------------------------------------------------- Internal connection state types (moved from Hpgsql)--- --------------------------------------------------------------------data Either3 a b c = Left3 !a | Middle3 !b | Right3 !c- deriving stock (Show)---- | An Integer avoids any headaches from wrap-around when comparing query ids.--- An Int64 should be fine, but the cost of this is negligible.-newtype QueryId = QueryId Integer- deriving newtype (Enum, Eq, Num, Ord, Show)--data CopyQueryState = StillCopying | CopyDoneAndSyncSent | CopyFailAndSyncSent- deriving stock (Eq, Show)--data QueryProtocol- = CopyQuery CopyQueryState- | ExtendedQuery- deriving stock (Eq, Show)---- | From the docs, "in GHC, if you have a ThreadId, you essentially have a pointer to the thread itself. This means the thread itself can't be garbage collected until you drop the ThreadId. This misfeature will hopefully be corrected at a later date.".--- And as per https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Control-Concurrent.html#v:mkWeakThreadId even BlockedIndefinitely exceptions aren't delivered if we held a ThreadId directly, so we only keep a WeakThreadId.-#if MIN_VERSION_base(4,19,0)-data WeakThreadId = WeakThreadId !(Weak ThreadId) !Word64-#else--- fromThreadId is not available in GHC 9.6 and below, so--- we rely on the String obtained from showThreadId instead-data WeakThreadId = WeakThreadId !(Weak ThreadId) !String-#endif--instance Eq WeakThreadId where- WeakThreadId _ t1 == WeakThreadId _ t2 = t1 == t2--#if MIN_VERSION_base(4,19,0)-instance Show WeakThreadId where- show (WeakThreadId _ threadIdentifier) = "WeakThreadId " ++ show threadIdentifier-#else-instance Show WeakThreadId where- show (WeakThreadId _ threadIdentifier) = "WeakThreadId " ++ threadIdentifier-#endif--data ResponseMsgsReceived = NoMsgsReceived | ParseCompleteReceived ParseComplete | BindCompleteReceived BindComplete | RowDescriptionOrNoDataOrCopyInResponseReceived (Either3 NoData RowDescription CopyInResponse) | ErrorResponseReceived (Maybe (Either3 NoData RowDescription CopyInResponse)) ErrorResponse | CommandCompleteReceived (Either3 NoData RowDescription CopyInResponse) CommandComplete | ReadyForQueryReceived (Either ErrorResponse CommandComplete) ReadyForQuery- deriving stock (Show)--data ResponseMsg = RespParseComplete ParseComplete | RespBindComplete BindComplete | RespNoData NoData | RespRowDescription RowDescription | RespCopyInResponse CopyInResponse | RespErrorResponse ErrorResponse | RespCommandComplete CommandComplete | RespDataRow DataRow | RespReadyForQuery ReadyForQuery- deriving stock (Show)--data QueryState = QueryState- { queryIdentifier :: !QueryId,- queryText :: !ByteString,- queryProtocol :: !QueryProtocol,- queryPrepStmtName :: !(Maybe String),- queryOwner :: !WeakThreadId,- -- | Storing every single "control" (i.e. not `DataRow`) message received for each query- -- means we can continue to drain its results from another thread at anytime, which is- -- necessary to continue using the connection in the presence of asynchronous exceptions.- responseMsgsState :: ResponseMsgsReceived- }- deriving stock (Show)--data InternalConnectionState = InternalConnectionState- { totalQueriesSent :: !Integer,- -- | We support only one pipeline sent to the backend at a time.- currentPipeline :: ![QueryState],- -- | In cases like when an asynchronous exception interrupts a `withTransaction`- -- section, we want a "ROLLBACK" to run _before_ any next commands, to preserve- -- the invariant that `withTransaction` ensures the started transaction is no more- -- regardless of what happens.- -- When such a thing happens, this field is True.- mustIssueRollbackBeforeNextCommand :: Bool,- -- | How did we end up with a TQueue inside an object that- -- is already a TVar? This is unnecessary and could live out of- -- InternalConnectionState.- notificationsReceived :: !(TQueue NotificationResponse),- preparedStatementNames :: Set String,- transactionStatusBeforeCurrentPipeline :: !TransactionStatus- }---- --------------------------------------------------------------------- Connection and Pipeline types--- --------------------------------------------------------------------data HPgConnection = HPgConnection- { socket :: !Socket,- socketClosed :: !(MVar Bool),- recvBuffer :: !(IORef LBS.ByteString),- sendBuffer :: !(MVar [(LBS.ByteString, STM ())]),- socketMutex :: !Mutex,- originalConnStr :: !ConnectionString,- connectedTo :: !AddrInfo,- encodingContext :: !(MVar EncodingContext),- parameterStatusMap :: !(MVar (Map Text Text)),- internalConnectionState :: !(TVar InternalConnectionState),- connPid :: !Int32,- cancelSecretKey :: !ByteString,- connOpts :: !ConnectOpts- }--instance Eq HPgConnection where- conn1 == conn2 = socket conn1 == socket conn2---- | A reentrant mutex, i.e. one that can be re-acquired by the same--- thread without deadlocking.-newtype Mutex = Mutex (TVar (Maybe (WeakThreadId, Int)))--mkMutex :: IO Mutex-mkMutex = Mutex <$> STM.newTVarIO Nothing--data Pipeline a = Pipeline [(SingleQuery, Maybe Int)] (HPgConnection -> [QueryId] -> a)- deriving stock (Functor)--instance Applicative Pipeline where- pure x = Pipeline [] (\_ _ -> x)- Pipeline queries runFunc <*> Pipeline moreQueries run2 = Pipeline (queries ++ moreQueries) $ \conn qryIds ->- let (firstQueries, lastQueries) = List.splitAt (length queries) qryIds- f = runFunc conn firstQueries- g = run2 conn lastQueries- in f g
src/Hpgsql/LanguageHaskell/ParseHaskellExpression.hs view
@@ -130,11 +130,15 @@ convertExpr (HsAppType _ (L _ e) _ (HsWC _ (L _ ty))) = TH.AppTypeE <$> convertExpr e <*> convertType ty #endif #if MIN_VERSION_ghc_lib_parser(9,10,0)-convertExpr (RecordCon _ (L _ conName) (HsRecFields _ flds _)) = do+convertExpr (RecordCon _ _ (HsRecFields _ _ (Just _))) =+ unsupportedLanguageFeatureMsg "RecordWildCards (the `..` syntax) in record construction"+convertExpr (RecordCon _ (L _ conName) (HsRecFields _ flds Nothing)) = do flds' <- traverse convertRecField flds Right $ TH.RecConE (rdrToName conName) flds' #elif MIN_VERSION_ghc_lib_parser(9,8,0)-convertExpr (RecordCon _ (L _ conName) (HsRecFields flds _)) = do+convertExpr (RecordCon _ _ (HsRecFields _ (Just _))) =+ unsupportedLanguageFeatureMsg "RecordWildCards (the `..` syntax) in record construction"+convertExpr (RecordCon _ (L _ conName) (HsRecFields flds Nothing)) = do flds' <- traverse convertRecField flds Right $ TH.RecConE (rdrToName conName) flds' #endif@@ -280,11 +284,15 @@ r' <- convertPat r Right (TH.InfixP l' (rdrToName con) r') #if MIN_VERSION_ghc_lib_parser(9,10,0)-convertConPatDetails con (RecCon (HsRecFields _ flds _)) = do+convertConPatDetails _ (RecCon (HsRecFields _ _ (Just _))) =+ unsupportedLanguageFeatureMsg "RecordWildCards (the `..` syntax) in pattern matching"+convertConPatDetails con (RecCon (HsRecFields _ flds Nothing)) = do flds' <- traverse convertPatRecField flds Right (TH.RecP (rdrToName con) flds') #elif MIN_VERSION_ghc_lib_parser(9,8,0)-convertConPatDetails con (RecCon (HsRecFields flds _)) = do+convertConPatDetails _ (RecCon (HsRecFields _ (Just _))) =+ unsupportedLanguageFeatureMsg "RecordWildCards (the `..` syntax) in pattern matching"+convertConPatDetails con (RecCon (HsRecFields flds Nothing)) = do flds' <- traverse convertPatRecField flds Right (TH.RecP (rdrToName con) flds') #endif@@ -379,8 +387,10 @@ TH.AppT . TH.AppT TH.ArrowT <$> convertType t1 <*> convertType t2 convertType (HsParTy _ (L _ t)) = convertType t-convertType (HsQualTy _ _ (L _ t)) =- convertType t+convertType (HsQualTy _ (L _ ctxt) (L _ t)) = do+ ctxt' <- traverse (\(L _ c) -> convertType c) ctxt+ t' <- convertType t+ Right (TH.ForallT [] ctxt' t') convertType (HsForAllTy{}) = unsupportedLanguageFeatureMsg "HsForAllTy in a type" convertType (HsAppKindTy{}) = unsupportedLanguageFeatureMsg "HsAppKindTy in a type" convertType (HsOpTy{}) = unsupportedLanguageFeatureMsg "HsOpTy in a type"
src/Hpgsql/Locking.hs view
@@ -4,7 +4,7 @@ import qualified Control.Concurrent.STM as STM import Control.Exception.Safe (bracket) import Control.Monad (void)-import Hpgsql.InternalTypes (Mutex (..), WeakThreadId (..), throwIrrecoverableError)+import Hpgsql.Internal.InternalTypes (Mutex (..), WeakThreadId (..), throwIrrecoverableError) #if MIN_VERSION_base(4,19,0) import GHC.Conc.Sync (fromThreadId) #else
src/Hpgsql/Msgs.hs view
@@ -1,4 +1,4 @@-module Hpgsql.Msgs (AuthenticationResponse (..), AuthenticationMethod (..), BackendKeyData (..), Bind (..), BindComplete (..), CancelRequest (..), CommandComplete (..), CopyData (..), CopyDone (..), CopyFail (..), CopyInResponse (..), DataRow (..), Describe (..), ErrorDetail (..), ErrorResponse (..), Execute (..), Flush (..), NoData (..), ParameterStatus (..), Query (..), ReadyForQuery (..), RowDescription (..), SASLInitialResponse (..), SASLResponse (..), StartupMessage (..), ToPgMessage (..), FromPgMessage (..), PgMsgParser (..), Terminate (..), TransactionStatus (..), NoticeResponse (..), NotificationResponse (..), Parse (..), ParseComplete (..), PasswordMessage (..), Sync (..), parsePgMessage, nulTermCString) where+module Hpgsql.Msgs (AuthenticationResponse (..), AuthenticationMethod (..), BackendKeyData (..), Bind (..), BindComplete (..), CancelRequest (..), CommandComplete (..), CopyData (..), CopyDone (..), CopyFail (..), CopyInResponse (..), DataRow (..), Describe (..), ErrorDetail (..), ErrorResponse (..), Execute (..), Flush (..), NoData (..), ParameterStatus (..), Query (..), ReadyForQuery (..), RowDescription (..), SASLInitialResponse (..), SASLResponse (..), StartupMessage (..), ToPgMessage (..), FromPgMessage (..), PgMsgParser (..), Terminate (..), TransactionStatus (..), NoticeResponse (..), NotificationResponse (..), Parse (..), ParseComplete (..), PasswordMessage (..), Sync (..), parsePgMessage) where import Control.Applicative (Alternative (..)) import Control.Arrow (Kleisli (..))@@ -23,8 +23,8 @@ import Data.Word (Word8) import Hpgsql.Builder (BinaryField, Builder, builderLength) import qualified Hpgsql.Builder as Builder-import qualified Hpgsql.Encoding.BinarySerializer as BinSer-import Hpgsql.InternalTypes (BindComplete (..), CommandComplete (..), CopyInResponse (..), DataRow (..), ErrorDetail (..), ErrorResponse (..), NoData (..), NotificationResponse (..), ParseComplete (..), ReadyForQuery (..), RowDescription (..), TransactionStatus (..))+import Hpgsql.Internal.InternalTypes (BindComplete (..), CommandComplete (..), CopyInResponse (..), DataRow (..), ErrorDetail (..), ErrorResponse (..), NoData (..), NotificationResponse (..), ParseComplete (..), ReadyForQuery (..), RowDescription (..), TransactionStatus (..))+import qualified Hpgsql.Internal.PinnedByteArray as PBA import Hpgsql.ScramSHA256 (ScramClientFinalMessage (..), ScramServerFirstMessage (..)) import Hpgsql.TypeInfo (Oid (..)) @@ -54,7 +54,7 @@ colName <- nulTerminatedCStringParser -- Column name as C string void $ Parsec.take (4 + 2) -- TODO: OIDs are unsigned integers! Try `select (-1)::oid` to see. Change to UInt32 somehow- typOid <- either fail pure . BinSer.decodeInt32BE 0 =<< Parsec.take 4+ typOid <- either fail pure . PBA.decodeInt32BE 0 . PBA.fromByteString =<< Parsec.take 4 void $ Parsec.take (2 + 4 + 2) pure (colName, Oid (fromIntegral typOid)) @@ -138,7 +138,7 @@ instance FromPgMessage AuthenticationResponse where msgParser = PgMsgParser $ \c (LBS.drop 5 -> restOfMsg) -> case c of- 'R' -> case first (BinSer.decodeInt32BE 0 . LBS.toStrict) $ LBS.splitAt 4 restOfMsg of+ 'R' -> case first (PBA.decodeInt32BE 0 . PBA.fromByteString . LBS.toStrict) $ LBS.splitAt 4 restOfMsg of (Right 0, _) -> Just $ AuthenticationResponse AuthOk (Right 2, _) -> Just $ AuthenticationResponse AuthKerberosV5 (Right 3, _) -> Just $ AuthenticationResponse AuthCleartextPassword@@ -155,7 +155,7 @@ instance FromPgMessage BackendKeyData where msgParser = PgMsgParser $ \c (LBS.splitAt 4 . LBS.drop 5 -> (pidBS, backendSecretKey)) -> case c of- 'K' -> case BinSer.decodeInt32BE 0 $ LBS.toStrict pidBS of+ 'K' -> case PBA.decodeInt32BE 0 $ PBA.fromByteString $ LBS.toStrict pidBS of Right pid -> Just $ BackendKeyData {backendPid = pid, backendSecretKey = LBS.toStrict backendSecretKey} Left _ -> Nothing _ -> Nothing@@ -193,9 +193,10 @@ in Builder.char7 'd' <> Builder.int32BE (4 + len) <> bs instance ToPgMessage CopyFail where- toPgMessage (CopyFail bs) =- let cstr = nulTermCString bs- in Builder.char7 'f' <> Builder.int32BE (4 + fromIntegral (length bs) + 1) <> cstr+ toPgMessage (CopyFail failMsg) =+ let bs = Builder.nulTerminatedUtf8String failMsg+ len = Builder.builderLength bs+ in Builder.char7 'f' <> Builder.int32BE (4 + len) <> bs instance ToPgMessage CopyDone where toPgMessage CopyDone =@@ -203,12 +204,12 @@ instance ToPgMessage Describe where toPgMessage Describe =- let unnamedPortal = nulTermCString ""+ let unnamedPortal = Builder.nulTerminatedUtf8String "" in Builder.char7 'D' <> Builder.int32BE (4 + 1 + 1) <> Builder.char7 'P' <> unnamedPortal instance ToPgMessage Execute where toPgMessage _ =- let unnamedPortal = nulTermCString ""+ let unnamedPortal = Builder.nulTerminatedUtf8String "" infiniteRowsToReturn :: Int32 = 0 contents = unnamedPortal <> Builder.int32BE infiniteRowsToReturn contentsLen = fromIntegral $ LBS.length $ Builder.toLazyByteString contents@@ -225,7 +226,7 @@ instance FromPgMessage DataRow where msgParser = PgMsgParser $ \c !fullDataRow -> case c of- 'D' -> Just $ DataRow {fullDataRow = LBS.toStrict fullDataRow}+ 'D' -> Just $ DataRow {fullDataRow = PBA.fromByteString $ LBS.toStrict fullDataRow} _ -> Nothing instance FromPgMessage NoData where@@ -248,10 +249,10 @@ instance ToPgMessage PasswordMessage where toPgMessage (PasswordMessage crypt username password) = let passwordBs = case crypt of- AuthCleartextPassword -> nulTermCString password+ AuthCleartextPassword -> Builder.nulTerminatedUtf8String password AuthMD5Password (LBS.toStrict -> salt) ->- let passwordBytes = Builder.toStrictByteString (Builder.string7 password)- usernameBytes = Builder.toStrictByteString (Builder.string7 username)+ let passwordBytes = Builder.toStrictByteString (Builder.nulTerminatedUtf8String password)+ usernameBytes = Builder.toStrictByteString (Builder.nulTerminatedUtf8String username) innerHex = bytestringToHex (md5Hash (passwordBytes <> usernameBytes)) outerHex = bytestringToHex (md5Hash (innerHex <> salt)) in Builder.byteString "md5" <> Builder.byteString outerHex <> Builder.word8 0@@ -289,9 +290,6 @@ toPgMessage Terminate = Builder.char7 'X' <> Builder.int32BE 4 -nulTermCString :: String -> Builder-nulTermCString s = Builder.string7 s <> Builder.word8 0- bytestringToHex :: ByteString -> ByteString bytestringToHex = BS.concatMap (\w -> let (hi, lo) = w `divMod` 16 in BS.pack [hexChar hi, hexChar lo]) where@@ -301,8 +299,8 @@ instance ToPgMessage Bind where toPgMessage Bind {..} =- let unnamedDestPortal = nulTermCString ""- preparedStmtName = nulTermCString $ fromMaybe "" preparedStmtHash+ let unnamedDestPortal = Builder.nulTerminatedUtf8String ""+ preparedStmtName = Builder.nulTerminatedUtf8String $ fromMaybe "" preparedStmtHash numParamFmtCodesAllBinary :: Int16 = 1 fmtCodesBinary :: Int16 = 1 numQryParams :: Int16 = fromIntegral $ length paramsValuesInOrder@@ -325,7 +323,7 @@ instance ToPgMessage Parse where toPgMessage Parse {..} =- let preparedStmtName = nulTermCString $ fromMaybe "" preparedStmtHash+ let preparedStmtName = Builder.nulTerminatedUtf8String $ fromMaybe "" preparedStmtHash numParamsSpecified :: Int16 = fromIntegral $ length specifiedParameterTypes -- A 0 for an OID means "type unspecified", and is what we use when we don't know the type paramOids = mconcat $ map (Builder.int32BE . maybe 0 fromIntegral) specifiedParameterTypes@@ -337,9 +335,9 @@ toPgMessage StartupMessage {..} = let protocolMajorVersion :: Int16 = 3 protocolMinorVersion :: Int16 = 0- userBS = nulTermCString "user" <> nulTermCString user- databaseBS = nulTermCString "database" <> nulTermCString database- optionsBS = nulTermCString "options" <> nulTermCString options+ userBS = Builder.nulTerminatedUtf8String "user" <> Builder.nulTerminatedUtf8String user+ databaseBS = Builder.nulTerminatedUtf8String "database" <> Builder.nulTerminatedUtf8String database+ optionsBS = Builder.nulTerminatedUtf8String "options" <> Builder.nulTerminatedUtf8String options contents = Builder.int16BE protocolMajorVersion <> Builder.int16BE protocolMinorVersion <> userBS <> databaseBS <> optionsBS <> Builder.word8 0 contentsLen = fromIntegral $ LBS.length $ Builder.toLazyByteString contents in -- TODO: What protocol version do we announce? We're following the example from the docs for now@@ -359,7 +357,7 @@ if c == 'T' then let (numColsBS, colContents) = LBS.splitAt 2 restOfMsg- numCols = either error id $ BinSer.decodeInt16BE 0 $ LBS.toStrict numColsBS+ numCols = either error id $ PBA.decodeInt16BE 0 $ PBA.fromByteString $ LBS.toStrict numColsBS allColOidsParser :: Parsec.Parser [(Text, Oid)] allColOidsParser = replicateM (fromIntegral numCols) colParser in case LazyParsec.parseOnly (allColOidsParser <* Parsec.endOfInput) colContents of@@ -406,7 +404,7 @@ then Nothing else let (notifierPidBs, channelNameAndPayload) = LBS.splitAt 4 restOfMsg- notifierPid = either error id $ BinSer.decodeInt32BE 0 $ LBS.toStrict notifierPidBs+ notifierPid = either error id $ PBA.decodeInt32BE 0 $ PBA.fromByteString $ LBS.toStrict notifierPidBs in case LazyParsec.parseOnly ((NotificationResponse notifierPid <$> nulTerminatedCStringParser <*> nulTerminatedCStringParser) <* Parsec.endOfInput) channelNameAndPayload of
src/Hpgsql/Networking.hs view
@@ -1,3 +1,6 @@+{-# LANGUAGE MagicHash #-}+{-# LANGUAGE UnliftedFFITypes #-}+ -- | -- This module contains code largely copied from the @network@ library -- (BSD-3-Clause), with modifications to remove blocking calls@@ -9,7 +12,9 @@ -- -- See: https://github.com/haskell/network/blob/master/LICENSE module Hpgsql.Networking- ( recvNonBlocking,+ ( RecvResult (..),+ SendResult (..),+ recvNonBlocking, socketWaitRead, socketWaitWrite, sendNonBlocking,@@ -18,13 +23,14 @@ import Control.Concurrent (threadWaitRead, threadWaitWrite) import Control.Exception.Safe (throw)-import Data.ByteString (ByteString)-import Data.ByteString.Internal (createAndTrim) import qualified Data.ByteString.Lazy as L import Data.ByteString.Unsafe (unsafeUseAsCStringLen) import Data.Int (Int64) import Foreign (Ptr, Storable (..), Word8, allocaArray, castPtr, nullPtr, plusPtr)-import Foreign.C (CChar (..), CInt (..), CSize (..), eAGAIN, eWOULDBLOCK, getErrno)+import Foreign.C (CInt (..), CSize (..), Errno (..), eAGAIN, eCONNRESET, ePIPE, eWOULDBLOCK, getErrno)+import GHC.Base (Addr#)+import Hpgsql.Internal.PinnedByteArray (PinnedByteArray, createPinnedByteArray')+import qualified Hpgsql.Internal.PinnedByteArray as PBA import Network.Socket (Socket, withFdSocket) import System.Posix.Types (CSsize (..)) @@ -34,25 +40,39 @@ socketWaitWrite :: Socket -> IO () socketWaitWrite socket = withFdSocket socket (threadWaitWrite . fromIntegral) -recvNonBlocking :: Socket -> Int -> IO ByteString-recvNonBlocking s nbytes = withFdSocket s $ \fd -> createAndTrim nbytes $ \buffer -> do+data RecvResult+ = -- | No bytes available in the socket yet, try again later+ RecvTryAgain+ | -- | When the remote peer has closed the connection.+ RecvRemotePeerClosed+ | RecvSuccess !PinnedByteArray++recvNonBlocking :: Socket -> Int -> IO RecvResult+recvNonBlocking s nbytes = fmap toRecvRes $ withFdSocket s $ \fd -> createPinnedByteArray' nbytes $ \buffer -> do -- Largely copied from https://hackage-content.haskell.org/package/network-3.2.8.0/docs/src/Network.Socket.Buffer.html#recvBufNoWait and other functions from the network library, -- but then modified to our needs.- r <- c_recv fd (castPtr buffer) (fromIntegral nbytes) 0 {-flags-}+ r <- c_recv fd buffer (fromIntegral nbytes) 0 {-flags-} if r >= 0 then do -- putStrLn $ "Asked for " ++ show nbytes ++ ", got " ++ show r- pure $ fromIntegral r+ pure (r, Just r) else do- err <- getErrno+ err@(Errno errnoNum) <- getErrno if err == eAGAIN || err == eWOULDBLOCK then do -- putStrLn $ "Asked for " ++ show nbytes ++ ", but got eAGAIN"- pure 0+ pure (0, Nothing) else- throw $ userError "Internal error in hpgsql's recvNonBlocking"+ throw $ userError $ "Error in recvNonBlocking. errno " ++ show errnoNum+ where+ toRecvRes :: (PinnedByteArray, Maybe CInt) -> RecvResult+ toRecvRes = \case+ (_, Nothing) -> RecvTryAgain+ (pba, Just _) -> if PBA.length pba == 0 then RecvRemotePeerClosed else RecvSuccess pba -sendNonBlocking :: Socket -> L.ByteString -> IO Int64+data SendResult = SendSuccess Int64 | SendTryAgain | SendRemotePeerClosed++sendNonBlocking :: Socket -> L.ByteString -> IO SendResult sendNonBlocking s lbs = do -- Largely copied from https://hackage-content.haskell.org/package/network-3.2.8.0/docs/src/Network.Socket.ByteString.Lazy.Posix.html#send, -- but then modified to our needs.@@ -64,7 +84,20 @@ -- original codebase, but we don't have that here because that -- calls threadWaitWrite, which blocks. c_writev fd ptr niovs- return $ fromIntegral siz+ if siz >= 0+ then+ pure $ SendSuccess (fromIntegral siz)+ else do+ err@(Errno errnoNum) <- getErrno+ if err == eAGAIN || err == eWOULDBLOCK+ then+ pure SendTryAgain+ else+ if err == eCONNRESET || err == ePIPE+ then+ pure SendRemotePeerClosed+ else+ throw $ userError $ "Error in sendNonBlocking. errno " ++ show errnoNum where withPokes ss p f = loop ss p 0 0 where@@ -115,7 +148,7 @@ -- pokeIov ptr (sPtr, sLen) = poke ptr $ IOVec sPtr (fromIntegral sLen) foreign import ccall unsafe "recv"- c_recv :: CInt -> Ptr CChar -> CSize -> CInt -> IO CInt+ c_recv :: CInt -> Addr# -> CSize -> CInt -> IO CInt foreign import ccall unsafe "writev" c_writev :: CInt -> Ptr IOVec -> CInt -> IO CSsize
src/Hpgsql/Notification.hs view
@@ -6,4 +6,4 @@ where import Hpgsql.Internal (getNotification, getNotificationNonBlocking)-import Hpgsql.InternalTypes (NotificationResponse (..))+import Hpgsql.Internal.InternalTypes (NotificationResponse (..))
src/Hpgsql/ParsingInternal.hs view
@@ -150,6 +150,8 @@ || c == 'E' || c+ == 'e'+ || c == '?' || ( case popts of AcceptQuasiQuoterExpressions _ -> c == '#' || c == '^'@@ -290,7 +292,7 @@ -- See https://www.postgresql.org/docs/current/sql-syntax-lexical.html cStyleEscapedString :: Parser BlockOrNotBlock cStyleEscapedString = fmap StaticSql $ do- openingChars <- string "E'"+ openingChars <- string "E'" <|> string "e'" rest <- Parsec.scan (False, False)
src/Hpgsql/Query.hs view
@@ -23,7 +23,7 @@ import Data.Proxy (Proxy (..)) import Hpgsql.Builder (BinaryField (..)) import Hpgsql.Encoding (RowEncoder (..), ToPgRow (..))-import Hpgsql.InternalTypes (Query (..), SingleQuery (..), SingleQueryFragment (..), breakQueryIntoStatements)+import Hpgsql.Internal.InternalTypes (Query (..), SingleQuery (..), SingleQueryFragment (..), breakQueryIntoStatements) import Hpgsql.QueryInternal (encodeParam, mkQuery, mkQueryInternal, sql, sqlPrep) import Hpgsql.TypeInfo (EncodingContext, Oid) @@ -57,7 +57,7 @@ commaSeparatedRowTuples :: [[EncodingContext -> (Maybe Oid, BinaryField)]] -> Query commaSeparatedRowTuples rowTuples = let (_, queryFragsPerRow) =- List.mapAccumR+ List.mapAccumL ( \(!maxArgSoFar) singleRow -> let numParams = length singleRow numberedArgs = map (QueryArgumentPlaceHolder . (+ maxArgSoFar)) [1 .. numParams]
src/Hpgsql/QueryInternal.hs view
@@ -21,7 +21,7 @@ import Data.Text.Encoding (decodeUtf8, encodeUtf8) import Hpgsql.Builder (BinaryField) import Hpgsql.Encoding (FieldEncoder (..), RowEncoder (..), ToPgField (..), ToPgRow (..))-import Hpgsql.InternalTypes (Query (..), SingleQuery (..), SingleQueryFragment (..), breakQueryIntoStatements, renumberParamsFrom)+import Hpgsql.Internal.InternalTypes (Query (..), SingleQuery (..), SingleQueryFragment (..), breakQueryIntoStatements, renumberParamsFrom) import Hpgsql.LanguageHaskell.ParseHaskellExpression (parseHaskellExpression) import Hpgsql.ParsingInternal (BlockOrNotBlock (..), ParsingOpts (..), QQExprKind (..), blockText, flattenBlocks, parseSql) import Hpgsql.TypeInfo (EncodingContext, Oid)@@ -227,8 +227,8 @@ let (queryFrags, allParams) = go parts 1 in Query {queryString = queryFrags, queryParams = allParams, isPrepared} where- go [] _ = ([], [])- go (part : rest) argNum = case part of+ go [] !_ = ([], [])+ go (part : rest) !argNum = case part of StaticSqlPart bs -> let (restFrags, restParams) = go rest argNum in (FragmentOfStaticSql bs : restFrags, restParams)@@ -243,7 +243,8 @@ in (FragmentWithSemiColon : restFrags, restParams) EmbeddedQueryPart q -> let (newMaxArgNum, renumberedFrags) = renumberParamsFrom q.queryString argNum- (restFrags, restParams) = go rest (newMaxArgNum + 1)+ nextArgNum = if null q.queryParams then argNum else newMaxArgNum + 1+ (restFrags, restParams) = go rest nextArgNum in (renumberedFrags ++ restFrags, q.queryParams ++ restParams) parseBlockQuasiQuoter :: BlockOrNotBlock -> [SqlFragment]
src/Hpgsql/ScramSHA256.hs view
@@ -22,7 +22,7 @@ -- TODO: We should test these scenarios, but we don't yet. import Control.Applicative ((<|>))-import Control.Monad (void)+import Control.Monad (unless, void) import Crypto.Hash (Digest, SHA256, hash) import Crypto.KDF.PBKDF2 (Parameters (..), fastPBKDF2_SHA256) import Crypto.MAC.HMAC (HMAC, hmac, hmacGetDigest)@@ -149,6 +149,12 @@ -- i=4096 void $ string "r=" <|> fail "Missing nonce in server-first-message (1)" nonce <- Parsec.takeWhile1 (/= ',') <|> fail "Missing nonce in server-first-message (2)"+ -- As per the RFC, the client MUST verify that the nonce in the server-first-message+ -- extends the nonce it sent in the client-first-message, aborting otherwise. clientFirstBare+ -- is always "n=,r=" <> clientNonce (see generateClientFirstMessage), so we drop 5+ -- characters to get the clientNonce+ unless (BS.drop 5 clientFirstBare `BS.isPrefixOf` nonce) $+ fail "Server's nonce in the server-first-message does not extend the client's nonce; aborting SCRAM authentication" void $ string "," void $ string "s=" <|> fail "Missing salt in server-first-message (1)" saltB64 <- Parsec.takeWhile1 (/= ',') <|> fail "Missing salt in server-first-message (2)"
src/Hpgsql/SimpleParser.hs view
@@ -1,197 +1,263 @@ -- |--- A minimal attoparsec-like parser that uses CPS to reduce allocations--- and perform better than attoparsec, at least the way we use it in--- hpgsql.------ In benchmarks, this improved performance by 12-15% materializing--- query results when it was introduced.--- With the INLINE pragma in RowDecoder's Applicative's (<*>), GHC's--- inliner was finally able to make full use of continuation passing,--- and performance was improved by another ~14.3%, with total memory--- allocations reduced by ~6%.+-- An attoparsec-like parser for 'PinnedByteArray'. module Hpgsql.SimpleParser ( Parser (..), ParseResult (..), parseOnly, take, endOfInput,- match,- parseMany,- matchLeftUnconsumed, takeInt16BE, takeInt32BE, takeInt64BE,- takeDataRow, parseManyRows, skip,+ takeInt64BEWithFieldLength,+ takeInt32BEWithFieldLength,+ takeInt16BEWithFieldLength,+ takeFloatBE,+ takeDoubleBE,+ takeFloatBEWithFieldLength,+ peekInt32BE,+ takeUtf8Text,+ takeWord64BE,+ takeWord8WithFieldLength, ) where -import Data.ByteString (ByteString)-import qualified Data.ByteString as BS+import Control.Applicative (Alternative (..)) import Data.Int (Int16, Int32, Int64)-import Hpgsql.Encoding.BinarySerializer (ByteStringIdx (..))-import qualified Hpgsql.Encoding.BinarySerializer as BinSer+import Data.Text (Text)+import Data.Word (Word32, Word64, Word8)+import GHC.Float (castWord32ToFloat, castWord64ToDouble)+import Hpgsql.Internal.PinnedByteArray (ByteStringIdx (..), PinnedByteArray)+import qualified Hpgsql.Internal.PinnedByteArray as PBA import Prelude hiding (take) data ParseResult a = ParseFail !String- | ParseOk !a+ | ParseOk !a !ByteStringIdx deriving stock (Show) --- | A parser that consumes a strict 'ByteString'.+-- | A parser that consumes a strict 'PinnedByteArray'. newtype Parser a = Parser { unParser ::- forall r. ByteStringIdx ->- ByteString ->- (String -> r) ->- -- \^ failure continuation- (a -> ByteStringIdx -> ByteString -> r) ->- -- \^ success continuation, taking original or new ByteString, the index into the original/new bytestring of the first yet-unparsed byte, and parsed value- r+ PinnedByteArray ->+ ParseResult a } instance Functor Parser where- fmap f (Parser p) = Parser $ \idx bs kf ks ->- p idx bs kf (\a bs' -> ks (f a) bs')+ fmap f (Parser p) = Parser $ \idx bs -> case p idx bs of+ ParseFail err -> ParseFail err+ ParseOk a idx' -> ParseOk (f a) idx' {-# INLINE fmap #-} instance Applicative Parser where- pure a = Parser $ \idx bs _ ks -> ks a idx bs+ pure a = Parser $ \idx _ -> ParseOk a idx {-# INLINE pure #-} - Parser pf <*> Parser pa = Parser $ \idx bs kf ks ->- pf idx bs kf (\f bs' idx' -> pa bs' idx' kf (\a bs'' idx'' -> ks (f a) bs'' idx''))+ Parser pf <*> Parser pa = Parser $ \idx bs -> case pf idx bs of+ ParseFail err -> ParseFail err+ ParseOk f idx' -> case pa idx' bs of+ ParseFail err -> ParseFail err+ ParseOk a idx'' -> ParseOk (f a) idx'' {-# INLINE (<*>) #-} +instance Alternative Parser where+ empty = fail "empty Alternative"+ {-# INLINE empty #-}+ Parser p1 <|> Parser p2 = Parser $ \idx bs -> case p1 idx bs of+ ParseFail _ -> p2 idx bs+ r -> r+ {-# INLINE (<|>) #-}+ instance Monad Parser where return = pure {-# INLINE return #-} - Parser p >>= k = Parser $ \idx bs kf ks ->- p idx bs kf (\a bs' idx' -> unParser (k a) bs' idx' kf ks)+ Parser p >>= k = Parser $ \idx bs -> case p idx bs of+ ParseFail err -> ParseFail err+ ParseOk a idx' -> unParser (k a) idx' bs {-# INLINE (>>=) #-} instance MonadFail Parser where- fail msg = Parser $ \_ _ kf _ -> kf msg+ fail msg = Parser $ \_ _ -> ParseFail msg {-# INLINE fail #-} -- | Run a parser and return either an error message or the parsed value, -- using the strict 'ParseResult' type. Any unconsumed trailing input is -- discarded.-parseOnly :: Parser a -> ByteString -> ParseResult a+parseOnly :: Parser a -> PinnedByteArray -> ParseResult a parseOnly p = parseOnlyOffset p 0 {-# INLINE parseOnly #-} -- | Run a parser and return either an error message or the parsed value, -- using the strict 'ParseResult' type. Any unconsumed trailing input is -- discarded.-parseOnlyOffset :: Parser a -> ByteStringIdx -> ByteString -> ParseResult a-parseOnlyOffset (Parser p) idx bs = p idx bs ParseFail (\a _ _ -> ParseOk a)+parseOnlyOffset :: Parser a -> ByteStringIdx -> PinnedByteArray -> ParseResult a+parseOnlyOffset (Parser p) = p {-# INLINE parseOnlyOffset #-} --- | Consume exactly @n@ bytes of input, failing if fewer than @n@ bytes+-- | Consume exactly `n` bytes of input, failing if fewer than `n` bytes -- remain.-take :: Int -> Parser ByteString-take n = Parser $ \idx bs kf ks ->+take :: Int -> Parser PinnedByteArray+take n = Parser $ \idx sbs -> let skip' = n + idx.idx- in if BS.length bs >= skip'- then case BS.take n $ BS.drop idx.idx bs of+ in if PBA.length sbs >= skip'+ -- TODO: dropAndTake in a single call+ then case PBA.take n $ PBA.drop idx.idx sbs of -- Strict on the bytestring because we're pretty sure -- the field decoder will need to evaluate this anyway, -- so no need for an extra thunk- !h -> ks h (ByteStringIdx skip') bs- else kf "take: insufficient bytes"+ !h -> ParseOk h (ByteStringIdx skip')+ else ParseFail "take: insufficient bytes" {-# INLINE take #-} +-- | Consume exactly `n` bytes of input, failing if fewer than `n` bytes+-- remain, and assumes those next `n` bytes are UTF8 text, so returns them+-- as `Text`.+takeUtf8Text :: Int -> Parser Text+takeUtf8Text n = Parser $ \idx sbs ->+ let skip' = n + idx.idx+ in if PBA.length sbs >= skip'+ then case PBA.unsafeToUtf8Text idx n sbs of+ Left err -> ParseFail err+ Right t -> ParseOk t (ByteStringIdx skip')+ else ParseFail ("take: wanted " <> show skip' <> " bytes but only " <> show (PBA.length sbs) <> " remain")+{-# INLINE takeUtf8Text #-}+ -- | Consume exactly @n@ bytes of input, failing if fewer than @n@ bytes -- remain. skip :: Int -> Parser ()-skip n = Parser $ \idx bs _ ks ->- ks () (ByteStringIdx $ idx.idx + n) bs+skip n = Parser $ \idx _ -> ParseOk () (ByteStringIdx $ idx.idx + n) {-# INLINE skip #-} {-# INLINE takeInt16BE #-} takeInt16BE :: Parser Int16-takeInt16BE = Parser $ \idx bs kf ks ->- case BinSer.decodeInt16BE idx bs of- Left err -> kf err- Right v -> ks v (idx + 2) bs+takeInt16BE = Parser $ \idx bs -> case PBA.decodeInt16BE idx bs of+ Right v -> ParseOk v (idx + 2)+ Left err -> ParseFail err {-# INLINE takeInt32BE #-} takeInt32BE :: Parser Int32-takeInt32BE = Parser $ \idx bs kf ks ->- case BinSer.decodeInt32BE idx bs of- Left err -> kf err- Right v -> ks v (idx + 4) bs+takeInt32BE = Parser $ \idx bs -> case PBA.decodeInt32BE idx bs of+ Right v -> ParseOk v (idx + 4)+ Left err -> ParseFail err +{-# INLINE peekInt32BE #-}+peekInt32BE :: Parser Int32+peekInt32BE = Parser $ \idx bs -> case PBA.decodeInt32BE idx bs of+ Right v -> ParseOk v idx+ Left err -> ParseFail err++{-# INLINE takeFloatBEWithFieldLength #-}++-- | Parses both a field length and the field itself, for+-- a Float in a row.+takeFloatBEWithFieldLength :: Parser (Maybe Float)+takeFloatBEWithFieldLength = do+ len <- takeInt32BE+ case len of+ (-1) -> pure Nothing+ _ -> Just . castWord32ToFloat <$> takeWord32BE++{-# INLINE takeFloatBE #-}+takeFloatBE :: Parser Float+takeFloatBE = Parser $ \idx bs -> case PBA.decodeWord32BE idx bs of+ Right v -> ParseOk (castWord32ToFloat v) (idx + 4)+ Left err -> ParseFail err++{-# INLINE takeDoubleBE #-}+takeDoubleBE :: Parser Double+takeDoubleBE = Parser $ \idx bs -> case PBA.decodeWord64BE idx bs of+ Right v -> ParseOk (castWord64ToDouble v) (idx + 8)+ Left err -> ParseFail err++{-# INLINE takeWord8 #-}+takeWord8 :: Parser Word8+takeWord8 = Parser $ \idx bs -> case PBA.decodeWord8 idx bs of+ Right v -> ParseOk v (idx + 1)+ Left err -> ParseFail err++{-# INLINE takeWord8WithFieldLength #-}+takeWord8WithFieldLength :: Parser (Maybe Word8)+takeWord8WithFieldLength = do+ fieldLen <- takeInt32BE+ if fieldLen == (-1)+ then pure Nothing+ else Just <$> takeWord8++{-# INLINE takeInt16BEWithFieldLength #-}++-- | Parses both a field length and the field itself, for+-- an Int16 in a row.+takeInt16BEWithFieldLength :: Parser (Maybe Int16)+takeInt16BEWithFieldLength = do+ fieldLen <- takeInt32BE+ if fieldLen == (-1)+ then pure Nothing+ else Just <$> takeInt16BE++{-# INLINE takeInt32BEWithFieldLength #-}++-- | Parses both a field length and the field itself, for+-- an Int32 in a row.+takeInt32BEWithFieldLength :: Parser (Maybe Int32)+takeInt32BEWithFieldLength = do+ fieldLen <- takeInt32BE+ if fieldLen == (-1)+ then pure Nothing+ else Just <$> takeInt32BE++{-# INLINE takeInt64BEWithFieldLength #-}++-- | Parses both a field length and the field itself, for+-- an Int64 in a row.+takeInt64BEWithFieldLength :: Parser (Maybe Int64)+takeInt64BEWithFieldLength = do+ fieldLen <- takeInt32BE+ if fieldLen == (-1)+ then pure Nothing+ else Just <$> takeInt64BE+ {-# INLINE takeInt64BE #-} takeInt64BE :: Parser Int64-takeInt64BE = Parser $ \idx bs kf ks ->- case BinSer.decodeInt64BE idx bs of- Left err -> kf err- Right v -> ks v (idx + 8) bs+takeInt64BE = Parser $ \idx bs -> case PBA.decodeInt64BE idx bs of+ Right v -> ParseOk v (idx + 8)+ Left err -> ParseFail err -{-# INLINE takeDataRow #-}+{-# INLINE takeWord32BE #-}+takeWord32BE :: Parser Word32+takeWord32BE = Parser $ \idx bs -> case PBA.decodeWord32BE idx bs of+ Right v -> ParseOk v (idx + 4)+ Left err -> ParseFail err --- | A specialized parser to parse a postgres DataRow,--- returning the index of the byte after this DataRow's last.-takeDataRow :: Parser ByteStringIdx-takeDataRow = Parser $ \idx bs kf ks ->- case BinSer.decodeDataRow idx bs of- Left err -> kf err- Right idxRest -> ks idxRest idxRest bs+{-# INLINE takeWord64BE #-}+takeWord64BE :: Parser Word64+takeWord64BE = Parser $ \idx bs -> case PBA.decodeWord64BE idx bs of+ Right v -> ParseOk v (idx + 8)+ Left err -> ParseFail err -parseMany :: Parser a -> Parser [a]-parseMany p = Parser $ \idx' bs' _kf ks -> let (vs, restIdx) = go idx' bs' in ks vs restIdx bs'- where- go idx bs = case parseOnlyOffset (matchLeftUnconsumed p) idx bs of- ParseOk (unconsumedIdx, v) -> let (vs, rest) = go unconsumedIdx bs in (v : vs, rest)- ParseFail _ -> ([], idx)-{-# INLINE parseMany #-}+{-# INLINE takeDataRow #-} --- | Parses as many PG rows as there are available, returns--- the index/offset of the first left-unparsed byte and the+-- | A specialized parser to parse a postgres DataRow.+takeDataRow :: Parser ()+takeDataRow = Parser $ \idx bs -> case PBA.decodeDataRow idx bs of+ Left err -> ParseFail err+ Right idxRest -> ParseOk () idxRest++-- | Parses as many PG rows as there are available, returns the -- number of rows parsed.-parseManyRows :: Parser (ByteStringIdx, Int)-parseManyRows = Parser $ \idx' bs' _kf ks -> let (restIdx, nParsed) = go idx' bs' 0 in ks (restIdx, nParsed) restIdx bs'+parseManyRows :: Parser Int+parseManyRows = Parser $ \idx' bs' -> go idx' bs' 0 where- go idx bs !nParsedSoFar = case parseOnlyOffset takeDataRow idx bs of- ParseOk unconsumedIdx -> go unconsumedIdx bs (nParsedSoFar + 1)- ParseFail _ -> (idx, nParsedSoFar)+ go !idx !bs !nParsedSoFar = case parseOnlyOffset takeDataRow idx bs of+ ParseOk _ unconsumedIdx -> go unconsumedIdx bs (nParsedSoFar + 1)+ ParseFail _ -> ParseOk nParsedSoFar idx {-# INLINE parseManyRows #-} -- | Succeeds only when the input has been fully consumed. endOfInput :: Parser ()-endOfInput = Parser $ \idx bs kf ks ->- if BS.length bs <= idx.idx then ks () idx bs else kf "endOfInput: input remaining"+endOfInput = Parser $ \idx bs ->+ if PBA.length bs <= idx.idx then ParseOk () idx else ParseFail "endOfInput: input remaining" {-# INLINE endOfInput #-}---- | Run a parser and additionally return the slice of input it consumed.--- Because the input is a strict 'ByteString', the returned slice is a view--- over the original buffer and allocates no extra memory.-match :: Parser a -> Parser (ByteString, a)-match (Parser p) = Parser $ \idx bs kf ks ->- p- idx- bs- kf- ( \a idx' bs' ->- let !consumed = BS.take (idx'.idx - idx.idx) $ BS.drop idx.idx bs- in ks (consumed, a) idx' bs'- )-{-# INLINE match #-}---- | Run a parser and additionally return the index to the first unconsumed/unparsed byte--- in the supplied ByteString.-matchLeftUnconsumed :: Parser a -> Parser (ByteStringIdx, a)-matchLeftUnconsumed (Parser p) = Parser $ \idx bs kf ks ->- p- idx- bs- kf- ( \a idx' bs' ->- ks (idx', a) idx' bs'- )-{-# INLINE matchLeftUnconsumed #-}
src/Hpgsql/Transaction.hs view
@@ -4,7 +4,7 @@ import Control.Exception.Safe (Exception (..), bracketWithError, throw, tryJust) import Control.Monad (unless) import Hpgsql.Internal (execute_, fullTransactionStatus, transactionStatus)-import Hpgsql.InternalTypes (HPgConnection (..), InternalConnectionState (..), IrrecoverableHpgsqlError)+import Hpgsql.Internal.InternalTypes (HPgConnection (..), InternalConnectionState (..), IrrecoverableHpgsqlError) import Hpgsql.Query (Query) import Hpgsql.TransactionStatusInternal (TransactionStatus (..))
src/Hpgsql/TypeInfo.hs view
@@ -9,6 +9,7 @@ TypeInfo (..), TypeDetails (..), ArrayTypeDetails (..),+ DomainTypeDetails (..), builtinPgTypesMap, buildTypeInfoCache, lookupTypeByName,@@ -120,8 +121,12 @@ { elemTypeOid :: Oid } -data TypeDetails = BasicType | ArrayType !ArrayTypeDetails | CompositeType | DomainType | EnumType | PseudoType | RangeType | MultiRangeType+newtype DomainTypeDetails = DomainTypeDetails+ { baseTypeOid :: Oid+ } +data TypeDetails = BasicType | ArrayType !ArrayTypeDetails | CompositeType | DomainType !DomainTypeDetails | EnumType | PseudoType | RangeType | MultiRangeType+ data TypeInfoCache = TypeInfoCache !(Map Oid TypeInfo) !(Map Text TypeInfo) instance Semigroup TypeInfoCache where@@ -148,7 +153,7 @@ -- The query to get all the code you find in this file starting from here is: -- 1. psql -X -t -d postgres -c "select typname || E'Oid :: Oid\n' || typname || 'Oid = Oid ' || oid from pg_catalog.pg_type order by oid" | sed 's|\+||g' -- for the many type bindings at the very bottom of the file--- 2. psql -X -t -d postgres -c "select 'TypeInfo ' || typname || 'Oid ' || '\"' || typname || '\" ' || case WHEN typarray=0 THEN 'Nothing' else '(Just ' || typarray || ')' end || ' ' || case WHEN typcategory='A' THEN '(ArrayType (ArrayTypeDetails ' || typelem || '))' WHEN typtype='c' THEN 'CompositeType' WHEN typtype='d' THEN 'DomainType' WHEN typtype='e' THEN 'EnumType' WHEN typtype='p' THEN 'PseudoType' WHEN typtype='r' THEN 'RangeType' WHEN typtype='m' THEN 'MultiRangeType' else 'BasicType' end || ',' from pg_catalog.pg_type order by oid"+-- 2. psql -X -t -d postgres -c "select 'TypeInfo ' || typname || 'Oid ' || '\"' || typname || '\" ' || case WHEN typarray=0 THEN 'Nothing' else '(Just ' || typarray || ')' end || ' ' || case WHEN typcategory='A' THEN '(ArrayType (ArrayTypeDetails ' || typelem || '))' WHEN typtype='c' THEN 'CompositeType' WHEN typtype='d' THEN '(DomainType (DomainTypeDetails ' || typbasetype || '))' WHEN typtype='e' THEN 'EnumType' WHEN typtype='p' THEN 'PseudoType' WHEN typtype='r' THEN 'RangeType' WHEN typtype='m' THEN 'MultiRangeType' else 'BasicType' end || ',' from pg_catalog.pg_type order by oid" -- for the Map -- | This contains every type that is builtin to PostgreSQL@@ -624,17 +629,17 @@ TypeInfo _pg_stat_subscription_statsOid "_pg_stat_subscription_stats" Nothing (ArrayType (ArrayTypeDetails 12342)), TypeInfo pg_stat_subscription_statsOid "pg_stat_subscription_stats" (Just 12341) CompositeType, TypeInfo _cardinal_numberOid "_cardinal_number" Nothing (ArrayType (ArrayTypeDetails 13292)),- TypeInfo cardinal_numberOid "cardinal_number" (Just 13291) DomainType,+ TypeInfo cardinal_numberOid "cardinal_number" (Just 13291) (DomainType (DomainTypeDetails 23)), TypeInfo _character_dataOid "_character_data" Nothing (ArrayType (ArrayTypeDetails 13295)),- TypeInfo character_dataOid "character_data" (Just 13294) DomainType,+ TypeInfo character_dataOid "character_data" (Just 13294) (DomainType (DomainTypeDetails 1043)), TypeInfo _sql_identifierOid "_sql_identifier" Nothing (ArrayType (ArrayTypeDetails 13297)),- TypeInfo sql_identifierOid "sql_identifier" (Just 13296) DomainType,+ TypeInfo sql_identifierOid "sql_identifier" (Just 13296) (DomainType (DomainTypeDetails 19)), TypeInfo _information_schema_catalog_nameOid "_information_schema_catalog_name" Nothing (ArrayType (ArrayTypeDetails 13300)), TypeInfo information_schema_catalog_nameOid "information_schema_catalog_name" (Just 13299) CompositeType, TypeInfo _time_stampOid "_time_stamp" Nothing (ArrayType (ArrayTypeDetails 13303)),- TypeInfo time_stampOid "time_stamp" (Just 13302) DomainType,+ TypeInfo time_stampOid "time_stamp" (Just 13302) (DomainType (DomainTypeDetails 1184)), TypeInfo _yes_or_noOid "_yes_or_no" Nothing (ArrayType (ArrayTypeDetails 13305)),- TypeInfo yes_or_noOid "yes_or_no" (Just 13304) DomainType,+ TypeInfo yes_or_noOid "yes_or_no" (Just 13304) (DomainType (DomainTypeDetails 1043)), TypeInfo _applicable_rolesOid "_applicable_roles" Nothing (ArrayType (ArrayTypeDetails 13309)), TypeInfo applicable_rolesOid "applicable_roles" (Just 13308) CompositeType, TypeInfo _administrable_role_authorizationsOid "_administrable_role_authorizations" Nothing (ArrayType (ArrayTypeDetails 13314)),
src/Hpgsql/Types.hs view
@@ -19,7 +19,9 @@ import Data.Tuple.Only (Only (..)) import Data.Typeable (Proxy (..)) import Hpgsql.Builder (BinaryField (..))-import Hpgsql.Encoding (FieldDecoder (..), FieldEncoder (..), FieldInfo (..), FromPgField (..), FromPgRow (..), RowEncoder (..), ToPgField (..), ToPgRow (..), arrayField, toPgVectorField)+import Hpgsql.Encoding.Internal (FieldDecoder (..), FieldEncoder (..), FieldInfo (..), FromPgField (..), FromPgRow (..), RowEncoder (..), ToPgField (..), ToPgRow (..), arrayFieldRowDec, nullableField, parsePgType, rootBaseTypeOf, singleField, toPgVectorField)+import qualified Hpgsql.Internal.PinnedByteArray as PBA+import qualified Hpgsql.SimpleParser as Parser import Hpgsql.TypeInfo (EncodingContext (..), TypeInfo (..), jsonOid, jsonbOid, lookupTypeByOid) -- | Encodes a Haskell list as a postgres array. You can also use `Vector` if you prefer.@@ -39,8 +41,24 @@ toPgField = \encCtx -> toPgVectorField encCtx . fromPGArray } +{-# RULES+"singleField pgArrayFieldDecoder" singleField pgArrayFieldDecoder = fieldRowDecoder+"singleField (nullableField pgArrayFieldDecoder)" singleField (nullableField pgArrayFieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE pgArrayFieldDecoder #-} -- See Note [singleField fieldDecoder rewrite rules]+pgArrayFieldDecoder :: (FromPgField a) => FieldDecoder (PGArray a)+pgArrayFieldDecoder = PGArray <$> fst (arrayFieldRowDec replicateM)+ instance forall a. (FromPgField a) => FromPgField (PGArray a) where- fieldDecoder = PGArray <$> arrayField replicateM fieldDecoder+ {-# INLINE fieldDecoder #-}+ fieldDecoder = pgArrayFieldDecoder+ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder = \finfo -> do+ len <- Parser.takeInt32BE+ case len of+ (-1) -> pure Nothing+ _ -> Just . PGArray <$> snd (arrayFieldRowDec replicateM) finfo -- | A way to compose two rows. data h :. t = !h :. !t deriving (Eq, Ord, Show, Read)@@ -63,7 +81,7 @@ instance (FromPgRow a, FromPgRow b) => FromPgRow (a :. b) where rowDecoder = (:.) <$> rowDecoder <*> rowDecoder --- | A JSON type that does not incur the costs of deserializing+-- | A JSON type that does not incur the costs of JSON/aeson deserializing -- in its `FromPgField` instance because it assumes postgres only generates -- valid JSON. Useful for extra performance if its opaqueness is not a problem. -- Although it does have a `toJSON` method, using it will incur a@@ -82,21 +100,37 @@ pgJsonByteString :: PgJson -> ByteString pgJsonByteString (PgJson bs) = bs +{-# RULES+"singleField pgJsonFieldDecoder" singleField pgJsonFieldDecoder = fieldRowDecoder+"singleField (nullableField pgJsonFieldDecoder)" singleField (nullableField pgJsonFieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE pgJsonFieldDecoder #-} -- See Note [singleField fieldDecoder rewrite rules]+pgJsonFieldDecoder :: FieldDecoder PgJson+pgJsonFieldDecoder = parsePgType "PgJson" [jsonOid, jsonbOid] $ \finfo ->+ let+ -- jsonb has a byte prepended to the contents and json does not+ !fixJsonb = if rootBaseTypeOf finfo == jsonbOid then BS.drop 1 else Prelude.id+ in+ \case+ Nothing -> Left "Cannot decode SQL null as the Haskell PgJson type. Use a `Maybe PgJson` if you want SQL nulls"+ Just bs -> Right $ PgJson $ fixJsonb bs+ instance FromPgField PgJson where- fieldDecoder =- FieldDecoder- { fieldValueDecoder =- \FieldInfo {fieldTypeOid} ->- let- -- jsonb has a byte prepended to the contents and json does not- !fixJsonb = if fieldTypeOid == jsonbOid then BS.drop 1 else Prelude.id- in- \case- Just bs -> Right $ PgJson $ fixJsonb bs- Nothing -> Left "Cannot decode SQL null as the Haskell PgJson type. Use a `Maybe PgJson` if you want SQL nulls",- allowedPgTypes = (`elem` [jsonOid, jsonbOid]) . fieldTypeOid- }+ {-# INLINE fieldDecoder #-}+ fieldDecoder = pgJsonFieldDecoder + {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder finfo = do+ len <- fromIntegral <$> Parser.takeInt32BE+ if len == (-1)+ then pure Nothing+ else+ fmap (Just . PgJson . PBA.toByteString) $+ if finfo.fieldTypeOid == jsonbOid+ then Parser.skip 1 >> Parser.take (len - 1)+ else Parser.take len+ -- | A newtype wrapper to decode a JSON value with Aeson -- into your type (from either json or jsonb), and to encode -- to jsonb.@@ -104,22 +138,35 @@ deriving stock (Functor, Read, Show) deriving newtype (Eq) +{-# RULES+"singleField aesonFieldDecoder" singleField aesonFieldDecoder = fieldRowDecoder+"singleField (nullableField aesonFieldDecoder)" singleField (nullableField aesonFieldDecoder) = fieldRowDecoder+ #-}++{-# NOINLINE aesonFieldDecoder #-} -- See Note [singleField fieldDecoder rewrite rules]+aesonFieldDecoder :: (FromJSON a) => FieldDecoder (Aeson a)+aesonFieldDecoder = parsePgType "(Aeson a)" [jsonOid, jsonbOid] $ \finfo ->+ let+ -- jsonb has a byte prepended to the contents and json does not+ !fixJsonb = if rootBaseTypeOf finfo == jsonbOid then BS.drop 1 else Prelude.id+ in+ \case+ Nothing -> Left "Cannot decode SQL null as a Haskell (Aeson a) type. Use a `Maybe (Aeson a)` if you want SQL nulls"+ Just bs -> case Aeson.decodeStrict $ fixJsonb bs of+ Just v -> Right $ Aeson v+ Nothing -> Left "Failed to decode the postgres JSON value into your `Aeson a` type with aeson"+ instance (FromJSON a) => FromPgField (Aeson a) where- fieldDecoder =- FieldDecoder- { fieldValueDecoder =- \FieldInfo {fieldTypeOid} ->- let- -- jsonb has a byte prepended to the contents and json does not- !fixJsonb = if fieldTypeOid == jsonbOid then BS.drop 1 else Prelude.id- in- \case- Just bs -> case Aeson.decodeStrict $ fixJsonb bs of- Just v -> Right $ Aeson v- Nothing -> Left "Failed to decode postgres JSON value into your `Aeson a` type. Are you sure it's proper JSON?"- Nothing -> Left "Cannot decode SQL null as a Haskell (Aeson a) type. Use a `Maybe (Aeson a)` if you want SQL nulls",- allowedPgTypes = (`elem` [jsonOid, jsonbOid]) . fieldTypeOid- }+ {-# INLINE fieldDecoder #-}+ fieldDecoder = aesonFieldDecoder+ {-# INLINE specializedFieldDecoder #-}+ specializedFieldDecoder finfo =+ specializedFieldDecoder finfo >>= \case+ Nothing -> pure Nothing+ Just (PgJson jsonBs) ->+ case Aeson.decodeStrict jsonBs of+ Just v -> pure $ Just $ Aeson v+ Nothing -> fail "Failed to decode the postgres JSON value into your `Aeson a` type with aeson" instance (ToJSON a) => ToPgField (Aeson a) where fieldEncoder =