module Database.MySQL.Base.C
(
mysql_init
, mysql_options
, mysql_ssl_set
, mysql_real_connect
, mysql_close
, mysql_ping
, mysql_autocommit
, mysql_change_user
, mysql_select_db
, mysql_set_character_set
, mysql_thread_id
, mysql_get_server_info
, mysql_get_host_info
, mysql_get_proto_info
, mysql_character_set_name
, mysql_get_ssl_cipher
, mysql_stat
, mysql_real_query
, mysql_real_escape_string
, mysql_field_count
, mysql_affected_rows
, mysql_store_result
, mysql_use_result
, mysql_fetch_lengths
, mysql_fetch_lengths_nonblock
, mysql_fetch_row
, mysql_fetch_row_nonblock
, mysql_free_result
, mysql_fetch_fields
, mysql_fetch_fields_nonblock
, mysql_data_seek
, mysql_row_seek
, mysql_row_tell
, mysql_next_result
, mysql_commit
, mysql_rollback
, mysql_get_client_info
, mysql_get_client_version
, mysql_errno
, mysql_error
, withRTSSignalsBlocked
) where
import Control.Concurrent (rtsSupportsBoundThreads, runInBoundThread)
import Control.Exception (finally)
import Data.ByteString.Unsafe (unsafeUseAsCString)
import Database.MySQL.Base.Types
import Foreign.C.String (CString, withCString)
import Foreign.C.Types (CInt, CUInt, CULLong, CULong)
import Foreign.ForeignPtr (ForeignPtr, mallocForeignPtr, withForeignPtr)
import Foreign.Marshal.Utils (with)
import Foreign.Ptr (Ptr, nullPtr)
import Foreign.Storable (Storable(..))
import System.IO.Unsafe (unsafePerformIO)
withRTSSignalsBlocked :: IO a -> IO a
withRTSSignalsBlocked act
| not rtsSupportsBoundThreads = act
| otherwise = runInBoundThread . withForeignPtr rtsSignals $ \set -> do
pthread_sigmask (0) set nullPtr
act `finally` pthread_sigmask (1) set nullPtr
rtsSignals :: ForeignPtr SigSet
rtsSignals = unsafePerformIO $ do
fp <- mallocForeignPtr
withForeignPtr fp $ \set -> do
sigemptyset set
sigaddset set (14)
sigaddset set (26)
return fp
data SigSet
instance Storable SigSet where
sizeOf _ = (128)
alignment _ = alignment (undefined :: Ptr CInt)
foreign import ccall unsafe "signal.h sigaddset" sigaddset
:: Ptr SigSet -> CInt -> IO ()
foreign import ccall unsafe "signal.h sigemptyset" sigemptyset
:: Ptr SigSet -> IO ()
foreign import ccall unsafe "signal.h pthread_sigmask" pthread_sigmask
:: CInt -> Ptr SigSet -> Ptr SigSet -> IO ()
foreign import ccall safe mysql_init
:: Ptr MYSQL
-> IO (Ptr MYSQL)
mysql_options :: Ptr MYSQL -> Option -> IO CInt
mysql_options ptr opt =
case opt of
ConnectTimeout secs ->
withIntegral secs $ go (0)
Compress ->
go (1) nullPtr
NamedPipe ->
go (2) nullPtr
InitCommand cmd ->
unsafeUseAsCString cmd $ go (3)
ReadDefaultFile path ->
withCString path $ go (4)
ReadDefaultGroup group ->
unsafeUseAsCString group $ go (5)
CharsetDir path ->
withCString path $ go (6)
CharsetName cs ->
withCString cs $ go (7)
LocalInFile b ->
withBool b $ go (8)
Protocol proto ->
withIntegral (fromEnum proto) $ go (9)
SharedMemoryBaseName name ->
unsafeUseAsCString name $ go (10)
ReadTimeout secs ->
withIntegral secs $ go (11)
WriteTimeout secs ->
withIntegral secs $ go (12)
UseRemoteConnection ->
go (14) nullPtr
UseEmbeddedConnection ->
go (15) nullPtr
GuessConnection ->
go (16) nullPtr
ClientIP ip ->
unsafeUseAsCString ip $ go (17)
SecureAuth b ->
withBool b $ go (18)
ReportDataTruncation b ->
withBool b $ go (19)
Reconnect b ->
withBool b $ go (20)
SSLVerifyServerCert b ->
withBool b $ go (21)
_ -> return 0
where
go = mysql_options_ ptr
withBool b = with (if b then 1 else 0 :: CUInt)
withIntegral i = with (fromIntegral i :: CUInt)
foreign import ccall safe "mysql.h mysql_options" mysql_options_
:: Ptr MYSQL -> CInt -> Ptr a -> IO CInt
foreign import ccall unsafe mysql_real_connect
:: Ptr MYSQL
-> CString
-> CString
-> CString
-> CString
-> CInt
-> CString
-> CULong
-> IO (Ptr MYSQL)
foreign import ccall safe mysql_ssl_set
:: Ptr MYSQL
-> CString
-> CString
-> CString
-> CString
-> CString
-> IO MyBool
foreign import ccall unsafe mysql_close
:: Ptr MYSQL -> IO ()
foreign import ccall unsafe mysql_ping
:: Ptr MYSQL -> IO CInt
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
-> CString
-> CString
-> IO MyBool
foreign import ccall unsafe mysql_select_db
:: Ptr MYSQL
-> CString
-> IO CInt
foreign import ccall safe mysql_get_server_info
:: Ptr MYSQL -> IO CString
foreign import ccall safe mysql_get_host_info
:: Ptr MYSQL -> IO CString
foreign import ccall safe mysql_get_proto_info
:: Ptr MYSQL -> IO CUInt
foreign import ccall safe mysql_character_set_name
:: Ptr MYSQL -> IO CString
foreign import ccall safe mysql_set_character_set
:: Ptr MYSQL -> CString -> IO CInt
foreign import ccall safe mysql_get_ssl_cipher
:: Ptr MYSQL -> IO CString
foreign import ccall unsafe mysql_stat
:: Ptr MYSQL -> IO CString
foreign import ccall unsafe mysql_real_query
:: Ptr MYSQL -> CString -> CULong -> IO CInt
foreign import ccall safe mysql_field_count
:: Ptr MYSQL -> IO CUInt
foreign import ccall safe mysql_affected_rows
:: Ptr MYSQL -> IO CULLong
foreign import ccall unsafe mysql_store_result
:: Ptr MYSQL -> IO (Ptr MYSQL_RES)
foreign import ccall unsafe mysql_use_result
:: Ptr MYSQL -> IO (Ptr MYSQL_RES)
foreign import ccall unsafe mysql_free_result
:: Ptr MYSQL_RES -> IO ()
foreign import ccall unsafe mysql_fetch_fields
:: Ptr MYSQL_RES -> IO (Ptr Field)
foreign import ccall safe "mysql.h mysql_fetch_fields" mysql_fetch_fields_nonblock
:: Ptr MYSQL_RES -> IO (Ptr Field)
foreign import ccall safe mysql_data_seek
:: Ptr MYSQL_RES -> CULLong -> IO ()
foreign import ccall safe mysql_row_seek
:: Ptr MYSQL_RES -> MYSQL_ROW_OFFSET -> IO MYSQL_ROW_OFFSET
foreign import ccall safe mysql_row_tell
:: Ptr MYSQL_RES -> IO MYSQL_ROW_OFFSET
foreign import ccall unsafe mysql_next_result
:: Ptr MYSQL -> IO CInt
foreign import ccall unsafe mysql_commit
:: Ptr MYSQL -> IO MyBool
foreign import ccall unsafe mysql_rollback
:: Ptr MYSQL -> IO MyBool
foreign import ccall unsafe mysql_fetch_row
:: Ptr MYSQL_RES -> IO MYSQL_ROW
foreign import ccall safe "mysql.h mysql_fetch_row" mysql_fetch_row_nonblock
:: Ptr MYSQL_RES -> IO MYSQL_ROW
foreign import ccall unsafe mysql_fetch_lengths
:: Ptr MYSQL_RES -> IO (Ptr CULong)
foreign import ccall safe "mysql.h mysql_fetch_lengths" mysql_fetch_lengths_nonblock
:: Ptr MYSQL_RES -> IO (Ptr CULong)
foreign import ccall safe mysql_real_escape_string
:: Ptr MYSQL -> CString -> CString -> CULong -> IO CULong
foreign import ccall safe mysql_get_client_info :: CString
foreign import ccall safe mysql_get_client_version :: CULong
foreign import ccall safe mysql_errno
:: Ptr MYSQL -> IO CInt
foreign import ccall safe mysql_error
:: Ptr MYSQL -> IO CString