Skip to content
This repository
Browse code

Quote find_in_batches ORDER BY clause [#6620 state:resolved]

  • Loading branch information...
commit 555d0163897601010ab1305f41ed393ec517b61e 1 parent a9dafbb
Andrew White authored March 27, 2011
13  activerecord/lib/active_record/attribute_methods/primary_key.rb
@@ -17,6 +17,11 @@ def primary_key
17 17
           @primary_key ||= reset_primary_key
18 18
         end
19 19
 
  20
+        # Returns a quoted version of the primary key name, used to construct SQL statements.
  21
+        def quoted_primary_key
  22
+          @quoted_primary_key ||= connection.quote_column_name(primary_key)
  23
+        end
  24
+
20 25
         def reset_primary_key #:nodoc:
21 26
           key = self == base_class ? get_primary_key(base_class.name) :
22 27
             base_class.primary_key
@@ -43,7 +48,12 @@ def get_primary_key(base_name) #:nodoc:
43 48
         end
44 49
 
45 50
         attr_accessor :original_primary_key
46  
-        attr_writer :primary_key
  51
+        
  52
+        # Attribute writer for the primary key column
  53
+        def primary_key=(value)
  54
+          @quoted_primary_key = nil
  55
+          @primary_key = value
  56
+        end
47 57
 
48 58
         # Sets the name of the primary key column to use to the given value,
49 59
         # or (if the value is nil or false) to the value returned by the given
@@ -53,6 +63,7 @@ def get_primary_key(base_name) #:nodoc:
53 63
         #     set_primary_key "sysid"
54 64
         #   end
55 65
         def set_primary_key(value = nil, &block)
  66
+          @quoted_primary_key = nil
56 67
           @primary_key ||= ''
57 68
           self.original_primary_key = @primary_key
58 69
           value &&= value.to_s
2  activerecord/lib/active_record/relation.rb
@@ -12,7 +12,7 @@ class Relation
12 12
 
13 13
     # These are explicitly delegated to improve performance (avoids method_missing)
14 14
     delegate :to_xml, :to_yaml, :length, :collect, :map, :each, :all?, :include?, :to => :to_a
15  
-    delegate :table_name, :primary_key, :to => :klass
  15
+    delegate :table_name, :quoted_table_name, :primary_key, :quoted_primary_key, :to => :klass
16 16
 
17 17
     attr_reader :table, :klass, :loaded
18 18
     attr_accessor :extensions
2  activerecord/lib/active_record/relation/batches.rb
@@ -83,7 +83,7 @@ def find_in_batches(options = {})
83 83
     private
84 84
 
85 85
     def batch_order
86  
-      "#{table_name}.#{primary_key} ASC"
  86
+      "#{quoted_table_name}.#{quoted_primary_key} ASC"
87 87
     end
88 88
   end
89 89
 end
10  activerecord/test/cases/batches_test.rb
@@ -83,4 +83,14 @@ def test_find_in_batches_shouldnt_excute_query_unless_needed
83 83
       Post.find_in_batches(:batch_size => post_count + 1) {|batch| assert_kind_of Array, batch }
84 84
     end
85 85
   end
  86
+
  87
+  def test_find_in_batches_should_quote_batch_order
  88
+    c = Post.connection
  89
+    assert_sql(/ORDER BY #{c.quote_table_name('posts')}.#{c.quote_column_name('id')}/) do
  90
+      Post.find_in_batches(:batch_size => 1) do |batch|
  91
+        assert_kind_of Array, batch
  92
+        assert_kind_of Post, batch.first
  93
+      end
  94
+    end
  95
+  end
86 96
 end
9  activerecord/test/cases/primary_keys_test.rb
@@ -136,4 +136,13 @@ def test_primary_key_returns_nil_if_it_does_not_exist
136 136
       assert_nil ActiveRecord::Base.connection.primary_key('developers_projects')
137 137
     end
138 138
   end
  139
+
  140
+  def test_quoted_primary_key_after_set_primary_key
  141
+    k = Class.new( ActiveRecord::Base )
  142
+    assert_equal k.connection.quote_column_name("id"), k.quoted_primary_key
  143
+    k.primary_key = "foo"
  144
+    assert_equal k.connection.quote_column_name("foo"), k.quoted_primary_key
  145
+    k.set_primary_key "bar"
  146
+    assert_equal k.connection.quote_column_name("bar"), k.quoted_primary_key
  147
+  end
139 148
 end

0 notes on commit 555d016

Please sign in to comment.
Something went wrong with that request. Please try again.