Skip to content

Commit

Permalink
Make dataset.limit(nil, nil) reset offset as well as limit (Fixes #571)
Browse files Browse the repository at this point in the history
Note that you should probably use unlimited instead of limit(nil,nil).
  • Loading branch information
jeremyevans committed Oct 23, 2012
1 parent c75082d commit a3680a1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG
@@ -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)
Expand Down
4 changes: 3 additions & 1 deletion lib/sequel/dataset/query.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions spec/core/dataset_spec.rb
Expand Up @@ -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'
Expand Down

0 comments on commit a3680a1

Please sign in to comment.