{-# OPTIONS_GHC -funbox-strict-fields #-}

{-|
Module      : Database.MySQL.Protocol.Command
Description : MySQL commands
Copyright   : (c) Winterland, 2016
License     : BSD
Maintainer  : drkoster@qq.com
Stability   : experimental
Portability : PORTABLE

Common MySQL commands supports.

-}

module Database.MySQL.Protocol.Command where

import           Control.Applicative
import           Control.Monad
import           Data.Binary
import           Data.Binary.Get
import           Data.Binary.Parser
import           Data.Binary.Put
import           Data.ByteString                    (ByteString)
import qualified Data.ByteString.Lazy               as L
import           Database.MySQL.Protocol.MySQLValue
import           Database.MySQL.Protocol.Packet

--------------------------------------------------------------------------------
--  Commands

type StmtID = Word32

-- | All support MySQL commands.
--
data Command
    = COM_QUIT                                    -- ^ 0x01
    | COM_INIT_DB        !ByteString              -- ^ 0x02
    | COM_QUERY          !L.ByteString            -- ^ 0x03
    | COM_PING                                    -- ^ 0x0E
    | COM_BINLOG_DUMP    !Word32 !Word16 !Word32 !ByteString -- ^ 0x12
            -- binlog-pos, flags(0x01), server-id, binlog-filename
    | COM_REGISTER_SLAVE !Word32 !ByteString !ByteString !ByteString !Word16 !Word32 !Word32 -- ^ 0x15
            -- server-id, slaves hostname, slaves user, slaves password,  slaves port, replication rank(ignored), master-id(usually 0)
    | COM_STMT_PREPARE   !L.ByteString            -- ^ 0x16 statement
    | COM_STMT_EXECUTE   !StmtID ![MySQLValue] !BitMap -- ^ 0x17 stmtId, params
    | COM_STMT_CLOSE     !StmtID                  -- ^ 0x19 stmtId
    | COM_STMT_RESET     !StmtID                  -- ^ 0x1A stmtId
    | COM_UNSUPPORTED
   deriving (Int -> Command -> ShowS
[Command] -> ShowS
Command -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Command] -> ShowS
$cshowList :: [Command] -> ShowS
show :: Command -> String
$cshow :: Command -> String
showsPrec :: Int -> Command -> ShowS
$cshowsPrec :: Int -> Command -> ShowS
Show, Command -> Command -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Command -> Command -> Bool
$c/= :: Command -> Command -> Bool
== :: Command -> Command -> Bool
$c== :: Command -> Command -> Bool
Eq)

putCommand :: Command -> Put
putCommand :: Command -> Put
putCommand Command
COM_QUIT              = Word8 -> Put
putWord8 Word8
0x01
putCommand (COM_INIT_DB ByteString
db)      = Word8 -> Put
putWord8 Word8
0x02 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ByteString -> Put
putByteString ByteString
db
putCommand (COM_QUERY ByteString
q)         = Word8 -> Put
putWord8 Word8
0x03 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ByteString -> Put
putLazyByteString ByteString
q
putCommand Command
COM_PING              = Word8 -> Put
putWord8 Word8
0x0E
putCommand (COM_BINLOG_DUMP Word32
pos Word16
flags Word32
sid ByteString
fname) = do
    Word8 -> Put
putWord8 Word8
0x12
    Word32 -> Put
putWord32le Word32
pos
    Word16 -> Put
putWord16le Word16
flags
    Word32 -> Put
putWord32le Word32
sid
    ByteString -> Put
putByteString ByteString
fname
putCommand (COM_REGISTER_SLAVE Word32
sid ByteString
shost ByteString
susr ByteString
spass Word16
sport Word32
rrank Word32
mid) = do
    Word8 -> Put
putWord8 Word8
0x15
    Word32 -> Put
putWord32le Word32
sid
    ByteString -> Put
putLenEncBytes ByteString
shost
    ByteString -> Put
putLenEncBytes ByteString
susr
    ByteString -> Put
