{-# LINE 1 "Database/HDBC/Sqlite3/Utils.hsc" #-}
{- -*- mode: haskell; -*- 
   vim: set filetype=haskell:
-}

module Database.HDBC.Sqlite3.Utils where
import Foreign.C.String
import Foreign.ForeignPtr
import Foreign.Ptr
import Database.HDBC(throwSqlError)
import Database.HDBC.Types
import Database.HDBC.Sqlite3.Types
import qualified Data.ByteString as B
import qualified Data.ByteString.UTF8 as BUTF8
import Foreign.C.Types
import Control.Exception
import Foreign.Storable



checkError :: String -> Sqlite3 -> CInt -> IO ()
checkError :: String -> Sqlite3 -> CInt -> IO ()
checkError String
_ Sqlite3
_ CInt
0 = () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
checkError String
msg Sqlite3
o CInt
res =
    Sqlite3 -> (Ptr CSqlite3 -> IO ()) -> IO ()
forall b. Sqlite3 -> (Ptr CSqlite3 -> IO b) -> IO b
withSqlite3 Sqlite3
o
     (\Ptr CSqlite3
p -> do CString
rc <- Ptr CSqlite3 -> IO CString
sqlite3_errmsg Ptr CSqlite3
p
               ByteString
bs <- CString -> IO ByteString
B.packCString CString
rc
               let str :: String
str = ByteString -> String
BUTF8.toString ByteString
bs
               SqlError -> IO ()
forall a. SqlError -> IO a
throwSqlError (SqlError -> IO ()) -> SqlError -> IO ()
forall a b. (a -> b) -> a -> b
$
                          SqlError {seState :: String
seState = String
"",
                                    seNativeError :: Int
seNativeError = CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
res,
                                    seErrorMsg :: String
seErrorMsg = String
msg String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
": " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
str}
     )

{- This is a little hairy.

We have a CSqlite3 object that is actually a finalizeonce wrapper around
the real object.  We use withSqlite3 to dereference the foreign pointer,
and then extract the pointer to the real object from the finalizeonce struct.

But, when we close the connection, we need the finalizeonce struct, so that's
done by withRawSqlite3.

Ditto for statements. -}

withSqlite3 :: Sqlite3 -> (Ptr CSqlite3 -> IO b) -> IO b
withSqlite3 :: forall b. Sqlite3 -> (Ptr CSqlite3 -> IO b) -> IO b
withSqlite3 = Sqlite3 -> (Ptr CSqlite3 -> IO b) -> IO b
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
genericUnwrap

withRawSqlite3 :: Sqlite3 -> (Ptr CSqlite3 -> IO b) -> IO b
withRawSqlite3 :: forall b. Sqlite3 -> (Ptr CSqlite3 -> IO b) -> IO b
withRawSqlite3 = Sqlite3 -> (Ptr CSqlite3 -> IO b) -> IO b
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr

withStmt :: Stmt -> (Ptr CStmt -> IO b) -> IO b
withStmt :: forall b. Stmt -> (Ptr CStmt -> IO b) -> IO b
withStmt = Stmt -> (Ptr CStmt -> IO b) -> IO b
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
genericUnwrap

withRawStmt :: Stmt -> (Ptr CStmt -> IO b) -> IO b
withRawStmt :: forall b. Stmt -> (Ptr CStmt -> IO b) -> IO b
withRawStmt = Stmt -> (Ptr CStmt -> IO b) -> IO b
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr


genericUnwrap :: ForeignPtr a -> (Ptr a -> IO b) -> IO b
genericUnwrap :: forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
genericUnwrap ForeignPtr a
fptr Ptr a -> IO b
action = ForeignPtr a -> (Ptr a -> IO b) -> IO b
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr a
fptr (\Ptr a
structptr ->
    do Ptr a
objptr <- (\Ptr a
hsc_ptr -> Ptr a -> Int -> IO (Ptr a)
forall b. Ptr b -> Int -> IO (Ptr a)
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr a
hsc_ptr Int
0) Ptr a
structptr
{-# LINE 60 "Database/HDBC/Sqlite3/Utils.hsc" #-}
       action objptr
                                                )

foreign import ccall unsafe "sqlite3.h sqlite3_errmsg"
  sqlite3_errmsg :: (Ptr CSqlite3) -> IO CString