Skip to content

Commit

Permalink
Merge pull request #512 from alexcwatt/execute-batch-return
Browse files Browse the repository at this point in the history
execute_batch returns result of last statement
  • Loading branch information
flavorjones committed Mar 7, 2024
2 parents 9e87ec0 + 620d912 commit d7d9449
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 @@ -239,13 +239,13 @@ 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.
def execute_batch(sql, bind_vars = [], *args)
sql = sql.strip
result = nil
until sql.empty?
prepare(sql) do |stmt|
unless stmt.closed?
Expand All @@ -254,13 +254,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 d7d9449

Please sign in to comment.