putLenEncBytes ByteString
spass
    Word16 -> Put
putWord16le Word16
sport
    Word32 -> Put
putWord32le Word32
rrank
    Word32 -> Put
putWord32le Word32
mid
putCommand (COM_STMT_PREPARE ByteString
stmt) = Word8 -> Put
putWord8 Word8
0x16 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ByteString -> Put
putLazyByteString ByteString
stmt
putCommand (COM_STMT_EXECUTE Word32
stid [MySQLValue]
params BitMap
nullmap) = do
    Word8 -> Put
putWord8 Word8
0x17
    Word32 -> Put
putWord32le Word32
stid
    Word8 -> Put
putWord8 Word8
0x00 -- we only use @CURSOR_TYPE_NO_CURSOR@ here
    Word32 -> Put
putWord32le Word32
1 -- const 1
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [MySQLValue]
params) forall a b. (a -> b) -> a -> b
$ do
        ByteString -> Put
putByteString (BitMap -> ByteString
fromBitMap BitMap
nullmap)
        Word8 -> Put
putWord8 Word8
0x01    -- always use new-params-bound-flag
        forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ MySQLValue -> Put
putParamMySQLType [MySQLValue]
params
        forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [MySQLValue]
params MySQLValue -> Put
putBinaryField

putCommand (COM_STMT_CLOSE Word32
stid) = Word8 -> Put
putWord8 Word8
0x19 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Word32 -> Put
putWord32le Word32
stid
putCommand (COM_STMT_RESET Word32
stid) = Word8 -> Put
putWord8 Word8
0x1A forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Word32 -> Put
putWord32le Word32
stid
putCommand Command
_                     = forall a. HasCallStack => String -> a
error String
"unsupported command"

--------------------------------------------------------------------------------
--  Prepared statment related

-- | call 'isOK' with this packet return true
data StmtPrepareOK = StmtPrepareOK
    { StmtPrepareOK -> Word32
stmtId        :: !StmtID
    , StmtPrepareOK -> Int
stmtColumnCnt :: !Int
    , StmtPrepareOK -> Int
stmtParamCnt  :: !Int
    , StmtPrepareOK -> Int
stmtWarnCnt   :: !Int
    } deriving (Int -> StmtPrepareOK -> ShowS
[StmtPrepareOK] -> ShowS
StmtPrepareOK -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [StmtPrepareOK] -> ShowS
$cshowList :: [StmtPrepareOK] -> ShowS
show :: StmtPrepareOK -> String
$cshow :: StmtPrepareOK -> String
showsPrec :: Int -> StmtPrepareOK -> ShowS
$cshowsPrec :: Int -> StmtPrepareOK -> ShowS
Show, StmtPrepareOK -> StmtPrepareOK -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: StmtPrepareOK -> StmtPrepareOK -> Bool
$c/= :: StmtPrepareOK -> StmtPrepareOK -> Bool
== :: StmtPrepareOK -> StmtPrepareOK -> Bool
$c== :: StmtPrepareOK -> StmtPrepareOK -> Bool
Eq)

getStmtPrepareOK :: Get StmtPrepareOK
getStmtPrepareOK :: Get StmtPrepareOK
getStmtPrepareOK = do
    Int -> Get ()
skipN Int
1 -- OK byte
    Word32
stmtid <- Get Word32
getWord32le
    Int
cc <- forall a b. (Integral a, Num b) => a -> b
fromIntegral forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word16
getWord16le
    Int
pc <- forall a b. (Integral a, Num b) => a -> b
fromIntegral forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word16
getWord16le
    Int -> Get ()
skipN Int
1 -- reserved
    Int
wc <- forall a b. (Integral a, Num b) => a -> b
fromIntegral forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word16
getWord16le
    forall (m :: * -> *) a. Monad m => a -> m a
return (Word32 -> Int -> Int -> Int -> StmtPrepareOK
StmtPrepareOK Word32
stmtid Int
cc Int
pc Int
wc)
{-# INLINE getStmtPrepareOK #-}