Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

execute_batch returns result of last statement #512

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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