2016-01-29 19:15:28 +00:00
|
|
|
require "./spec_helper"
|
|
|
|
|
|
|
|
def sql(s : String)
|
|
|
|
"#{s.inspect}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def sql(s)
|
|
|
|
"#{s}"
|
|
|
|
end
|
|
|
|
|
2016-01-31 17:03:05 +00:00
|
|
|
def sqlite_type_for(v)
|
|
|
|
case v
|
|
|
|
when String ; "text"
|
|
|
|
when Int32, Int64 ; "int"
|
|
|
|
when Float32, Float64; "float"
|
2016-06-22 19:17:47 +00:00
|
|
|
when Time ; "text"
|
2016-01-31 17:03:05 +00:00
|
|
|
else
|
|
|
|
raise "not implemented for #{typeof(v)}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-31 00:14:46 +00:00
|
|
|
def assert_single_read(rs, value_type, value)
|
|
|
|
rs.move_next.should be_true
|
|
|
|
rs.read(value_type).should eq(value)
|
|
|
|
rs.move_next.should be_false
|
2016-01-29 20:13:22 +00:00
|
|
|
end
|
|
|
|
|
2016-02-04 00:29:19 +00:00
|
|
|
def assert_filename(uri, filename)
|
|
|
|
SQLite3::Connection.filename(URI.parse(uri)).should eq(filename)
|
|
|
|
end
|
|
|
|
|
2016-06-22 03:45:00 +00:00
|
|
|
class NotSupportedType
|
|
|
|
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
|
|
|
|
|
2016-02-04 00:29:19 +00:00
|
|
|
it "should get filename from uri" do
|
|
|
|
assert_filename("sqlite3:%3Amemory%3A", ":memory:")
|
|
|
|
assert_filename("sqlite3://%3Amemory%3A", ":memory:")
|
|
|
|
|
|
|
|
assert_filename("sqlite3:./file.db", "./file.db")
|
|
|
|
assert_filename("sqlite3://./file.db", "./file.db")
|
|
|
|
|
|
|
|
assert_filename("sqlite3:/path/to/file.db", "/path/to/file.db")
|
|
|
|
assert_filename("sqlite3:///path/to/file.db", "/path/to/file.db")
|
|
|
|
end
|
|
|
|
|
2016-01-29 19:15:28 +00:00
|
|
|
it "should use database option as file to open" do
|
|
|
|
with_db do |db|
|
2016-02-04 00:29:19 +00:00
|
|
|
db.driver.should be_a(SQLite3::Driver)
|
2016-01-29 19:15:28 +00:00
|
|
|
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|
|
2016-02-04 00:29:19 +00:00
|
|
|
db.scalar("select #{sql({{value}})}").should eq({{value}})
|
2016-01-31 00:14:46 +00:00
|
|
|
|
|
|
|
db.query "select #{sql({{value}})}" do |rs|
|
|
|
|
assert_single_read rs, typeof({{value}}), {{value}}
|
|
|
|
end
|
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|
|
2016-02-04 00:29:19 +00:00
|
|
|
db.scalar("select null").should be_nil
|
2016-01-31 00:14:46 +00:00
|
|
|
|
|
|
|
db.query "select null" do |rs|
|
2016-06-28 17:04:27 +00:00
|
|
|
assert_single_read rs, typeof({{value}} || nil), nil
|
2016-01-31 00:14:46 +00:00
|
|
|
end
|
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|
|
2016-02-04 00:29:19 +00:00
|
|
|
db.scalar(%(select ?), {{value}}).should eq({{value}})
|
2016-01-29 20:13:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "executes with bind nil as typeof {{value.id}}" do
|
|
|
|
with_db do |db|
|
2016-02-04 00:29:19 +00:00
|
|
|
db.scalar("select ?", nil).should be_nil
|
2016-01-29 20:13:22 +00:00
|
|
|
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|
|
2016-02-04 00:29:19 +00:00
|
|
|
db.scalar(%(select ?), [{{value}}]).should eq({{value}})
|
2016-01-29 20:13:22 +00:00
|
|
|
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|
|
2016-06-22 17:13:44 +00:00
|
|
|
slice = db.scalar(%(select X'53514C697465')).as(Bytes)
|
2016-01-31 00:14:46 +00:00
|
|
|
slice.to_a.should eq([0x53, 0x51, 0x4C, 0x69, 0x74, 0x65])
|
2016-01-29 22:22:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "executes with bind blob" do
|
|
|
|
with_db do |db|
|
|
|
|
ary = UInt8[0x53, 0x51, 0x4C, 0x69, 0x74, 0x65]
|
2016-06-22 17:13:44 +00:00
|
|
|
slice = db.scalar(%(select cast(? as BLOB)), Bytes.new(ary.to_unsafe, ary.size)).as(Bytes)
|
2016-01-31 00:14:46 +00:00
|
|
|
slice.to_a.should eq(ary)
|
2016-01-29 22:22:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-30 00:58:04 +00:00
|
|
|
it "gets column count" do
|
|
|
|
with_mem_db do |db|
|
2016-01-31 00:14:46 +00:00
|
|
|
db.exec "create table person (name string, age integer)"
|
2016-01-30 00:58:04 +00:00
|
|
|
|
2016-01-31 00:14:46 +00:00
|
|
|
db.query "select * from person" do |rs|
|
|
|
|
rs.column_count.should eq(2)
|
2016-01-30 00:58:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "gets column name" do
|
|
|
|
with_mem_db do |db|
|
2016-01-31 00:14:46 +00:00
|
|
|
db.exec "create table person (name string, age integer)"
|
|
|
|
|
|
|
|
db.query "select * from person" do |rs|
|
|
|
|
rs.column_name(0).should eq("name")
|
|
|
|
rs.column_name(1).should eq("age")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-31 17:03:05 +00:00
|
|
|
it "gets last insert row id" do
|
|
|
|
with_mem_db do |db|
|
2016-02-04 00:29:19 +00:00
|
|
|
db.exec "create table person (name string, age integer)"
|
2016-01-31 17:03:05 +00:00
|
|
|
|
2016-02-04 00:29:19 +00:00
|
|
|
db.exec %(insert into person values ("foo", 10))
|
|
|
|
|
|
|
|
res = db.exec %(insert into person values ("foo", 10))
|
|
|
|
res.last_insert_id.should eq(2)
|
|
|
|
res.rows_affected.should eq(1)
|
2016-01-31 17:03:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
{% for value in [1, 1_i64, "hello", 1.5, 1.5_f32] %}
|
|
|
|
it "insert/get value {{value.id}} from table" do
|
|
|
|
with_db do |db|
|
|
|
|
db.exec "create table table1 (col1 #{sqlite_type_for({{value}})})"
|
|
|
|
db.exec %(insert into table1 values (#{sql({{value}})}))
|
2016-02-04 00:29:19 +00:00
|
|
|
db.scalar("select col1 from table1").should eq({{value}})
|
2016-01-31 17:03:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
{% end %}
|
|
|
|
|
|
|
|
it "insert/get blob value from table" do
|
|
|
|
with_db do |db|
|
|
|
|
ary = UInt8[0x53, 0x51, 0x4C, 0x69, 0x74, 0x65]
|
|
|
|
|
|
|
|
db.exec "create table table1 (col1 blob)"
|
2016-06-22 17:13:44 +00:00
|
|
|
db.exec %(insert into table1 values (?)), Bytes.new(ary.to_unsafe, ary.size)
|
2016-02-04 00:29:19 +00:00
|
|
|
|
2016-06-22 17:13:44 +00:00
|
|
|
slice = db.scalar("select cast(col1 as blob) from table1").as(Bytes)
|
2016-01-31 17:03:05 +00:00
|
|
|
slice.to_a.should eq(ary)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-22 19:17:47 +00:00
|
|
|
it "insert/get value date from table" do
|
|
|
|
with_db do |db|
|
|
|
|
value = Time.new(2016, 7, 22, 15, 0, 0, 0)
|
|
|
|
db.exec "create table table1 (col1 #{sqlite_type_for(value)})"
|
|
|
|
db.exec %(insert into table1 values (?)), value
|
|
|
|
|
|
|
|
db.query "select col1 from table1" do |rs|
|
|
|
|
rs.move_next
|
|
|
|
rs.read(Time).should eq(value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-22 03:45:00 +00:00
|
|
|
it "raises on unsupported param types" do
|
|
|
|
with_db do |db|
|
|
|
|
expect_raises Exception, "SQLite3::Statement does not support NotSupportedType params" do
|
|
|
|
db.query "select 1", NotSupportedType.new
|
|
|
|
end
|
|
|
|
# TODO raising exception does not close the connection and pool is exhausted
|
|
|
|
end
|
|
|
|
|
|
|
|
with_db do |db|
|
|
|
|
expect_raises Exception, "SQLite3::Statement does not support NotSupportedType params" do
|
|
|
|
db.exec "select 1", NotSupportedType.new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-31 17:03:05 +00:00
|
|
|
it "gets many rows from table" do
|
2016-01-31 00:14:46 +00:00
|
|
|
with_mem_db do |db|
|
|
|
|
db.exec "create table person (name string, age integer)"
|
|
|
|
db.exec %(insert into person values ("foo", 10))
|
2016-01-31 17:03:05 +00:00
|
|
|
db.exec %(insert into person values ("bar", 20))
|
|
|
|
db.exec %(insert into person values ("baz", 30))
|
2016-01-31 00:14:46 +00:00
|
|
|
|
2016-01-31 17:03:05 +00:00
|
|
|
names = [] of String
|
|
|
|
ages = [] of Int32
|
2016-01-31 00:14:46 +00:00
|
|
|
db.query "select * from person" do |rs|
|
2016-01-31 17:03:05 +00:00
|
|
|
rs.each do
|
|
|
|
names << rs.read(String)
|
|
|
|
ages << rs.read(Int32)
|
|
|
|
end
|
2016-01-31 00:14:46 +00:00
|
|
|
end
|
2016-01-31 17:03:05 +00:00
|
|
|
names.should eq(["foo", "bar", "baz"])
|
|
|
|
ages.should eq([10, 20, 30])
|
2016-01-31 00:14:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "ensures statements are closed" do
|
|
|
|
begin
|
2016-02-04 00:29:19 +00:00
|
|
|
DB.open "sqlite3:#{DB_FILENAME}" do |db|
|
2016-01-31 00:14:46 +00:00
|
|
|
db.exec %(create table if not exists a (i int not null, str text not null);)
|
|
|
|
db.exec %(insert into a (i, str) values (23, "bai bai");)
|
|
|
|
end
|
2016-01-30 00:58:04 +00:00
|
|
|
|
2016-01-31 00:14:46 +00:00
|
|
|
2.times do |i|
|
2016-02-04 00:29:19 +00:00
|
|
|
DB.open "sqlite3:#{DB_FILENAME}" do |db|
|
2016-01-31 00:14:46 +00:00
|
|
|
begin
|
|
|
|
db.query("SELECT i, str FROM a WHERE i = ?", 23) do |rs|
|
|
|
|
rs.move_next
|
|
|
|
break
|
|
|
|
end
|
|
|
|
rescue e : SQLite3::Exception
|
|
|
|
fail("Expected no exception, but got \"#{e.message}\"")
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
db.exec("UPDATE a SET i = ? WHERE i = ?", 23, 23)
|
|
|
|
rescue e : SQLite3::Exception
|
|
|
|
fail("Expected no exception, but got \"#{e.message}\"")
|
|
|
|
end
|
|
|
|
end
|
2016-01-30 00:58:04 +00:00
|
|
|
end
|
2016-01-31 00:14:46 +00:00
|
|
|
ensure
|
|
|
|
File.delete(DB_FILENAME)
|
2016-01-30 00:58:04 +00:00
|
|
|
end
|
|
|
|
end
|
2016-12-14 15:27:43 +00:00
|
|
|
|
|
|
|
describe "transactions" do
|
|
|
|
it "can read inside transaction and rollback after" do
|
|
|
|
with_db do |db|
|
|
|
|
db.exec "create table person (name varchar(25))"
|
|
|
|
db.transaction do |tx|
|
|
|
|
tx.connection.scalar("select count(*) from person").should eq(0)
|
|
|
|
tx.connection.exec "insert into person (name) values (?)", "John Doe"
|
|
|
|
tx.connection.scalar("select count(*) from person").should eq(1)
|
|
|
|
tx.rollback
|
|
|
|
end
|
|
|
|
db.scalar("select count(*) from person").should eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can read inside transaction or after commit" do
|
|
|
|
with_db do |db|
|
|
|
|
db.exec "create table person (name varchar(25))"
|
|
|
|
db.transaction do |tx|
|
|
|
|
tx.connection.scalar("select count(*) from person").should eq(0)
|
|
|
|
tx.connection.exec "insert into person (name) values (?)", "John Doe"
|
|
|
|
tx.connection.scalar("select count(*) from person").should eq(1)
|
|
|
|
# using other connection
|
|
|
|
db.scalar("select count(*) from person").should eq(0)
|
|
|
|
end
|
|
|
|
db.scalar("select count(*) from person").should eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "nested transactions" do
|
|
|
|
it "can read inside transaction and rollback after" do
|
|
|
|
with_db do |db|
|
|
|
|
db.exec "create table person (name varchar(25))"
|
|
|
|
db.transaction do |tx_0|
|
|
|
|
tx_0.connection.scalar("select count(*) from person").should eq(0)
|
|
|
|
tx_0.connection.exec "insert into person (name) values (?)", "John Doe"
|
|
|
|
tx_0.transaction do |tx_1|
|
|
|
|
tx_1.connection.exec "insert into person (name) values (?)", "Sarah"
|
|
|
|
tx_1.connection.scalar("select count(*) from person").should eq(2)
|
|
|
|
tx_1.transaction do |tx_2|
|
|
|
|
tx_2.connection.exec "insert into person (name) values (?)", "Jimmy"
|
|
|
|
tx_2.connection.scalar("select count(*) from person").should eq(3)
|
|
|
|
tx_2.rollback
|
|
|
|
end
|
|
|
|
end
|
|
|
|
tx_0.connection.scalar("select count(*) from person").should eq(2)
|
|
|
|
tx_0.rollback
|
|
|
|
end
|
|
|
|
db.scalar("select count(*) from person").should eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-01-29 19:15:28 +00:00
|
|
|
end
|