Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use trailing ROWS modifier instead of FIRST x SKIP y syntax to enable…

… updates with limits.

git-svn-id: https://svn.ttiltd.com/svn/dev.rb/trunk/libraries/fb_adapter2@10046 e6efdd46-4312-0410-9032-dd8a18d5a4db
  • Loading branch information...
commit 770a929d9486f716579d3832b44885cd6d903d44 1 parent ea280ab
brent authored
View
11 lib/active_record/connection_adapters/fb_adapter.rb
@@ -356,11 +356,14 @@ def add_lock!(sql, options) # :nodoc:
end
def add_limit_offset!(sql, options) # :nodoc:
- if options[:limit]
- limit_string = "FIRST #{options[:limit]}"
- limit_string << " SKIP #{options[:offset]}" if options[:offset]
- sql.sub!(/\A(\s*SELECT\s)/i, '\&' + limit_string + ' ')
+ if limit = options[:limit]
+ if offset = options[:offset]
+ sql << " ROWS #{offset.to_i + 1} TO #{offset.to_i + limit.to_i}"
+ else
+ sql << " ROWS #{limit.to_i}"
+ end
end
+ sql
end
# Returns the next sequence value from a sequence generator. Not generally
View
21 test/limits_test.rb
@@ -21,4 +21,25 @@ def test_select_with_limit_and_offset
assert_equal (10..19).to_a, Foo.all(:limit => 10, :offset => 10).map(&:v)
assert_equal (25..29).to_a, Foo.all(:limit => 40, :offset => 25).map(&:v)
end
+
+ def test_update_with_limit
+ assert_equal 1, Foo.count(:conditions => "V = 7")
+ Foo.update_all("V = 7", nil, :limit => 5)
+ assert_equal 6, Foo.count(:conditions => "V = 7")
+ assert_equal [7,7,7,7,7,5,6,7,8,9], Foo.all(:limit => 10).map(&:v)
+ end
+
+ def test_update_with_limit_and_offset
+ assert_equal 1, Foo.count(:conditions => "V = 7")
+ Foo.update_all("V = 7", nil, :limit => 3, :offset => 2)
+ assert_equal 4, Foo.count(:conditions => "V = 7")
+ assert_equal [0,1,7,7,7,5,6,7,8,9], Foo.all(:limit => 10).map(&:v)
+ end
+
+ def test_update_with_limit_and_offset_ordered_desc
+ assert_equal 1, Foo.count(:conditions => "V = 7")
+ Foo.update_all("V = 7", nil, :limit => 3, :offset => 2, :order => "ID DESC")
+ assert_equal 4, Foo.count(:conditions => "V = 7")
+ assert_equal [29,28,7,7,7,24,23,22,21,20], Foo.all(:limit => 10, :order => "ID DESC").map(&:v)
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.