mirror of
https://gitea.invidious.io/iv-org/shard-crystal-sqlite3.git
synced 2024-08-15 00:53:26 +00:00
Merge pull request #1 from jhass/ensure_close
Ensure close of statement
This commit is contained in:
commit
b98c31c506
2 changed files with 31 additions and 0 deletions
|
@ -109,4 +109,34 @@ describe Database do
|
|||
it "gets first value" do
|
||||
with_db(&.get_first_value(%(select 1))).should eq(1)
|
||||
end
|
||||
|
||||
it "ensures statements are closed" do
|
||||
begin
|
||||
Database.new(DB_FILENAME) do |db|
|
||||
db.execute(%(create table if not exists a (i int not null, str text not null);))
|
||||
db.execute(%(insert into a (i, str) values (23, "bai bai");))
|
||||
end
|
||||
|
||||
2.times do |i|
|
||||
Database.new(DB_FILENAME) do |db|
|
||||
begin
|
||||
db.query("SELECT i, str FROM a WHERE i = ?", 23) do |rs|
|
||||
rs.next
|
||||
break
|
||||
end
|
||||
rescue e : SQLite3::Exception
|
||||
fail("Expected no exception, but got \"#{e.message}\"")
|
||||
end
|
||||
|
||||
begin
|
||||
db.execute("UPDATE a SET i = ? WHERE i = ?", 23, 23)
|
||||
rescue e : SQLite3::Exception
|
||||
fail("Expected no exception, but got \"#{e.message}\"")
|
||||
end
|
||||
end
|
||||
end
|
||||
ensure
|
||||
File.delete(DB_FILENAME)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -71,6 +71,7 @@ class SQLite3::Statement
|
|||
def execute(binds : Enumerable | Slice(UInt8), &block)
|
||||
result_set = execute(binds)
|
||||
yield result_set
|
||||
ensure
|
||||
close
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue