squeal-postgresql-0.1.1.4: Squeal PostgreSQL Library

Copyright(c) Eitan Chatav 2017
Maintainereitan@morphism.tech
Stabilityexperimental
Safe HaskellNone
LanguageHaskell2010

Squeal.PostgreSQL.Binary

Contents

Description

Binary encoding and decoding between Haskell and PostgreSQL types.

Synopsis

Encoding

class ToParam (x :: Type) (pg :: PGType) where #

A ToParam constraint gives an encoding of a Haskell Type into into the binary format of a PostgreSQL PGType.

Minimal complete definition

toParam

Methods

toParam :: x -> K Encoding pg #

>>> :set -XTypeApplications -XDataKinds
>>> toParam @Bool @'PGbool False
K "\NUL"
>>> toParam @Int16 @'PGint2 0
K "\NUL\NUL"
>>> toParam @Int32 @'PGint4 0
K "\NUL\NUL\NUL\NUL"
>>> :set -XMultiParamTypeClasses
>>> newtype Id = Id { getId :: Int16 } deriving Show
>>> instance ToParam Id 'PGint2 where toParam = toParam . getId
>>> toParam @Id @'PGint2 (Id 1)
K "\NUL\SOH"

Instances

ToParam Bool PGbool # 
ToParam Double PGfloat8 # 
ToParam Float PGfloat4 # 
ToParam Int16 PGint2 # 
ToParam Int32 PGint4 # 
ToParam Int64 PGint8 # 
ToParam Word16 PGint2 # 
ToParam Word32 PGint4 # 
ToParam Word64 PGint8 # 
ToParam ByteString PGbytea # 
ToParam ByteString PGbytea # 
ToParam Scientific PGnumeric # 
ToParam Text PGtext # 
ToParam UTCTime PGtimestamptz # 
ToParam Value PGjson # 
ToParam Value PGjsonb # 
ToParam Text PGtext # 
ToParam UUID PGuuid # 
ToParam Day PGdate # 
ToParam DiffTime PGinterval # 
ToParam TimeOfDay PGtime # 
ToParam LocalTime PGtimestamp # 
ToParam Char (PGchar 1) # 

Methods

toParam :: Char -> K PGType Encoding (PGchar 1) #

ToParam (NetAddr IP) PGinet # 
ToParam (TimeOfDay, TimeZone) PGtimetz # 

class ToColumnParam (x :: Type) (ty :: ColumnType) where #

A ToColumnParam constraint lifts the ToParam encoding of a Type to a ColumnType, encoding Maybes to Nulls. You should not define instances of ToColumnParam, just use the provided instances.

Minimal complete definition

toColumnParam

Methods

toColumnParam :: x -> K (Maybe ByteString) ty #

