PostgreSQL 0.1 → 0.2
raw patch · 2 files changed
+55/−10 lines, 2 files
Files
- Database/PostgreSQL.hsc +54/−9
- PostgreSQL.cabal +1/−1
Database/PostgreSQL.hsc view
@@ -57,13 +57,31 @@ data PGresult +withPGResults :: MonadDatabase e m+ => m (Ptr PGresult) -> (Ptr PGresult -> m a) -> m a+withPGResults x f+ = do p <- x+ res <- f p+ clear p+ return res++withoutPGResults :: MonadDatabase e m => m (Ptr PGresult) -> m ()+withoutPGResults x = x >>= clear+ foreign import ccall unsafe "static libpq-fe.h PQexec" pqExec :: Ptr PGconn -> CString -> IO (Ptr PGresult) -execute :: MonadDatabase e m => String -> m ()-execute sql = do DatabaseHandle dbh <- getConnection- checkResultStatus "execute" $ withCString sql $ pqExec dbh+exec :: MonadDatabase e m => String -> m (Ptr PGresult)+exec sql = do DatabaseHandle dbh <- getConnection+ checkResultStatus "execute" $ withCString sql $ pqExec dbh +withExec :: MonadDatabase e m+ => String -> (Ptr PGresult -> m a) -> m a+withExec sql f = withPGResults (exec sql) f++withoutExec :: MonadDatabase e m => String -> m ()+withoutExec sql = withoutPGResults (exec sql)+ type Oid = #type Oid withCStrings :: [String] -> (Ptr CString -> IO a) -> IO a@@ -81,7 +99,7 @@ -> Ptr CInt -- paramFormats -> CInt -- resultFormat -> IO (Ptr PGresult)-execParams :: MonadDatabase e m => String -> [String] -> m ()+execParams :: MonadDatabase e m => String -> [String] -> m (Ptr PGresult) execParams sql params = do let nparams = genericLength params -- We don't currently let the user tell us which Oid they want@@ -101,6 +119,13 @@ -- XXX Again, should use binary (1) rather than text (0) pqExecParams dbh sql' nparams oids params' lengths formats 0 +withExecParams :: MonadDatabase e m+ => String -> [String] -> (Ptr PGresult -> m a) -> m a+withExecParams sql params f = withPGResults (execParams sql params) f++withoutExecParams :: MonadDatabase e m => String -> [String] -> m ()+withoutExecParams sql params = withoutPGResults (execParams sql params)+ -- Docs in http://www.postgresql.org/docs/7.4/interactive/libpq-exec.html -- PGresult *PQexecPrepared(PGconn *conn, -- const char *stmtName,@@ -134,7 +159,8 @@ foreign import ccall unsafe "static libpq-fe.h PQresultErrorMessage" pqResultErrorMessage :: Ptr PGresult -> IO CString -checkResultStatus :: MonadDatabase e m => String -> IO (Ptr PGresult) -> m ()+checkResultStatus :: MonadDatabase e m+ => String -> IO (Ptr PGresult) -> m (Ptr PGresult) checkResultStatus s f = do res <- liftIO f res' <- liftIO $ pqResultStatus res@@ -143,14 +169,33 @@ err_code <- liftIO $ pqResStatus res' >>= peekCString let err = s ++ " failed (" ++ err_code ++ "): " ++ err_msg throwError $ strMsg err+ return res --- char *PQresultErrorMessage(const PGresult *res);+foreign import ccall unsafe "static libpq-fe.h PQclear"+ pqClear :: Ptr PGresult -> IO () --- void PQclear(PQresult *res); (essentially = free)+clear :: MonadDatabase e m => Ptr PGresult -> m ()+clear res = liftIO $ pqClear res --- int PQntuples(const PGresult *res);+foreign import ccall unsafe "static libpq-fe.h PQntuples"+ pqNTuples :: Ptr PGresult -> IO CInt --- char* PQgetvalue(const PGresult *res, int row_number, int column_number);+nTuples :: MonadDatabase e m => Ptr PGresult -> m CInt+nTuples res = liftIO $ pqNTuples res++foreign import ccall unsafe "static libpq-fe.h PQnfields"+ pqNFields :: Ptr PGresult -> IO CInt++nFields :: MonadDatabase e m => Ptr PGresult -> m CInt+nFields res = liftIO $ pqNFields res++foreign import ccall unsafe "static libpq-fe.h PQgetvalue"+ pqGetValue :: Ptr PGresult -> CInt -> CInt -> IO CString++getValue :: MonadDatabase e m => Ptr PGresult -> CInt -> CInt -> m String+getValue res row col = do cstr <- liftIO $ pqGetValue res row col+ liftIO $ peekCString cstr+ -- int PQgetisnull(const PGresult *res, int row_number, int column_number); -- int PQgetlength(const PGresult *res, int row_number, int column_number);
PostgreSQL.cabal view
@@ -1,5 +1,5 @@ Name: PostgreSQL-Version: 0.1+Version: 0.2 License: OtherLicense License-File: COPYING Copyright: Ian Lynagh, 2006, 2007