<commit_before>{-# LANGUAGE CPP, EmptyDataDecls, ForeignFunctionInterface #-}

-- |
-- Module:      Database.MySQL.Base.C
-- Copyright:   (c) 2011 MailRank, Inc.
-- License:     BSD3
-- Maintainer:  <NAME> <<EMAIL>>
-- 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_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
    ) where

#include "mysql_signals.h"
#include "mysql.h"

import Data.ByteString.Unsafe (unsafeUseAsCString)
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 opt =
    case opt of
      ConnectTimeout secs ->
        withIntegral secs $ go (#const MYSQL_OPT_CONNECT_TIMEOUT)
      Compress ->
        go (#const MYSQL_OPT_COMPRESS) nullPtr
      NamedPipe ->
        go (#const MYSQL_OPT_NAMED_PIPE) nullPtr
      InitCommand cmd ->
        unsafeUseAsCString cmd $ go (#const MYSQL_INIT_COMMAND)
      ReadDefaultFile path ->
        withCString path $ go (#const MYSQL_READ_DEFAULT_FILE)
      ReadDefaultGroup group ->
        unsafeUseAsCString group $ go (#const MYSQL_READ_DEFAULT_GROUP)
      CharsetDir path ->
        withCString path $ go (#const MYSQL_SET_CHARSET_DIR)
      CharsetName cs ->
        withCString cs $ go (#const MYSQL_SET_CHARSET_NAME)
      LocalInFile b ->
        withBool b $ go (#const MYSQL_OPT_LOCAL_INFILE)
      Protocol proto ->
        withIntegral (fromEnum proto) $ go (#const MYSQL_OPT_PROTOCOL)
      SharedMemoryBaseName name ->
        unsafeUseAsCString name $ go (#const MYSQL_SHARED_MEMORY_BASE_NAME)
      ReadTimeout secs ->
        withIntegral secs $ go (#const MYSQL_OPT_READ_TIMEOUT)
      WriteTimeout secs ->
        withIntegral secs $ go (#const MYSQL_OPT_WRITE_TIMEOUT)
      UseRemoteConnection ->
        go (#const MYSQL_OPT_USE_REMOTE_CONNECTION) nullPtr
      UseEmbeddedConnection ->
        go (#const MYSQL_OPT_USE_EMBEDDED_CONNECTION) nullPtr
      GuessConnection ->
        go (#const MYSQL_OPT_GUESS_CONNECTION) nullPtr
      ClientIP ip ->
        unsafeUseAsCString ip $ go (#const MYSQL_SET_CLIENT_IP)
      SecureAuth b ->
        withBool b $ go (#const MYSQL_SECURE_AUTH)
      ReportDataTruncation b ->
        withBool b $ go (#const MYSQL_REPORT_DATA_TRUNCATION)<commit_msg>Use useAsCString instead of unsafe version for InitCommand<commit_after>{-# LANGUAGE CPP, EmptyDataDecls, ForeignFunctionInterface #-}

-- |
-- Module:      Database.MySQL.Base.C
-- Copyright:   (c) 2011 MailRank, Inc.
-- License:     BSD3
-- Maintainer:  <NAME> <<EMAIL>>
-- 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_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
    ) where

#include "mysql_signals.h"
#include "mysql.h"

import Data.ByteString (useAsCString)
import Data.ByteString.Unsafe (unsafeUseAsCString)
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 opt =
    case opt of
      ConnectTimeout secs ->
        withIntegral secs $ go (#const MYSQL_OPT_CONNECT_TIMEOUT)
      Compress ->
        go (#const MYSQL_OPT_COMPRESS) nullPtr
      NamedPipe ->
        go (#const MYSQL_OPT_NAMED_PIPE) nullPtr
      InitCommand cmd ->
        useAsCString cmd $ go (#const MYSQL_INIT_COMMAND)
      ReadDefaultFile path ->
        withCString path $ go (#const MYSQL_READ_DEFAULT_FILE)
      ReadDefaultGroup group ->
        unsafeUseAsCString group $ go (#const MYSQL_READ_DEFAULT_GROUP)
      CharsetDir path ->
        withCString path $ go (#const MYSQL_SET_CHARSET_DIR)
      CharsetName cs ->
        withCString cs $ go (#const MYSQL_SET_CHARSET_NAME)
      LocalInFile b ->
        withBool b $ go (#const MYSQL_OPT_LOCAL_INFILE)
      Protocol proto ->
        withIntegral (fromEnum proto) $ go (#const MYSQL_OPT_PROTOCOL)
      SharedMemoryBaseName name ->
        unsafeUseAsCString name $ go (#const MYSQL_SHARED_MEMORY_BASE_NAME)
      ReadTimeout secs ->
        withIntegral secs $ go (#const MYSQL_OPT_READ_TIMEOUT)
      WriteTimeout secs ->
        withIntegral secs $ go (#const MYSQL_OPT_WRITE_TIMEOUT)
      UseRemoteConnection ->
        go (#const MYSQL_OPT_USE_REMOTE_CONNECTION) nullPtr
      UseEmbeddedConnection ->
        go (#const MYSQL_OPT_USE_EMBEDDED_CONNECTION) nullPtr
      GuessConnection ->
        go (#const MYSQL_OPT_GUESS_CONNECTION) nullPtr
      ClientIP ip ->
        unsafeUseAsCString ip $ go (#const MYSQL_SET_CLIENT_IP)
      SecureAuth b ->
        withBool b $ go (#const MYSQL_SECURE_AUTH)
      ReportDataTruncation b ->
        withBool b $ go (#const MYSQL_REPORT_DATA_TRUNCATION)
{'fraction_non_alphanumeric': 0.08344801701488802, 'fraction_numerical': 0.0020017515325910173, 'mean_word_length': 2.891918208373905, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6478649', 'n_tokens_mistral': 2639, 'n_tokens_neox': 2611, 'n_words': 565}