Skip to content

Commit

Permalink
relax bind! Vector{UInt8} -> AbstractVector{UInt8} (#340)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
maxfreu and Max Freudenberg committed May 22, 2024
1 parent 95131f8 commit e3c6814
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/SQLite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,16 @@ function bind!(stmt::Stmt, i::Integer, val::Vector{UInt8})
C.SQLITE_STATIC,
)
end
function bind!(stmt::Stmt, i::Integer, val::Base.ReinterpretArray{UInt8, 1, T, <:DenseVector{T}, false}) where T
stmt.params[i] = val
@CHECK stmt.db C.sqlite3_bind_blob(
_get_stmt_handle(stmt),
i,
Ref(val, 1),
sizeof(eltype(val)) * length(val),
C.SQLITE_STATIC,
)
end
# Fallback is BLOB and defaults to serializing the julia value

# internal wrapper mutable struct to, in-effect, mark something which has been serialized
Expand Down
18 changes: 18 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,24 @@ end
close(db)
rm(dbfile)
end

@testset "ReinterpretArray" begin
binddb = SQLite.DB()
DBInterface.execute(
binddb,
"CREATE TABLE temp (b BLOB)",
)
DBInterface.execute(
binddb,
"INSERT INTO temp VALUES (?)",
[reinterpret(UInt8, [0x6f46, 0x426f, 0x7261]),],
)
rr = DBInterface.execute(rowtable, binddb, "SELECT b FROM temp")
@test length(rr) == 1
r = first(rr)
@test r.b == codeunits("FooBar")
@test typeof.(Tuple(r)) == (Vector{UInt8},)
end
end # @testset

struct UnknownSchemaTable end
Expand Down

0 comments on commit e3c6814

Please sign in to comment.