Commits

Bryan O'Sullivan committed c633444

Implement autocommit and changeUser.

Comments (0)

Files changed (2)

     -- * Connection management
     , connect
     , close
+    , autocommit
     , ping
+    , changeUser
     , threadId
     ) where
 
 threadId :: Connection -> IO Word
 threadId conn = fromIntegral <$> withConn conn mysql_thread_id
 
+autocommit :: Connection -> Bool -> IO ()
+autocommit conn onOff = withConn conn $ \ptr ->
+   withRTSSignalsBlocked (mysql_autocommit ptr b) >>= check "autocommit" ptr
+ where b = if onOff then 1 else 0
+
+changeUser :: Connection -> String -> String -> Maybe String -> IO ()
+changeUser conn user pass mdb =
+  withCString user $ \cuser ->
+   withCString pass $ \cpass ->
+    withMaybeString mdb $ \cdb ->
+     withConn conn $ \ptr ->
+      withRTSSignalsBlocked (mysql_change_user ptr cuser cpass cdb) >>=
+      check "changeUser" ptr
+
 withConn :: Connection -> (Ptr MYSQL -> IO a) -> IO a
 withConn conn = withForeignPtr (connFP conn)
 
 withString [] act = act nullPtr
 withString xs act = withCString xs act
 
-check :: String -> Ptr MYSQL -> CInt -> IO ()
+withMaybeString :: Maybe String -> (CString -> IO a) -> IO a
+withMaybeString Nothing act = act nullPtr
+withMaybeString (Just xs) act = withCString xs act
+
+check :: Num a => String -> Ptr MYSQL -> a -> IO ()
 check func ptr r = unless (r == 0) $ connectionError func ptr
+{-# INLINE check #-}
 
 connectionError :: String -> Ptr MYSQL -> IO a
 connectionError func ptr = do
     -- * Types
       MYSQL
     , MYSQL_STMT
+    , MyBool
     -- * Connection management
     , mysql_init
     , mysql_real_connect
     , mysql_close
     , mysql_ping
     , mysql_thread_id
+    , mysql_autocommit
+    , mysql_change_user
     -- * Error handling
     , mysql_errno
     , mysql_error
 
 data MYSQL
 data MYSQL_STMT
+type MyBool = CChar
 
 -- | Execute an 'IO' action with signals used by GHC's runtime signals
 -- blocked.  The @mysqlclient@ C library does not correctly restart
 foreign import ccall safe mysql_thread_id
     :: Ptr MYSQL -> IO CULong
 
+foreign import ccall unsafe mysql_autocommit
+    :: Ptr MYSQL -> MyBool -> IO MyBool
+
+foreign import ccall unsafe mysql_change_user
+    :: Ptr MYSQL
+    -> CString                  -- ^ user
+    -> CString                  -- ^ password
+    -> CString                  -- ^ database
+    -> IO MyBool
+
 foreign import ccall safe mysql_errno
     :: Ptr MYSQL -> IO CInt