Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Database/Oracle/Simple/Execute.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Database.Oracle.Simple.Internal
Connection,
DPIModeExec (DPI_MODE_EXEC_DEFAULT),
dpiExecute,
closeStatement,
getRowCount,
prepareStmt,
)
Expand All @@ -29,14 +30,18 @@ execute conn sql param = do
stmt <- prepareStmt conn sql
_ <- evalStateT (runRowWriter (toRow param) stmt) (Column 0)
_ <- dpiExecute stmt DPI_MODE_EXEC_DEFAULT
getRowCount stmt
rowCount <- getRowCount stmt
closeStatement stmt
pure rowCount

-- | A version of 'execute' that does not perform query substitution.
execute_ :: Connection -> String -> IO Word64
execute_ conn sql = do
stmt <- prepareStmt conn sql
_ <- dpiExecute stmt DPI_MODE_EXEC_DEFAULT
getRowCount stmt
rowCount <- getRowCount stmt
closeStatement stmt
pure rowCount

{- | Execute a multi-row INSERT, UPDATE or other SQL query that is not expected to return results.
Returns the number of rows affected. If the list of parameters is empty, the function will simply
Expand All @@ -52,4 +57,5 @@ executeMany conn sql params = do
_ <- evalStateT (runRowWriter (toRow param) stmt) (Column 0)
_ <- dpiExecute stmt DPI_MODE_EXEC_DEFAULT
rowsAffected <- getRowCount stmt
closeStatement stmt
pure (totalRowsAffected + rowsAffected)
15 changes: 13 additions & 2 deletions src/Database/Oracle/Simple/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ module Database.Oracle.Simple.Internal
ping,
fetch,
close,
closeStatement,
connect,
withConnection,
withConnCreateParams,
Expand Down Expand Up @@ -1908,6 +1909,18 @@ foreign import ccall unsafe "dpiConn_getIsHealthy"
Ptr CInt ->
IO CInt

foreign import ccall "dpiStmt_close"
dpiStmt_close ::
DPIStmt ->
CString ->
CUInt ->
IO CInt

-- | Close the statement and make it unusable for further work immediately.
closeStatement :: DPIStmt -> IO ()
closeStatement stmt =
throwOracleError =<< dpiStmt_close stmt nullPtr 0

-- | A pointer to an integer defining whether the connection is healthy (1) or not (0), which will be populated upon successful completion of this function.
isHealthy :: Connection -> IO Bool
isHealthy (Connection fptr) =
Expand All @@ -1922,5 +1935,3 @@ Structurally equivalent to 'Data.Functor.Identity.Identity'.
newtype Only a = Only {fromOnly :: a}
deriving stock (Eq, Ord, Read, Show, Generic)
deriving newtype (Enum)


13 changes: 10 additions & 3 deletions src/Database/Oracle/Simple/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Database.Oracle.Simple.Internal
Connection,
DPIModeExec (DPI_MODE_EXEC_DEFAULT),
dpiExecute,
closeStatement,
fetch,
prepareStmt,
)
Expand All @@ -29,7 +30,9 @@ query conn sql param = do
found <- fetch stmt
loop stmt found
where
loop _ n | n < 1 = pure []
loop _ n | n < 1 = do
closeStatement stmt
pure []
loop stmt _ = do
tsVal <- getRow stmt
found <- fetch stmt
Expand All @@ -43,7 +46,9 @@ query_ conn sql = do
found <- fetch stmt
loop stmt found
where
loop _ n | n < 1 = pure []
loop _ n | n < 1 = do
closeStatement stmt
pure []
loop stmt _ = do
tsVal <- getRow stmt
found <- fetch stmt
Expand All @@ -57,7 +62,9 @@ forEach_ conn sql cont = do
found <- fetch stmt
loop stmt found
where
loop _ n | n < 1 = pure ()
loop _ n | n < 1 = do
closeStatement stmt
pure ()
loop stmt _ = do
tsVal <- getRow stmt
cont tsVal
Expand Down
Loading