>>> toColumnParam @Int16 @('Required ('NotNull 'PGint2)) 0
K (Just "\NUL\NUL")
>>> toColumnParam @(Maybe Int16) @('Required ('Null 'PGint2)) (Just 0)
K (Just "\NUL\NUL")
>>> toColumnParam @(Maybe Int16) @('Required ('Null 'PGint2)) Nothing
K Nothing

Instances

ToParam x pg => ToColumnParam x (optionality (NotNull pg)) # 

Methods

toColumnParam :: x -> K ColumnType (Maybe ByteString) (optionality (NotNull pg)) #

ToParam x pg => ToColumnParam (Maybe x) (optionality (Null pg)) # 

Methods

toColumnParam :: Maybe x -> K ColumnType (Maybe ByteString) (optionality (Null pg)) #

class SListI tys => ToParams (x :: Type) (tys :: [ColumnType]) where #

A ToParams constraint generically sequences the encodings of Types of the fields of a tuple or record to a row of ColumnTypes. You should not define instances of ToParams. Instead define Generic instances which in turn provide ToParams instances.

Minimal complete definition

toParams

Methods

toParams :: x -> NP (K (Maybe ByteString)) tys #

>>> type PGparams = '[ 'Required ('NotNull 'PGbool), 'Required ('Null 'PGint2)]
>>> toParams @(Bool, Maybe Int16) @PGparams (False, Just 0)
K (Just "\NUL") :* (K (Just "\NUL\NUL") :* Nil)
>>> :set -XDeriveGeneric
>>> data Hparams = Hparams { col1 :: Bool, col2 :: Maybe Int16} deriving GHC.Generic
>>> instance Generic Hparams
>>> toParams @Hparams @PGparams (Hparams False (Just 0))
K (Just "\NUL") :* (K (Just "\NUL\NUL") :* Nil)

Instances

Decoding

class FromValue (pg :: PGType) (y :: Type) where #

A FromValue constraint gives a parser from the binary format of a PostgreSQL PGType into a Haskell Type.

Minimal complete definition

fromValue

Methods

fromValue :: proxy pg -> Value y #

>>> newtype Id = Id { getId :: Int16 } deriving Show
>>> instance FromValue 'PGint2 Id where fromValue = fmap Id . fromValue

Instances

FromValue PGbool Bool # 

Methods

fromValue :: proxy PGbool -> Value Bool #

FromValue PGint2 Int16 # 

Methods

fromValue :: proxy PGint2 -> Value Int16 #

FromValue PGint4 Int32 # 

Methods

fromValue :: proxy PGint4 -> Value Int32 #

FromValue PGint8 Int64 # 

Methods

fromValue :: proxy PGint8 -> Value Int64 #

FromValue PGnumeric Scientific # 

Methods

fromValue :: proxy PGnumeric -> Value Scientific #

FromValue PGfloat4 Float # 

Methods

fromValue :: proxy PGfloat4 -> Value Float #

FromValue PGfloat8 Double # 

Methods

fromValue :: proxy PGfloat8 -> Value Double #

FromValue PGtext Text # 

Methods

fromValue :: proxy PGtext -> Value Text #

FromValue PGtext Text # 

Methods

fromValue :: proxy PGtext -> Value Text #

FromValue PGbytea ByteString # 

Methods

fromValue :: proxy PGbytea -> Value ByteString #

FromValue PGbytea ByteString # 

Methods

fromValue :: proxy PGbytea -> Value ByteString #

FromValue PGtimestamp LocalTime # 
FromValue PGtimestamptz UTCTime # 
FromValue PGdate Day # 

Methods

fromValue :: proxy PGdate -> Value Day #

FromValue PGtime TimeOfDay # 

Methods

fromValue :: proxy PGtime -> Value TimeOfDay #

FromValue PGinterval DiffTime # 

Methods

fromValue :: proxy PGinterval -> Value DiffTime #

FromValue PGuuid UUID # 

Methods

fromValue :: proxy PGuuid -> Value UUID #

FromValue PGjson Value # 

Methods

fromValue :: proxy PGjson -> Value Value #

FromValue PGjsonb Value # 

Methods

fromValue :: proxy PGjsonb -> Value Value #

FromValue PGinet (NetAddr IP) # 

Methods

fromValue :: proxy PGinet -> Value (NetAddr IP) #

FromValue PGtimetz (TimeOfDay, TimeZone) # 

Methods

fromValue :: proxy PGtimetz -> Value (TimeOfDay, TimeZone) #

FromValue (PGchar 1) Char # 

Methods

fromValue :: proxy (PGchar 1) -> Value Char #

class FromColumnValue (colty :: (Symbol, ColumnType)) (y :: Type) where #

A FromColumnValue constraint lifts the FromValue parser to a decoding of a (Symbol, ColumnType) to a Type, decoding Nulls to Maybes. You should not define instances for FromColumnValue, just use the provided instances.

Minimal complete definition

fromColumnValue

Methods

fromColumnValue :: K (Maybe ByteString) colty -> y #

>>> :set -XTypeOperators -XOverloadedStrings
>>> newtype Id = Id { getId :: Int16 } deriving Show
>>> instance FromValue 'PGint2 Id where fromValue = fmap Id . fromValue
>>> fromColumnValue @("col" ::: 'Required ('NotNull 'PGint2)) @Id (K (Just "\NUL\SOH"))
Id {getId = 1}
>>> fromColumnValue @("col" ::: 'Required ('Null 'PGint2)) @(Maybe Id) (K (Just "\NUL\SOH"))
Just (Id {getId = 1})

Instances

class SListI results => FromRow (results :: ColumnsType) y where #

A FromRow constraint generically sequences the parsings of the columns of a ColumnsType into the fields of a record Type provided they have the same field names. You should not define instances of FromRow. Instead define Generic and HasDatatypeInfo instances which in turn provide FromRow instances.

Minimal complete definition

fromRow

Methods

fromRow :: NP (K (Maybe ByteString)) results -> y #

>>> :set -XOverloadedStrings
>>> import Data.Text
>>> newtype Id = Id { getId :: Int16 } deriving Show
>>> instance FromValue 'PGint2 Id where fromValue = fmap Id . fromValue
>>> data Hrow = Hrow { userId :: Id, userName :: Maybe Text } deriving (Show, GHC.Generic)
>>> instance Generic Hrow
>>> instance HasDatatypeInfo Hrow
>>> type PGrow = '["userId" ::: 'Required ('NotNull 'PGint2), "userName" ::: 'Required ('Null 'PGtext)]
>>> fromRow @PGrow @Hrow (K (Just "\NUL\SOH") :* K (Just "bloodninja") :* Nil)
Hrow {userId = Id {getId = 1}, userName = Just "bloodninja"}

Instances

(SListI (Symbol, ColumnType) results, IsProductType y ys, AllZip (Symbol, ColumnType) Type FromColumnValue results ys, SameFields (DatatypeInfoOf y) results) => FromRow results y # 

Methods

fromRow :: NP (Symbol, ColumnType) (K (Symbol, ColumnType) (Maybe ByteString)) results -> y #

Only

newtype Only x #

Only is a 1-tuple type, useful for encoding a single parameter with toParams or decoding a single value with fromRow.

>>> import Data.Text
>>> toParams @(Only (Maybe Text)) @'[ 'Required ('Null 'PGtext)] (Only (Just "foo"))
K (Just "foo") :* Nil
>>> type PGShortRow = '["fromOnly" ::: 'Required ('Null 'PGtext)]
>>> fromRow @PGShortRow @(Only (Maybe Text)) (K (Just "bar") :* Nil)
Only {fromOnly = Just "bar"}

Constructors

Only 

Fields

Instances

Functor Only # 

Methods

fmap :: (a -> b) -> Only a -> Only b #

(<$) :: a -> Only b -> Only a #

Foldable Only # 

Methods

fold :: Monoid m => Only m -> m #

foldMap :: Monoid m => (a -> m) -> Only a -> m #

foldr :: (a -> b -> b) -> b -> Only a -> b #

foldr' :: (a -> b -> b) -> b -> Only a -> b #

foldl :: (b -> a -> b) -> b -> Only a -> b #

foldl' :: (b -> a -> b) -> b -> Only a -> b #

foldr1 :: (a -> a -> a) -> Only a -> a #

foldl1 :: (a -> a -> a) -> Only a -> a #

toList :: Only a -> [a] #

null :: Only a -> Bool #

length :: Only a -> Int #

elem :: Eq a => a -> Only a -> Bool #

maximum :: Ord a => Only a -> a #

minimum :: Ord a => Only a -> a #

sum :: Num a => Only a -> a #

product :: Num a => Only a -> a #

Traversable Only # 

Methods

traverse :: Applicative f => (a -> f b) -> Only a -> f (Only b) #

sequenceA :: Applicative f => Only (f a) -> f (Only a) #

mapM :: Monad m => (a -> m b) -> Only a -> m (Only b) #

sequence :: Monad m => Only (m a) -> m (Only a) #

Eq x => Eq (Only x) # 

Methods

(==) :: Only x -> Only x -> Bool #

(/=) :: Only x -> Only x -> Bool #

Ord x => Ord (Only x) # 

Methods

compare :: Only x -> Only x -> Ordering #

(<) :: Only x -> Only x -> Bool #

(<=) :: Only x -> Only x -> Bool #

(>) :: Only x -> Only x -> Bool #

(>=) :: Only x -> Only x -> Bool #

max :: Only x -> Only x -> Only x #

min :: Only x -> Only x -> Only x #

Read x => Read (Only x) # 
Show x => Show (Only x) # 

Methods

showsPrec :: Int -> Only x -> ShowS #

show :: Only x -> String #

showList :: [Only x] -> ShowS #

Generic (Only x) # 

Associated Types

type Rep (Only x) :: * -> * #

Methods

from :: Only x -> Rep (Only x) x #

to :: Rep (Only x) x -> Only x #

Generic (Only x) # 

Associated Types

type Code (Only x) :: [[*]] #

Methods

from :: Only x -> Rep (Only x) #

to :: Rep (Only x) -> Only x #

HasDatatypeInfo (Only x) # 

Associated Types

type DatatypeInfoOf (Only x) :: DatatypeInfo #

Methods

datatypeInfo :: proxy (Only x) -> DatatypeInfo (Code (Only x)) #

type Rep (Only x) # 
type Rep (Only x) = D1 * (MetaData "Only" "Squeal.PostgreSQL.Binary" "squeal-postgresql-0.1.1.4-5lGnpnW6Fai1MJSuhAYIz" True) (C1 * (MetaCons "Only" PrefixI True) (S1 * (MetaSel (Just Symbol "fromOnly") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 * x)))
type Code (Only x) # 
type Code (Only x) = GCode (Only x)
type DatatypeInfoOf (Only x) #