1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

more to_sql

This commit is contained in:
Nick Kallen 2007-12-31 13:25:32 -08:00
parent 9a976c1bbc
commit 0496a59e1f
6 changed files with 59 additions and 4 deletions

View file

@ -8,4 +8,12 @@ class OrderRelation < Relation
def ==(other) def ==(other)
relation == other.relation and attributes.eql?(other.attributes) relation == other.relation and attributes.eql?(other.attributes)
end end
def to_sql(builder = SelectBuilder.new)
relation.to_sql(builder).call do
attributes.each do |attribute|
order_by attribute.to_sql(self)
end
end
end
end end

View file

@ -8,4 +8,10 @@ class ProjectionRelation < Relation
def ==(other) def ==(other)
relation == other.relation and attributes.eql?(other.attributes) relation == other.relation and attributes.eql?(other.attributes)
end end
def to_sql(builder = SelectBuilder.new)
relation.to_sql(builder).call do
select attributes.collect { |a| a.to_sql(self) }
end
end
end end

View file

@ -8,4 +8,11 @@ class RangeRelation < Relation
def ==(other) def ==(other)
relation == other.relation and range == other.range relation == other.relation and range == other.range
end end
def to_sql(builder = SelectBuilder.new)
relation.to_sql(builder).call do
limit range.last - range.first + 1
offset range.first
end
end
end end

View file

@ -8,11 +8,22 @@ describe OrderRelation do
@attribute2 = @relation2[:bar] @attribute2 = @relation2[:bar]
end end
describe OrderRelation, '==' do describe '==' do
it "obtains if the relation and attributes are identical" do it "obtains if the relation and attributes are identical" do
OrderRelation.new(@relation1, @attribute1, @attribute2).should == OrderRelation.new(@relation1, @attribute1, @attribute2) OrderRelation.new(@relation1, @attribute1, @attribute2).should == OrderRelation.new(@relation1, @attribute1, @attribute2)
OrderRelation.new(@relation1, @attribute1).should_not == OrderRelation.new(@relation2, @attribute1) OrderRelation.new(@relation1, @attribute1).should_not == OrderRelation.new(@relation2, @attribute1)
OrderRelation.new(@relation1, @attribute1, @attribute2).should_not == OrderRelation.new(@relation1, @attribute2, @attribute1) OrderRelation.new(@relation1, @attribute1, @attribute2).should_not == OrderRelation.new(@relation1, @attribute2, @attribute1)
end end
end end
describe '#to_s' do
it "manufactures sql with an order clause" do
OrderRelation.new(@relation1, @attribute1).to_sql.should == SelectBuilder.new do
select :*
from :foo
order_by 'foo.foo'
end
end
end
end end

View file

@ -8,11 +8,20 @@ describe ProjectionRelation do
@attribute2 = @relation2[:bar] @attribute2 = @relation2[:bar]
end end
describe ProjectionRelation, '==' do describe '==' do
it "obtains if the relations and attributes are identical" do it "obtains if the relations and attributes are identical" do
ProjectionRelation.new(@relation1, @attribute1, @attribute2).should == ProjectionRelation.new(@relation1, @attribute1, @attribute2) ProjectionRelation.new(@relation1, @attribute1, @attribute2).should == ProjectionRelation.new(@relation1, @attribute1, @attribute2)
ProjectionRelation.new(@relation1, @attribute1).should_not == ProjectionRelation.new(@relation2, @attribute1) ProjectionRelation.new(@relation1, @attribute1).should_not == ProjectionRelation.new(@relation2, @attribute1)
ProjectionRelation.new(@relation1, @attribute1).should_not == ProjectionRelation.new(@relation1, @attribute2) ProjectionRelation.new(@relation1, @attribute1).should_not == ProjectionRelation.new(@relation1, @attribute2)
end end
end end
describe '#to_sql' do
it "manufactures sql with a limited select clause" do
ProjectionRelation.new(@relation1, @attribute1).to_sql.should == SelectBuilder.new do
select 'foo.foo'
from :foo
end
end
end
end end

View file

@ -5,14 +5,28 @@ describe RangeRelation do
@relation1 = TableRelation.new(:foo) @relation1 = TableRelation.new(:foo)
@relation2 = TableRelation.new(:bar) @relation2 = TableRelation.new(:bar)
@range1 = 1..2 @range1 = 1..2
@range2 = Time.now..2.days.from_now @range2 = 4..9
end end
describe RangeRelation, '==' do describe '==' do
it "obtains if the relation and range are identical" do it "obtains if the relation and range are identical" do
RangeRelation.new(@relation1, @range1).should == RangeRelation.new(@relation1, @range1) RangeRelation.new(@relation1, @range1).should == RangeRelation.new(@relation1, @range1)
RangeRelation.new(@relation1, @range1).should_not == RangeRelation.new(@relation2, @range1) RangeRelation.new(@relation1, @range1).should_not == RangeRelation.new(@relation2, @range1)
RangeRelation.new(@relation1, @range1).should_not == RangeRelation.new(@relation1, @range2) RangeRelation.new(@relation1, @range1).should_not == RangeRelation.new(@relation1, @range2)
end end
end end
describe '#to_sql' do
it "manufactures sql with limit and offset" do
range_size = @range2.last - @range2.first + 1
range_start = @range2.first
RangeRelation.new(@relation1, @range2).to_sql.to_s.should == SelectBuilder.new do
select :*
from :foo
limit range_size
offset range_start
end.to_s
end
end
end end