arguments support

This commit is contained in:
Brian J. Cardiff 2016-01-29 17:13:22 -03:00
parent efa010e2ad
commit 82f68ed6cf
3 changed files with 95 additions and 16 deletions

View file

@ -27,6 +27,7 @@ describe Database do
end end
end end
# pending
it "executes and selects blob" do it "executes and selects blob" do
rows = with_db_old(&.execute(%(select X'53514C697465'))) rows = with_db_old(&.execute(%(select X'53514C697465')))
row = rows[0] row = rows[0]

View file

@ -14,6 +14,18 @@ def sql(s)
"#{s}" "#{s}"
end end
def assert_single_read(result_set, value_type, value)
result_set.move_next.should be_true
result_set.read(value_type).should eq(value)
result_set.move_next.should be_false
end
def assert_single_read?(result_set, value_type, value)
result_set.move_next.should be_true
result_set.read?(value_type).should eq(value)
result_set.move_next.should be_false
end
describe Driver do describe Driver do
it "should register sqlite3 name" do it "should register sqlite3 name" do
DB.driver_class("sqlite3").should eq(SQLite3::Driver) DB.driver_class("sqlite3").should eq(SQLite3::Driver)
@ -30,36 +42,64 @@ describe Driver do
it "executes and select {{value.id}}" do it "executes and select {{value.id}}" do
with_db do |db| with_db do |db|
result_set = db.exec("select #{sql({{value}})}") result_set = db.exec("select #{sql({{value}})}")
result_set.move_next.should be_true assert_single_read result_set, typeof({{value}}), {{value}}
result_set.read(typeof({{value}})).should eq({{value}})
result_set.move_next.should be_false
end end
end end
it "executes and select {{value.id}} as nillable" do it "executes and select {{value.id}} as nillable" do
with_db do |db| with_db do |db|
result_set = db.exec("select #{sql({{value}})}") result_set = db.exec("select #{sql({{value}})}")
result_set.move_next.should be_true assert_single_read? result_set, typeof({{value}}), {{value}}
result_set.read?(typeof({{value}})).should eq({{value}})
result_set.move_next.should be_false
end end
end end
it "executes and select nil as type of {{value.id}}" do it "executes and select nil as type of {{value.id}}" do
with_db do |db| with_db do |db|
result_set = db.exec("select null") result_set = db.exec("select null")
result_set.move_next.should be_true assert_single_read? result_set, typeof({{value}}), nil
result_set.read?(typeof({{value}})).should be_nil
result_set.move_next.should be_false
end end
end end
# it "executes with bind #{value}" do it "executes with bind {{value.id}}" do
# with_db(&.execute(%(select ?), value)).should eq([[value]]) with_db do |db|
# end result_set = db.exec(%(select ?), {{value}})
assert_single_read result_set, typeof({{value}}), {{value}}
end
end
# it "executes with bind #{value} as array" do it "executes with bind {{value.id}} read as nillable" do
# with_db(&.execute(%(select ?), [value])).should eq([[value]]) with_db do |db|
# end result_set = db.exec(%(select ?), {{value}})
assert_single_read? result_set, typeof({{value}}), {{value}}
end
end
it "executes with bind nil as typeof {{value.id}}" do
with_db do |db|
result_set = db.exec(%(select ?), nil)
assert_single_read? result_set, typeof({{value}}), nil
end
end
it "executes with bind {{value.id}} as array" do
with_db do |db|
result_set = db.exec(%(select ?), [{{value}}])
assert_single_read result_set, typeof({{value}}), {{value}}
end
end
{% end %} {% end %}
it "executes with named bind using symbol" do
with_db do |db|
result_set = db.exec(%(select :value), {value: "hello"})
assert_single_read result_set, String, "hello"
end
end
it "executes with named bind using string" do
with_db do |db|
result_set = db.exec(%(select :value), {"value": "hello"})
assert_single_read result_set, String, "hello"
end
end
end end

View file

@ -4,11 +4,49 @@ class SQLite3::Statement2 < DB::Statement
# @closed = false # @closed = false
end end
def exec(*args) protected def before_execute
LibSQLite3.reset(self) LibSQLite3.reset(self)
end
protected def add_parameter(index : Int32, value)
bind_arg(index, value)
end
protected def add_parameter(name : String, value)
converted_name = ":#{name}"
index = LibSQLite3.bind_parameter_index(self, converted_name)
raise "Unknown parameter: #{name}" if index == 0
bind_arg(index, value)
end
protected def execute
ResultSet2.new(self) ResultSet2.new(self)
end end
private def bind_arg(index, value : Nil)
check LibSQLite3.bind_null(self, index)
end
private def bind_arg(index, value : Int32)
check LibSQLite3.bind_int(self, index, value)
end
private def bind_arg(index, value : Int64)
check LibSQLite3.bind_int64(self, index, value)
end
private def bind_arg(index, value : Float32)
check LibSQLite3.bind_double(self, index, value.to_f64)
end
private def bind_arg(index, value : Float64)
check LibSQLite3.bind_double(self, index, value)
end
private def bind_arg(index, value : String)
check LibSQLite3.bind_text(self, index, value, value.bytesize, nil)
end
private def check(code) private def check(code)
raise Exception.new(@driver) unless code == 0 raise Exception.new(@driver) unless code == 0
end end