diff options
author | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2007-12-30 11:35:44 -0800 |
---|---|---|
committer | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2007-12-30 11:35:44 -0800 |
commit | 960bbcb3ce4a82cf4f031d1c6682ce4c1a04474d (patch) | |
tree | 24e8e29629450700940d3bdc233265648b5f73f2 /lib/sql_algebra/relations | |
download | rails-960bbcb3ce4a82cf4f031d1c6682ce4c1a04474d.tar.gz rails-960bbcb3ce4a82cf4f031d1c6682ce4c1a04474d.tar.bz2 rails-960bbcb3ce4a82cf4f031d1c6682ce4c1a04474d.zip |
initial import
Diffstat (limited to 'lib/sql_algebra/relations')
15 files changed, 138 insertions, 0 deletions
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 --- /dev/null +++ b/lib/sql_algebra/relations/predicates/range_inclusion_predicate.rb 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 |