Skip to content

Commit 620d912

Browse files
committed
execute_batch returns result of last statement
1 parent 62dc6e7 commit 620d912

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

lib/sqlite3/database.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,7 @@ def execute2(sql, *bind_vars)
251251
# in turn. The same bind parameters, if given, will be applied to each
252252
# statement.
253253
#
254-
# This always returns +nil+, making it unsuitable for queries that return
255-
# rows.
254+
# This always returns the result of the last statement.
256255
#
257256
# See also #execute_batch2 for additional ways of
258257
# executing statements.
@@ -279,6 +278,7 @@ def execute_batch(sql, bind_vars = [], *args)
279278
end
280279

281280
sql = sql.strip
281+
result = nil
282282
until sql.empty?
283283
prepare(sql) do |stmt|
284284
unless stmt.closed?
@@ -287,13 +287,13 @@ def execute_batch(sql, bind_vars = [], *args)
287287
if bind_vars.length == stmt.bind_parameter_count
288288
stmt.bind_params(bind_vars)
289289
end
290-
stmt.step
290+
result = stmt.step
291291
end
292292
sql = stmt.remainder.strip
293293
end
294294
end
295-
# FIXME: we should not return `nil` as a success return value
296-
nil
295+
296+
result
297297
end
298298

299299
# Executes all SQL statements in the given string. By contrast, the other

test/test_database.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,21 @@ def test_changes
145145
end
146146

147147
def test_batch_last_comment_is_processed
148-
# FIXME: nil as a successful return value is kinda dumb
149148
assert_nil @db.execute_batch <<-EOSQL
150149
CREATE TABLE items (id integer PRIMARY KEY AUTOINCREMENT);
151150
-- omg
152151
EOSQL
153152
end
154153

154+
def test_batch_last_expression_value_is_returned
155+
batch = <<-EOSQL
156+
CREATE TABLE items (id integer PRIMARY KEY AUTOINCREMENT);
157+
SELECT COUNT(*) FROM items;
158+
EOSQL
159+
160+
assert_equal [0], @db.execute_batch(batch)
161+
end
162+
155163
def test_execute_batch2
156164
@db.results_as_hash = true
157165
return_value = @db.execute_batch2 <<-EOSQL

0 commit comments

Comments
 (0)