diff --git a/lib/sql_algebra/relations/order_relation.rb b/lib/sql_algebra/relations/order_relation.rb index 8d07c58d64..e3c29dcc27 100644 --- a/lib/sql_algebra/relations/order_relation.rb +++ b/lib/sql_algebra/relations/order_relation.rb @@ -8,4 +8,12 @@ class OrderRelation < Relation def ==(other) relation == other.relation and attributes.eql?(other.attributes) 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 \ No newline at end of file diff --git a/lib/sql_algebra/relations/projection_relation.rb b/lib/sql_algebra/relations/projection_relation.rb index caebfc1b62..ca5f0bfca4 100644 --- a/lib/sql_algebra/relations/projection_relation.rb +++ b/lib/sql_algebra/relations/projection_relation.rb @@ -8,4 +8,10 @@ class ProjectionRelation < Relation def ==(other) relation == other.relation and attributes.eql?(other.attributes) end + + def to_sql(builder = SelectBuilder.new) + relation.to_sql(builder).call do + select attributes.collect { |a| a.to_sql(self) } + end + end end \ No newline at end of file diff --git a/lib/sql_algebra/relations/range_relation.rb b/lib/sql_algebra/relations/range_relation.rb index fd7e2898fa..9225d5615b 100644 --- a/lib/sql_algebra/relations/range_relation.rb +++ b/lib/sql_algebra/relations/range_relation.rb @@ -8,4 +8,11 @@ class RangeRelation < Relation def ==(other) relation == other.relation and range == other.range 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 \ No newline at end of file diff --git a/spec/relations/order_relation_spec.rb b/spec/relations/order_relation_spec.rb index 362544d0d1..4f7a18fc8e 100644 --- a/spec/relations/order_relation_spec.rb +++ b/spec/relations/order_relation_spec.rb @@ -8,11 +8,22 @@ describe OrderRelation do @attribute2 = @relation2[:bar] end - describe OrderRelation, '==' do + describe '==' 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).should_not == OrderRelation.new(@relation2, @attribute1) OrderRelation.new(@relation1, @attribute1, @attribute2).should_not == OrderRelation.new(@relation1, @attribute2, @attribute1) 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 \ No newline at end of file diff --git a/spec/relations/projection_relation_spec.rb b/spec/relations/projection_relation_spec.rb index f802a2e293..ba5620dcde 100644 --- a/spec/relations/projection_relation_spec.rb +++ b/spec/relations/projection_relation_spec.rb @@ -8,11 +8,20 @@ describe ProjectionRelation do @attribute2 = @relation2[:bar] end - describe ProjectionRelation, '==' do + describe '==' 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).should_not == ProjectionRelation.new(@relation2, @attribute1) ProjectionRelation.new(@relation1, @attribute1).should_not == ProjectionRelation.new(@relation1, @attribute2) 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 \ No newline at end of file diff --git a/spec/relations/range_relation_spec.rb b/spec/relations/range_relation_spec.rb index 2a1cd1d070..fc7094c873 100644 --- a/spec/relations/range_relation_spec.rb +++ b/spec/relations/range_relation_spec.rb @@ -5,14 +5,28 @@ describe RangeRelation do @relation1 = TableRelation.new(:foo) @relation2 = TableRelation.new(:bar) @range1 = 1..2 - @range2 = Time.now..2.days.from_now + @range2 = 4..9 end - describe RangeRelation, '==' do + describe '==' 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_not == RangeRelation.new(@relation2, @range1) RangeRelation.new(@relation1, @range1).should_not == RangeRelation.new(@relation1, @range2) 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 \ No newline at end of file