Skip to content

Commit

Permalink
Fixes afair#13 SQL Syntax Errors referencing a $1
Browse files Browse the repository at this point in the history
Found resolution here:

  rails/rails#15331
  • Loading branch information
afair committed Sep 11, 2014
1 parent 6ce2dc6 commit e8630d5
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/postgresql_cursor/active_record/relation/cursor_iterators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module CursorIterators
# Returns the number of rows yielded to the block
def each_row(options={}, &block)
options = {:connection => self.connection}.merge(options)
cursor = PostgreSQLCursor::Cursor.new(to_sql, options)
cursor = PostgreSQLCursor::Cursor.new(to_unprepared_sql, options)
return cursor.each_row(&block) if block_given?
cursor
end
Expand All @@ -37,7 +37,7 @@ def each_row(options={}, &block)
# Returns the number of rows yielded to the block
def each_instance(options={}, &block)
options = {:connection => self.connection}.merge(options)
cursor = PostgreSQLCursor::Cursor.new(to_sql, options)
cursor = PostgreSQLCursor::Cursor.new(to_unprepared_sql, options)
return cursor.each_instance(self, &block) if block_given?
cursor.iterate_type(self)
end
Expand All @@ -58,6 +58,21 @@ def pluck_instances(*cols)
end
alias :pluck_instance :pluck_instances

private

# Returns sql string like #to_sql, but with bind parameters interpolated.
# ActiveRecord sets up query as prepared statements with bind variables.
# Cursors will prepare statements regardless.
def to_unprepared_sql
if self.connection.respond_to?(:unprepared_statement)
self.connection.unprepared_statement do
to_sql
end
else
to_sql
end
end

end
end
end
Expand Down

0 comments on commit e8630d5

Please sign in to comment.