Skip to content

Commit faca48a

Browse files
gerritluislavena
authored andcommitted
modified Database.open/Database.new to optionally accept a block á la File.open
Block will ensure db is closed Signed-off-by: Luis Lavena <luislavena@gmail.com>
1 parent de27913 commit faca48a

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

lib/sqlite3/database.rb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@ module SQLite3
1212
#
1313
# require 'sqlite3'
1414
#
15-
# db = SQLite3::Database.new( "data.db" )
16-
#
17-
# db.execute( "select * from table" ) do |row|
18-
# p row
15+
# SQLite3::Database.new( "data.db" ) do |db|
16+
# db.execute( "select * from table" ) do |row|
17+
# p row
18+
# end
1919
# end
2020
#
21-
# db.close
22-
#
2321
# It wraps the lower-level methods provides by the selected driver, and
2422
# includes the Pragmas module for access to various pragma convenience
2523
# methods.
@@ -69,7 +67,7 @@ def quote( string )
6967
#
7068
# By default, the new database will return result rows as arrays
7169
# (#results_as_hash) and has type translation disabled (#type_translation=).
72-
def initialize( file_name, options={} )
70+
def initialize( file_name, options={} ) # :yields: db
7371
utf16 = options.fetch(:utf16, false)
7472
load_driver( options[:driver] )
7573

@@ -83,6 +81,14 @@ def initialize( file_name, options={} )
8381
@type_translation = options.fetch(:type_translation,false)
8482
@translator = nil
8583
@transaction_active = false
84+
85+
if block_given?
86+
begin
87+
yield self
88+
ensure
89+
self.close
90+
end
91+
end
8692
end
8793

8894
# Return +true+ if the string is a valid (ie, parsable) SQL statement, and

test/test_database.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ def test_new
1111
assert !db.results_as_hash
1212
assert !db.type_translation
1313
end
14+
15+
def test_new_with_block
16+
driver = Driver.new
17+
driver.expects(:open).once.with('foo.db', false).returns([0, 'cookie'])
18+
Driver.stubs(:new).returns(driver)
19+
returned_db = SQLite3::Database.new( "foo.db", :driver => Driver ) do |db|
20+
assert !db.closed?
21+
assert !db.results_as_hash
22+
assert !db.type_translation
23+
end
24+
assert returned_db.closed?
25+
end
1426

1527
def test_open
1628
driver = Driver.new
@@ -22,6 +34,18 @@ def test_open
2234
assert !db.type_translation
2335
end
2436

37+
def test_open_with_block
38+
driver = Driver.new
39+
driver.expects(:open).once.with('foo.db', false).returns([0, 'cookie'])
40+
Driver.stubs(:new).returns(driver)
41+
returned_db = SQLite3::Database.open( "foo.db", :driver => Driver ) do |db|
42+
assert !db.closed?
43+
assert !db.results_as_hash
44+
assert !db.type_translation
45+
end
46+
assert returned_db.closed?
47+
end
48+
2549
def test_with_type_translation
2650
db = SQLite3::Database.open( "foo.db", :driver => Driver,
2751
:type_translation => true )

0 commit comments

Comments
 (0)