diff options
author | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-04-11 18:24:18 -0700 |
---|---|---|
committer | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-04-11 18:24:18 -0700 |
commit | ae0fe8f6c8c41b5abf8ced6a99b19dacdf8f57eb (patch) | |
tree | 3f3e9a0bc779b489033a4def6dcf31603953fec6 | |
parent | 3967bd524da7b3c0b985c57670ceb7f5d48a0f1b (diff) | |
download | rails-ae0fe8f6c8c41b5abf8ced6a99b19dacdf8f57eb.tar.gz rails-ae0fe8f6c8c41b5abf8ced6a99b19dacdf8f57eb.tar.bz2 rails-ae0fe8f6c8c41b5abf8ced6a99b19dacdf8f57eb.zip |
redesigned the way limit and offset work
- was range now have special 'take' and 'skip' operations. (the terminology comes from linq)
-rw-r--r-- | lib/active_relation/relations.rb | 3 | ||||
-rw-r--r-- | lib/active_relation/relations/compound.rb | 4 | ||||
-rw-r--r-- | lib/active_relation/relations/range.rb | 26 | ||||
-rw-r--r-- | lib/active_relation/relations/relation.rb | 18 | ||||
-rw-r--r-- | lib/active_relation/relations/skip.rb | 18 | ||||
-rw-r--r-- | lib/active_relation/relations/take.rb | 18 | ||||
-rw-r--r-- | spec/active_relation/unit/relations/relation_spec.rb | 6 | ||||
-rw-r--r-- | spec/active_relation/unit/relations/skip_spec.rb (renamed from spec/active_relation/unit/relations/range_spec.rb) | 15 | ||||
-rw-r--r-- | spec/active_relation/unit/relations/take_spec.rb | 32 |
9 files changed, 90 insertions, 50 deletions
diff --git a/lib/active_relation/relations.rb b/lib/active_relation/relations.rb index d914fca094..240c20736e 100644 --- a/lib/active_relation/relations.rb +++ b/lib/active_relation/relations.rb @@ -8,7 +8,8 @@ require 'active_relation/relations/aggregation' require 'active_relation/relations/projection' require 'active_relation/relations/selection' require 'active_relation/relations/order' -require 'active_relation/relations/range' +require 'active_relation/relations/take' +require 'active_relation/relations/skip' require 'active_relation/relations/rename' require 'active_relation/relations/deletion' require 'active_relation/relations/insertion' diff --git a/lib/active_relation/relations/compound.rb b/lib/active_relation/relations/compound.rb index 5778fe9b22..c5af453e4b 100644 --- a/lib/active_relation/relations/compound.rb +++ b/lib/active_relation/relations/compound.rb @@ -4,8 +4,8 @@ module ActiveRelation hash_on :relation - delegate :joins, :selects, :orders, :groupings, :table_sql, :inserts, :limit, - :offset, :name, :alias, :aggregation?, :alias?, :prefix_for, :column_for, + delegate :joins, :selects, :orders, :groupings, :table_sql, :inserts, :take, + :skip, :name, :alias, :aggregation?, :alias?, :prefix_for, :column_for, :engine, :to => :relation diff --git a/lib/active_relation/relations/range.rb b/lib/active_relation/relations/range.rb deleted file mode 100644 index fafdef5902..0000000000 --- a/lib/active_relation/relations/range.rb +++ /dev/null @@ -1,26 +0,0 @@ -module ActiveRelation - class Range < Compound - attr_reader :range - - def initialize(relation, range) - @relation, @range = relation, range - end - - def ==(other) - relation == other.relation and - range == other.range - end - - def limit - range.end - range.begin + 1 - end - - def offset - range.begin - end - - def descend(&block) - Range.new(relation.descend(&block), range) - end - end -end
\ No newline at end of file diff --git a/lib/active_relation/relations/relation.rb b/lib/active_relation/relations/relation.rb index 1b80494cd7..b26fac4e85 100644 --- a/lib/active_relation/relations/relation.rb +++ b/lib/active_relation/relations/relation.rb @@ -35,8 +35,6 @@ module ActiveRelation case index when Symbol, String attribute_for_name(index) - when ::Range - Range.new(self, index) when Attribute, Expression attribute_for_attribute(index) end @@ -57,6 +55,14 @@ module ActiveRelation def order(*attributes) Order.new(self, *attributes) end + + def take(taken) + Take.new(self, taken) + end + + def skip(skipped) + Skip.new(self, skipped) + end def rename(attribute, aliaz) Rename.new(self, attribute => aliaz) @@ -114,8 +120,8 @@ module ActiveRelation ("WHERE #{selects.collect { |s| s.to_sql(Sql::WhereClause.new(engine)) }.join("\n\tAND ")}" unless selects.blank? ), ("ORDER BY #{orders.collect { |o| o.to_sql(Sql::OrderClause.new(engine)) }.join(', ')}" unless orders.blank? ), ("GROUP BY #{groupings.collect(&:to_sql)}" unless groupings.blank? ), - ("LIMIT #{limit}" unless limit.blank? ), - ("OFFSET #{offset}" unless offset.blank? ) + ("LIMIT #{take}" unless take.blank? ), + ("OFFSET #{skip}" unless skip.blank? ) ].compact.join("\n"), self.alias end alias_method :to_s, :to_sql @@ -153,8 +159,8 @@ module ActiveRelation def inserts; [] end def groupings; [] end def joins; nil end - def limit; nil end - def offset; nil end + def take; nil end + def skip; nil end def alias; nil end end end
\ No newline at end of file diff --git a/lib/active_relation/relations/skip.rb b/lib/active_relation/relations/skip.rb new file mode 100644 index 0000000000..f963ee6468 --- /dev/null +++ b/lib/active_relation/relations/skip.rb @@ -0,0 +1,18 @@ +module ActiveRelation + class Skip < Compound + attr_reader :skip + + def initialize(relation, skip) + @relation, @skip = relation, skip + end + + def ==(other) + relation == other.relation and + skip == other.skip + end + + def descend(&block) + Skip.new(relation.descend(&block), skip) + end + end +end
\ No newline at end of file diff --git a/lib/active_relation/relations/take.rb b/lib/active_relation/relations/take.rb new file mode 100644 index 0000000000..9f98207798 --- /dev/null +++ b/lib/active_relation/relations/take.rb @@ -0,0 +1,18 @@ +module ActiveRelation + class Take < Compound + attr_reader :take + + def initialize(relation, take) + @relation, @take = relation, take + end + + def ==(other) + relation == other.relation and + take == other.take + end + + def descend(&block) + Take.new(relation.descend(&block), take) + end + end +end
\ No newline at end of file diff --git a/spec/active_relation/unit/relations/relation_spec.rb b/spec/active_relation/unit/relations/relation_spec.rb index 70bf87271a..d10ae490de 100644 --- a/spec/active_relation/unit/relations/relation_spec.rb +++ b/spec/active_relation/unit/relations/relation_spec.rb @@ -9,12 +9,6 @@ module ActiveRelation end describe '[]' do - describe 'when given a', Range do - it "manufactures a range relation when given a range" do - @relation[1..2].should == Range.new(@relation, 1..2) - end - end - describe 'when given an', Attribute do it "return the attribute congruent to the provided attribute" do @relation[@attribute1].should == @attribute1 diff --git a/spec/active_relation/unit/relations/range_spec.rb b/spec/active_relation/unit/relations/skip_spec.rb index a0207c7342..77f2b8db27 100644 --- a/spec/active_relation/unit/relations/range_spec.rb +++ b/spec/active_relation/unit/relations/skip_spec.rb @@ -1,33 +1,30 @@ require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') module ActiveRelation - describe Range do + describe Skip do before do @relation = Table.new(:users) - @range = 4..9 + @skip = 4 end describe '#qualify' do it "descends" do - Range.new(@relation, @range).qualify.should == Range.new(@relation, @range).descend(&:qualify) + Skip.new(@relation, @skip).qualify.should == Skip.new(@relation, @skip).descend(&:qualify) end end describe '#descend' do it "distributes a block over the relation" do - Range.new(@relation, @range).descend(&:qualify).should == Range.new(@relation.descend(&:qualify), @range) + Skip.new(@relation, @skip).descend(&:qualify).should == Skip.new(@relation.descend(&:qualify), @skip) end end describe '#to_sql' do it "manufactures sql with limit and offset" do - range_size = @range.last - @range.first + 1 - range_start = @range.first - Range.new(@relation, @range).to_s.should be_like(" + Skip.new(@relation, @skip).to_s.should be_like(" SELECT `users`.`id`, `users`.`name` FROM `users` - LIMIT #{range_size} - OFFSET #{range_start} + OFFSET #{@skip} ") end end diff --git a/spec/active_relation/unit/relations/take_spec.rb b/spec/active_relation/unit/relations/take_spec.rb new file mode 100644 index 0000000000..6523ff85f9 --- /dev/null +++ b/spec/active_relation/unit/relations/take_spec.rb @@ -0,0 +1,32 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') + +module ActiveRelation + describe Take do + before do + @relation = Table.new(:users) + @take = 4 + end + + describe '#qualify' do + it "descends" do + Take.new(@relation, @take).qualify.should == Take.new(@relation, @take).descend(&:qualify) + end + end + + describe '#descend' do + it "distributes a block over the relation" do + Take.new(@relation, @take).descend(&:qualify).should == Take.new(@relation.descend(&:qualify), @take) + end + end + + describe '#to_sql' do + it "manufactures sql with limit and offset" do + Take.new(@relation, @take).to_s.should be_like(" + SELECT `users`.`id`, `users`.`name` + FROM `users` + LIMIT #{@take} + ") + end + end + end +end
\ No newline at end of file |