module Database.EJDB2.Query
( Query
, fromString
, setBool
, setBoolAtIndex
, setI64
, setI64AtIndex
, setF64
, setF64AtIndex
, setString
, setStringAtIndex
, setRegex
, setRegexAtIndex
, setNull
, setNullAtIndex
) where
import qualified Data.Bool as Bool
import Data.IORef
import Data.Int
import Database.EJDB2.Bindings.JQL
import Database.EJDB2.QueryConstructor
import Database.EJDB2.Result
import Foreign
import Foreign.C.String
import Foreign.C.Types
import Foreign.Marshal.Alloc
fromString :: String
-> IO Query
fromString string = do
jqlPtr <- malloc
withCString string $ \cString -> do
c_jql_create jqlPtr nullPtr cString >>= checkRC
jql <- peek jqlPtr
jqlFPtr <- newForeignPtr p_finalizerJQL jqlPtr
ioRef <- newIORef []
return $ Query jql jqlFPtr ioRef
setBool :: Bool
-> String
-> Query
-> IO ()
setBool bool placeholder (Query jql _ _) =
withCString placeholder $ \cPlaceholder ->
c_jql_set_bool jql cPlaceholder 0 (CBool (Bool.bool 0 1 bool)) >>= checkRC
setBoolAtIndex :: Bool
-> Int
-> Query
-> IO ()
setBoolAtIndex bool index (Query jql _ _) =
c_jql_set_bool jql
nullPtr
(CInt $ fromIntegral index)
(CBool (Bool.bool 0 1 bool)) >>= checkRC
setI64 :: Int64
-> String
-> Query
-> IO ()
setI64 number placeholder (Query jql _ _) = withCString placeholder $
\cPlaceholder -> c_jql_set_i64 jql cPlaceholder 0 (CIntMax number)
>>= checkRC
setI64AtIndex :: Int64
-> Int
-> Query
-> IO ()
setI64AtIndex number index (Query jql _ _) =
c_jql_set_i64 jql nullPtr (CInt $ fromIntegral index) (CIntMax number)
>>= checkRC
setF64 :: Double
-> String
-> Query
-> IO ()
setF64 number placeholder (Query jql _ _) = withCString placeholder $
\cPlaceholder -> c_jql_set_f64 jql cPlaceholder 0 (CDouble number)
>>= checkRC
setF64AtIndex :: Double
-> Int
-> Query
-> IO ()
setF64AtIndex number index (Query jql _ _) =
c_jql_set_f64 jql nullPtr (CInt $ fromIntegral index) (CDouble number)
>>= checkRC
newCStringInIORef :: String -> IORef [ForeignPtr CChar] -> IO CString
newCStringInIORef string ioRef = do
cString <- newCString string
cStringPtr <- newForeignPtr finalizerFree cString
modifyIORef' ioRef ((:) cStringPtr)
return cString
setString :: String
-> String
-> Query
-> IO ()
setString string placeholder (Query jql _ ioRef) = do
cString <- newCStringInIORef string ioRef
withCString placeholder $
\cPlaceholder -> c_jql_set_str jql cPlaceholder 0 cString >>= checkRC
setStringAtIndex :: String
-> Int
-> Query
-> IO ()
setStringAtIndex string index (Query jql _ ioRef) = do
cString <- newCStringInIORef string ioRef
c_jql_set_str jql nullPtr (CInt $ fromIntegral index) cString >>= checkRC
setRegex :: String
-> String
-> Query
-> IO ()
setRegex string placeholder (Query jql _ ioRef) = do
cString <- newCStringInIORef string ioRef
withCString placeholder $
\cPlaceholder -> c_jql_set_regexp jql cPlaceholder 0 cString >>= checkRC
setRegexAtIndex :: String
-> Int
-> Query
-> IO ()
setRegexAtIndex string index (Query jql _ ioRef) = do
cString <- newCStringInIORef string ioRef
c_jql_set_regexp jql nullPtr (CInt $ fromIntegral index) cString >>= checkRC
setNull :: String
-> Query
-> IO ()
setNull placeholder (Query jql _ _) = withCString placeholder $
\cPlaceholder -> c_jql_set_null jql cPlaceholder 0 >>= checkRC
setNullAtIndex :: Int
-> Query
-> IO ()
setNullAtIndex index (Query jql _ _) =
c_jql_set_null jql nullPtr (CInt $ fromIntegral index) >>= checkRC