Skip to content

Commit e3c6814

Browse files
maxfreuMax Freudenberg
andauthored
relax bind! Vector{UInt8} -> AbstractVector{UInt8} (#340)
* relax bind! Vector{UInt8} -> AbstractVector{UInt8} This allows zero-copy writes e.g. for ReinterpretArrays. * special case for ReinterpretArray * fix size calculation, add tests * pass pointer of ReinterpA. to ccall * replace pointer by Ref --------- Co-authored-by: Max Freudenberg <[email protected]>
1 parent 95131f8 commit e3c6814

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/SQLite.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,16 @@ function bind!(stmt::Stmt, i::Integer, val::Vector{UInt8})
356356
C.SQLITE_STATIC,
357357
)
358358
end
359+
function bind!(stmt::Stmt, i::Integer, val::Base.ReinterpretArray{UInt8, 1, T, <:DenseVector{T}, false}) where T
360+
stmt.params[i] = val
361+
@CHECK stmt.db C.sqlite3_bind_blob(
362+
_get_stmt_handle(stmt),
363+
i,
364+
Ref(val, 1),
365+
sizeof(eltype(val)) * length(val),
366+
C.SQLITE_STATIC,
367+
)
368+
end
359369
# Fallback is BLOB and defaults to serializing the julia value
360370

361371
# internal wrapper mutable struct to, in-effect, mark something which has been serialized

test/runtests.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,24 @@ end
906906
close(db)
907907
rm(dbfile)
908908
end
909+
910+
@testset "ReinterpretArray" begin
911+
binddb = SQLite.DB()
912+
DBInterface.execute(
913+
binddb,
914+
"CREATE TABLE temp (b BLOB)",
915+
)
916+
DBInterface.execute(
917+
binddb,
918+
"INSERT INTO temp VALUES (?)",
919+
[reinterpret(UInt8, [0x6f46, 0x426f, 0x7261]),],
920+
)
921+
rr = DBInterface.execute(rowtable, binddb, "SELECT b FROM temp")
922+
@test length(rr) == 1
923+
r = first(rr)
924+
@test r.b == codeunits("FooBar")
925+
@test typeof.(Tuple(r)) == (Vector{UInt8},)
926+
end
909927
end # @testset
910928

911929
struct UnknownSchemaTable end

0 commit comments

Comments
 (0)