{-# LINE 1 "Database/MySQL/Base/C.hsc" #-}
{-# LANGUAGE CPP, EmptyDataDecls, ForeignFunctionInterface #-}

-- |
-- Module:      Database.MySQL.Base.C
-- Copyright:   (c) 2011 MailRank, Inc.
-- License:     BSD3
-- Maintainer:  Paul Rouse <pyr@doynton.org>
-- Stability:   experimental
-- Portability: portable
--
-- Direct bindings to the C @mysqlclient@ API.
module Database.MySQL.Base.C
    (
    -- * Connection management
      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
    -- ** Connection information
    , mysql_thread_id
    , mysql_get_server_info
    , mysql_get_server_version
    , mysql_get_host_info
    , mysql_get_proto_info
    , mysql_character_set_name
    , mysql_get_ssl_cipher
    , mysql_stat
    -- * Querying
    , mysql_real_query
    , mysql_insert_id
    -- ** Escaping
    , mysql_real_escape_string
    -- ** Results
    , 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
    -- * Working with results
    , mysql_free_result
    , mysql_free_result_nonblock
    , mysql_fetch_fields
    , mysql_fetch_fields_nonblock
    , mysql_data_seek
    , mysql_row_seek
    , mysql_row_tell
    -- ** Multiple results
    , mysql_next_result
    -- * Transactions
    , mysql_commit
    , mysql_rollback
    -- * General information
    , mysql_get_client_info
    , mysql_get_client_version
    -- * Error handling
    , mysql_errno
    , mysql_error
    -- * Concurrency
    , mysql_library_init
    , mysql_thread_init
    , mysql_thread_end
    ) where




import Data.ByteString (useAsCString)
import Database.MySQL.Base.Types
import Foreign.C.String (CString, withCString)
#if __GLASGOW_HASKELL__ >= 704
import Foreign.C.Types (CChar(..), CInt(..), CUInt(..), CULLong(..), CULong(..))
#else
import Foreign.C.Types (CInt, CUInt, CULLong, CULong)
#endif
import Foreign.Marshal.Utils (with)
import Foreign.Ptr (Ptr, nullPtr)

foreign import ccall safe mysql_init
    :: Ptr MYSQL                -- ^ should usually be 'nullPtr'
    -> IO (Ptr MYSQL)

mysql_options :: Ptr MYSQL -> Option -> IO CInt
mysql_options :: Ptr MYSQL -> Option -> IO CInt
mysql_options Ptr MYSQL
ptr Option
opt =
    case Option
opt of
      ConnectTimeout Seconds
secs ->
        Seconds -> (Ptr CUInt -> IO CInt) -> IO CInt
forall a b. Integral a => a -> (Ptr CUInt -> IO b) -> IO b
withIntegral Seconds
secs ((Ptr CUInt -> IO CInt) -> IO CInt)
-> (Ptr CUInt -> IO CInt) -> IO CInt
forall a b. (a -> b) -> a -> b
$ CInt -> Ptr CUInt -> IO CInt
forall a. CInt -> Ptr a -> IO CInt
go (CInt
0)
{-# LINE 96 "Database/MySQL/Base/C.hsc" #-}
      Compress ->
        go (1) nullPtr
{-# LINE 98 "Database/MySQL/Base/C.hsc" #-}
      NamedPipe ->
        go (2) nullPtr
{-# LINE 100 "Database/MySQL/Base/C.hsc" #-}
      InitCommand cmd ->
        useAsCString cmd $ go (3)
{-# LINE 102 "Database/MySQL/Base/C.hsc" #-}
      ReadDefaultFile path ->
        withCString path $ go (4)
{-# LINE 104 "Database/MySQL/Base/C.hsc" #-}
      ReadDefaultGroup group ->
        useAsCString group $ go (5)
{-# LINE 106 "Database/MySQL/Base/C.hsc" #-}
      CharsetDir path ->
        withCString path $ go (6)
{-# LINE 108 "Database/MySQL/Base/C.hsc" #-}
      CharsetName cs ->
        withCString cs $ go (7)
{-# LINE 110 "Database/MySQL/Base/C.hsc" #-}
      LocalInFile b ->
        withBool b $ go (8)
{-# LINE 112 "Database/MySQL/Base/C.hsc" #-}
      Protocol proto ->
        withIntegral (fromEnum proto) $ go (9)
{-# LINE 114 "Database/MySQL/Base/C.hsc" #-}
      SharedMemoryBaseName name ->
        useAsCString name $ go (10)
{-# LINE 116 "Database/MySQL/Base/C.hsc" #-}
      ReadTimeout secs ->
        withIntegral secs $ go (11)
{-# LINE 118 "Database/MySQL/Base/C.hsc" #-}
      WriteTimeout secs ->
        withIntegral secs $ go (12)
{-# LINE 120 "Database/MySQL/Base/C.hsc" #-}

{-# LINE 122 "Database/MySQL/Base/C.hsc" #-}
      Option
UseRemoteConnection ->
        CInt -> Ptr Any -> IO CInt
forall a. CInt -> Ptr a -> IO CInt
go (CInt
14) Ptr Any
forall a. Ptr a
nullPtr
{-# LINE 124 "Database/MySQL/Base/C.hsc" #-}
      UseEmbeddedConnection ->
        go (15) nullPtr
{-# LINE 126 "Database/MySQL/Base/C.hsc" #-}
      GuessConnection ->
        go (16) nullPtr
{-# LINE 128 "Database/MySQL/Base/C.hsc" #-}
      ClientIP ip ->
        useAsCString ip $ go (17)
{-# LINE 130 "Database/MySQL/Base/C.hsc" #-}
      SecureAuth b ->
        withBool b $ go (18)
{-# LINE 132 "Database/MySQL/Base/C.hsc" #-}

{-# LINE 133 "Database/MySQL/Base/C.hsc" #-}
      ReportDataTruncation b ->
        withBool b $ go (19)
{-# LINE 135 "Database/MySQL/Base/C.hsc" #-}
      Reconnect b ->
        withBool b $ go (20)
{-# LINE 137 "Database/MySQL/Base/C.hsc" #-}

{-# LINE 139 "Database/MySQL/Base/C.hsc" #-}
      SSLVerifyServerCert Bool
b ->
        Bool -> (Ptr CUInt -> IO CInt) -> IO CInt
forall b. Bool -> (Ptr CUInt -> IO b) -> IO b
withBool Bool
b ((Ptr CUInt -> IO CInt) -> IO CInt)
-> (Ptr CUInt -> IO CInt) -> IO CInt
forall a b. (a -> b) -> a -> b
$ CInt -> Ptr CUInt -> IO CInt
forall a. CInt -> Ptr a -> IO CInt
go (CInt
21)
{-# LINE 141 "Database/MySQL/Base/C.hsc" #-}

{-# LINE 142 "Database/MySQL/Base/C.hsc" #-}
      -- Other options are accepted by mysql_real_connect, so ignore them.
      Option
_ -> CInt -> IO CInt
forall (m :: * -> *) a. Monad m => a -> m a
return CInt
0
  where
    go :: CInt -> Ptr a -> IO CInt
go = Ptr MYSQL -> CInt -> Ptr a -> IO CInt
forall a. Ptr MYSQL -> CInt -> Ptr a -> IO CInt
mysql_options_ Ptr MYSQL
ptr
    withBool :: Bool -> (Ptr CUInt -> IO b) -> IO b
withBool Bool
b = CUInt -> (Ptr CUInt -> IO b) -> IO b
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with (if Bool
b then CUInt
1 else CUInt
0 :: CUInt)
    withIntegral :: a -> (Ptr CUInt -> IO b) -> IO b
withIntegral a
i = CUInt -> (Ptr CUInt -> IO b) -> IO b
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with (a -> CUInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
i :: CUInt)

foreign import ccall safe "mysql.h mysql_options" mysql_options_
    :: Ptr MYSQL -> CInt -> Ptr a -> IO CInt

foreign import ccall safe "mysql_signals.h _hs_mysql_real_connect"
        mysql_real_connect
    :: Ptr MYSQL -- ^ Context (from 'mysql_init').
    -> CString   -- ^ Host name.
    -> CString   -- ^ User name.
    -> CString   -- ^ Password.
    -> CString   -- ^ Database.
    -> CInt      -- ^ Port.
    -> CString   -- ^ Unix socket.
    -> CULong    -- ^ Flags.
    -> IO (Ptr MYSQL)

foreign import ccall safe mysql_ssl_set
    :: Ptr MYSQL
    -> CString                  -- ^ Key.
    -> CString                  -- ^ Cert.
    -> CString                  -- ^ CA.
    -> CString                  -- ^ CA path.
    -> CString                  -- ^ Ciphers.
    -> IO MyBool

foreign import ccall safe "mysql_signals.h _hs_mysql_close" mysql_close
    :: Ptr MYSQL -> IO ()

foreign import ccall safe "mysql_signals.h _hs_mysql_ping" mysql_ping
    :: Ptr MYSQL -> IO CInt

foreign import ccall safe mysql_thread_id
    :: Ptr MYSQL -> IO CULong

foreign import ccall safe "mysql_signals.h _hs_mysql_autocommit" mysql_autocommit
    :: Ptr MYSQL -> MyBool -> IO MyBool

foreign import ccall safe "mysql_signals.h _hs_mysql_change_user" mysql_change_user
    :: Ptr MYSQL
    -> CString                  -- ^ user
    -> CString                  -- ^ password
    -> CString                  -- ^ database
    -> IO MyBool

foreign import ccall safe "mysql_signals.h _hs_mysql_select_db" 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_server_version
    :: Ptr MYSQL -> IO CULong

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 safe "mysql_signals.h _hs_mysql_stat" mysql_stat
    :: Ptr MYSQL -> IO CString

foreign import ccall safe "mysql_signals.h _hs_mysql_real_query" mysql_real_query
    :: Ptr MYSQL -> CString -> CULong -> IO CInt

foreign import ccall safe mysql_insert_id
    :: Ptr MYSQL -> IO CULLong

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 safe "mysql_signals.h _hs_mysql_store_result" mysql_store_result
    :: Ptr MYSQL -> IO (Ptr MYSQL_RES)

foreign import ccall safe "mysql_signals.h _hs_mysql_use_result"  mysql_use_result
    :: Ptr MYSQL -> IO (Ptr MYSQL_RES)

foreign import ccall safe "mysql_signals.h _hs_mysql_free_result" mysql_free_result
    :: Ptr MYSQL_RES -> IO ()

foreign import ccall safe "mysql.h mysql_free_result" mysql_free_result_nonblock
    :: Ptr MYSQL_RES -> IO ()

foreign import ccall safe 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 safe "mysql_signals.h _hs_mysql_next_result" mysql_next_result
    :: Ptr MYSQL -> IO CInt

foreign import ccall safe "mysql_signals.h _hs_mysql_commit" mysql_commit
    :: Ptr MYSQL -> IO MyBool

foreign import ccall safe "mysql_signals.h _hs_mysql_rollback" mysql_rollback
    :: Ptr MYSQL -> IO MyBool

foreign import ccall safe "mysql_signals.h _hs_mysql_fetch_row" 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 safe 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

foreign import ccall safe "mysql.h mysql_server_init" mysql_library_init
    :: CInt -> Ptr (Ptr Char) -> Ptr (Ptr Char) -> IO CInt

foreign import ccall safe "mysql.h" mysql_thread_init
    :: IO MyBool

foreign import ccall safe "mysql.h" mysql_thread_end
    :: IO ()