Permalink
Browse files

Make dataset.limit(nil, nil) reset offset as well as limit (Fixes #571)

Note that you should probably use unlimited instead of limit(nil,nil).
  • Loading branch information...
jeremyevans committed Oct 23, 2012
1 parent c75082d commit a3680a191eee31a54ebe9d221c90132a1764d0ba
Showing with 11 additions and 1 deletion.
  1. +2 −0 CHANGELOG
  2. +3 −1 lib/sequel/dataset/query.rb
  3. +6 −0 spec/core/dataset_spec.rb
View
@@ -1,5 +1,7 @@
=== HEAD
+* Make dataset.limit(nil, nil) reset offset as well as limit (jeremyevans) (#571)
+
* Support IMMEDIATE/EXCLUSIVE/DEFERRED transaction modes on SQLite (Eric Wong)
* Major change in the Database <-> ConnectionPool interface (jeremyevans)
@@ -592,7 +592,7 @@ def join_table(type, table, expr=nil, options={}, &block)
# DB[:items].limit(10...20) # SELECT * FROM items LIMIT 10 OFFSET 10
# DB[:items].limit(10..20) # SELECT * FROM items LIMIT 11 OFFSET 10
# DB[:items].limit(nil, 20) # SELECT * FROM items OFFSET 20
- def limit(l, o = nil)
+ def limit(l, o = (no_offset = true; nil))
return from_self.limit(l, o) if @opts[:sql]
if Range === l
@@ -610,6 +610,8 @@ def limit(l, o = nil)
raise(Error, 'Offsets must be greater than or equal to 0') unless o >= 0
end
opts[:offset] = o
+ elsif !no_offset
+ opts[:offset] = nil
end
clone(opts)
end
@@ -1620,6 +1620,12 @@ def d.to_s; "adsf" end
@dataset.limit(6, Sequel.function(:a) - 1).sql.should == 'SELECT * FROM test LIMIT 6 OFFSET (a() - 1)'
end
+ specify "should be able to reset limit and offset with nil values" do
+ @dataset.limit(6).limit(nil).sql.should == 'SELECT * FROM test'
+ @dataset.limit(6, 1).limit(nil).sql.should == 'SELECT * FROM test OFFSET 1'
+ @dataset.limit(6, 1).limit(nil, nil).sql.should == 'SELECT * FROM test'
+ end
+
specify "should work with fixed sql datasets" do
@dataset.opts[:sql] = 'select * from cccc'
@dataset.limit(6, 10).sql.should == 'SELECT * FROM (select * from cccc) AS t1 LIMIT 6 OFFSET 10'

0 comments on commit a3680a1

Please sign in to comment.