Skip to content

Commit

Permalink
fix: raise an exception if bind parameters aren't an array
Browse files Browse the repository at this point in the history
Make sure Database#execute, #query, and #execute_batch raise an
ArgumentError to avoid silent problems.

This should have been done in ae12904
  • Loading branch information
flavorjones committed Apr 19, 2024
1 parent 59eee1d commit f61da5e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/sqlite3/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def filename db_name = "main"
#
# See also #execute2, #query, and #execute_batch for additional ways of
# executing statements.
def execute sql, bind_vars = [], *args, &block
def execute sql, bind_vars = [], &block
prepare(sql) do |stmt|
stmt.bind_params(bind_vars)
stmt = build_result_set stmt
Expand Down Expand Up @@ -243,7 +243,7 @@ def execute2(sql, *bind_vars)
#
# See also #execute_batch2 for additional ways of
# executing statements.
def execute_batch(sql, bind_vars = [], *args)
def execute_batch(sql, bind_vars = [])
sql = sql.strip
result = nil
until sql.empty?
Expand Down Expand Up @@ -298,7 +298,7 @@ def execute_batch2(sql, &block)
# returned, or you could have problems with locks on the table. If called
# with a block, +close+ will be invoked implicitly when the block
# terminates.
def query(sql, bind_vars = [], *args)
def query(sql, bind_vars = [])
result = prepare(sql).execute(bind_vars)
if block_given?
begin
Expand Down
14 changes: 14 additions & 0 deletions test/test_statement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -480,5 +480,19 @@ def test_memused

stmt.close
end

def test_raise_if_bind_params_not_an_array
assert_raises(ArgumentError) do
@db.execute "SELECT * from table1 where a = ? and b = ?", 1, 2
end

assert_raises(ArgumentError) do
@db.query "SELECT * from table1 where a = ? and b = ?", 1, 2
end

assert_raises(ArgumentError) do
@db.execute_batch "SELECT * from table1 where a = ? and b = ?", 1, 2
end
end
end
end

0 comments on commit f61da5e

Please sign in to comment.