diff options
author | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-01-05 15:24:46 -0800 |
---|---|---|
committer | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-01-05 15:24:46 -0800 |
commit | d7b89d957dbceb4eeceb0b1d381474a4de70a14d (patch) | |
tree | b5d84358426ad29410451eec87d7cc77ca39ad8c /lib | |
parent | 346d9c6bde5ae0a0c918b4e0b25d79aa2760234a (diff) | |
download | rails-d7b89d957dbceb4eeceb0b1d381474a4de70a14d.tar.gz rails-d7b89d957dbceb4eeceb0b1d381474a4de70a14d.tar.bz2 rails-d7b89d957dbceb4eeceb0b1d381474a4de70a14d.zip |
qualified naming
Diffstat (limited to 'lib')
-rw-r--r-- | lib/sql_algebra.rb | 1 | ||||
-rw-r--r-- | lib/sql_algebra/extensions/array.rb | 5 | ||||
-rw-r--r-- | lib/sql_algebra/extensions/base.rb | 12 | ||||
-rw-r--r-- | lib/sql_algebra/extensions/object.rb | 4 | ||||
-rw-r--r-- | lib/sql_algebra/predicates/binary_predicate.rb | 4 | ||||
-rw-r--r-- | lib/sql_algebra/predicates/equality_predicate.rb | 2 | ||||
-rw-r--r-- | lib/sql_algebra/relations/attribute.rb | 57 | ||||
-rw-r--r-- | lib/sql_algebra/relations/join_relation.rb | 2 | ||||
-rw-r--r-- | lib/sql_algebra/relations/order_relation.rb | 6 | ||||
-rw-r--r-- | lib/sql_algebra/relations/projection_relation.rb | 4 | ||||
-rw-r--r-- | lib/sql_algebra/relations/relation.rb | 2 | ||||
-rw-r--r-- | lib/sql_algebra/relations/rename_relation.rb | 34 | ||||
-rw-r--r-- | lib/sql_algebra/relations/selection_relation.rb | 5 | ||||
-rw-r--r-- | lib/sql_algebra/relations/table_relation.rb | 8 |
14 files changed, 113 insertions, 33 deletions
diff --git a/lib/sql_algebra.rb b/lib/sql_algebra.rb index 61625ff270..475bb23138 100644 --- a/lib/sql_algebra.rb +++ b/lib/sql_algebra.rb @@ -33,6 +33,7 @@ require 'sql_algebra/predicates/match_predicate' require 'sql_algebra/extensions/range' require 'sql_algebra/extensions/object' +require 'sql_algebra/extensions/array' require 'sql_algebra/sql_builder/sql_builder' require 'sql_algebra/sql_builder/select_builder' diff --git a/lib/sql_algebra/extensions/array.rb b/lib/sql_algebra/extensions/array.rb new file mode 100644 index 0000000000..5b6d6d6abd --- /dev/null +++ b/lib/sql_algebra/extensions/array.rb @@ -0,0 +1,5 @@ +class Array + def to_hash + Hash[*flatten] + end +end
\ No newline at end of file diff --git a/lib/sql_algebra/extensions/base.rb b/lib/sql_algebra/extensions/base.rb index 79f2ce75d1..0143caf23d 100644 --- a/lib/sql_algebra/extensions/base.rb +++ b/lib/sql_algebra/extensions/base.rb @@ -3,12 +3,12 @@ class ActiveRecord::Base object = cache.get(record % klass.primary_key) { Klass.instantiate(record % Klass.attributes) } includes.each do |include| case include - when Symbol - object.send(association = include).bring_forth(record) - when Hash - include.each do |association, nested_associations| - object.send(association).bring_forth(record, nested_associations) - end + when Symbol + object.send(association = include).bring_forth(record) + when Hash + include.each do |association, nested_associations| + object.send(association).bring_forth(record, nested_associations) + end end end end diff --git a/lib/sql_algebra/extensions/object.rb b/lib/sql_algebra/extensions/object.rb index 639e810a97..6ea66d7484 100644 --- a/lib/sql_algebra/extensions/object.rb +++ b/lib/sql_algebra/extensions/object.rb @@ -1,4 +1,8 @@ class Object + def qualify + self + end + def to_sql(builder = EqualsConditionBuilder.new) me = self builder.call do diff --git a/lib/sql_algebra/predicates/binary_predicate.rb b/lib/sql_algebra/predicates/binary_predicate.rb index 9463f162c5..f5c420c833 100644 --- a/lib/sql_algebra/predicates/binary_predicate.rb +++ b/lib/sql_algebra/predicates/binary_predicate.rb @@ -10,6 +10,10 @@ class BinaryPredicate < Predicate (attribute1.eql?(other.attribute1) and attribute2.eql?(other.attribute2)) end + def qualify + self.class.new(attribute1.qualify, attribute2.qualify) + end + def to_sql(builder = ConditionsBuilder.new) builder.call do send(predicate_name) do diff --git a/lib/sql_algebra/predicates/equality_predicate.rb b/lib/sql_algebra/predicates/equality_predicate.rb index 2061d0f644..7040c45f67 100644 --- a/lib/sql_algebra/predicates/equality_predicate.rb +++ b/lib/sql_algebra/predicates/equality_predicate.rb @@ -4,7 +4,7 @@ class EqualityPredicate < BinaryPredicate ((attribute1.eql?(other.attribute1) and attribute2.eql?(other.attribute2)) or (attribute1.eql?(other.attribute2) and attribute2.eql?(other.attribute1))) end - + protected def predicate_name :equals diff --git a/lib/sql_algebra/relations/attribute.rb b/lib/sql_algebra/relations/attribute.rb index 89ac495245..7583553b80 100644 --- a/lib/sql_algebra/relations/attribute.rb +++ b/lib/sql_algebra/relations/attribute.rb @@ -1,45 +1,56 @@ class Attribute - attr_reader :relation, :attribute_name, :aliaz + attr_reader :relation, :name, :aliaz - def initialize(relation, attribute_name, aliaz = nil) - @relation, @attribute_name, @aliaz = relation, attribute_name, aliaz + def initialize(relation, name, aliaz = nil) + @relation, @name, @aliaz = relation, name, aliaz end def aliazz(aliaz) - Attribute.new(relation, attribute_name, aliaz) + Attribute.new(relation, name, aliaz) end - def eql?(other) - relation == other.relation and attribute_name == other.attribute_name + def qualified_name + "#{relation.table}.#{name}" end - def ==(other) - EqualityPredicate.new(self, other) + def qualify + aliazz(qualified_name) end + + module Predications + def eql?(other) + relation == other.relation and name == other.name and aliaz == other.aliaz + end - def <(other) - LessThanPredicate.new(self, other) - end + def ==(other) + EqualityPredicate.new(self, other) + end - def <=(other) - LessThanOrEqualToPredicate.new(self, other) - end + def <(other) + LessThanPredicate.new(self, other) + end - def >(other) - GreaterThanPredicate.new(self, other) - end + def <=(other) + LessThanOrEqualToPredicate.new(self, other) + end - def >=(other) - GreaterThanOrEqualToPredicate.new(self, other) - end + def >(other) + GreaterThanPredicate.new(self, other) + end - def =~(regexp) - MatchPredicate.new(self, regexp) + def >=(other) + GreaterThanOrEqualToPredicate.new(self, other) + end + + def =~(regexp) + MatchPredicate.new(self, regexp) + end end + include Predications def to_sql(builder = SelectsBuilder.new) builder.call do - column relation.table, attribute_name, aliaz + column relation.table, name, aliaz end 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 index dc57e24c96..c65b07225f 100644 --- a/lib/sql_algebra/relations/join_relation.rb +++ b/lib/sql_algebra/relations/join_relation.rb @@ -16,7 +16,7 @@ class JoinRelation < Relation end def selects - relation1.selects + relation2.selects + relation1.send(:selects) + relation2.send(:selects) end def attributes diff --git a/lib/sql_algebra/relations/order_relation.rb b/lib/sql_algebra/relations/order_relation.rb index 384a007bc2..b39dc45c3f 100644 --- a/lib/sql_algebra/relations/order_relation.rb +++ b/lib/sql_algebra/relations/order_relation.rb @@ -1,4 +1,4 @@ -class OrderRelation < Relation +class OrderRelation < CompoundRelation attr_reader :relation, :attributes def initialize(relation, *attributes) @@ -9,6 +9,10 @@ class OrderRelation < Relation relation == other.relation and attributes.eql?(other.attributes) end + def qualify + OrderRelation.new(relation.qualify, *attributes.collect { |a| a.qualify }) + end + def to_sql(builder = SelectBuilder.new) relation.to_sql(builder).call do attributes.each do |attribute| diff --git a/lib/sql_algebra/relations/projection_relation.rb b/lib/sql_algebra/relations/projection_relation.rb index 0b5d645d79..1a0e8dbfe4 100644 --- a/lib/sql_algebra/relations/projection_relation.rb +++ b/lib/sql_algebra/relations/projection_relation.rb @@ -9,6 +9,10 @@ class ProjectionRelation < Relation relation == other.relation and attributes.eql?(other.attributes) end + def qualify + ProjectionRelation.new(relation.qualify, *attributes.collect(&:qualify)) + end + def to_sql(builder = SelectBuilder.new) relation.to_sql(builder).call do select do diff --git a/lib/sql_algebra/relations/relation.rb b/lib/sql_algebra/relations/relation.rb index 02723ae0cc..82266fd7e8 100644 --- a/lib/sql_algebra/relations/relation.rb +++ b/lib/sql_algebra/relations/relation.rb @@ -34,7 +34,7 @@ class Relation end def rename(attribute, aliaz) - RenameRelation.new(self, attribute, aliaz) + RenameRelation.new(self, attribute => aliaz) end end include Operations diff --git a/lib/sql_algebra/relations/rename_relation.rb b/lib/sql_algebra/relations/rename_relation.rb new file mode 100644 index 0000000000..8acf5091b2 --- /dev/null +++ b/lib/sql_algebra/relations/rename_relation.rb @@ -0,0 +1,34 @@ +class RenameRelation < CompoundRelation + attr_reader :relation, :schmattribute, :aliaz + + def initialize(relation, renames) + @schmattribute, @aliaz = renames.shift + @relation = renames.empty?? relation : RenameRelation.new(relation, renames) + end + + def ==(other) + relation == other.relation and schmattribute.eql?(other.schmattribute) and aliaz == other.aliaz + end + + def attributes + relation.attributes.collect { |a| substitute(a) } + end + + def qualify + RenameRelation.new(relation.qualify, schmattribute.qualify => aliaz) + end + + protected + def attribute(name) + case + when name == aliaz then schmattribute.aliazz(aliaz) + when relation[name].eql?(schmattribute) then nil + else relation[name] + end + end + + private + def substitute(a) + a.eql?(schmattribute) ? a.aliazz(aliaz) : a + 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 index 72911aa65a..dcf5f4745f 100644 --- a/lib/sql_algebra/relations/selection_relation.rb +++ b/lib/sql_algebra/relations/selection_relation.rb @@ -10,6 +10,11 @@ class SelectionRelation < CompoundRelation relation == other.relation and predicate == other.predicate end + def qualify + SelectionRelation.new(relation.qualify, predicate.qualify) + end + + protected def selects [predicate] end diff --git a/lib/sql_algebra/relations/table_relation.rb b/lib/sql_algebra/relations/table_relation.rb index 1915b42565..5a47ae7a34 100644 --- a/lib/sql_algebra/relations/table_relation.rb +++ b/lib/sql_algebra/relations/table_relation.rb @@ -8,6 +8,10 @@ class TableRelation < Relation def attributes attributes_by_name.values end + + def qualify + RenameRelation.new self, qualifications + end protected def attribute(name) @@ -20,4 +24,8 @@ class TableRelation < Relation attributes_by_name.merge(column.name => Attribute.new(self, column.name.to_sym)) end end + + def qualifications + attributes.zip(attributes.collect(&:qualified_name)).to_hash + end end
\ No newline at end of file |