Skip to content

Commit d69e2d6

Browse files
authored
fix(dbinterface): preserve values and bind nothing (#239)
1 parent 9db9900 commit d69e2d6

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

src/load.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,9 @@ end
118118
function DBInterface.transaction(f::Function, conn::Connection)
119119
DBInterface.execute(conn, "START TRANSACTION")
120120
try
121-
f()
121+
result = f()
122122
API.commit(conn.mysql)
123+
return result
123124
catch
124125
API.rollback(conn.mysql)
125126
rethrow()

src/prepare.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,8 @@ function bind!(helper, binds, i, x::Missing)
317317
return
318318
end
319319

320+
bind!(helper, binds, i, ::Nothing) = bind!(helper, binds, i, missing)
321+
320322
function bind!(helper, binds, i, x::Real)
321323
if !helper.typeset
322324
inithelper!(helper, x)

test/runtests.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,15 @@ end
329329
DBInterface.execute(stmt, [missing, missing, missing, missing, missing, missing, missing, missing, missing, DateTime("2015-09-05T12:31:30"), missing, missing, missing, missing, missing])
330330
DBInterface.close!(stmt)
331331

332+
DBInterface.execute(conn, "CREATE TABLE NullBindingTest (value INT NULL)")
333+
stmt = DBInterface.prepare(conn, "INSERT INTO NullBindingTest (value) VALUES (?)")
334+
DBInterface.execute(stmt, (nothing,))
335+
DBInterface.execute(stmt, (1,))
336+
DBInterface.execute(stmt, (missing,))
337+
DBInterface.close!(stmt)
338+
res = DBInterface.execute(conn, "SELECT value FROM NullBindingTest") |> columntable
339+
@test isequal(res.value, [missing, 1, missing])
340+
332341
stmt = DBInterface.prepare(conn, "select * from Employee")
333342
res = DBInterface.execute(stmt) |> columntable
334343
DBInterface.close!(stmt)
@@ -511,7 +520,7 @@ ret = columntable(res)
511520

512521
try
513522
# happy path
514-
DBInterface.transaction(conn) do
523+
result = DBInterface.transaction(conn) do
515524
DBInterface.execute(conn, "INSERT INTO TransactionTest (a) VALUES (1)")
516525

517526
# we can see the result inside our transaction
@@ -521,7 +530,9 @@ ret = columntable(res)
521530
# and can't see it outside our transaction
522531
result = DBInterface.execute(conn2, "SELECT * FROM TransactionTest") |> Tables.columntable
523532
@test isempty(result.a)
533+
return 42
524534
end
535+
@test result == 42
525536
result = DBInterface.execute(conn, "SELECT * FROM TransactionTest") |> Tables.columntable
526537
@test result.a == [1]
527538
result = DBInterface.execute(conn2, "SELECT * FROM TransactionTest") |> Tables.columntable

0 commit comments

Comments
 (0)