public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Search Repo:
Merge 5876 to release.


git-svn-id: 
http://svn-commit.rubyonrails.org/rails/branches/1-2-stable@6122 
5ecf4fe2-1ee6-0310-87b1-e25e094e27de
NZKoz (author)
Sun Feb 04 13:30:59 -0800 2007
commit  41108fec89da225aac3311e284432591e892cfb5
tree    e23eaf76070246e175703d65b5e7ccd0ee12e9a8
parent  c4d8d97e9cb7261e8150b952e8eab3c15d4ad635
...
1
2
 
 
 
3
4
5
...
1
2
3
4
5
6
7
8
0
@@ -1,5 +1,8 @@
0
 *SVN*
0
 
0
+* Pass a range in :conditions to use the SQL BETWEEN operator. #6974 [dcmanges]
0
+ Student.find(:all, :conditions => { :grade => 9..12 })
0
+
0
 * Don't create instance writer methods for class attributes. [Rick]
0
 
0
 * When dealing with SQLite3, use the table_info pragma helper, so that the bindings can do some translation for when sqlite3 breaks incompatibly between point releases. [Jamis Buck]
...
84
85
86
87
 
88
89
90
91
...
120
121
122
 
123
 
 
124
125
126
...
1264
1265
1266
 
1267
1268
1269
1270
...
1392
1393
1394
 
 
1395
1396
1397
1398
1399
1400
 
1401
1402
1403
...
1431
1432
1433
 
 
 
 
 
 
 
1434
1435
1436
...
84
85
86
 
87
88
89
90
91
...
120
121
122
123
124
125
126
127
128
129
...
1267
1268
1269
1270
1271
1272
1273
1274
...
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
 
1406
1407
1408
1409
...
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
0
@@ -84,7 +84,7 @@
0
   # Conditions can either be specified as a string, array, or hash representing the WHERE-part of an SQL statement.
0
   # The array form is to be used when the condition input is tainted and requires sanitization. The string form can
0
   # be used for statements that don't involve tainted data. The hash form works much like the array form, except
0
- # only equality is possible. Examples:
0
+ # only equality and range is possible. Examples:
0
   #
0
   # class User < ActiveRecord::Base
0
   # def self.authenticate_unsafely(user_name, password)
0
0
@@ -120,7 +120,10 @@
0
   # Student.find(:all, :conditions => { :first_name => "Harvey", :status => 1 })
0
   # Student.find(:all, :conditions => params[:student])
0
   #
0
+ # A range may be used in the hash to use the SQL BETWEEN operator:
0
   #
0
+ # Student.find(:all, :conditions => { :grade => 9..12 })
0
+ #
0
   # == Overwriting default accessors
0
   #
0
   # All column values are automatically available through basic accessors on the Active Record object, but some times you
0
@@ -1264,6 +1267,7 @@
0
           case argument
0
             when nil then "IS ?"
0
             when Array then "IN (?)"
0
+ when Range then "BETWEEN ? AND ?"
0
             else "= ?"
0
           end
0
         end
0
0
@@ -1392,12 +1396,14 @@
0
         # # => "name='foo''bar' and group_id= 4"
0
         # { :status => nil, :group_id => [1,2,3] }
0
         # # => "status IS NULL and group_id IN (1,2,3)"
0
+ # { :age => 13..18 }
0
+ # # => "age BETWEEN 13 AND 18"
0
         def sanitize_sql_hash(attrs)
0
           conditions = attrs.map do |attr, value|
0
             "#{table_name}.#{connection.quote_column_name(attr)} #{attribute_condition(value)}"
0
           end.join(' AND ')
0
 
0
- replace_bind_variables(conditions, attrs.values)
0
+ replace_bind_variables(conditions, expand_range_bind_variables(attrs.values))
0
         end
0
 
0
         # Accepts an array of conditions. The array has each value
0
@@ -1431,6 +1437,13 @@
0
               raise PreparedStatementInvalid, "missing value for :#{match} in #{statement}"
0
             end
0
           end
0
+ end
0
+
0
+ def expand_range_bind_variables(bind_vars) #:nodoc:
0
+ bind_vars.each_with_index do |var, index|
0
+ bind_vars[index, 1] = [var.first, var.last] if var.is_a?(Range)
0
+ end
0
+ bind_vars
0
         end
0
 
0
         def quote_bound_value(value) #:nodoc:
...
136
137
138
 
 
 
 
 
 
 
 
 
 
139
140
141
...
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
0
@@ -136,6 +136,16 @@
0
     assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :approved => true }) }
0
   end
0
   
0
+ def test_find_on_hash_conditions_with_range
0
+ assert_equal [1,2], Topic.find(:all, :conditions => { :id => 1..2 }).map(&:id).sort
0
+ assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :id => 2..3 }) }
0
+ end
0
+
0
+ def test_find_on_hash_conditions_with_multiple_ranges
0
+ assert_equal [1,2,3], Comment.find(:all, :conditions => { :id => 1..3, :post_id => 1..2 }).map(&:id).sort
0
+ assert_equal [1], Comment.find(:all, :conditions => { :id => 1..1, :post_id => 1..10 }).map(&:id).sort
0
+ end
0
+
0
   def test_find_on_multiple_hash_conditions
0
     assert Topic.find(1, :conditions => { :author_name => "David", :title => "The First Topic", :replies_count => 1, :approved => false })
0
     assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :author_name => "David", :title => "The First Topic", :replies_count => 1, :approved => true }) }

Comments

    No one has commented yet.