Skip to content

Commit b70e947

Browse files
authored
Fix issue loading Int8/UInt8 values into mysql table (#199)
Fixes #194. Basically just a typo in the core 1-byte loading code. We probably started out with treating `Bool` separately from Int8/UInt8 and then combined the code support, but forgot to change the bitcast to `UInt8` instead of `Bool`, which resulted in the "truncation" result shared in the original issue.
1 parent 0893dff commit b70e947

File tree

4 files changed

+11
-2
lines changed

4 files changed

+11
-2
lines changed

src/MySQL.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ function DBInterface.close!(conn::Connection)
302302
end
303303

304304
Base.close(conn::Connection) = DBInterface.close!(conn)
305-
Base.isopen(conn::Connection) = API.isopen(conn.mysql)
305+
Base.isopen(conn::Connection) = conn.mysql.ptr != C_NULL && API.isopen(conn.mysql)
306306

307307
function juliatype(field_type, notnullable, isunsigned, isbinary, date_and_time)
308308
T = API.juliatype(field_type)

src/load.jl

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ function load end
6969
load(conn::Connection, table::AbstractString="mysql_"*Random.randstring(5); kw...) = x->load(x, conn, table; kw...)
7070

7171
function load(itr, conn::Connection, name::AbstractString="mysql_"*Random.randstring(5); append::Bool=true, quoteidentifiers::Bool=true, debug::Bool=false, limit::Integer=typemax(Int64), kw...)
72+
isopen(conn) || throw(ArgumentError("`MySQL.Connection` is closed"))
7273
# get data
7374
rows = Tables.rows(itr)
7475
sch = Tables.schema(rows)

src/prepare.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ end
219219

220220
inithelper!(helper, x::Union{Bool, UInt8, Int8}) = helper.uint8 = UInt8[Core.bitcast(UInt8, x)]
221221
ptrhelper(helper, x::Union{Bool, UInt8, Int8}) = pointer(helper.uint8)
222-
sethelper!(helper, x::Union{Bool, UInt8, Int8}) = helper.uint8[1] = Core.bitcast(Bool, x)
222+
sethelper!(helper, x::Union{Bool, UInt8, Int8}) = helper.uint8[1] = Core.bitcast(UInt8, x)
223223
getvalue(stmt, helper, values, i, ::Type{T}) where {T <: Union{Bool, UInt8, Int8}} = Core.bitcast(T, helper.uint8[1])
224224

225225
inithelper!(helper, x::Union{UInt16, Int16}) = helper.uint16 = UInt16[Core.bitcast(UInt16, x)]

test/runtests.jl

+8
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,15 @@ DBInterface.execute(stmt, [SubString("foo")])
331331
# escaping AbstractString
332332
@test MySQL.escape(conn, SubString("'); DROP TABLE Employee; --")) == "\\'); DROP TABLE Employee; --"
333333

334+
# https://github.com/JuliaDatabases/MySQL.jl/issues/194
335+
ct = (x = UInt8[1, 2, 3],)
336+
MySQL.load(ct, conn, "test194")
337+
ct2 = columntable(DBInterface.execute(conn, "select * from test194"))
338+
334339
# 156
335340
res = DBInterface.execute(conn, "select * from Employee")
336341
DBInterface.close!(conn)
337342
ret = columntable(res)
343+
344+
# load on closed connection should throw
345+
@test_throws ArgumentError MySQL.load(ct, conn, "test194")

0 commit comments

Comments
 (0)