{-# OPTIONS_HADDOCK hide #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TemplateHaskell #-}
module Database.MSSQLServer.Query.RpcQuerySet ( RpcQuerySet (..)
, RpcQuery (..)
, RpcQueryId (..)
, StoredProcedure (..)
, RpcParamSet (..)
, RpcParam (..)
, RpcParamName
, rpcReqBatchParam
, bitVal
, tinyintVal
, smallintVal
, intVal
, bigintVal
, smallmoneyVal
, moneyVal
, smalldatetimeVal
, datetimeVal
, float24Val
, realVal
, float53Val
, doubleVal
, uniqueidentifierVal
, decimalVal
, numericVal
, charVal
, varcharVal
, textVal
, ncharVal
, nvarcharVal
, ntextVal
, binaryVal
, varbinaryVal
, imageVal
, bitRef
, tinyintRef
, smallintRef
, intRef
, bigintRef
, smallmoneyRef
, moneyRef
, smalldatetimeRef
, datetimeRef
, float24Ref
, realRef
, float53Ref
, doubleRef
, uniqueidentifierRef
, decimalRef
, numericRef
, charRef
, varcharRef
, textRef
, ncharRef
, nvarcharRef
, ntextRef
, binaryRef
, varbinaryRef
, imageRef
, bitDefRef
, tinyintDefRef
, smallintDefRef
, intDefRef
, bigintDefRef
, smallmoneyDefRef
, moneyDefRef
, smalldatetimeDefRef
, datetimeDefRef
, float24DefRef
, realDefRef
, float53DefRef
, doubleDefRef
, uniqueidentifierDefRef
, decimalDefRef
, numericDefRef
, charDefRef
, varcharDefRef
, textDefRef
, ncharDefRef
, nvarcharDefRef
, ntextDefRef
, binaryDefRef
, varbinaryDefRef
, imageDefRef
) where
import qualified Data.Text as T
import qualified Data.ByteString as B
import Data.Word (Word16(..))
import Database.Tds.Message
import Database.MSSQLServer.Query.Only
import Database.MSSQLServer.Query.Template
import Control.Monad(forM)
import Language.Haskell.TH (runIO,pprint)
import Data.Time (UTCTime(..))
import Data.UUID.Types (UUID)
import Data.Fixed (Fixed(..),HasResolution(..))
data RpcQuery a b = RpcQuery !a !b
deriving (Int -> RpcQuery a b -> ShowS
[RpcQuery a b] -> ShowS
RpcQuery a b -> String
(Int -> RpcQuery a b -> ShowS)
-> (RpcQuery a b -> String)
-> ([RpcQuery a b] -> ShowS)
-> Show (RpcQuery a b)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall a b. (Show a, Show b) => Int -> RpcQuery a b -> ShowS
forall a b. (Show a, Show b) => [RpcQuery a b] -> ShowS
forall a b. (Show a, Show b) => RpcQuery a b -> String
showList :: [RpcQuery a b] -> ShowS
$cshowList :: forall a b. (Show a, Show b) => [RpcQuery a b] -> ShowS
show :: RpcQuery a b -> String
$cshow :: forall a b. (Show a, Show b) => RpcQuery a b -> String
showsPrec :: Int -> RpcQuery a b -> ShowS
$cshowsPrec :: forall a b. (Show a, Show b) => Int -> RpcQuery a b -> ShowS
Show)
class RpcQueryId a where
toRpcReqBatch :: (RpcParamSet b) => a -> b -> RpcReqBatch
instance RpcQueryId Word16 where
toRpcReqBatch :: Word16 -> b -> RpcReqBatch
toRpcReqBatch Word16
id b
ps = Word16 -> Word16 -> [RpcReqBatchParam] -> RpcReqBatch
RpcReqBatchProcId Word16
id Word16
0x0000 ([RpcReqBatchParam] -> RpcReqBatch)
-> [RpcReqBatchParam] -> RpcReqBatch
forall a b. (a -> b) -> a -> b
$ b -> [RpcReqBatchParam]
forall a. RpcParamSet a => a -> [RpcReqBatchParam]
toRpcReqBatchParams b
ps
instance RpcQueryId T.Text where
toRpcReqBatch :: Text -> b -> RpcReqBatch
toRpcReqBatch Text
name b
ps = Text -> Word16 -> [RpcReqBatchParam] -> RpcReqBatch
RpcReqBatchProcName Text
name Word16
0x0000 ([RpcReqBatchParam] -> RpcReqBatch)
-> [RpcReqBatchParam] -> RpcReqBatch
forall a b. (a -> b) -> a -> b
$ b -> [RpcReqBatchParam]
forall a. RpcParamSet a => a -> [RpcReqBatchParam]
toRpcReqBatchParams b
ps
data StoredProcedure = SP_Cursor
| SP_CursorOpen
| SP_CursorPrepare
| SP_CursorExecute
| SP_CursorPrepExec
| SP_CursorUnprepare
| SP_CursorFetch
| SP_CursorOption
| SP_CursorClose
| SP_ExecuteSql
| SP_Prepare
| SP_Execute
| SP_PrepExec
| SP_PrepExecRpc
| SP_Unprepare
deriving (Int -> StoredProcedure -> ShowS
[StoredProcedure] -> ShowS
StoredProcedure -> String
(Int -> StoredProcedure -> ShowS)
-> (StoredProcedure -> String)
-> ([StoredProcedure] -> ShowS)
-> Show StoredProcedure
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [StoredProcedure] -> ShowS
$cshowList :: [StoredProcedure] -> ShowS
show :: StoredProcedure -> String
$cshow :: StoredProcedure -> String
showsPrec :: Int -> StoredProcedure -> ShowS
$cshowsPrec :: Int -> StoredProcedure -> ShowS
Show,Int -> StoredProcedure
StoredProcedure -> Int
StoredProcedure -> [StoredProcedure]
StoredProcedure -> StoredProcedure
StoredProcedure -> StoredProcedure -> [StoredProcedure]
StoredProcedure
-> StoredProcedure -> StoredProcedure -> [StoredProcedure]
(StoredProcedure -> StoredProcedure)
-> (StoredProcedure -> StoredProcedure)
-> (Int -> StoredProcedure)
-> (StoredProcedure -> Int)
-> (StoredProcedure -> [StoredProcedure])
-> (StoredProcedure -> StoredProcedure -> [StoredProcedure])
-> (StoredProcedure -> StoredProcedure -> [StoredProcedure])
-> (StoredProcedure
-> StoredProcedure -> StoredProcedure -> [StoredProcedure])
-> Enum StoredProcedure
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: StoredProcedure
-> StoredProcedure -> StoredProcedure -> [StoredProcedure]
$cenumFromThenTo :: StoredProcedure
-> StoredProcedure -> StoredProcedure -> [StoredProcedure]
enumFromTo :: StoredProcedure -> StoredProcedure -> [StoredProcedure]
$cenumFromTo :: StoredProcedure -> StoredProcedure -> [StoredProcedure]
enumFromThen :: StoredProcedure -> StoredProcedure -> [StoredProcedure]
$cenumFromThen :: StoredProcedure -> StoredProcedure -> [StoredProcedure]
enumFrom :: StoredProcedure -> [StoredProcedure]
$cenumFrom :: StoredProcedure -> [StoredProcedure]
fromEnum :: StoredProcedure -> Int
$cfromEnum :: StoredProcedure -> Int
toEnum :: Int -> StoredProcedure
$ctoEnum :: Int -> StoredProcedure
pred :: StoredProcedure -> StoredProcedure
$cpred :: StoredProcedure -> StoredProcedure
succ :: StoredProcedure -> StoredProcedure
$csucc :: StoredProcedure -> StoredProcedure
Enum,StoredProcedure
StoredProcedure -> StoredProcedure -> Bounded StoredProcedure
forall a. a -> a -> Bounded a
maxBound :: StoredProcedure
$cmaxBound :: StoredProcedure
minBound :: StoredProcedure
$cminBound :: StoredProcedure
Bounded)
instance RpcQueryId StoredProcedure where
toRpcReqBatch :: StoredProcedure -> b -> RpcReqBatch
toRpcReqBatch StoredProcedure
sp b
ps = Word16 -> Word16 -> [RpcReqBatchParam] -> RpcReqBatch
RpcReqBatchProcId (Int -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word16) -> Int -> Word16
forall a b. (a -> b) -> a -> b
$ (StoredProcedure -> Int
forall a. Enum a => a -> Int
fromEnum StoredProcedure
sp) Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) Word16
0x0000 ([RpcReqBatchParam] -> RpcReqBatch)
-> [RpcReqBatchParam] -> RpcReqBatch
forall a b. (a -> b) -> a -> b
$ b -> [RpcReqBatchParam]
forall a. RpcParamSet a => a -> [RpcReqBatchParam]
toRpcReqBatchParams b
ps
type RpcParamName = T.Text
data RpcParam a = RpcParamVal !RpcParamName !TypeInfo !a
| RpcParamRef !RpcParamName !TypeInfo !a
| RpcParamDefVal !RpcParamName !TypeInfo !a
| RpcParamDefRef !RpcParamName !TypeInfo !a
deriving (Int -> RpcParam a -> ShowS
[RpcParam a] -> ShowS
RpcParam a -> String
(Int -> RpcParam a -> ShowS)
-> (RpcParam a -> String)
-> ([RpcParam a] -> ShowS)
-> Show (RpcParam a)
forall a. Show a => Int -> RpcParam a -> ShowS
forall a. Show a => [RpcParam a] -> ShowS
forall a. Show a => RpcParam a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RpcParam a] -> ShowS
$cshowList :: forall a. Show a => [RpcParam a] -> ShowS
show :: RpcParam a -> String
$cshow :: forall a. Show a => RpcParam a -> String
showsPrec :: Int -> RpcParam a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> RpcParam a -> ShowS
Show)
rpcReqBatchParam :: (Data a) => RpcParam a -> RpcReqBatchParam
rpcReqBatchParam :: RpcParam a -> RpcReqBatchParam
rpcReqBatchParam = RpcParam a -> RpcReqBatchParam
forall a. Data a => RpcParam a -> RpcReqBatchParam
f
where
f :: RpcParam a -> RpcReqBatchParam
f (RpcParamVal Text
name TypeInfo
ti a
dt) = Text -> StatusFlag -> TypeInfo -> RawBytes -> RpcReqBatchParam
RpcReqBatchParam Text
name StatusFlag
0 TypeInfo
ti (TypeInfo -> a -> RawBytes
forall a. Data a => TypeInfo -> a -> RawBytes
toRawBytes TypeInfo
ti a
dt)
f (RpcParamRef Text
name TypeInfo
ti a
dt) = Text -> StatusFlag -> TypeInfo -> RawBytes -> RpcReqBatchParam
RpcReqBatchParam Text
name StatusFlag
1 TypeInfo
ti (TypeInfo -> a -> RawBytes
forall a. Data a => TypeInfo -> a -> RawBytes
toRawBytes TypeInfo
ti a
dt)
f (RpcParamDefVal Text
name TypeInfo
ti a
dt) = Text -> StatusFlag -> TypeInfo -> RawBytes -> RpcReqBatchParam
RpcReqBatchParam Text
name StatusFlag
2 TypeInfo
ti (TypeInfo -> a -> RawBytes
forall a. Data a => TypeInfo -> a -> RawBytes
toRawBytes TypeInfo
ti a
dt)
f (RpcParamDefRef Text
name TypeInfo
ti a
dt) = Text -> StatusFlag -> TypeInfo -> RawBytes -> RpcReqBatchParam
RpcReqBatchParam Text
name StatusFlag
3 TypeInfo
ti (TypeInfo -> a -> RawBytes
forall a. Data a => TypeInfo -> a -> RawBytes
toRawBytes TypeInfo
ti a
dt)
class RpcParamSet a where
toRpcReqBatchParams :: a -> [RpcReqBatchParam]
instance RpcParamSet () where
toRpcReqBatchParams :: () -> [RpcReqBatchParam]
toRpcReqBatchParams ()
_ = []
instance (Data a) => RpcParamSet (RpcParam a) where
toRpcReqBatchParams :: RpcParam a -> [RpcReqBatchParam]
toRpcReqBatchParams RpcParam a
v1 = [RpcReqBatchParam
b1]
where
!b1 :: RpcReqBatchParam
b1 = RpcParam a -> RpcReqBatchParam
forall a. Data a => RpcParam a -> RpcReqBatchParam
rpcReqBatchParam RpcParam a
v1