Skip to content

Commit

Permalink
execute_batch returns result of last statement
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcwatt committed Feb 28, 2024
1 parent 62dc6e7 commit 620d912
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
10 changes: 5 additions & 5 deletions lib/sqlite3/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,7 @@ def execute2(sql, *bind_vars)
# in turn. The same bind parameters, if given, will be applied to each
# statement.
#
# This always returns +nil+, making it unsuitable for queries that return
# rows.
# This always returns the result of the last statement.
#
# See also #execute_batch2 for additional ways of
# executing statements.
Expand All @@ -279,6 +278,7 @@ def execute_batch(sql, bind_vars = [], *args)
end

sql = sql.strip
result = nil
until sql.empty?
prepare(sql) do |stmt|
unless stmt.closed?
Expand All @@ -287,13 +287,13 @@ def execute_batch(sql, bind_vars = [], *args)
if bind_vars.length == stmt.bind_parameter_count
stmt.bind_params(bind_vars)
end
stmt.step
result = stmt.step
end
sql = stmt.remainder.strip
end
end
# FIXME: we should not return `nil` as a success return value
nil

result
end

# Executes all SQL statements in the given string. By contrast, the other
Expand Down
10 changes: 9 additions & 1 deletion test/test_database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,21 @@ def test_changes
end

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

def test_batch_last_expression_value_is_returned
batch = <<-EOSQL
CREATE TABLE items (id integer PRIMARY KEY AUTOINCREMENT);
SELECT COUNT(*) FROM items;
EOSQL

assert_equal [0], @db.execute_batch(batch)
end

def test_execute_batch2
@db.results_as_hash = true
return_value = @db.execute_batch2 <<-EOSQL
Expand Down

0 comments on commit 620d912

Please sign in to comment.