Skip to content

Commit d7d9449

Browse files
authored
Merge pull request #512 from alexcwatt/execute-batch-return
execute_batch returns result of last statement
2 parents 9e87ec0 + 620d912 commit d7d9449

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
@@ -239,13 +239,13 @@ def execute2(sql, *bind_vars)
239239
# in turn. The same bind parameters, if given, will be applied to each
240240
# statement.
241241
#
242-
# This always returns +nil+, making it unsuitable for queries that return
243-
# rows.
242+
# This always returns the result of the last statement.
244243
#
245244
# See also #execute_batch2 for additional ways of
246245
# executing statements.
247246
def execute_batch(sql, bind_vars = [], *args)
248247
sql = sql.strip
248+
result = nil
249249
until sql.empty?
250250
prepare(sql) do |stmt|
251251
unless stmt.closed?
@@ -254,13 +254,13 @@ def execute_batch(sql, bind_vars = [], *args)
254254
if bind_vars.length == stmt.bind_parameter_count
255255
stmt.bind_params(bind_vars)
256256
end
257-
stmt.step
257+
result = stmt.step
258258
end
259259
sql = stmt.remainder.strip
260260
end
261261
end
262-
# FIXME: we should not return `nil` as a success return value
263-
nil
262+
263+
result
264264
end
265265

266266
# 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)