Skip to content
Browse files

Rename Param and QueryParams, Move Field into FromField

  • Loading branch information...
1 parent 194683a commit 9014819ce50c797c9f2cafa2a4258fe42d794702 @lpsmith committed May 4, 2012
View
9 postgresql-simple.cabal
@@ -1,5 +1,5 @@
Name: postgresql-simple
-Version: 0.0.99
+Version: 0.1
Synopsis: Mid-Level PostgreSQL client library
Description:
Mid-Level PostgreSQL client library, forked from mysql-simple.
@@ -19,15 +19,14 @@ Library
Exposed-modules:
Database.PostgreSQL.Simple
Database.PostgreSQL.Simple.BuiltinTypes
- Database.PostgreSQL.Simple.Field
Database.PostgreSQL.Simple.FromField
Database.PostgreSQL.Simple.FromRow
Database.PostgreSQL.Simple.LargeObjects
Database.PostgreSQL.Simple.Notification
Database.PostgreSQL.Simple.Ok
- Database.PostgreSQL.Simple.Param
- Database.PostgreSQL.Simple.QueryParams
Database.PostgreSQL.Simple.SqlQQ
+ Database.PostgreSQL.Simple.ToField
+ Database.PostgreSQL.Simple.ToRow
Database.PostgreSQL.Simple.Types
-- Other-modules:
Database.PostgreSQL.Simple.Internal
@@ -63,4 +62,4 @@ source-repository head
source-repository this
type: git
location: http://github.com/lpsmith/postgresql-simple
- tag: v0.0.5
+ tag: v0.1
View
24 src/Database/PostgreSQL/Simple.hs
@@ -125,8 +125,8 @@ import Database.PostgreSQL.Simple.Compat ( mask )
import Database.PostgreSQL.Simple.FromField (ResultError(..))
import Database.PostgreSQL.Simple.FromRow (FromRow(..))
import Database.PostgreSQL.Simple.Ok
-import Database.PostgreSQL.Simple.Param (Action(..), inQuotes)
-import Database.PostgreSQL.Simple.QueryParams (QueryParams(..))
+import Database.PostgreSQL.Simple.ToField (Action(..), inQuotes)
+import Database.PostgreSQL.Simple.ToRow (ToRow(..))
import Database.PostgreSQL.Simple.Types
( Binary(..), In(..), Only(..), Query(..) )
import Database.PostgreSQL.Simple.Internal as Base
@@ -167,11 +167,11 @@ instance Exception QueryError
--
-- Throws 'FormatError' if the query string could not be formatted
-- correctly.
-formatQuery :: QueryParams q => Connection -> Query -> q -> IO ByteString
+formatQuery :: ToRow q => Connection -> Query -> q -> IO ByteString
formatQuery conn q@(Query template) qs
| null xs && '?' `B.notElem` template = return template
| otherwise = toByteString <$> buildQuery conn q template xs
- where xs = renderParams qs
+ where xs = toRow qs
-- | Format a query string with a variable number of rows.
--
@@ -186,12 +186,12 @@ formatQuery conn q@(Query template) qs
--
-- Throws 'FormatError' if the query string could not be formatted
-- correctly.
-formatMany :: (QueryParams q) => Connection -> Query -> [q] -> IO ByteString
+formatMany :: (ToRow q) => Connection -> Query -> [q] -> IO ByteString
formatMany _ q [] = fmtError "no rows supplied" q []
formatMany conn q@(Query template) qs = do
case match re template [] of
Just [_,before,qbits,after] -> do
- bs <- mapM (buildQuery conn q qbits . renderParams) qs
+ bs <- mapM (buildQuery conn q qbits . toRow) qs
return . toByteString . mconcat $ fromByteString before :
intersperse (fromChar ',') bs ++
[fromByteString after]
@@ -226,7 +226,7 @@ buildQuery conn q template xs = zipParams (split template) <$> mapM sub xs
-- Returns the number of rows affected.
--
-- Throws 'FormatError' if the query could not be formatted correctly.
-execute :: (QueryParams q) => Connection -> Query -> q -> IO Int64
+execute :: (ToRow q) => Connection -> Query -> q -> IO Int64
execute conn template qs = do
result <- exec conn =<< formatQuery conn template qs
finishExecute conn template result
@@ -243,7 +243,7 @@ execute_ conn q@(Query stmt) = do
-- Returns the number of rows affected.
--
-- Throws 'FormatError' if the query could not be formatted correctly.
-executeMany :: (QueryParams q) => Connection -> Query -> [q] -> IO Int64
+executeMany :: (ToRow q) => Connection -> Query -> [q] -> IO Int64
executeMany _ _ [] = return 0
executeMany conn q qs = do
result <- exec conn =<< formatMany conn q qs
@@ -302,7 +302,7 @@ finishExecute _conn q result = do
-- using 'execute' instead of 'query').
--
-- * 'ResultError': result conversion failed.
-query :: (QueryParams q, FromRow r)
+query :: (ToRow q, FromRow r)
=> Connection -> Query -> q -> IO [r]
query conn template qs = do
result <- exec conn =<< formatQuery conn template qs
@@ -333,7 +333,7 @@ query_ conn q@(Query que) = do
--
-- * 'ResultError': result conversion failed.
-fold :: ( FromRow row, QueryParams params )
+fold :: ( FromRow row, ToRow params )
=> Connection
-> Query
-> params
@@ -358,7 +358,7 @@ defaultFoldOptions = FoldOptions {
transactionMode = TransactionMode ReadCommitted ReadOnly
}
-foldWithOptions :: ( FromRow row, QueryParams params )
+foldWithOptions :: ( FromRow row, ToRow params )
=> FoldOptions
-> Connection
-> Query
@@ -436,7 +436,7 @@ doFold FoldOptions{..} conn _template q a f = do
if null rs then return a else foldM f a rs >>= loop
-- | A version of 'fold' that does not transform a state value.
-forEach :: (QueryParams q, FromRow r) =>
+forEach :: (ToRow q, FromRow r) =>
Connection
-> Query -- ^ Query template.
-> q -- ^ Query parameters.
View
15 src/Database/PostgreSQL/Simple/Field.hs
@@ -1,15 +0,0 @@
-module Database.PostgreSQL.Simple.Field
- ( Field
- , typename
- , name
- , tableOid
- , tableColumn
- , format
- , typeOid
- , Oid
- , Format(..)
- , RawResult(..)
- ) where
-
-import Database.PostgreSQL.Simple.Internal
-import Database.PostgreSQL.LibPQ (Format(..), Oid)
View
53 src/Database/PostgreSQL/Simple/FromField.hs
@@ -27,32 +27,45 @@ module Database.PostgreSQL.Simple.FromField
FromField(..)
, ResultError(..)
, returnError
+
+ , Field
+ , typename
+ , name
+ , tableOid
+ , tableColumn
+ , format
+ , typeOid
+ , PQ.Oid(..)
+ , PQ.Format(..)
) where
#include "MachDeps.h"
-import Control.Applicative (Applicative, (<|>), (<$>), (<*>), (<*), pure)
-import Control.Exception (SomeException(..), Exception)
-import Data.Attoparsec.Char8 hiding (Result)
-import Data.Bits ((.&.), (.|.), shiftL)
-import Data.ByteString (ByteString)
+import Control.Applicative
+ ( Applicative, (<|>), (<$>), (<*>), (<*), pure )
+import Control.Exception (SomeException(..), Exception)
+import Data.Attoparsec.Char8 hiding (Result)
+import Data.Bits ((.&.), (.|.), shiftL)
+import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as B
-import Data.Int (Int16, Int32, Int64)
-import Data.List (foldl')
-import Data.Ratio (Ratio)
-import Data.Time.Calendar (Day, fromGregorian)
-import Data.Time.Clock (UTCTime)
-import Data.Time.Format (parseTime)
-import Data.Time.LocalTime (TimeOfDay, makeTimeOfDayValid)
-import Data.Typeable (Typeable, typeOf)
-import Data.Word (Word64)
-import Database.PostgreSQL.Simple.Internal
-import Database.PostgreSQL.Simple.BuiltinTypes
-import Database.PostgreSQL.Simple.Ok
-import Database.PostgreSQL.Simple.Types (Binary(..), Null(..))
+import Data.Int (Int16, Int32, Int64)
+import Data.List (foldl')
+import Data.Ratio (Ratio)
+import Data.Time.Calendar (Day, fromGregorian)
+import Data.Time.Clock (UTCTime)
+import Data.Time.Format (parseTime)
+import Data.Time.LocalTime (TimeOfDay, makeTimeOfDayValid)
+import Data.Typeable (Typeable, typeOf)
+import Data.Word (Word64)
+import Database.PostgreSQL.Simple.Internal
+import Database.PostgreSQL.Simple.BuiltinTypes
+-- import qualified Database.PostgreSQ
+import Database.PostgreSQL.Simple.Ok
+import Database.PostgreSQL.Simple.Types (Binary(..), Null(..))
import qualified Database.PostgreSQL.LibPQ as PQ
-import System.IO.Unsafe (unsafePerformIO)
-import System.Locale (defaultTimeLocale)
+-- import Database.PostgreSQL.LibPQ (Format(..), Oid(..))
+import System.IO.Unsafe (unsafePerformIO)
+import System.Locale (defaultTimeLocale)
import qualified Data.ByteString as SB
import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Lazy as LB
View
205 src/Database/PostgreSQL/Simple/Param.hs
@@ -1,205 +0,0 @@
-{-# LANGUAGE DeriveDataTypeable, DeriveFunctor, FlexibleInstances #-}
-
--- |
--- Module: Database.PostgreSQL.Simple.Param
--- Copyright: (c) 2011 MailRank, Inc.
--- (c) 2011 Leon P Smith
--- License: BSD3
--- Maintainer: Leon P Smith <leon@melding-monads.com>
--- Stability: experimental
--- Portability: portable
---
--- The 'Param' typeclass, for rendering a parameter to a SQL query.
-
-module Database.PostgreSQL.Simple.Param
- (
- Action(..)
- , Param(..)
- , inQuotes
- ) where
-
-import Blaze.ByteString.Builder (Builder, fromByteString, fromLazyByteString,
- toByteString)
-import Blaze.ByteString.Builder.Char8 (fromChar)
-import Blaze.Text (integral, double, float)
-import Data.ByteString (ByteString)
-import qualified Data.ByteString.Base16 as B16
-import qualified Data.ByteString.Base16.Lazy as L16
-import Data.Int (Int8, Int16, Int32, Int64)
-import Data.List (intersperse)
-import Data.Monoid (mappend)
-import Data.Time.Calendar (Day, showGregorian)
-import Data.Time.Clock (UTCTime)
-import Data.Time.Format (formatTime)
-import Data.Time.LocalTime (TimeOfDay)
-import Data.Typeable (Typeable)
-import Data.Word (Word, Word8, Word16, Word32, Word64)
-import Database.PostgreSQL.Simple.Types (Binary(..), In(..), Null)
-import System.Locale (defaultTimeLocale)
-import qualified Blaze.ByteString.Builder.Char.Utf8 as Utf8
-import qualified Data.ByteString as SB
-import qualified Data.ByteString.Lazy as LB
-import qualified Data.Text as ST
-import qualified Data.Text.Encoding as ST
-import qualified Data.Text.Lazy as LT
-import qualified Database.PostgreSQL.LibPQ as PQ
-
--- | How to render an element when substituting it into a query.
-data Action =
- Plain Builder
- -- ^ Render without escaping or quoting. Use for non-text types
- -- such as numbers, when you are /certain/ that they will not
- -- introduce formatting vulnerabilities via use of characters such
- -- as spaces or \"@'@\".
- | Escape ByteString
- -- ^ Escape and enclose in quotes before substituting. Use for all
- -- text-like types, and anything else that may contain unsafe
- -- characters when rendered.
- | Many [Action]
- -- ^ Concatenate a series of rendering actions.
- deriving (Typeable)
-
-instance Show Action where
- show (Plain b) = "Plain " ++ show (toByteString b)
- show (Escape b) = "Escape " ++ show b
- show (Many b) = "Many " ++ show b
-
--- | A type that may be used as a single parameter to a SQL query.
-class Param a where
- render :: a -> Action
- -- ^ Prepare a value for substitution into a query string.
-
-instance Param Action where
- render a = a
- {-# INLINE render #-}
-
-instance (Param a) => Param (Maybe a) where
- render Nothing = renderNull
- render (Just a) = render a
- {-# INLINE render #-}
-
-instance (Param a) => Param (In [a]) where
- render (In []) = Plain $ fromByteString "(null)"
- render (In xs) = Many $
- Plain (fromChar '(') :
- (intersperse (Plain (fromChar ',')) . map render $ xs) ++
- [Plain (fromChar ')')]
-
-instance Param (Binary SB.ByteString) where
- render (Binary bs) = Plain $ fromByteString "'\\x" `mappend`
- fromByteString (B16.encode bs) `mappend`
- fromChar '\''
-
-instance Param (Binary LB.ByteString) where
- render (Binary bs) = Plain $ fromByteString "'\\x" `mappend`
- fromLazyByteString (L16.encode bs) `mappend`
- fromChar '\''
-
-renderNull :: Action
-renderNull = Plain (fromByteString "null")
-
-instance Param Null where
- render _ = renderNull
- {-# INLINE render #-}
-
-instance Param Bool where
- render True = Plain (fromByteString "true")
- render False = Plain (fromByteString "false")
- {-# INLINE render #-}
-
-instance Param Int8 where
- render = Plain . integral
- {-# INLINE render #-}
-
-instance Param Int16 where
- render = Plain . integral
- {-# INLINE render #-}
-
-instance Param Int32 where
- render = Plain . integral
- {-# INLINE render #-}
-
-instance Param Int where
- render = Plain . integral
- {-# INLINE render #-}
-
-instance Param Int64 where
- render = Plain . integral
- {-# INLINE render #-}
-
-instance Param Integer where
- render = Plain . integral
- {-# INLINE render #-}
-
-instance Param Word8 where
- render = Plain . integral
- {-# INLINE render #-}
-
-instance Param Word16 where
- render = Plain . integral
- {-# INLINE render #-}
-
-instance Param Word32 where
- render = Plain . integral
- {-# INLINE render #-}
-
-instance Param Word where
- render = Plain . integral
- {-# INLINE render #-}
-
-instance Param Word64 where
- render = Plain . integral
- {-# INLINE render #-}
-
-instance Param PQ.Oid where
- render = Plain . integral . \(PQ.Oid x) -> x
- {-# INLINE render #-}
-
-instance Param Float where
- render v | isNaN v || isInfinite v = Plain (inQuotes (float v))
- | otherwise = Plain (float v)
- {-# INLINE render #-}
-
-instance Param Double where
- render v | isNaN v || isInfinite v = Plain (inQuotes (double v))
- | otherwise = Plain (double v)
- {-# INLINE render #-}
-
-instance Param SB.ByteString where
- render = Escape
- {-# INLINE render #-}
-
-instance Param LB.ByteString where
- render = render . SB.concat . LB.toChunks
- {-# INLINE render #-}
-
-instance Param ST.Text where
- render = Escape . ST.encodeUtf8
- {-# INLINE render #-}
-
-instance Param [Char] where
- render = Escape . toByteString . Utf8.fromString
- {-# INLINE render #-}
-
-instance Param LT.Text where
- render = render . LT.toStrict
- {-# INLINE render #-}
-
-instance Param UTCTime where
- render = Plain . Utf8.fromString . formatTime defaultTimeLocale "'%F %T%Q+00'"
- {-# INLINE render #-}
-
-instance Param Day where
- render = Plain . inQuotes . Utf8.fromString . showGregorian
- {-# INLINE render #-}
-
-instance Param TimeOfDay where
- render = Plain . inQuotes . Utf8.fromString . show
- {-# INLINE render #-}
-
--- | Surround a string with single-quote characters: \"@'@\"
---
--- This function /does not/ perform any other escaping.
-inQuotes :: Builder -> Builder
-inQuotes b = quote `mappend` b `mappend` quote
- where quote = Utf8.fromChar '\''
View
85 src/Database/PostgreSQL/Simple/QueryParams.hs
@@ -1,85 +0,0 @@
--- |
--- Module: Database.PostgreSQL.Simple.QueryParams
--- Copyright: (c) 2011 MailRank, Inc.
--- (c) 2011 Leon P Smith
--- License: BSD3
--- Maintainer: Leon P Smith <leon@melding-monads.com>
--- Stability: experimental
--- Portability: portable
---
--- The 'QueryParams' typeclass, for rendering a collection of
--- parameters to a SQL query.
---
--- Predefined instances are provided for tuples containing up to ten
--- elements.
-
-module Database.PostgreSQL.Simple.QueryParams
- (
- QueryParams(..)
- ) where
-
-import Database.PostgreSQL.Simple.Param (Action(..), Param(..))
-import Database.PostgreSQL.Simple.Types (Only(..))
-
--- | A collection type that can be turned into a list of rendering
--- 'Action's.
---
--- Instances should use the 'render' method of the 'Param' class
--- to perform conversion of each element of the collection.
-class QueryParams a where
- renderParams :: a -> [Action]
- -- ^ Render a collection of values.
-
-instance QueryParams () where
- renderParams _ = []
-
-instance (Param a) => QueryParams (Only a) where
- renderParams (Only v) = [render v]
-
-instance (Param a, Param b) => QueryParams (a,b) where
- renderParams (a,b) = [render a, render b]
-
-instance (Param a, Param b, Param c) => QueryParams (a,b,c) where
- renderParams (a,b,c) = [render a, render b, render c]
-
-instance (Param a, Param b, Param c, Param d) => QueryParams (a,b,c,d) where
- renderParams (a,b,c,d) = [render a, render b, render c, render d]
-
-instance (Param a, Param b, Param c, Param d, Param e)
- => QueryParams (a,b,c,d,e) where
- renderParams (a,b,c,d,e) =
- [render a, render b, render c, render d, render e]
-
-instance (Param a, Param b, Param c, Param d, Param e, Param f)
- => QueryParams (a,b,c,d,e,f) where
- renderParams (a,b,c,d,e,f) =
- [render a, render b, render c, render d, render e, render f]
-
-instance (Param a, Param b, Param c, Param d, Param e, Param f, Param g)
- => QueryParams (a,b,c,d,e,f,g) where
- renderParams (a,b,c,d,e,f,g) =
- [render a, render b, render c, render d, render e, render f, render g]
-
-instance (Param a, Param b, Param c, Param d, Param e, Param f, Param g,
- Param h)
- => QueryParams (a,b,c,d,e,f,g,h) where
- renderParams (a,b,c,d,e,f,g,h) =
- [render a, render b, render c, render d, render e, render f, render g,
- render h]
-
-instance (Param a, Param b, Param c, Param d, Param e, Param f, Param g,
- Param h, Param i)
- => QueryParams (a,b,c,d,e,f,g,h,i) where
- renderParams (a,b,c,d,e,f,g,h,i) =
- [render a, render b, render c, render d, render e, render f, render g,
- render h, render i]
-
-instance (Param a, Param b, Param c, Param d, Param e, Param f, Param g,
- Param h, Param i, Param j)
- => QueryParams (a,b,c,d,e,f,g,h,i,j) where
- renderParams (a,b,c,d,e,f,g,h,i,j) =
- [render a, render b, render c, render d, render e, render f, render g,
- render h, render i, render j]
-
-instance (Param a) => QueryParams [a] where
- renderParams = map render
View
208 src/Database/PostgreSQL/Simple/ToField.hs
@@ -0,0 +1,208 @@
+{-# LANGUAGE DeriveDataTypeable, DeriveFunctor, FlexibleInstances #-}
+
+------------------------------------------------------------------------------
+-- |
+-- Module: Database.PostgreSQL.Simple.ToField
+-- Copyright: (c) 2011 MailRank, Inc.
+-- (c) 2011 Leon P Smith
+-- License: BSD3
+-- Maintainer: Leon P Smith <leon@melding-monads.com>
+-- Stability: experimental
+-- Portability: portable
+--
+-- The 'ToField' typeclass, for rendering a parameter to a SQL query.
+--
+------------------------------------------------------------------------------
+
+module Database.PostgreSQL.Simple.ToField
+ (
+ Action(..)
+ , ToField(..)
+ , inQuotes
+ ) where
+
+import Blaze.ByteString.Builder (Builder, fromByteString, fromLazyByteString,
+ toByteString)
+import Blaze.ByteString.Builder.Char8 (fromChar)
+import Blaze.Text (integral, double, float)
+import Data.ByteString (ByteString)
+import qualified Data.ByteString.Base16 as B16
+import qualified Data.ByteString.Base16.Lazy as L16
+import Data.Int (Int8, Int16, Int32, Int64)
+import Data.List (intersperse)
+import Data.Monoid (mappend)
+import Data.Time.Calendar (Day, showGregorian)
+import Data.Time.Clock (UTCTime)
+import Data.Time.Format (formatTime)
+import Data.Time.LocalTime (TimeOfDay)
+import Data.Typeable (Typeable)
+import Data.Word (Word, Word8, Word16, Word32, Word64)
+import Database.PostgreSQL.Simple.Types (Binary(..), In(..), Null)
+import System.Locale (defaultTimeLocale)
+import qualified Blaze.ByteString.Builder.Char.Utf8 as Utf8
+import qualified Data.ByteString as SB
+import qualified Data.ByteString.Lazy as LB
+import qualified Data.Text as ST
+import qualified Data.Text.Encoding as ST
+import qualified Data.Text.Lazy as LT
+import qualified Database.PostgreSQL.LibPQ as PQ
+
+-- | How to render an element when substituting it into a query.
+data Action =
+ Plain Builder
+ -- ^ Render without escaping or quoting. Use for non-text types
+ -- such as numbers, when you are /certain/ that they will not
+ -- introduce formatting vulnerabilities via use of characters such
+ -- as spaces or \"@'@\".
+ | Escape ByteString
+ -- ^ Escape and enclose in quotes before substituting. Use for all
+ -- text-like types, and anything else that may contain unsafe
+ -- characters when rendered.
+ | Many [Action]
+ -- ^ Concatenate a series of rendering actions.
+ deriving (Typeable)
+
+instance Show Action where
+ show (Plain b) = "Plain " ++ show (toByteString b)
+ show (Escape b) = "Escape " ++ show b
+ show (Many b) = "Many " ++ show b
+
+-- | A type that may be used as a single parameter to a SQL query.
+class ToField a where
+ toField :: a -> Action
+ -- ^ Prepare a value for substitution into a query string.
+
+instance ToField Action where
+ toField a = a
+ {-# INLINE toField #-}
+
+instance (ToField a) => ToField (Maybe a) where
+ toField Nothing = renderNull
+ toField (Just a) = toField a
+ {-# INLINE toField #-}
+
+instance (ToField a) => ToField (In [a]) where
+ toField (In []) = Plain $ fromByteString "(null)"
+ toField (In xs) = Many $
+ Plain (fromChar '(') :
+ (intersperse (Plain (fromChar ',')) . map toField $ xs) ++
+ [Plain (fromChar ')')]
+
+instance ToField (Binary SB.ByteString) where
+ toField (Binary bs) = Plain $ fromByteString "'\\x" `mappend`
+ fromByteString (B16.encode bs) `mappend`
+ fromChar '\''
+
+instance ToField (Binary LB.ByteString) where
+ toField (Binary bs) = Plain $ fromByteString "'\\x" `mappend`
+ fromLazyByteString (L16.encode bs) `mappend`
+ fromChar '\''
+
+renderNull :: Action
+renderNull = Plain (fromByteString "null")
+
+instance ToField Null where
+ toField _ = renderNull
+ {-# INLINE toField #-}
+
+instance ToField Bool where
+ toField True = Plain (fromByteString "true")
+ toField False = Plain (fromByteString "false")
+ {-# INLINE toField #-}
+
+instance ToField Int8 where
+ toField = Plain . integral
+ {-# INLINE toField #-}
+
+instance ToField Int16 where
+ toField = Plain . integral
+ {-# INLINE toField #-}
+
+instance ToField Int32 where
+ toField = Plain . integral
+ {-# INLINE toField #-}
+
+instance ToField Int where
+ toField = Plain . integral
+ {-# INLINE toField #-}
+
+instance ToField Int64 where
+ toField = Plain . integral
+ {-# INLINE toField #-}
+
+instance ToField Integer where
+ toField = Plain . integral
+ {-# INLINE toField #-}
+
+instance ToField Word8 where
+ toField = Plain . integral
+ {-# INLINE toField #-}
+
+instance ToField Word16 where
+ toField = Plain . integral
+ {-# INLINE toField #-}
+
+instance ToField Word32 where
+ toField = Plain . integral
+ {-# INLINE toField #-}
+
+instance ToField Word where
+ toField = Plain . integral
+ {-# INLINE toField #-}
+
+instance ToField Word64 where
+ toField = Plain . integral
+ {-# INLINE toField #-}
+
+instance ToField PQ.Oid where
+ toField = Plain . integral . \(PQ.Oid x) -> x
+ {-# INLINE toField #-}
+
+instance ToField Float where
+ toField v | isNaN v || isInfinite v = Plain (inQuotes (float v))
+ | otherwise = Plain (float v)
+ {-# INLINE toField #-}
+
+instance ToField Double where
+ toField v | isNaN v || isInfinite v = Plain (inQuotes (double v))
+ | otherwise = Plain (double v)
+ {-# INLINE toField #-}
+
+instance ToField SB.ByteString where
+ toField = Escape
+ {-# INLINE toField #-}
+
+instance ToField LB.ByteString where
+ toField = toField . SB.concat . LB.toChunks
+ {-# INLINE toField #-}
+
+instance ToField ST.Text where
+ toField = Escape . ST.encodeUtf8
+ {-# INLINE toField #-}
+
+instance ToField [Char] where
+ toField = Escape . toByteString . Utf8.fromString
+ {-# INLINE toField #-}
+
+instance ToField LT.Text where
+ toField = toField . LT.toStrict
+ {-# INLINE toField #-}
+
+instance ToField UTCTime where
+ toField = Plain . Utf8.fromString . formatTime defaultTimeLocale "'%F %T%Q+00'"
+ {-# INLINE toField #-}
+
+instance ToField Day where
+ toField = Plain . inQuotes . Utf8.fromString . showGregorian
+ {-# INLINE toField #-}
+
+instance ToField TimeOfDay where
+ toField = Plain . inQuotes . Utf8.fromString . show
+ {-# INLINE toField #-}
+
+-- | Surround a string with single-quote characters: \"@'@\"
+--
+-- This function /does not/ perform any other escaping.
+inQuotes :: Builder -> Builder
+inQuotes b = quote `mappend` b `mappend` quote
+ where quote = Utf8.fromChar '\''
View
90 src/Database/PostgreSQL/Simple/ToRow.hs
@@ -0,0 +1,90 @@
+------------------------------------------------------------------------------
+-- |
+-- Module: Database.PostgreSQL.Simple.QueryParams
+-- Copyright: (c) 2011 MailRank, Inc.
+-- (c) 2011 Leon P Smith
+-- License: BSD3
+-- Maintainer: Leon P Smith <leon@melding-monads.com>
+-- Stability: experimental
+-- Portability: portable
+--
+-- The 'QueryParams' typeclass, for rendering a collection of
+-- parameters to a SQL query.
+--
+-- Predefined instances are provided for tuples containing up to ten
+-- elements.
+--
+------------------------------------------------------------------------------
+
+module Database.PostgreSQL.Simple.ToRow
+ (
+ ToRow(..)
+ ) where
+
+import Database.PostgreSQL.Simple.ToField (Action(..), ToField(..))
+import Database.PostgreSQL.Simple.Types (Only(..))
+
+-- | A collection type that can be turned into a list of rendering
+-- 'Action's.
+--
+-- Instances should use the 'render' method of the 'Param' class
+-- to perform conversion of each element of the collection.
+class ToRow a where
+ toRow :: a -> [Action]
+ -- ^ ToField a collection of values.
+
+instance ToRow () where
+ toRow _ = []
+
+instance (ToField a) => ToRow (Only a) where
+ toRow (Only v) = [toField v]
+
+instance (ToField a, ToField b) => ToRow (a,b) where
+ toRow (a,b) = [toField a, toField b]
+
+instance (ToField a, ToField b, ToField c) => ToRow (a,b,c) where
+ toRow (a,b,c) = [toField a, toField b, toField c]
+
+instance (ToField a, ToField b, ToField c, ToField d) => ToRow (a,b,c,d) where
+ toRow (a,b,c,d) = [toField a, toField b, toField c, toField d]
+
+instance (ToField a, ToField b, ToField c, ToField d, ToField e)
+ => ToRow (a,b,c,d,e) where
+ toRow (a,b,c,d,e) =
+ [toField a, toField b, toField c, toField d, toField e]
+
+instance (ToField a, ToField b, ToField c, ToField d, ToField e, ToField f)
+ => ToRow (a,b,c,d,e,f) where
+ toRow (a,b,c,d,e,f) =
+ [toField a, toField b, toField c, toField d, toField e, toField f]
+
+instance (ToField a, ToField b, ToField c, ToField d, ToField e, ToField f,
+ ToField g)
+ => ToRow (a,b,c,d,e,f,g) where
+ toRow (a,b,c,d,e,f,g) =
+ [toField a, toField b, toField c, toField d, toField e, toField f,
+ toField g]
+
+instance (ToField a, ToField b, ToField c, ToField d, ToField e, ToField f,
+ ToField g, ToField h)
+ => ToRow (a,b,c,d,e,f,g,h) where
+ toRow (a,b,c,d,e,f,g,h) =
+ [toField a, toField b, toField c, toField d, toField e, toField f,
+ toField g, toField h]
+
+instance (ToField a, ToField b, ToField c, ToField d, ToField e, ToField f,
+ ToField g, ToField h, ToField i)
+ => ToRow (a,b,c,d,e,f,g,h,i) where
+ toRow (a,b,c,d,e,f,g,h,i) =
+ [toField a, toField b, toField c, toField d, toField e, toField f,
+ toField g, toField h, toField i]
+
+instance (ToField a, ToField b, ToField c, ToField d, ToField e, ToField f,
+ ToField g, ToField h, ToField i, ToField j)
+ => ToRow (a,b,c,d,e,f,g,h,i,j) where
+ toRow (a,b,c,d,e,f,g,h,i,j) =
+ [toField a, toField b, toField c, toField d, toField e, toField f,
+ toField g, toField h, toField i, toField j]
+
+instance (ToField a) => ToRow [a] where
+ toRow = map toField

0 comments on commit 9014819

Please sign in to comment.
Something went wrong with that request. Please try again.