Skip to content

Commit

Permalink
fix: handle SQL_ERROR in Base.iterate(::Cursor) (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
pankgeorg committed Oct 4, 2022
1 parent 8226ef9 commit 2d61d28
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/consts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,6 @@ const C_TYPES = Dict(
# success codes
const SQL_SUCCESS = Int16(0)
const SQL_SUCCESS_WITH_INFO = Int16(1)
const SQL_STILL_EXECUTING = Int16(2)

# error codes
const SQL_ERROR = Int16(-1)
Expand All @@ -295,7 +294,10 @@ const SQL_NO_DATA = Int16(100)

const RETURN_VALUES = Dict(SQL_ERROR => "SQL_ERROR",
SQL_NO_DATA => "SQL_NO_DATA",
SQL_INVALID_HANDLE => "SQL_INVALID_HANDLE",
SQL_STILL_EXECUTING => "SQL_STILL_EXECUTING")
SQL_SUCCESS => "SQL_SUCCESS",
SQL_NTS => "SQL_NTS",
SQL_STILL_EXECUTING => "SQL_STILL_EXECUTING",
SQL_INVALID_HANDLE => "SQL_INVALID_HANDLE",
SQL_SUCCESS_WITH_INFO => "SQL_SUCCESS_WITH_INFO")

const SQL_NO_NULLS = Int16(0)
13 changes: 12 additions & 1 deletion src/dbinterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,18 @@ function Base.iterate(x::Cursor{false}, st=1)
while status == API.SQL_STILL_EXECUTING
status = API.SQLFetchScroll(API.getptr(x.stmt), API.SQL_FETCH_NEXT, 0)
end
status == API.SQL_SUCCESS || status == API.SQL_SUCCESS_WITH_INFO || return nothing

# iteration end
status == API.SQL_NO_DATA && return nothing
status == API.SQL_NTS && return nothing

# errors
status == API.SQL_ERROR && error(API.diagnostics(x.stmt))
status == API.SQL_INVALID_HANDLE && error(API.diagnostics(x.stmt))

# continue
status == API.SQL_SUCCESS_WITH_INFO && @warn(API.diagnostics(x.stmt))

x.current_rownumber = st
for (i, binding) in enumerate(x.bindings)
getdata(x.stmt, i, binding)
Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ expected = (
Senior = Union{Missing, Bool}[true, true, false, true],
)


# Validate that iteration of results throws runtime Error on DivisionByZero
@test_throws ErrorException DBInterface.execute(
conn, "SELECT a, b, a/b FROM (VALUES (2,1),(1,0),(2,1)) AS t(a,b)"
) |> columntable

cursor = DBInterface.execute(conn, "select * from Employee")
@test eltype(cursor) == ODBC.Row
@test Tables.istable(cursor)
Expand Down

0 comments on commit 2d61d28

Please sign in to comment.