add blob support

This commit is contained in:
Brian J. Cardiff 2016-01-29 19:22:30 -03:00
parent 82f68ed6cf
commit 63f98d18d1
3 changed files with 37 additions and 2 deletions

View file

@ -89,6 +89,23 @@ describe Driver do
end
{% end %}
it "executes and selects blob" do
with_db do |db|
result_set = db.exec %(select X'53514C697465')
result_set.move_next
result_set.read(Slice(UInt8)).to_a.should eq([0x53, 0x51, 0x4C, 0x69, 0x74, 0x65])
end
end
it "executes with bind blob" do
with_db do |db|
ary = UInt8[0x53, 0x51, 0x4C, 0x69, 0x74, 0x65]
result_set = db.exec %(select cast(? as BLOB)), Slice.new(ary.to_unsafe, ary.size)
result_set.move_next
result_set.read(Slice(UInt8)).to_a.should eq(ary)
end
end
it "executes with named bind using symbol" do
with_db do |db|
result_set = db.exec(%(select :value), {value: "hello"})

View file

@ -44,7 +44,17 @@ class SQLite3::ResultSet2 < DB::ResultSet
end
def read(t : Float64.class) : Float64
moving_column { LibSQLite3.column_double(self, @column_index) }
moving_column { |col| LibSQLite3.column_double(self, col) }
end
def read(t : Slice(UInt8).class) : Slice(UInt8)
moving_column do |col|
blob = LibSQLite3.column_blob(self, col)
bytes = LibSQLite3.column_bytes(self, col)
ptr = Pointer(UInt8).malloc(bytes)
ptr.copy_from(blob, bytes)
Slice(UInt8).new(ptr, bytes)
end
end
def to_unsafe
@ -52,7 +62,11 @@ class SQLite3::ResultSet2 < DB::ResultSet
end
private def read_nil?
LibSQLite3.column_type(self, @column_index) == Type::NULL
column_sqlite_type == Type::NULL
end
private def column_sqlite_type
LibSQLite3.column_type(self, @column_index)
end
# :nodoc:

View file

@ -47,6 +47,10 @@ class SQLite3::Statement2 < DB::Statement
check LibSQLite3.bind_text(self, index, value, value.bytesize, nil)
end
private def bind_arg(index, value : Slice(UInt8))
check LibSQLite3.bind_blob(self, index, value, value.size, nil)
end
private def check(code)
raise Exception.new(@driver) unless code == 0
end