postgresql-simple 0.2.2.0 → 0.2.3.0
raw patch · 3 files changed
+39/−7 lines, 3 files
Files
- CONTRIBUTORS +1/−0
- postgresql-simple.cabal +2/−2
- src/Database/PostgreSQL/Simple.hs +36/−5
CONTRIBUTORS view
@@ -5,3 +5,4 @@ Joey Adams <joeyadams3.14159@gmail.com> Rekado <rekado@elephly.net> Leonid Onokhov <sopvop@gmail.com>+Jason Dusek <jason.dusek@gmail.com>
postgresql-simple.cabal view
@@ -1,5 +1,5 @@ Name: postgresql-simple-Version: 0.2.2.0+Version: 0.2.3.0 Synopsis: Mid-Level PostgreSQL client library Description: Mid-Level PostgreSQL client library, forked from mysql-simple.@@ -66,7 +66,7 @@ source-repository this type: git location: http://github.com/lpsmith/postgresql-simple- tag: v0.2.2.0+ tag: v0.2.3.0 test-suite test type: exitcode-stdio-1.0
src/Database/PostgreSQL/Simple.hs view
@@ -43,6 +43,9 @@ -- ** Modifying multiple rows at once -- $many + -- ** @RETURNING@: modifications that returns results+ -- $returning+ -- * Extracting results -- $result @@ -86,6 +89,7 @@ , foldWithOptions_ , forEach , forEach_+ , returning -- * Statements that do not return results , execute , execute_@@ -126,8 +130,7 @@ import Data.List (intersperse) import Data.Monoid (mappend, mconcat) import Data.Typeable (Typeable)-import Database.PostgreSQL.Simple.BuiltinTypes- ( oid2builtin, builtin2typname )+import Database.PostgreSQL.Simple.BuiltinTypes ( oid2typname ) import Database.PostgreSQL.Simple.Compat ( mask ) import Database.PostgreSQL.Simple.FromField (ResultError(..)) import Database.PostgreSQL.Simple.FromRow (FromRow(..))@@ -194,7 +197,7 @@ return . toByteString . mconcat $ fromByteString before : intersperse (fromChar ',') bs ++ [fromByteString after]- Nothing -> fmtError "syntax error in query template for executeMany" q []+ Nothing -> fmtError "syntax error in multi-row template" q [] -- Split the input string into three pieces, @before@, @qbits@, and @after@, -- following this grammar:@@ -345,6 +348,20 @@ result <- exec conn =<< formatMany conn q qs finishExecute conn q result +-- | Execute @INSERT ... RETURNING@, @UPDATE ... RETURNING@, or other SQL+-- query that accepts multi-row input and is expected to return results.+-- Note that it is possible to write+-- @'query' conn "INSERT ... RETURNING ..." ...@+-- in cases where you are only inserting a single row, and do not need+-- functionality analogous to 'executeMany'.+--+-- Throws 'FormatError' if the query could not be formatted correctly.+returning :: (ToRow q, FromRow r) => Connection -> Query -> [q] -> IO [r]+returning _ _ [] = return []+returning conn q qs = do+ result <- exec conn =<< formatMany conn q qs+ finishQuery conn q result+ -- | Perform a @SELECT@ or other SQL query that is expected to return -- results. All results are retrieved and converted before this -- function returns.@@ -857,6 +874,20 @@ -- > insert into users (first_name,last_name) values -- > ('Boris','Karloff'),('Ed','Wood') +-- $returning+--+-- The 'returning' function is similar to 'executeMany' but is+-- intended for use with multi-tuple @INSERT@ or @UPDATE@ statements+-- that make use of @RETURNING@.+--+-- For example, were there an auto-incrementing @id@ column and+-- timestamp column @t@ that defaulted to the present time for the+-- @sales@ table, then the following query would insert two new+-- sales records and also return their new @id@s and timestamps.+--+-- > let q = "insert into sales (amount, label) values (?,?) returning id, t"+-- > xs :: [(Int, UTCTime)] <- returning conn q [(20,"Chips"),(300,"Wood")]+ -- $result -- -- The 'query' and 'query_' functions return a list of values in the@@ -934,8 +965,8 @@ getTypename :: Connection -> PQ.Oid -> IO ByteString getTypename conn@Connection{..} oid =- case oid2builtin oid of- Just builtin -> return $! builtin2typname builtin+ case oid2typname oid of+ Just name -> return name Nothing -> modifyMVar connectionObjects $ \oidmap -> do case IntMap.lookup (oid2int oid) oidmap of Just name -> return (oidmap, name)