From 960bbcb3ce4a82cf4f031d1c6682ce4c1a04474d Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Sun, 30 Dec 2007 11:35:44 -0800 Subject: initial import --- lib/sql_algebra/extensions/range.rb | 0 lib/sql_algebra/relations/attribute.rb | 35 ++++++++++++++++++++++ lib/sql_algebra/relations/join_operation.rb | 16 ++++++++++ lib/sql_algebra/relations/join_relation.rb | 13 ++++++++ .../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/relation.rb | 13 ++++++++ lib/sql_algebra/relations/table_relation.rb | 11 +++++++ lib/sql_algebra/sql/select.rb | 23 ++++++++++++++ 17 files changed, 161 insertions(+) create mode 100644 lib/sql_algebra/extensions/range.rb create mode 100644 lib/sql_algebra/relations/attribute.rb create mode 100644 lib/sql_algebra/relations/join_operation.rb create mode 100644 lib/sql_algebra/relations/join_relation.rb create mode 100644 lib/sql_algebra/relations/predicates/binary_predicate.rb create mode 100644 lib/sql_algebra/relations/predicates/equality_predicate.rb create mode 100644 lib/sql_algebra/relations/predicates/greater_than_or_equal_to_predicate.rb create mode 100644 lib/sql_algebra/relations/predicates/greater_than_predicate.rb create mode 100644 lib/sql_algebra/relations/predicates/less_than_or_equal_to_predicate.rb create mode 100644 lib/sql_algebra/relations/predicates/less_than_predicate.rb create mode 100644 lib/sql_algebra/relations/predicates/match_predicate.rb create mode 100644 lib/sql_algebra/relations/predicates/predicate.rb create mode 100644 lib/sql_algebra/relations/predicates/range_inclusion_predicate.rb create mode 100644 lib/sql_algebra/relations/predicates/relation_inclusion_predicate.rb create mode 100644 lib/sql_algebra/relations/relation.rb create mode 100644 lib/sql_algebra/relations/table_relation.rb create mode 100644 lib/sql_algebra/sql/select.rb (limited to 'lib/sql_algebra') diff --git a/lib/sql_algebra/extensions/range.rb b/lib/sql_algebra/extensions/range.rb new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/sql_algebra/relations/attribute.rb b/lib/sql_algebra/relations/attribute.rb new file mode 100644 index 0000000000..c0b8df3083 --- /dev/null +++ b/lib/sql_algebra/relations/attribute.rb @@ -0,0 +1,35 @@ +class Attribute + attr_reader :relation, :attribute_name + + def initialize(relation, attribute_name) + @relation, @attribute_name = relation, attribute_name + end + + def eql?(other) + relation == other.relation and attribute_name == other.attribute_name + end + + def ==(other) + EqualityPredicate.new(self, other) + end + + def <(other) + LessThanPredicate.new(self, other) + end + + def <=(other) + LessThanOrEqualToPredicate.new(self, other) + end + + def >(other) + GreaterThanPredicate.new(self, other) + end + + def >=(other) + GreaterThanOrEqualToPredicate.new(self, other) + end + + def =~(regexp) + MatchPredicate.new(self, regexp) + end +end \ No newline at end of file diff --git a/lib/sql_algebra/relations/join_operation.rb b/lib/sql_algebra/relations/join_operation.rb new file mode 100644 index 0000000000..dab8e9e6bd --- /dev/null +++ b/lib/sql_algebra/relations/join_operation.rb @@ -0,0 +1,16 @@ +class JoinOperation + attr_reader :relation1, :relation2 + + def initialize(relation1, relation2) + @relation1, @relation2 = relation1, relation2 + end + + def on(*predicates) + JoinRelation.new(relation1, relation2, *predicates) + end + + def ==(other) + (relation1 == other.relation1 and relation2 == other.relation2) or + (relation1 == other.relation2 and relation2 == other.relation1) + end +end \ No newline at end of file diff --git a/lib/sql_algebra/relations/join_relation.rb b/lib/sql_algebra/relations/join_relation.rb new file mode 100644 index 0000000000..64db8e0e68 --- /dev/null +++ b/lib/sql_algebra/relations/join_relation.rb @@ -0,0 +1,13 @@ +class JoinRelation < Relation + attr_reader :relation1, :relation2, :predicates + + def initialize(relation1, relation2, *predicates) + @relation1, @relation2, @predicates = relation1, relation2, predicates + end + + def ==(other) + predicates == other.predicates and + ((relation1 == other.relation1 and relation2 == other.relation2) or + (relation2 == other.relation1 and relation1 == other.relation2)) + 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 new file mode 100644 index 0000000000..d7f4cb20e7 --- /dev/null +++ b/lib/sql_algebra/relations/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/relations/predicates/equality_predicate.rb b/lib/sql_algebra/relations/predicates/equality_predicate.rb new file mode 100644 index 0000000000..2d206e6c12 --- /dev/null +++ b/lib/sql_algebra/relations/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/relations/predicates/greater_than_or_equal_to_predicate.rb b/lib/sql_algebra/relations/predicates/greater_than_or_equal_to_predicate.rb new file mode 100644 index 0000000000..49127c312c --- /dev/null +++ b/lib/sql_algebra/relations/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/relations/predicates/greater_than_predicate.rb b/lib/sql_algebra/relations/predicates/greater_than_predicate.rb new file mode 100644 index 0000000000..03aecaed62 --- /dev/null +++ b/lib/sql_algebra/relations/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/relations/predicates/less_than_or_equal_to_predicate.rb b/lib/sql_algebra/relations/predicates/less_than_or_equal_to_predicate.rb new file mode 100644 index 0000000000..fee6ea7f35 --- /dev/null +++ b/lib/sql_algebra/relations/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/relations/predicates/less_than_predicate.rb b/lib/sql_algebra/relations/predicates/less_than_predicate.rb new file mode 100644 index 0000000000..03cbdcf000 --- /dev/null +++ b/lib/sql_algebra/relations/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/relations/predicates/match_predicate.rb b/lib/sql_algebra/relations/predicates/match_predicate.rb new file mode 100644 index 0000000000..90a13090d4 --- /dev/null +++ b/lib/sql_algebra/relations/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/relations/predicates/predicate.rb b/lib/sql_algebra/relations/predicates/predicate.rb new file mode 100644 index 0000000000..4c395a3fdc --- /dev/null +++ b/lib/sql_algebra/relations/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/relations/predicates/range_inclusion_predicate.rb b/lib/sql_algebra/relations/predicates/range_inclusion_predicate.rb new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/sql_algebra/relations/predicates/relation_inclusion_predicate.rb b/lib/sql_algebra/relations/predicates/relation_inclusion_predicate.rb new file mode 100644 index 0000000000..5881a85d99 --- /dev/null +++ b/lib/sql_algebra/relations/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/relation.rb b/lib/sql_algebra/relations/relation.rb new file mode 100644 index 0000000000..f1f8fc5884 --- /dev/null +++ b/lib/sql_algebra/relations/relation.rb @@ -0,0 +1,13 @@ +class Relation + def *(other) + JoinOperation.new(self, other) + end + + def [](attribute_name) + Attribute.new(self, attribute_name) + end + + def include?(attribute) + RelationInclusionPredicate.new(attribute, self) + end +end \ No newline at end of file diff --git a/lib/sql_algebra/relations/table_relation.rb b/lib/sql_algebra/relations/table_relation.rb new file mode 100644 index 0000000000..dd4987c3e1 --- /dev/null +++ b/lib/sql_algebra/relations/table_relation.rb @@ -0,0 +1,11 @@ +class TableRelation < Relation + attr_reader :table + + def initialize(table) + @table = table + end + + def to_sql + Select.new(:*).from(table) + end +end \ No newline at end of file diff --git a/lib/sql_algebra/sql/select.rb b/lib/sql_algebra/sql/select.rb new file mode 100644 index 0000000000..da92ad3a52 --- /dev/null +++ b/lib/sql_algebra/sql/select.rb @@ -0,0 +1,23 @@ +class Select + attr_reader :attributes, :tables, :predicates + + def initialize(*attributes) + @attributes = attributes + end + + def from(*tables) + returning self do |select| + @tables = tables + end + end + + def where(*predicates) + returning self do |select| + @predicates = predicates + end + end + + def ==(other) + attributes == other.attributes and tables == other.tables and predicates == other.predicates + end +end \ No newline at end of file -- cgit v1.2.3