2016-01-29 19:15:28 +00:00
|
|
|
require "./spec_helper"
|
|
|
|
|
|
|
|
def with_db
|
|
|
|
yield DB.open "sqlite3", {"database": DB_FILENAME}
|
|
|
|
ensure
|
|
|
|
File.delete(DB_FILENAME)
|
|
|
|
end
|
|
|
|
|
2016-01-30 00:58:04 +00:00
|
|
|
def with_mem_db
|
|
|
|
yield DB.open "sqlite3", {"database": ":memory:"}
|
|
|
|
end
|
|
|
|
|
2016-01-29 19:15:28 +00:00
|
|
|
def sql(s : String)
|
|
|
|
"#{s.inspect}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def sql(s)
|
|
|
|
"#{s}"
|
|
|
|
end
|
|
|
|
|
2016-01-29 20:13:22 +00:00
|
|
|
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
|
|
|
|
|
2016-01-29 19:15:28 +00:00
|
|
|
describe Driver do
|
|
|
|
it "should register sqlite3 name" do
|
|
|
|
DB.driver_class("sqlite3").should eq(SQLite3::Driver)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should use database option as file to open" do
|
|
|
|
with_db do |db|
|
|
|
|
db.driver_class.should eq(SQLite3::Driver)
|
|
|
|
File.exists?(DB_FILENAME).should be_true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
{% for value in [1, 1_i64, "hello", 1.5, 1.5_f32] %}
|
|
|
|
it "executes and select {{value.id}}" do
|
|
|
|
with_db do |db|
|
|
|
|
result_set = db.exec("select #{sql({{value}})}")
|
2016-01-29 20:13:22 +00:00
|
|
|
assert_single_read result_set, typeof({{value}}), {{value}}
|
2016-01-29 19:15:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "executes and select {{value.id}} as nillable" do
|
|
|
|
with_db do |db|
|
|
|
|
result_set = db.exec("select #{sql({{value}})}")
|
2016-01-29 20:13:22 +00:00
|
|
|
assert_single_read? result_set, typeof({{value}}), {{value}}
|
2016-01-29 19:15:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "executes and select nil as type of {{value.id}}" do
|
|
|
|
with_db do |db|
|
|
|
|
result_set = db.exec("select null")
|
2016-01-29 20:13:22 +00:00
|
|
|
assert_single_read? result_set, typeof({{value}}), nil
|
2016-01-29 19:15:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-29 20:13:22 +00:00
|
|
|
it "executes with bind {{value.id}}" do
|
|
|
|
with_db do |db|
|
|
|
|
result_set = db.exec(%(select ?), {{value}})
|
|
|
|
assert_single_read result_set, typeof({{value}}), {{value}}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "executes with bind {{value.id}} read as nillable" do
|
|
|
|
with_db do |db|
|
|
|
|
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
|
2016-01-29 19:15:28 +00:00
|
|
|
|
2016-01-29 20:13:22 +00:00
|
|
|
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
|
2016-01-29 19:15:28 +00:00
|
|
|
{% end %}
|
2016-01-29 20:13:22 +00:00
|
|
|
|
2016-01-29 22:22:30 +00:00
|
|
|
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
|
|
|
|
|
2016-01-29 20:13:22 +00:00
|
|
|
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
|
2016-01-30 00:58:04 +00:00
|
|
|
|
|
|
|
it "gets column count" do
|
|
|
|
with_mem_db do |db|
|
|
|
|
db.exec_non_query "create table person (name string, age integer)"
|
|
|
|
db.exec_non_query %(insert into person values ("foo", 10))
|
|
|
|
|
|
|
|
run = false
|
|
|
|
db.exec_query_each "select * from person" do |result_set|
|
|
|
|
run = true
|
|
|
|
result_set.column_count.should eq(2)
|
|
|
|
end
|
|
|
|
run.should be_true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "gets column name" do
|
|
|
|
with_mem_db do |db|
|
|
|
|
db.exec_non_query "create table person (name string, age integer)"
|
|
|
|
db.exec_non_query %(insert into person values ("foo", 10))
|
|
|
|
|
|
|
|
db.exec_query_each("select * from person") do |result_set|
|
|
|
|
result_set.column_name(0).should eq("name")
|
|
|
|
result_set.column_name(1).should eq("age")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-01-29 19:15:28 +00:00
|
|
|
end
|