Skip to content

Commit

Permalink
Use trailing ROWS modifier instead of FIRST x SKIP y syntax to enable…
Browse files Browse the repository at this point in the history
… 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
brent committed Jan 7, 2011
1 parent ea280ab commit 770a929
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/active_record/connection_adapters/fb_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions test/limits_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 770a929

Please sign in to comment.