From 032d45640bd825c953e01c297df5b7af1f3016ff Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Sat, 23 Oct 2021 01:32:14 -0600 Subject: [PATCH] Support DBInterface.jl v2.5 DBInterface.jl now supports overloading `DBInterface.transaction` that is now used in `DBInterface.executemany` to wrap multiple `execute` calls in transactions for efficiency. This speeds up the use of `executemany` on SQLite prepared statements, as reported as slow in --- Project.toml | 4 ++-- src/SQLite.jl | 4 ++++ src/tables.jl | 2 +- test/runtests.jl | 12 ++++++++++++ 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index 110313c..033f9d0 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "SQLite" uuid = "0aa819cd-b072-5ff4-a722-6bc24af294d9" authors = ["Jacob Quinn "] -version = "1.3.0" +version = "1.4.0" [deps] BinaryProvider = "b99e7846-7c00-51b0-8f62-c81ae34c0232" @@ -17,7 +17,7 @@ WeakRefStrings = "ea10d353-3f73-51f8-a26c-33c1cb351aa5" [compat] BinaryProvider = "0.5" -DBInterface = "2.4" +DBInterface = "2.5" SQLite_jll = "3" Tables = "1" WeakRefStrings = "0.4,0.5,0.6,1" diff --git a/src/SQLite.jl b/src/SQLite.jl index 947e6ad..9bf4243 100644 --- a/src/SQLite.jl +++ b/src/SQLite.jl @@ -177,6 +177,8 @@ end # it from the db.stmts collection _finalize(stmt::Stmt) = DBInterface.close!(stmt) +DBInterface.getconnection(stmt::Stmt) = stmt.db + # explicitly close prepared statement function DBInterface.close!(stmt::Stmt) _st = _stmt_safe(stmt) @@ -549,6 +551,8 @@ function transaction(db::DB, mode="DEFERRED") end end +DBInterface.transaction(f, db::DB) = transaction(f, db) + @inline function transaction(f::Function, db::DB) # generate a random name for the savepoint name = string("SQLITE", Random.randstring(10)) diff --git a/src/tables.jl b/src/tables.jl index 707cb22..163ea37 100644 --- a/src/tables.jl +++ b/src/tables.jl @@ -238,7 +238,7 @@ function load!(sch::Tables.Schema, rows, db::DB, name::AbstractString, db_tablei kind = replace ? "REPLACE" : "INSERT" stmt = _Stmt(db, "$kind INTO $(esc_id(string(name))) ($columns) VALUES ($params)") # start a transaction for inserting rows - transaction(db) do + DBInterface.transaction(db) do if row === nothing state = iterate(rows) state === nothing && return diff --git a/test/runtests.jl b/test/runtests.jl index 3dc4a13..01866ff 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -566,4 +566,16 @@ DBInterface.execute(db, "insert into tmp values (?)", (:a,)) tbl = DBInterface.execute(db, "select x from tmp") |> columntable @test isequal(tbl.x, [missing, :a]) +db = SQLite.DB() +DBInterface.execute(db, "create table tmp (a integer, b integer, c integer)") +stmt = DBInterface.prepare(db, "INSERT INTO tmp VALUES(?, ?, ?)") +tbl = ( + a = [1, 1, 1], + b = [3, 4, 5], + c = [4, 5, 6] +) +DBInterface.executemany(stmt, tbl) +tbl2 = DBInterface.execute(db, "select * from tmp") |> columntable +@test tbl == tbl2 + end \ No newline at end of file