Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support DBInterface.execute(f, stmt) method #226

Merged
merged 2 commits into from
Jan 27, 2021
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ WeakRefStrings = "ea10d353-3f73-51f8-a26c-33c1cb351aa5"

[compat]
BinaryProvider = "0.5"
DBInterface = "2.3"
DBInterface = "2.4"
SQLite_jll = "3"
Tables = "1"
WeakRefStrings = "0.4,0.5,0.6"
Expand Down
5 changes: 5 additions & 0 deletions src/tables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ function reset!(q::Query)
return
end

function DBInterface.close!(q::Query)
_st = _stmt_safe(q.stmt)
(_st !== nothing) && sqlite3_reset(_st.handle)
end

function done(q::Query)
st = q.status[]
if st == SQLITE_DONE
Expand Down
26 changes: 23 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ db = DBInterface.connect(SQLite.DB, dbfile2)
# syntax correct, table missing
@test_throws SQLiteException DBInterface.execute(db, "SELECT name FROM sqlite_nomaster WHERE type='table';")

@testset "close!(query)" begin
qry = DBInterface.execute(db, "SELECT name FROM sqlite_master WHERE type='table';")
DBInterface.close!(qry)
DBInterface.close!(qry) # test it doesn't throw on double-close
end

ds = DBInterface.execute(db, "SELECT name FROM sqlite_master WHERE type='table';") |> columntable
@test length(ds) == 1
@test keys(ds) == (:name,)
Expand All @@ -60,9 +66,23 @@ ds = DBInterface.execute(db, "SELECT name FROM sqlite_master WHERE type='table';
results1 = SQLite.tables(db)
@test isequal(ds, results1)

results = DBInterface.execute(db, "SELECT * FROM Employee;") |> columntable
@test length(results) == 15
@test length(results[1]) == 8
@testset "DBInterface.execute([f])" begin
# pipe approach
results = DBInterface.execute(db, "SELECT * FROM Employee;") |> columntable
@test length(results) == 15
@test length(results[1]) == 8
# callable approach
@test isequal(DBInterface.execute(columntable, db, "SELECT * FROM Employee"), results)
employees_stmt = DBInterface.prepare(db, "SELECT * FROM Employee")
@test isequal(columntable(DBInterface.execute(employees_stmt)), results)
@test isequal(DBInterface.execute(columntable, employees_stmt), results)
@testset "throwing from f()" begin
f(::SQLite.Query) = error("I'm throwing!")
@test_throws ErrorException DBInterface.execute(f, employees_stmt)
@test_throws ErrorException DBInterface.execute(f, db, "SELECT * FROM Employee")
end
DBInterface.close!(employees_stmt)
end

DBInterface.execute(db, "create table temp as select * from album")
DBInterface.execute(db, "alter table temp add column colyear int")
Expand Down