Skip to content

Commit

Permalink
backwards compatibility with sequel
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed May 11, 2010
1 parent 22b0c3d commit 248846a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
19 changes: 16 additions & 3 deletions lib/sqlite3/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,26 @@ def prepare sql
#
# See also #execute2, #query, and #execute_batch for additional ways of
# executing statements.
def execute sql, *bind_vars, &block
def execute sql, bind_vars = [], *args, &block
# FIXME: This is a terrible hack and should be removed but is required
# for older versions of rails
hack = Object.const_defined?(:ActiveRecord) && sql =~ /^PRAGMA index_list/

if bind_vars.nil? || !args.empty?
if args.empty?
bind_vars = []
else
bind_vars = [nil] + args
end

warn(<<-eowarn) if $VERBOSE
#{caller[0]} is calling SQLite3::Database#execute with nil or multiple bind params
without using an array. Please switch to passing bind parameters as an array.
eowarn
end

prepare( sql ) do |stmt|
stmt.bind_params( *bind_vars )
stmt.bind_params(bind_vars)
if block_given?
stmt.each do |row|
if @results_as_hash
Expand Down Expand Up @@ -213,7 +226,7 @@ def query( sql, bind_vars = [], *args )
eowarn
end

result = prepare( sql ).execute( *bind_vars )
result = prepare( sql ).execute( bind_vars )
if block_given?
begin
yield result
Expand Down
8 changes: 8 additions & 0 deletions test/test_database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,5 +267,13 @@ def test_close_with_open_statements
def test_execute_with_empty_bind_params
assert_equal [['foo']], @db.execute("select 'foo'", [])
end

def test_query_with_named_bind_params
assert_equal [['foo']], @db.query("select :n", {'n' => 'foo'}).to_a
end

def test_execute_with_named_bind_params
assert_equal [['foo']], @db.execute("select :n", {'n' => 'foo'})
end
end
end
10 changes: 10 additions & 0 deletions test/test_statement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,15 @@ def test_bind_parameter_count
stmt = SQLite3::Statement.new(@db, "select ?, ?, ?")
assert_equal 3, stmt.bind_parameter_count
end

def test_execute_with_varargs
stmt = @db.prepare('select ?, ?')
assert_equal [[nil, nil]], stmt.execute(nil, nil).to_a
end

def test_execute_with_hash
stmt = @db.prepare('select :n, :h')
assert_equal [[10, nil]], stmt.execute('n' => 10, 'h' => nil).to_a
end
end
end

0 comments on commit 248846a

Please sign in to comment.