From e8630d5f04e926a3fa152c78bc629d85c5cc573d Mon Sep 17 00:00:00 2001 From: Allen Fair Date: Thu, 11 Sep 2014 11:27:59 -0400 Subject: [PATCH] Fixes #13 SQL Syntax Errors referencing a $1 Found resolution here: https://github.com/rails/rails/issues/15331 --- .../relation/cursor_iterators.rb | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/postgresql_cursor/active_record/relation/cursor_iterators.rb b/lib/postgresql_cursor/active_record/relation/cursor_iterators.rb index 6b55a71..64898ab 100644 --- a/lib/postgresql_cursor/active_record/relation/cursor_iterators.rb +++ b/lib/postgresql_cursor/active_record/relation/cursor_iterators.rb @@ -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 @@ -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 @@ -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