From f9cc8bba39b1deb6b3f70cecb96beaf988054915 Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Sun, 30 Dec 2007 13:22:31 -0800 Subject: various --- lib/sql_algebra.rb | 24 +++++++++++-------- lib/sql_algebra/predicates/binary_predicate.rb | 12 ++++++++++ lib/sql_algebra/predicates/equality_predicate.rb | 7 ++++++ .../greater_than_or_equal_to_predicate.rb | 2 ++ .../predicates/greater_than_predicate.rb | 2 ++ .../predicates/less_than_or_equal_to_predicate.rb | 2 ++ lib/sql_algebra/predicates/less_than_predicate.rb | 2 ++ lib/sql_algebra/predicates/match_predicate.rb | 7 ++++++ lib/sql_algebra/predicates/predicate.rb | 5 ++++ .../predicates/range_inclusion_predicate.rb | 0 .../predicates/relation_inclusion_predicate.rb | 11 +++++++++ lib/sql_algebra/relations/order_relation.rb | 11 +++++++++ .../relations/predicates/binary_predicate.rb | 12 ---------- .../relations/predicates/equality_predicate.rb | 7 ------ .../greater_than_or_equal_to_predicate.rb | 2 -- .../relations/predicates/greater_than_predicate.rb | 2 -- .../predicates/less_than_or_equal_to_predicate.rb | 2 -- .../relations/predicates/less_than_predicate.rb | 2 -- .../relations/predicates/match_predicate.rb | 7 ------ lib/sql_algebra/relations/predicates/predicate.rb | 5 ---- .../predicates/range_inclusion_predicate.rb | 0 .../predicates/relation_inclusion_predicate.rb | 11 --------- lib/sql_algebra/relations/projection_relation.rb | 11 +++++++++ lib/sql_algebra/relations/range_relation.rb | 11 +++++++++ lib/sql_algebra/relations/selection_relation.rb | 12 ++++++++++ spec/predicates/binary_predicate_spec.rb | 6 +++++ spec/relations/order_relation_spec.rb | 18 ++++++++++++++ spec/relations/projection_relation_spec.rb | 18 ++++++++++++++ spec/relations/range_relation_spec.rb | 18 ++++++++++++++ spec/relations/relation_spec.rb | 23 +++++++++++++----- spec/relations/selection_relation_spec.rb | 28 ++++++++++++++++++++++ 31 files changed, 214 insertions(+), 66 deletions(-) create mode 100644 lib/sql_algebra/predicates/binary_predicate.rb create mode 100644 lib/sql_algebra/predicates/equality_predicate.rb create mode 100644 lib/sql_algebra/predicates/greater_than_or_equal_to_predicate.rb create mode 100644 lib/sql_algebra/predicates/greater_than_predicate.rb create mode 100644 lib/sql_algebra/predicates/less_than_or_equal_to_predicate.rb create mode 100644 lib/sql_algebra/predicates/less_than_predicate.rb create mode 100644 lib/sql_algebra/predicates/match_predicate.rb create mode 100644 lib/sql_algebra/predicates/predicate.rb create mode 100644 lib/sql_algebra/predicates/range_inclusion_predicate.rb create mode 100644 lib/sql_algebra/predicates/relation_inclusion_predicate.rb create mode 100644 lib/sql_algebra/relations/order_relation.rb delete mode 100644 lib/sql_algebra/relations/predicates/binary_predicate.rb delete mode 100644 lib/sql_algebra/relations/predicates/equality_predicate.rb delete mode 100644 lib/sql_algebra/relations/predicates/greater_than_or_equal_to_predicate.rb delete mode 100644 lib/sql_algebra/relations/predicates/greater_than_predicate.rb delete mode 100644 lib/sql_algebra/relations/predicates/less_than_or_equal_to_predicate.rb delete mode 100644 lib/sql_algebra/relations/predicates/less_than_predicate.rb delete mode 100644 lib/sql_algebra/relations/predicates/match_predicate.rb delete mode 100644 lib/sql_algebra/relations/predicates/predicate.rb delete mode 100644 lib/sql_algebra/relations/predicates/range_inclusion_predicate.rb delete mode 100644 lib/sql_algebra/relations/predicates/relation_inclusion_predicate.rb create mode 100644 lib/sql_algebra/relations/projection_relation.rb create mode 100644 lib/sql_algebra/relations/range_relation.rb create mode 100644 lib/sql_algebra/relations/selection_relation.rb create mode 100644 spec/relations/order_relation_spec.rb create mode 100644 spec/relations/projection_relation_spec.rb create mode 100644 spec/relations/range_relation_spec.rb create mode 100644 spec/relations/selection_relation_spec.rb diff --git a/lib/sql_algebra.rb b/lib/sql_algebra.rb index b1816844e4..7407dc264b 100644 --- a/lib/sql_algebra.rb +++ b/lib/sql_algebra.rb @@ -7,17 +7,21 @@ require 'sql_algebra/relations/table_relation' require 'sql_algebra/relations/join_operation' require 'sql_algebra/relations/join_relation' require 'sql_algebra/relations/attribute' +require 'sql_algebra/relations/projection_relation' +require 'sql_algebra/relations/selection_relation' +require 'sql_algebra/relations/order_relation' +require 'sql_algebra/relations/range_relation' -require 'sql_algebra/relations/predicates/predicate' -require 'sql_algebra/relations/predicates/binary_predicate' -require 'sql_algebra/relations/predicates/equality_predicate' -require 'sql_algebra/relations/predicates/less_than_predicate' -require 'sql_algebra/relations/predicates/less_than_or_equal_to_predicate' -require 'sql_algebra/relations/predicates/greater_than_predicate' -require 'sql_algebra/relations/predicates/greater_than_or_equal_to_predicate' -require 'sql_algebra/relations/predicates/range_inclusion_predicate' -require 'sql_algebra/relations/predicates/relation_inclusion_predicate' -require 'sql_algebra/relations/predicates/match_predicate' +require 'sql_algebra/predicates/predicate' +require 'sql_algebra/predicates/binary_predicate' +require 'sql_algebra/predicates/equality_predicate' +require 'sql_algebra/predicates/less_than_predicate' +require 'sql_algebra/predicates/less_than_or_equal_to_predicate' +require 'sql_algebra/predicates/greater_than_predicate' +require 'sql_algebra/predicates/greater_than_or_equal_to_predicate' +require 'sql_algebra/predicates/range_inclusion_predicate' +require 'sql_algebra/predicates/relation_inclusion_predicate' +require 'sql_algebra/predicates/match_predicate' require 'sql_algebra/extensions/range' diff --git a/lib/sql_algebra/predicates/binary_predicate.rb b/lib/sql_algebra/predicates/binary_predicate.rb new file mode 100644 index 0000000000..d7f4cb20e7 --- /dev/null +++ b/lib/sql_algebra/predicates/binary_predicate.rb @@ -0,0 +1,12 @@ +class BinaryPredicate < Predicate + attr_reader :attribute1, :attribute2 + + def initialize(attribute1, attribute2) + @attribute1, @attribute2 = attribute1, attribute2 + end + + def ==(other) + super and + (attribute1.eql?(other.attribute1) and attribute2.eql?(other.attribute2)) + end +end \ No newline at end of file diff --git a/lib/sql_algebra/predicates/equality_predicate.rb b/lib/sql_algebra/predicates/equality_predicate.rb new file mode 100644 index 0000000000..2d206e6c12 --- /dev/null +++ b/lib/sql_algebra/predicates/equality_predicate.rb @@ -0,0 +1,7 @@ +class EqualityPredicate < BinaryPredicate + def ==(other) + self.class == other.class and + ((attribute1.eql?(other.attribute1) and attribute2.eql?(other.attribute2)) or + (attribute1.eql?(other.attribute2) and attribute2.eql?(other.attribute1))) + end +end \ No newline at end of file diff --git a/lib/sql_algebra/predicates/greater_than_or_equal_to_predicate.rb b/lib/sql_algebra/predicates/greater_than_or_equal_to_predicate.rb new file mode 100644 index 0000000000..49127c312c --- /dev/null +++ b/lib/sql_algebra/predicates/greater_than_or_equal_to_predicate.rb @@ -0,0 +1,2 @@ +class GreaterThanOrEqualToPredicate < BinaryPredicate +end \ No newline at end of file diff --git a/lib/sql_algebra/predicates/greater_than_predicate.rb b/lib/sql_algebra/predicates/greater_than_predicate.rb new file mode 100644 index 0000000000..03aecaed62 --- /dev/null +++ b/lib/sql_algebra/predicates/greater_than_predicate.rb @@ -0,0 +1,2 @@ +class GreaterThanPredicate < BinaryPredicate +end \ No newline at end of file diff --git a/lib/sql_algebra/predicates/less_than_or_equal_to_predicate.rb b/lib/sql_algebra/predicates/less_than_or_equal_to_predicate.rb new file mode 100644 index 0000000000..fee6ea7f35 --- /dev/null +++ b/lib/sql_algebra/predicates/less_than_or_equal_to_predicate.rb @@ -0,0 +1,2 @@ +class LessThanOrEqualToPredicate < BinaryPredicate +end \ No newline at end of file diff --git a/lib/sql_algebra/predicates/less_than_predicate.rb b/lib/sql_algebra/predicates/less_than_predicate.rb new file mode 100644 index 0000000000..03cbdcf000 --- /dev/null +++ b/lib/sql_algebra/predicates/less_than_predicate.rb @@ -0,0 +1,2 @@ +class LessThanPredicate < BinaryPredicate +end \ No newline at end of file diff --git a/lib/sql_algebra/predicates/match_predicate.rb b/lib/sql_algebra/predicates/match_predicate.rb new file mode 100644 index 0000000000..90a13090d4 --- /dev/null +++ b/lib/sql_algebra/predicates/match_predicate.rb @@ -0,0 +1,7 @@ +class MatchPredicate < Predicate + attr_reader :attribute, :regexp + + def initialize(attribute, regexp) + @attribute, @regexp = attribute, regexp + end +end \ No newline at end of file diff --git a/lib/sql_algebra/predicates/predicate.rb b/lib/sql_algebra/predicates/predicate.rb new file mode 100644 index 0000000000..4c395a3fdc --- /dev/null +++ b/lib/sql_algebra/predicates/predicate.rb @@ -0,0 +1,5 @@ +class Predicate + def ==(other) + self.class == other.class + end +end \ No newline at end of file diff --git a/lib/sql_algebra/predicates/range_inclusion_predicate.rb b/lib/sql_algebra/predicates/range_inclusion_predicate.rb new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/sql_algebra/predicates/relation_inclusion_predicate.rb b/lib/sql_algebra/predicates/relation_inclusion_predicate.rb new file mode 100644 index 0000000000..5881a85d99 --- /dev/null +++ b/lib/sql_algebra/predicates/relation_inclusion_predicate.rb @@ -0,0 +1,11 @@ +class RelationInclusionPredicate < Predicate + attr_reader :attribute, :relation + + def initialize(attribute, relation) + @attribute, @relation = attribute, relation + end + + def ==(other) + super and attribute == other.attribute and relation == other.relation + end +end \ No newline at end of file diff --git a/lib/sql_algebra/relations/order_relation.rb b/lib/sql_algebra/relations/order_relation.rb new file mode 100644 index 0000000000..8d07c58d64 --- /dev/null +++ b/lib/sql_algebra/relations/order_relation.rb @@ -0,0 +1,11 @@ +class OrderRelation < Relation + attr_reader :relation, :attributes + + def initialize(relation, *attributes) + @relation, @attributes = relation, attributes + end + + def ==(other) + relation == other.relation and attributes.eql?(other.attributes) + end +end \ No newline at end of file diff --git a/lib/sql_algebra/relations/predicates/binary_predicate.rb b/lib/sql_algebra/relations/predicates/binary_predicate.rb deleted file mode 100644 index d7f4cb20e7..0000000000 --- a/lib/sql_algebra/relations/predicates/binary_predicate.rb +++ /dev/null @@ -1,12 +0,0 @@ -class BinaryPredicate < Predicate - attr_reader :attribute1, :attribute2 - - def initialize(attribute1, attribute2) - @attribute1, @attribute2 = attribute1, attribute2 - end - - def ==(other) - super and - (attribute1.eql?(other.attribute1) and attribute2.eql?(other.attribute2)) - end -end \ No newline at end of file diff --git a/lib/sql_algebra/relations/predicates/equality_predicate.rb b/lib/sql_algebra/relations/predicates/equality_predicate.rb deleted file mode 100644 index 2d206e6c12..0000000000 --- a/lib/sql_algebra/relations/predicates/equality_predicate.rb +++ /dev/null @@ -1,7 +0,0 @@ -class EqualityPredicate < BinaryPredicate - def ==(other) - self.class == other.class and - ((attribute1.eql?(other.attribute1) and attribute2.eql?(other.attribute2)) or - (attribute1.eql?(other.attribute2) and attribute2.eql?(other.attribute1))) - end -end \ No newline at end of file diff --git a/lib/sql_algebra/relations/predicates/greater_than_or_equal_to_predicate.rb b/lib/sql_algebra/relations/predicates/greater_than_or_equal_to_predicate.rb deleted file mode 100644 index 49127c312c..0000000000 --- a/lib/sql_algebra/relations/predicates/greater_than_or_equal_to_predicate.rb +++ /dev/null @@ -1,2 +0,0 @@ -class GreaterThanOrEqualToPredicate < BinaryPredicate -end \ No newline at end of file diff --git a/lib/sql_algebra/relations/predicates/greater_than_predicate.rb b/lib/sql_algebra/relations/predicates/greater_than_predicate.rb deleted file mode 100644 index 03aecaed62..0000000000 --- a/lib/sql_algebra/relations/predicates/greater_than_predicate.rb +++ /dev/null @@ -1,2 +0,0 @@ -class GreaterThanPredicate < BinaryPredicate -end \ No newline at end of file diff --git a/lib/sql_algebra/relations/predicates/less_than_or_equal_to_predicate.rb b/lib/sql_algebra/relations/predicates/less_than_or_equal_to_predicate.rb deleted file mode 100644 index fee6ea7f35..0000000000 --- a/lib/sql_algebra/relations/predicates/less_than_or_equal_to_predicate.rb +++ /dev/null @@ -1,2 +0,0 @@ -class LessThanOrEqualToPredicate < BinaryPredicate -end \ No newline at end of file diff --git a/lib/sql_algebra/relations/predicates/less_than_predicate.rb b/lib/sql_algebra/relations/predicates/less_than_predicate.rb deleted file mode 100644 index 03cbdcf000..0000000000 --- a/lib/sql_algebra/relations/predicates/less_than_predicate.rb +++ /dev/null @@ -1,2 +0,0 @@ -class LessThanPredicate < BinaryPredicate -end \ No newline at end of file diff --git a/lib/sql_algebra/relations/predicates/match_predicate.rb b/lib/sql_algebra/relations/predicates/match_predicate.rb deleted file mode 100644 index 90a13090d4..0000000000 --- a/lib/sql_algebra/relations/predicates/match_predicate.rb +++ /dev/null @@ -1,7 +0,0 @@ -class MatchPredicate < Predicate - attr_reader :attribute, :regexp - - def initialize(attribute, regexp) - @attribute, @regexp = attribute, regexp - end -end \ No newline at end of file diff --git a/lib/sql_algebra/relations/predicates/predicate.rb b/lib/sql_algebra/relations/predicates/predicate.rb deleted file mode 100644 index 4c395a3fdc..0000000000 --- a/lib/sql_algebra/relations/predicates/predicate.rb +++ /dev/null @@ -1,5 +0,0 @@ -class Predicate - def ==(other) - self.class == other.class - end -end \ No newline at end of file diff --git a/lib/sql_algebra/relations/predicates/range_inclusion_predicate.rb b/lib/sql_algebra/relations/predicates/range_inclusion_predicate.rb deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/lib/sql_algebra/relations/predicates/relation_inclusion_predicate.rb b/lib/sql_algebra/relations/predicates/relation_inclusion_predicate.rb deleted file mode 100644 index 5881a85d99..0000000000 --- a/lib/sql_algebra/relations/predicates/relation_inclusion_predicate.rb +++ /dev/null @@ -1,11 +0,0 @@ -class RelationInclusionPredicate < Predicate - attr_reader :attribute, :relation - - def initialize(attribute, relation) - @attribute, @relation = attribute, relation - end - - def ==(other) - super and attribute == other.attribute and relation == other.relation - 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 new file mode 100644 index 0000000000..caebfc1b62 --- /dev/null +++ b/lib/sql_algebra/relations/projection_relation.rb @@ -0,0 +1,11 @@ +class ProjectionRelation < Relation + attr_reader :relation, :attributes + + def initialize(relation, *attributes) + @relation, @attributes = relation, attributes + end + + def ==(other) + relation == other.relation and attributes.eql?(other.attributes) + 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 new file mode 100644 index 0000000000..fd7e2898fa --- /dev/null +++ b/lib/sql_algebra/relations/range_relation.rb @@ -0,0 +1,11 @@ +class RangeRelation < Relation + attr_reader :relation, :range + + def initialize(relation, range) + @relation, @range = relation, range + end + + def ==(other) + relation == other.relation and range == other.range + end +end \ No newline at end of file diff --git a/lib/sql_algebra/relations/selection_relation.rb b/lib/sql_algebra/relations/selection_relation.rb new file mode 100644 index 0000000000..5654d8de31 --- /dev/null +++ b/lib/sql_algebra/relations/selection_relation.rb @@ -0,0 +1,12 @@ +class SelectionRelation < Relation + attr_reader :relation, :predicate + + def initialize(relation, *predicates) + @predicate = predicates.shift + @relation = predicates.empty?? relation : SelectionRelation.new(relation, *predicates) + end + + def ==(other) + relation == other.relation and predicate == other.predicate + end +end \ No newline at end of file diff --git a/spec/predicates/binary_predicate_spec.rb b/spec/predicates/binary_predicate_spec.rb index 1be7dd067d..58e395b08d 100644 --- a/spec/predicates/binary_predicate_spec.rb +++ b/spec/predicates/binary_predicate_spec.rb @@ -8,6 +8,12 @@ describe BinaryPredicate do @attribute2 = Attribute.new(@relation2, :attribute_name) end + describe BinaryPredicate, '#initialize' do + it "requires that both columns come from the same relation" do + pending + end + end + describe BinaryPredicate, '==' do before do class ConcreteBinaryPredicate < BinaryPredicate diff --git a/spec/relations/order_relation_spec.rb b/spec/relations/order_relation_spec.rb new file mode 100644 index 0000000000..362544d0d1 --- /dev/null +++ b/spec/relations/order_relation_spec.rb @@ -0,0 +1,18 @@ +require File.join(File.dirname(__FILE__), '..', 'spec_helper') + +describe OrderRelation do + before do + @relation1 = TableRelation.new(:foo) + @relation2 = TableRelation.new(:bar) + @attribute1 = @relation1[:foo] + @attribute2 = @relation2[:bar] + end + + describe OrderRelation, '==' 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 +end \ No newline at end of file diff --git a/spec/relations/projection_relation_spec.rb b/spec/relations/projection_relation_spec.rb new file mode 100644 index 0000000000..f802a2e293 --- /dev/null +++ b/spec/relations/projection_relation_spec.rb @@ -0,0 +1,18 @@ +require File.join(File.dirname(__FILE__), '..', 'spec_helper') + +describe ProjectionRelation do + before do + @relation1 = TableRelation.new(:foo) + @relation2 = TableRelation.new(:bar) + @attribute1 = @relation1[:foo] + @attribute2 = @relation2[:bar] + end + + describe ProjectionRelation, '==' 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 +end \ No newline at end of file diff --git a/spec/relations/range_relation_spec.rb b/spec/relations/range_relation_spec.rb new file mode 100644 index 0000000000..2a1cd1d070 --- /dev/null +++ b/spec/relations/range_relation_spec.rb @@ -0,0 +1,18 @@ +require File.join(File.dirname(__FILE__), '..', 'spec_helper') + +describe RangeRelation do + before do + @relation1 = TableRelation.new(:foo) + @relation2 = TableRelation.new(:bar) + @range1 = 1..2 + @range2 = Time.now..2.days.from_now + end + + describe RangeRelation, '==' 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 +end \ No newline at end of file diff --git a/spec/relations/relation_spec.rb b/spec/relations/relation_spec.rb index c482eb4a01..7434bd563b 100644 --- a/spec/relations/relation_spec.rb +++ b/spec/relations/relation_spec.rb @@ -12,19 +12,17 @@ describe Relation do end end - describe Relation, 'attributes' do - end - describe Relation, '[]' do it "manufactures a attribute" do @relation1[:id].should be_eql(Attribute.new(@relation1, :id)) end it "raises an error if the named attribute is not part of the relation" do + pending end end - describe Relation, 'include?' do + describe Relation, '#include?' do before do @attribute = Attribute.new(@relation1, :id) end @@ -34,19 +32,32 @@ describe Relation do end end - describe Relation, 'project' do + describe Relation, '#project' do before do @attribute1 = Attribute.new(@relation1, :id) @attribute2 = Attribute.new(@relation1, :name) end it "only allows projecting attributes in the relation" do + pending end it "collapses identical projections" do + pending + end + + it "manufactures a projected relation" do + @relation1.project(@attribute1, @attribute2).should == ProjectedRelation(@relation1, @attribute1, @attribute2) end end - describe Relation, 'select' do + describe Relation, '#select' do + before do + @predicate = EqualityPredicate.new() + end + + it "manufactures a selected relation" do + @relation1.select(@attribute1, @attribute2).should == SelectedRelation(@relation1, @attribute1, @attribute2) + end end end \ No newline at end of file diff --git a/spec/relations/selection_relation_spec.rb b/spec/relations/selection_relation_spec.rb new file mode 100644 index 0000000000..0cfd55449c --- /dev/null +++ b/spec/relations/selection_relation_spec.rb @@ -0,0 +1,28 @@ +require File.join(File.dirname(__FILE__), '..', 'spec_helper') + +describe SelectionRelation do + before do + @relation1 = TableRelation.new(:foo) + @relation2 = TableRelation.new(:bar) + @predicate1 = EqualityPredicate.new(@relation1[:id], @relation2[:foo_id]) + @predicate2 = LessThanPredicate.new(@relation1[:age], 2) + end + + describe SelectionRelation, '==' do + it "obtains if both the predicate and the relation are identical" do + SelectionRelation.new(@relation1, @predicate1). \ + should == SelectionRelation.new(@relation1, @predicate1) + SelectionRelation.new(@relation1, @predicate1). \ + should_not == SelectionRelation.new(@relation2, @predicate1) + SelectionRelation.new(@relation1, @predicate1). \ + should_not == SelectionRelation.new(@relation1, @predicate2) + end + end + + describe SelectionRelation, '#initialize' do + it "manufactures nested selection relations if multiple predicates are provided" do + SelectionRelation.new(@relation1, @predicate1, @predicate2). \ + should == SelectionRelation.new(SelectionRelation.new(@relation1, @predicate2), @predicate1) + end + end +end \ No newline at end of file -- cgit v1.2.3