Skip to content

Commit 0ef9a94

Browse files
committed
make SQLite3::Database.open return the block result instead of the db instance
To make it follow common pattern, like for File: result = File.open('/dev/zero') do |f| f.read(10) end See discussion in #414
1 parent 2611034 commit 0ef9a94

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

lib/sqlite3/database.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,22 @@ class Database
3939

4040
class << self
4141

42-
alias :open :new
42+
# Without block works exactly as new.
43+
# With block, like new closes the database at the end, but unlike new
44+
# returns the result of the block instead of the database instance.
45+
def open( *args )
46+
database = new(*args)
47+
48+
if block_given?
49+
begin
50+
yield database
51+
ensure
52+
database.close
53+
end
54+
else
55+
database
56+
end
57+
end
4358

4459
# Quotes the given string, making it safe to use in an SQL statement.
4560
# It replaces all instances of the single-quote character with two

test/test_database.rb

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,25 @@ def test_execute_batch2
198198

199199
def test_new
200200
db = SQLite3::Database.new(':memory:')
201-
assert db
201+
assert_instance_of(SQLite3::Database, db)
202202
ensure
203203
db.close if db
204204
end
205205

206+
def test_open
207+
db = SQLite3::Database.open(':memory:')
208+
assert_instance_of(SQLite3::Database, db)
209+
ensure
210+
db.close if db
211+
end
212+
213+
def test_open_returns_block_result
214+
result = SQLite3::Database.open(':memory:') do |db|
215+
:foo
216+
end
217+
assert_equal :foo, result
218+
end
219+
206220
def test_new_yields_self
207221
thing = nil
208222
SQLite3::Database.new(':memory:') do |db|
@@ -211,6 +225,14 @@ def test_new_yields_self
211225
assert_instance_of(SQLite3::Database, thing)
212226
end
213227

228+
def test_open_yields_self
229+
thing = nil
230+
SQLite3::Database.open(':memory:') do |db|
231+
thing = db
232+
end
233+
assert_instance_of(SQLite3::Database, thing)
234+
end
235+
214236
def test_new_with_options
215237
# determine if Ruby is running on Big Endian platform
216238
utf16 = ([1].pack("I") == [1].pack("N")) ? "UTF-16BE" : "UTF-16LE"
@@ -221,7 +243,7 @@ def test_new_with_options
221243
db = SQLite3::Database.new(Iconv.conv(utf16, 'UTF-8', ':memory:'),
222244
:utf16 => true)
223245
end
224-
assert db
246+
assert_instance_of(SQLite3::Database, db)
225247
ensure
226248
db.close if db
227249
end
@@ -241,6 +263,15 @@ def test_block_closes_self
241263
assert thing.closed?
242264
end
243265

266+
def test_open_with_block_closes_self
267+
thing = nil
268+
SQLite3::Database.open(':memory:') do |db|
269+
thing = db
270+
assert !thing.closed?
271+
end
272+
assert thing.closed?
273+
end
274+
244275
def test_block_closes_self_even_raised
245276
thing = nil
246277
begin
@@ -253,6 +284,18 @@ def test_block_closes_self_even_raised
253284
assert thing.closed?
254285
end
255286

287+
def test_open_with_block_closes_self_even_raised
288+
thing = nil
289+
begin
290+
SQLite3::Database.open(':memory:') do |db|
291+
thing = db
292+
raise
293+
end
294+
rescue
295+
end
296+
assert thing.closed?
297+
end
298+
256299
def test_prepare
257300
db = SQLite3::Database.new(':memory:')
258301
stmt = db.prepare('select "hello world"')

0 commit comments

Comments
 (0)