diff options
author | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-01-10 21:33:35 -0800 |
---|---|---|
committer | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-01-10 21:33:35 -0800 |
commit | 45b6111c2961e018df87fe2230caf1d5a14f2aee (patch) | |
tree | 205d91c84a63d71774488c40fbe3f3183d877ef5 | |
parent | 5f03d1616cc6f66013fbaae9c92553df74a7aee4 (diff) | |
download | rails-45b6111c2961e018df87fe2230caf1d5a14f2aee.tar.gz rails-45b6111c2961e018df87fe2230caf1d5a14f2aee.tar.bz2 rails-45b6111c2961e018df87fe2230caf1d5a14f2aee.zip |
namespacing
46 files changed, 556 insertions, 569 deletions
diff --git a/lib/active_relation.rb b/lib/active_relation.rb index 2120d09bc8..3f277df1a5 100644 --- a/lib/active_relation.rb +++ b/lib/active_relation.rb @@ -9,12 +9,7 @@ require 'active_relation/sql_builder' require 'active_relation/relations/relation' require 'active_relation/relations/compound_relation' require 'active_relation/relations/table_relation' -require 'active_relation/relations/join_operation' -require 'active_relation/relations/inner_join_operation' -require 'active_relation/relations/left_outer_join_operation' require 'active_relation/relations/join_relation' -require 'active_relation/relations/inner_join_relation' -require 'active_relation/relations/left_outer_join_relation' require 'active_relation/relations/attribute' require 'active_relation/relations/projection_relation' require 'active_relation/relations/selection_relation' @@ -24,15 +19,7 @@ require 'active_relation/relations/rename_relation' require 'active_relation/relations/deletion_relation' require 'active_relation/relations/insertion_relation' -require 'active_relation/predicates/predicate' -require 'active_relation/predicates/binary_predicate' -require 'active_relation/predicates/equality_predicate' -require 'active_relation/predicates/less_than_predicate' -require 'active_relation/predicates/less_than_or_equal_to_predicate' -require 'active_relation/predicates/greater_than_predicate' -require 'active_relation/predicates/greater_than_or_equal_to_predicate' -require 'active_relation/predicates/relation_inclusion_predicate' -require 'active_relation/predicates/match_predicate' +require 'active_relation/predicates' require 'active_relation/extensions/object' require 'active_relation/extensions/array' diff --git a/lib/active_relation/extensions/base.rb b/lib/active_relation/extensions/base.rb index 0dbdef703f..53523e9d12 100644 --- a/lib/active_relation/extensions/base.rb +++ b/lib/active_relation/extensions/base.rb @@ -5,7 +5,7 @@ class ActiveRecord::Base end def relation - @relation ||= TableRelation.new(table_name) + @relation ||= ActiveRelation::Relations::Table.new(table_name) end end diff --git a/lib/active_relation/extensions/object.rb b/lib/active_relation/extensions/object.rb index 79d7613d9a..ea582d1ca1 100644 --- a/lib/active_relation/extensions/object.rb +++ b/lib/active_relation/extensions/object.rb @@ -1,5 +1,5 @@ class Object - include SqlBuilder + include ActiveRelation::SqlBuilder def qualify self diff --git a/lib/active_relation/predicates.rb b/lib/active_relation/predicates.rb index 0179a15035..5ac879899b 100644 --- a/lib/active_relation/predicates.rb +++ b/lib/active_relation/predicates.rb @@ -1,4 +1,74 @@ module ActiveRelation module Predicates + class Base + def ==(other) + self.class == other.class + end + end + + class Binary < Base + 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 + + def qualify + self.class.new(attribute1.qualify, attribute2.qualify) + end + + def to_sql(options = {}) + "#{attribute1.to_sql} #{predicate_sql} #{attribute2.to_sql}" + end + end + + class Equality < Binary + 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 + + protected + def predicate_sql + '=' + end + end + + class GreaterThanOrEqualTo < Binary + end + + class GreaterThan < Binary + end + + class LessThanOrEqualTo < Binary + end + + class LessThan < Binary + end + + class Match < Base + attr_reader :attribute, :regexp + + def initialize(attribute, regexp) + @attribute, @regexp = attribute, regexp + end + end + + class RelationInclusion < Base + 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 end end
\ No newline at end of file diff --git a/lib/active_relation/predicates/binary_predicate.rb b/lib/active_relation/predicates/binary_predicate.rb deleted file mode 100644 index f3ce430d00..0000000000 --- a/lib/active_relation/predicates/binary_predicate.rb +++ /dev/null @@ -1,19 +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 - - def qualify - self.class.new(attribute1.qualify, attribute2.qualify) - end - - def to_sql(options = {}) - "#{attribute1.to_sql} #{predicate_sql} #{attribute2.to_sql}" - end -end
\ No newline at end of file diff --git a/lib/active_relation/predicates/equality_predicate.rb b/lib/active_relation/predicates/equality_predicate.rb deleted file mode 100644 index 4f4113c740..0000000000 --- a/lib/active_relation/predicates/equality_predicate.rb +++ /dev/null @@ -1,12 +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 - - protected - def predicate_sql - '=' - end -end
\ No newline at end of file diff --git a/lib/active_relation/predicates/greater_than_or_equal_to_predicate.rb b/lib/active_relation/predicates/greater_than_or_equal_to_predicate.rb deleted file mode 100644 index 49127c312c..0000000000 --- a/lib/active_relation/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/active_relation/predicates/greater_than_predicate.rb b/lib/active_relation/predicates/greater_than_predicate.rb deleted file mode 100644 index 03aecaed62..0000000000 --- a/lib/active_relation/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/active_relation/predicates/less_than_or_equal_to_predicate.rb b/lib/active_relation/predicates/less_than_or_equal_to_predicate.rb deleted file mode 100644 index fee6ea7f35..0000000000 --- a/lib/active_relation/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/active_relation/predicates/less_than_predicate.rb b/lib/active_relation/predicates/less_than_predicate.rb deleted file mode 100644 index 03cbdcf000..0000000000 --- a/lib/active_relation/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/active_relation/predicates/match_predicate.rb b/lib/active_relation/predicates/match_predicate.rb deleted file mode 100644 index 90a13090d4..0000000000 --- a/lib/active_relation/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/active_relation/predicates/predicate.rb b/lib/active_relation/predicates/predicate.rb deleted file mode 100644 index 4c395a3fdc..0000000000 --- a/lib/active_relation/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/active_relation/predicates/relation_inclusion_predicate.rb b/lib/active_relation/predicates/relation_inclusion_predicate.rb deleted file mode 100644 index 5881a85d99..0000000000 --- a/lib/active_relation/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/active_relation/relations/attribute.rb b/lib/active_relation/relations/attribute.rb index 8193132de6..30cd9798d9 100644 --- a/lib/active_relation/relations/attribute.rb +++ b/lib/active_relation/relations/attribute.rb @@ -1,56 +1,60 @@ -class Attribute - include SqlBuilder +module ActiveRelation + module Primitives + class Attribute + include ::ActiveRelation::SqlBuilder - attr_reader :relation, :name, :alias + attr_reader :relation, :name, :alias - def initialize(relation, name, aliaz = nil) - @relation, @name, @alias = relation, name, aliaz - end + def initialize(relation, name, aliaz = nil) + @relation, @name, @alias = relation, name, aliaz + end - def alias(aliaz = nil) - aliaz ? Attribute.new(relation, name, aliaz) : @alias - end + def alias(aliaz = nil) + aliaz ? ActiveRelation::Primitives::Attribute.new(relation, name, aliaz) : @alias + end - def qualified_name - "#{relation.table}.#{name}" - end + def qualified_name + "#{relation.table}.#{name}" + end - def qualify - self.alias(qualified_name) - end + def qualify + self.alias(qualified_name) + end - def eql?(other) - relation == other.relation and name == other.name and self.alias == other.alias - end + def eql?(other) + relation == other.relation and name == other.name and self.alias == other.alias + end - module Predications - def ==(other) - EqualityPredicate.new(self, other) - end - - def <(other) - LessThanPredicate.new(self, other) - end - - def <=(other) - LessThanOrEqualToPredicate.new(self, other) + module Predications + def ==(other) + Predicates::Equality.new(self, other) + end + + def <(other) + Predicates::LessThan.new(self, other) + end + + def <=(other) + Predicates::LessThanOrEqualTo.new(self, other) + end + + def >(other) + Predicates::GreaterThan.new(self, other) + end + + def >=(other) + Predicates::GreaterThanOrEqualTo.new(self, other) + end + + def =~(regexp) + Predicates::Match.new(self, regexp) + end + end + include Predications + + def to_sql(options = {}) + "#{quote_table_name(relation.table)}.#{quote_column_name(name)}" + (options[:use_alias] && self.alias ? " AS #{self.alias.to_s.to_sql}" : "") + end end - - def >(other) - GreaterThanPredicate.new(self, other) - end - - def >=(other) - GreaterThanOrEqualToPredicate.new(self, other) - end - - def =~(regexp) - MatchPredicate.new(self, regexp) - end - end - include Predications - - def to_sql(options = {}) - "#{quote_table_name(relation.table)}.#{quote_column_name(name)}" + (options[:use_alias] && self.alias ? " AS #{self.alias.to_s.to_sql}" : "") end end
\ No newline at end of file diff --git a/lib/active_relation/relations/compound_relation.rb b/lib/active_relation/relations/compound_relation.rb index b18921e06d..442224a011 100644 --- a/lib/active_relation/relations/compound_relation.rb +++ b/lib/active_relation/relations/compound_relation.rb @@ -1,5 +1,9 @@ -class CompoundRelation < Relation - attr_reader :relation +module ActiveRelation + module Relations + class Compound < Base + attr_reader :relation - delegate :attributes, :attribute, :joins, :selects, :orders, :table, :inserts, :limit, :offset, :to => :relation + delegate :attributes, :attribute, :joins, :selects, :orders, :table, :inserts, :limit, :offset, :to => :relation + end + end end
\ No newline at end of file diff --git a/lib/active_relation/relations/deletion_relation.rb b/lib/active_relation/relations/deletion_relation.rb index 8418319055..f218d9da6d 100644 --- a/lib/active_relation/relations/deletion_relation.rb +++ b/lib/active_relation/relations/deletion_relation.rb @@ -1,13 +1,17 @@ -class DeletionRelation < CompoundRelation - def initialize(relation) - @relation = relation - end +module ActiveRelation + module Relations + class Deletion < Compound + def initialize(relation) + @relation = relation + end - def to_sql(options = {}) - [ - "DELETE", - "FROM #{quote_table_name(table)}", - ("WHERE #{selects.collect(&:to_sql).join('\n\tAND ')}" unless selects.blank?) - ].compact.join("\n") - end + def to_sql(options = {}) + [ + "DELETE", + "FROM #{quote_table_name(table)}", + ("WHERE #{selects.collect(&:to_sql).join('\n\tAND ')}" unless selects.blank?) + ].compact.join("\n") + end + end + end end
\ No newline at end of file diff --git a/lib/active_relation/relations/inner_join_operation.rb b/lib/active_relation/relations/inner_join_operation.rb deleted file mode 100644 index 6b5c5ce8d0..0000000000 --- a/lib/active_relation/relations/inner_join_operation.rb +++ /dev/null @@ -1,6 +0,0 @@ -class InnerJoinOperation < JoinOperation - protected - def relation_class - InnerJoinRelation - end -end
\ No newline at end of file diff --git a/lib/active_relation/relations/inner_join_relation.rb b/lib/active_relation/relations/inner_join_relation.rb deleted file mode 100644 index 74160c559f..0000000000 --- a/lib/active_relation/relations/inner_join_relation.rb +++ /dev/null @@ -1,6 +0,0 @@ -class InnerJoinRelation < JoinRelation - protected - def join_sql - "INNER JOIN" - end -end
\ No newline at end of file diff --git a/lib/active_relation/relations/insertion_relation.rb b/lib/active_relation/relations/insertion_relation.rb index 002ebbf062..a0042a18a5 100644 --- a/lib/active_relation/relations/insertion_relation.rb +++ b/lib/active_relation/relations/insertion_relation.rb @@ -1,21 +1,25 @@ -class InsertionRelation < CompoundRelation - attr_reader :record +module ActiveRelation + module Relations + class Insertion < Compound + attr_reader :record - def initialize(relation, record) - @relation, @record = relation, record - end + def initialize(relation, record) + @relation, @record = relation, record + end - def to_sql(options = {}) - [ - "INSERT", - "INTO #{quote_table_name(table)}", - "(#{record.keys.collect(&:to_sql).join(', ')})", - "VALUES #{inserts.collect(&:to_sql).join(', ')}" - ].join("\n") - end + def to_sql(options = {}) + [ + "INSERT", + "INTO #{quote_table_name(table)}", + "(#{record.keys.collect(&:to_sql).join(', ')})", + "VALUES #{inserts.collect(&:to_sql).join(', ')}" + ].join("\n") + end - protected - def inserts - relation.inserts + [record] + protected + def inserts + relation.inserts + [record] + end + end end end
\ No newline at end of file diff --git a/lib/active_relation/relations/join_operation.rb b/lib/active_relation/relations/join_operation.rb deleted file mode 100644 index 2b4548a041..0000000000 --- a/lib/active_relation/relations/join_operation.rb +++ /dev/null @@ -1,16 +0,0 @@ -class JoinOperation - attr_reader :relation1, :relation2 - - def initialize(relation1, relation2) - @relation1, @relation2 = relation1, relation2 - end - - def on(*predicates) - relation_class.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/active_relation/relations/join_relation.rb b/lib/active_relation/relations/join_relation.rb index 845dfd732f..1bd1439dd6 100644 --- a/lib/active_relation/relations/join_relation.rb +++ b/lib/active_relation/relations/join_relation.rb @@ -1,41 +1,45 @@ -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 +module ActiveRelation + module Relations + class Join < Base + attr_reader :join_sql, :relation1, :relation2, :predicates + + def initialize(join_sql, relation1, relation2, *predicates) + @join_sql, @relation1, @relation2, @predicates = join_sql, 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 - def qualify - self.class.new(relation1.qualify, relation2.qualify, *predicates.collect(&:qualify)) - end - - protected - def joins - [relation1.joins, relation2.joins, join].compact.join(" ") - end - - def selects - relation1.send(:selects) + relation2.send(:selects) - end - - def attributes - relation1.attributes + relation2.attributes - end - - def attribute(name) - relation1[name] || relation2[name] - end - - delegate :table, :to => :relation1 - - private - def join - "#{join_sql} #{quote_table_name(relation2.table)} ON #{predicates.collect { |p| p.to_sql(:quote => false) }.join(' AND ')}" + def qualify + Join.new(join_sql, relation1.qualify, relation2.qualify, *predicates.collect(&:qualify)) + end + + protected + def joins + [relation1.joins, relation2.joins, join].compact.join(" ") + end + + def selects + relation1.send(:selects) + relation2.send(:selects) + end + + def attributes + relation1.attributes + relation2.attributes + end + + def attribute(name) + relation1[name] || relation2[name] + end + + delegate :table, :to => :relation1 + + private + def join + "#{join_sql} #{quote_table_name(relation2.table)} ON #{predicates.collect { |p| p.to_sql(:quote => false) }.join(' AND ')}" + end + end end end
\ No newline at end of file diff --git a/lib/active_relation/relations/left_outer_join_operation.rb b/lib/active_relation/relations/left_outer_join_operation.rb deleted file mode 100644 index fbb2a4e2ed..0000000000 --- a/lib/active_relation/relations/left_outer_join_operation.rb +++ /dev/null @@ -1,6 +0,0 @@ -class LeftOuterJoinOperation < JoinOperation - protected - def relation_class - LeftOuterJoinRelation - end -end
\ No newline at end of file diff --git a/lib/active_relation/relations/left_outer_join_relation.rb b/lib/active_relation/relations/left_outer_join_relation.rb deleted file mode 100644 index 57eda4e1fc..0000000000 --- a/lib/active_relation/relations/left_outer_join_relation.rb +++ /dev/null @@ -1,6 +0,0 @@ -class LeftOuterJoinRelation < JoinRelation - protected - def join_sql - "LEFT OUTER JOIN" - end -end
\ No newline at end of file diff --git a/lib/active_relation/relations/order_relation.rb b/lib/active_relation/relations/order_relation.rb index dfb0c0bf25..99ff939528 100644 --- a/lib/active_relation/relations/order_relation.rb +++ b/lib/active_relation/relations/order_relation.rb @@ -1,15 +1,19 @@ -class OrderRelation < CompoundRelation - attr_reader :relation, :orders +module ActiveRelation + module Relations + class Order < Compound + attr_reader :relation, :orders - def initialize(relation, *orders) - @relation, @orders = relation, orders - end + def initialize(relation, *orders) + @relation, @orders = relation, orders + end - def ==(other) - relation == other.relation and orders.eql?(other.orders) - end + def ==(other) + relation == other.relation and orders.eql?(other.orders) + end - def qualify - OrderRelation.new(relation.qualify, *orders.collect { |o| o.qualify }) + def qualify + Order.new(relation.qualify, *orders.collect { |o| o.qualify }) + end + end end end
\ No newline at end of file diff --git a/lib/active_relation/relations/projection_relation.rb b/lib/active_relation/relations/projection_relation.rb index 53b0ad1e91..b30c76898d 100644 --- a/lib/active_relation/relations/projection_relation.rb +++ b/lib/active_relation/relations/projection_relation.rb @@ -1,15 +1,19 @@ -class ProjectionRelation < CompoundRelation - attr_reader :relation, :attributes +module ActiveRelation + module Relations + class Projection < Compound + attr_reader :relation, :attributes - def initialize(relation, *attributes) - @relation, @attributes = relation, attributes - end + def initialize(relation, *attributes) + @relation, @attributes = relation, attributes + end - def ==(other) - relation == other.relation and attributes.eql?(other.attributes) - end + def ==(other) + relation == other.relation and attributes.eql?(other.attributes) + end - def qualify - ProjectionRelation.new(relation.qualify, *attributes.collect(&:qualify)) + def qualify + Projection.new(relation.qualify, *attributes.collect(&:qualify)) + end + end end end
\ No newline at end of file diff --git a/lib/active_relation/relations/range_relation.rb b/lib/active_relation/relations/range_relation.rb index 6a2b0b3470..d7e08efa06 100644 --- a/lib/active_relation/relations/range_relation.rb +++ b/lib/active_relation/relations/range_relation.rb @@ -1,19 +1,23 @@ -class RangeRelation < CompoundRelation - attr_reader :range +module ActiveRelation + module Relations + class Range < Compound + attr_reader :range - def initialize(relation, range) - @relation, @range = relation, range - end + def initialize(relation, range) + @relation, @range = relation, range + end - def ==(other) - relation == other.relation and range == other.range - end + def ==(other) + relation == other.relation and range == other.range + end - def limit - range.end - range.begin + 1 - end + def limit + range.end - range.begin + 1 + end - def offset - range.begin + def offset + range.begin + end + end end end
\ No newline at end of file diff --git a/lib/active_relation/relations/relation.rb b/lib/active_relation/relations/relation.rb index 0a8455b39c..c4a887eecd 100644 --- a/lib/active_relation/relations/relation.rb +++ b/lib/active_relation/relations/relation.rb @@ -1,90 +1,111 @@ -class Relation - include SqlBuilder +module ActiveRelation + module Relations + class Base + include SqlBuilder - module Iteration - include Enumerable + module Iteration + include Enumerable - def each(&block) - connection.select_all(to_s).each(&block) - end + def each(&block) + connection.select_all(to_s).each(&block) + end - def first - connection.select_one(to_s) - end - end - include Iteration + def first + connection.select_one(to_s) + end + end + include Iteration - module Operations - def <=>(other) - InnerJoinOperation.new(self, other) - end + module Operations + def <=>(other) + JoinOperation.new("INNER JOIN", self, other) + end - def <<(other) - LeftOuterJoinOperation.new(self, other) - end + def <<(other) + JoinOperation.new("LEFT OUTER JOIN", self, other) + end - def [](index) - case index - when Symbol - attribute(index) - when Range - RangeRelation.new(self, index) - end - end + def [](index) + case index + when Symbol + attribute(index) + when ::Range + Range.new(self, index) + end + end - def include?(attribute) - RelationInclusionPredicate.new(attribute, self) - end + def include?(attribute) + Predicates::RelationInclusion.new(attribute, self) + end - def select(*predicates) - SelectionRelation.new(self, *predicates) - end + def select(*s) + Selection.new(self, *s) + end - def project(*attributes) - ProjectionRelation.new(self, *attributes) - end + def project(*attributes) + Projection.new(self, *attributes) + end - def order(*attributes) - OrderRelation.new(self, *attributes) - end + def order(*attributes) + Order.new(self, *attributes) + end - def rename(attribute, aliaz) - RenameRelation.new(self, attribute => aliaz) - end + def rename(attribute, aliaz) + Rename.new(self, attribute => aliaz) + end - def insert(record) - InsertionRelation.new(self, record) - end + def insert(record) + Insertion.new(self, record) + end - def delete - DeletionRelation.new(self) - end - end - include Operations + def delete + Deletion.new(self) + end + + class JoinOperation + attr_reader :join_sql, :relation1, :relation2 + + def initialize(join_sql, relation1, relation2) + @join_sql, @relation1, @relation2 = join_sql, relation1, relation2 + end + + def on(*predicates) + Join.new(join_sql, relation1, relation2, *predicates) + end + + def ==(other) + (relation1 == other.relation1 and relation2 == other.relation2) or + (relation1 == other.relation2 and relation2 == other.relation1) + end + end + end + include Operations - def connection - ActiveRecord::Base.connection - end + def connection + ActiveRecord::Base.connection + end - def to_sql(options = {}) - [ - "SELECT #{attributes.collect{ |a| a.to_sql(:use_alias => true) }.join(', ')}", - "FROM #{quote_table_name(table)}", - (joins.to_sql(:quote => false) unless joins.blank?), - ("WHERE #{selects.collect{|s| s.to_sql(:quote => false)}.join("\n\tAND ")}" unless selects.blank?), - ("ORDER BY #{orders.collect(&:to_sql)}" unless orders.blank?), - ("LIMIT #{limit.to_sql}" unless limit.blank?), - ("OFFSET #{offset.to_sql}" unless offset.blank?) - ].compact.join("\n") - end - alias_method :to_s, :to_sql + def to_sql(options = {}) + [ + "SELECT #{attributes.collect{ |a| a.to_sql(:use_alias => true) }.join(', ')}", + "FROM #{quote_table_name(table)}", + (joins.to_sql(:quote => false) unless joins.blank?), + ("WHERE #{selects.collect{|s| s.to_sql(:quote => false)}.join("\n\tAND ")}" unless selects.blank?), + ("ORDER BY #{orders.collect(&:to_sql)}" unless orders.blank?), + ("LIMIT #{limit.to_sql}" unless limit.blank?), + ("OFFSET #{offset.to_sql}" unless offset.blank?) + ].compact.join("\n") + end + alias_method :to_s, :to_sql - protected - def attributes; [] end - def selects; [] end - def orders; [] end - def inserts; [] end - def joins; nil end - def limit; nil end - def offset; nil end + protected + def attributes; [] end + def selects; [] end + def orders; [] end + def inserts; [] end + def joins; nil end + def limit; nil end + def offset; nil end + end + end end
\ No newline at end of file diff --git a/lib/active_relation/relations/rename_relation.rb b/lib/active_relation/relations/rename_relation.rb index 3218889f33..7a1693df57 100644 --- a/lib/active_relation/relations/rename_relation.rb +++ b/lib/active_relation/relations/rename_relation.rb @@ -1,34 +1,38 @@ -class RenameRelation < CompoundRelation - attr_reader :relation, :schmattribute, :alias +module ActiveRelation + module Relations + class Rename < Compound + attr_reader :relation, :schmattribute, :alias - def initialize(relation, renames) - @schmattribute, @alias = renames.shift - @relation = renames.empty?? relation : RenameRelation.new(relation, renames) - end + def initialize(relation, renames) + @schmattribute, @alias = renames.shift + @relation = renames.empty?? relation : Rename.new(relation, renames) + end - def ==(other) - relation == other.relation and schmattribute.eql?(other.schmattribute) and self.alias == other.alias - end + def ==(other) + relation == other.relation and schmattribute.eql?(other.schmattribute) and self.alias == other.alias + end - def attributes - relation.attributes.collect { |a| substitute(a) } - end + def attributes + relation.attributes.collect { |a| substitute(a) } + end - def qualify - RenameRelation.new(relation.qualify, schmattribute.qualify => self.alias) - end + def qualify + Rename.new(relation.qualify, schmattribute.qualify => self.alias) + end - protected - def attribute(name) - case - when name == self.alias then schmattribute.alias(self.alias) - when relation[name].eql?(schmattribute) then nil - else relation[name] - end - end + protected + def attribute(name) + case + when name == self.alias then schmattribute.alias(self.alias) + when relation[name].eql?(schmattribute) then nil + else relation[name] + end + end - private - def substitute(a) - a.eql?(schmattribute) ? a.alias(self.alias) : a + private + def substitute(a) + a.eql?(schmattribute) ? a.alias(self.alias) : a + end + end end end
\ No newline at end of file diff --git a/lib/active_relation/relations/selection_relation.rb b/lib/active_relation/relations/selection_relation.rb index 77864efb28..e102d105a0 100644 --- a/lib/active_relation/relations/selection_relation.rb +++ b/lib/active_relation/relations/selection_relation.rb @@ -1,21 +1,25 @@ -class SelectionRelation < CompoundRelation - attr_reader :relation, :predicate +module ActiveRelation + module Relations + class Selection < Compound + attr_reader :relation, :predicate - def initialize(relation, *predicates) - @predicate = predicates.shift - @relation = predicates.empty?? relation : SelectionRelation.new(relation, *predicates) - end + def initialize(relation, *predicates) + @predicate = predicates.shift + @relation = predicates.empty?? relation : Selection.new(relation, *predicates) + end - def ==(other) - relation == other.relation and predicate == other.predicate - end + def ==(other) + relation == other.relation and predicate == other.predicate + end - def qualify - SelectionRelation.new(relation.qualify, predicate.qualify) - end + def qualify + Selection.new(relation.qualify, predicate.qualify) + end - protected - def selects - relation.send(:selects) + [predicate] + protected + def selects + relation.send(:selects) + [predicate] + end + end end end
\ No newline at end of file diff --git a/lib/active_relation/relations/table_relation.rb b/lib/active_relation/relations/table_relation.rb index 5a47ae7a34..38f540cc52 100644 --- a/lib/active_relation/relations/table_relation.rb +++ b/lib/active_relation/relations/table_relation.rb @@ -1,31 +1,35 @@ -class TableRelation < Relation - attr_reader :table +module ActiveRelation + module Relations + class Table < Base + attr_reader :table - def initialize(table) - @table = table - end + def initialize(table) + @table = table + end - def attributes - attributes_by_name.values - end + def attributes + attributes_by_name.values + end - def qualify - RenameRelation.new self, qualifications - end + def qualify + Rename.new self, qualifications + end - protected - def attribute(name) - attributes_by_name[name.to_s] - end + protected + def attribute(name) + attributes_by_name[name.to_s] + end - private - def attributes_by_name - @attributes_by_name ||= connection.columns(table, "#{table} Columns").inject({}) do |attributes_by_name, column| - attributes_by_name.merge(column.name => Attribute.new(self, column.name.to_sym)) - end - end + private + def attributes_by_name + @attributes_by_name ||= connection.columns(table, "#{table} Columns").inject({}) do |attributes_by_name, column| + attributes_by_name.merge(column.name => ActiveRelation::Primitives::Attribute.new(self, column.name.to_sym)) + end + end - def qualifications - attributes.zip(attributes.collect(&:qualified_name)).to_hash + def qualifications + attributes.zip(attributes.collect(&:qualified_name)).to_hash + end + end end end
\ No newline at end of file diff --git a/lib/active_relation/sql_builder.rb b/lib/active_relation/sql_builder.rb index 0d2187173e..07a4ebabb7 100644 --- a/lib/active_relation/sql_builder.rb +++ b/lib/active_relation/sql_builder.rb @@ -1,7 +1,9 @@ -module SqlBuilder - def connection - ActiveRecord::Base.connection - end +module ActiveRelation + module SqlBuilder + def connection + ActiveRecord::Base.connection + end - delegate :quote_table_name, :quote_column_name, :quote, :to => :connection + delegate :quote_table_name, :quote_column_name, :quote, :to => :connection + end end
\ No newline at end of file diff --git a/spec/active_relation/predicates/binary_predicate_spec.rb b/spec/active_relation/predicates/binary_predicate_spec.rb index 0bddae8491..02c72ef96d 100644 --- a/spec/active_relation/predicates/binary_predicate_spec.rb +++ b/spec/active_relation/predicates/binary_predicate_spec.rb @@ -1,12 +1,12 @@ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper') -describe BinaryPredicate do +describe ActiveRelation::Predicates::Binary do before do - @relation1 = TableRelation.new(:foo) - @relation2 = TableRelation.new(:bar) - @attribute1 = Attribute.new(@relation1, :name1) - @attribute2 = Attribute.new(@relation2, :name2) - class ConcreteBinaryPredicate < BinaryPredicate + @relation1 = ActiveRelation::Relations::Table.new(:foo) + @relation2 = ActiveRelation::Relations::Table.new(:bar) + @attribute1 = ActiveRelation::Primitives::Attribute.new(@relation1, :name1) + @attribute2 = ActiveRelation::Primitives::Attribute.new(@relation2, :name2) + class ActiveRelation::Predicates::ConcreteBinary < ActiveRelation::Predicates::Binary def predicate_sql "<=>" end @@ -15,26 +15,26 @@ describe BinaryPredicate do describe '==' do it "obtains if attribute1 and attribute2 are identical" do - BinaryPredicate.new(@attribute1, @attribute2).should == BinaryPredicate.new(@attribute1, @attribute2) - BinaryPredicate.new(@attribute1, @attribute2).should_not == BinaryPredicate.new(@attribute1, @attribute1) + ActiveRelation::Predicates::Binary.new(@attribute1, @attribute2).should == ActiveRelation::Predicates::Binary.new(@attribute1, @attribute2) + ActiveRelation::Predicates::Binary.new(@attribute1, @attribute2).should_not == ActiveRelation::Predicates::Binary.new(@attribute1, @attribute1) end - it "obtains if the concrete type of the BinaryPredicates are identical" do - ConcreteBinaryPredicate.new(@attribute1, @attribute2).should == ConcreteBinaryPredicate.new(@attribute1, @attribute2) - BinaryPredicate.new(@attribute1, @attribute2).should_not == ConcreteBinaryPredicate.new(@attribute1, @attribute2) + it "obtains if the concrete type of the ActiveRelation::Predicates::Binarys are identical" do + ActiveRelation::Predicates::Binary.new(@attribute1, @attribute2).should == ActiveRelation::Predicates::Binary.new(@attribute1, @attribute2) + ActiveRelation::Predicates::Binary.new(@attribute1, @attribute2).should_not == ActiveRelation::Predicates::ConcreteBinary.new(@attribute1, @attribute2) end end describe '#qualify' do it "distributes over the predicates and attributes" do - ConcreteBinaryPredicate.new(@attribute1, @attribute2).qualify. \ - should == ConcreteBinaryPredicate.new(@attribute1.qualify, @attribute2.qualify) + ActiveRelation::Predicates::ConcreteBinary.new(@attribute1, @attribute2).qualify. \ + should == ActiveRelation::Predicates::ConcreteBinary.new(@attribute1.qualify, @attribute2.qualify) end end describe '#to_sql' do it 'manufactures correct sql' do - ConcreteBinaryPredicate.new(@attribute1, @attribute2).to_sql.should be_like(""" + ActiveRelation::Predicates::ConcreteBinary.new(@attribute1, @attribute2).to_sql.should be_like(""" `foo`.`name1` <=> `bar`.`name2` """) end diff --git a/spec/active_relation/predicates/equality_predicate_spec.rb b/spec/active_relation/predicates/equality_predicate_spec.rb index af43b754e0..b3c7b597a0 100644 --- a/spec/active_relation/predicates/equality_predicate_spec.rb +++ b/spec/active_relation/predicates/equality_predicate_spec.rb @@ -1,25 +1,25 @@ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper') -describe EqualityPredicate do +describe ActiveRelation::Predicates::Equality do before do - @relation1 = TableRelation.new(:foo) - @relation2 = TableRelation.new(:bar) - @attribute1 = Attribute.new(@relation1, :name) - @attribute2 = Attribute.new(@relation2, :name) + @relation1 = ActiveRelation::Relations::Table.new(:foo) + @relation2 = ActiveRelation::Relations::Table.new(:bar) + @attribute1 = ActiveRelation::Primitives::Attribute.new(@relation1, :name) + @attribute2 = ActiveRelation::Primitives::Attribute.new(@relation2, :name) end describe '==' do it "obtains if attribute1 and attribute2 are identical" do - EqualityPredicate.new(@attribute1, @attribute2).should == EqualityPredicate.new(@attribute1, @attribute2) - EqualityPredicate.new(@attribute1, @attribute2).should_not == EqualityPredicate.new(@attribute1, @attribute1) + ActiveRelation::Predicates::Equality.new(@attribute1, @attribute2).should == ActiveRelation::Predicates::Equality.new(@attribute1, @attribute2) + ActiveRelation::Predicates::Equality.new(@attribute1, @attribute2).should_not == ActiveRelation::Predicates::Equality.new(@attribute1, @attribute1) end it "obtains if the concrete type of the predicates are identical" do - EqualityPredicate.new(@attribute1, @attribute2).should_not == BinaryPredicate.new(@attribute1, @attribute2) + ActiveRelation::Predicates::Equality.new(@attribute1, @attribute2).should_not == ActiveRelation::Predicates::Binary.new(@attribute1, @attribute2) end it "is commutative on the attributes" do - EqualityPredicate.new(@attribute1, @attribute2).should == EqualityPredicate.new(@attribute2, @attribute1) + ActiveRelation::Predicates::Equality.new(@attribute1, @attribute2).should == ActiveRelation::Predicates::Equality.new(@attribute2, @attribute1) end end end
\ No newline at end of file diff --git a/spec/active_relation/predicates/relation_inclusion_predicate_spec.rb b/spec/active_relation/predicates/relation_inclusion_predicate_spec.rb index f8c911429b..a01f4fb76b 100644 --- a/spec/active_relation/predicates/relation_inclusion_predicate_spec.rb +++ b/spec/active_relation/predicates/relation_inclusion_predicate_spec.rb @@ -1,16 +1,16 @@ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper') -describe RelationInclusionPredicate do +describe ActiveRelation::Predicates::RelationInclusion do before do - @relation1 = TableRelation.new(:foo) - @relation2 = TableRelation.new(:bar) + @relation1 = ActiveRelation::Relations::Table.new(:foo) + @relation2 = ActiveRelation::Relations::Table.new(:bar) @attribute = @relation1[:baz] end - describe RelationInclusionPredicate, '==' do + describe ActiveRelation::Predicates::RelationInclusion, '==' do it "obtains if attribute1 and attribute2 are identical" do - RelationInclusionPredicate.new(@attribute, @relation1).should == RelationInclusionPredicate.new(@attribute, @relation1) - RelationInclusionPredicate.new(@attribute, @relation1).should_not == RelationInclusionPredicate.new(@attribute, @relation2) + ActiveRelation::Predicates::RelationInclusion.new(@attribute, @relation1).should == ActiveRelation::Predicates::RelationInclusion.new(@attribute, @relation1) + ActiveRelation::Predicates::RelationInclusion.new(@attribute, @relation1).should_not == ActiveRelation::Predicates::RelationInclusion.new(@attribute, @relation2) end end end
\ No newline at end of file diff --git a/spec/active_relation/relations/attribute_spec.rb b/spec/active_relation/relations/attribute_spec.rb index 4154a91b85..71bba26aff 100644 --- a/spec/active_relation/relations/attribute_spec.rb +++ b/spec/active_relation/relations/attribute_spec.rb @@ -1,14 +1,14 @@ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper') -describe Attribute do +describe ActiveRelation::Primitives::Attribute do before do - @relation1 = TableRelation.new(:foo) - @relation2 = TableRelation.new(:bar) + @relation1 = ActiveRelation::Relations::Table.new(:foo) + @relation2 = ActiveRelation::Relations::Table.new(:bar) end describe '#alias' do it "manufactures an aliased attributed" do - @relation1[:id].alias(:alias).should == Attribute.new(@relation1, :id, :alias) + @relation1[:id].alias(:alias).should == ActiveRelation::Primitives::Attribute.new(@relation1, :id, :alias) end end @@ -26,51 +26,51 @@ describe Attribute do describe '#eql?' do it "obtains if the relation and attribute name are identical" do - Attribute.new(@relation1, :name).should be_eql(Attribute.new(@relation1, :name)) - Attribute.new(@relation1, :name).should_not be_eql(Attribute.new(@relation1, :another_name)) - Attribute.new(@relation1, :name).should_not be_eql(Attribute.new(@relation2, :name)) + ActiveRelation::Primitives::Attribute.new(@relation1, :name).should be_eql(ActiveRelation::Primitives::Attribute.new(@relation1, :name)) + ActiveRelation::Primitives::Attribute.new(@relation1, :name).should_not be_eql(ActiveRelation::Primitives::Attribute.new(@relation1, :another_name)) + ActiveRelation::Primitives::Attribute.new(@relation1, :name).should_not be_eql(ActiveRelation::Primitives::Attribute.new(@relation2, :name)) end end describe 'predications' do before do - @attribute1 = Attribute.new(@relation1, :name) - @attribute2 = Attribute.new(@relation2, :name) + @attribute1 = ActiveRelation::Primitives::Attribute.new(@relation1, :name) + @attribute2 = ActiveRelation::Primitives::Attribute.new(@relation2, :name) end describe '==' do it "manufactures an equality predicate" do - (@attribute1 == @attribute2).should == EqualityPredicate.new(@attribute1, @attribute2) + (@attribute1 == @attribute2).should == ActiveRelation::Predicates::Equality.new(@attribute1, @attribute2) end end describe '<' do it "manufactures a less-than predicate" do - (@attribute1 < @attribute2).should == LessThanPredicate.new(@attribute1, @attribute2) + (@attribute1 < @attribute2).should == ActiveRelation::Predicates::LessThan.new(@attribute1, @attribute2) end end describe '<=' do it "manufactures a less-than or equal-to predicate" do - (@attribute1 <= @attribute2).should == LessThanOrEqualToPredicate.new(@attribute1, @attribute2) + (@attribute1 <= @attribute2).should == ActiveRelation::Predicates::LessThanOrEqualTo.new(@attribute1, @attribute2) end end describe '>' do it "manufactures a greater-than predicate" do - (@attribute1 > @attribute2).should == GreaterThanPredicate.new(@attribute1, @attribute2) + (@attribute1 > @attribute2).should == ActiveRelation::Predicates::GreaterThan.new(@attribute1, @attribute2) end end describe '>=' do it "manufactures a greater-than or equal to predicate" do - (@attribute1 >= @attribute2).should == GreaterThanOrEqualToPredicate.new(@attribute1, @attribute2) + (@attribute1 >= @attribute2).should == ActiveRelation::Predicates::GreaterThanOrEqualTo.new(@attribute1, @attribute2) end end describe '=~' do it "manufactures a match predicate" do - (@attribute1 =~ /.*/).should == MatchPredicate.new(@attribute1, @attribute2) + (@attribute1 =~ /.*/).should == ActiveRelation::Predicates::Match.new(@attribute1, @attribute2) end end end diff --git a/spec/active_relation/relations/deletion_relation_spec.rb b/spec/active_relation/relations/deletion_relation_spec.rb index 5e8c5137b2..b80589bde6 100644 --- a/spec/active_relation/relations/deletion_relation_spec.rb +++ b/spec/active_relation/relations/deletion_relation_spec.rb @@ -1,20 +1,20 @@ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper') -describe DeletionRelation do +describe ActiveRelation::Relations::Deletion do before do - @relation = TableRelation.new(:users) + @relation = ActiveRelation::Relations::Table.new(:users) end describe '#to_sql' do it 'manufactures sql deleting a table relation' do - DeletionRelation.new(@relation).to_sql.should be_like(""" + ActiveRelation::Relations::Deletion.new(@relation).to_sql.should be_like(""" DELETE FROM `users` """) end it 'manufactures sql deleting a selection relation' do - DeletionRelation.new(@relation.select(@relation[:id] == 1)).to_sql.should be_like(""" + ActiveRelation::Relations::Deletion.new(@relation.select(@relation[:id] == 1)).to_sql.should be_like(""" DELETE FROM `users` WHERE `users`.`id` = 1 diff --git a/spec/active_relation/relations/insertion_relation_spec.rb b/spec/active_relation/relations/insertion_relation_spec.rb index 177918a94a..da39edf773 100644 --- a/spec/active_relation/relations/insertion_relation_spec.rb +++ b/spec/active_relation/relations/insertion_relation_spec.rb @@ -1,13 +1,13 @@ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper') -describe InsertionRelation do +describe ActiveRelation::Relations::Insertion do before do - @relation = TableRelation.new(:users) + @relation = ActiveRelation::Relations::Table.new(:users) end describe '#to_sql' do it 'manufactures sql inserting the data for one item' do - InsertionRelation.new(@relation, @relation[:name] => "nick").to_sql.should be_like(""" + ActiveRelation::Relations::Insertion.new(@relation, @relation[:name] => "nick").to_sql.should be_like(""" INSERT INTO `users` (`users`.`name`) VALUES ('nick') @@ -15,8 +15,8 @@ describe InsertionRelation do end it 'manufactures sql inserting the data for multiple items' do - nested_insertion = InsertionRelation.new(@relation, @relation[:name] => "cobra") - InsertionRelation.new(nested_insertion, nested_insertion[:name] => "commander").to_sql.should be_like(""" + nested_insertion = ActiveRelation::Relations::Insertion.new(@relation, @relation[:name] => "cobra") + ActiveRelation::Relations::Insertion.new(nested_insertion, nested_insertion[:name] => "commander").to_sql.should be_like(""" INSERT INTO `users` (`users`.`name`) VALUES ('cobra'), ('commander') diff --git a/spec/active_relation/relations/join_operation_spec.rb b/spec/active_relation/relations/join_operation_spec.rb deleted file mode 100644 index a8ab85123b..0000000000 --- a/spec/active_relation/relations/join_operation_spec.rb +++ /dev/null @@ -1,39 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper') - -describe 'between two relations' do - before do - @relation1 = TableRelation.new(:foo) - @relation2 = TableRelation.new(:bar) - end - - describe '==' do - it "obtains if the relations of both joins are identical" do - JoinOperation.new(@relation1, @relation2).should == JoinOperation.new(@relation1, @relation2) - JoinOperation.new(@relation1, @relation2).should_not == JoinOperation.new(@relation1, @relation1) - end - - it "is commutative on the relations" do - JoinOperation.new(@relation1, @relation2).should == JoinOperation.new(@relation2, @relation1) - end - end - - describe 'on' do - before do - @predicate = Predicate.new - @join_operation = JoinOperation.new(@relation1, @relation2) - class << @join_operation - def relation_class - JoinRelation - end - end - end - - it "manufactures a join relation of the appropriate type" do - @join_operation.on(@predicate).should == JoinRelation.new(@relation1, @relation2, @predicate) - end - - it "accepts arbitrary strings" do - @join_operation.on("arbitrary").should == JoinRelation.new(@relation1, @relation2, "arbitrary") - end - end -end
\ No newline at end of file diff --git a/spec/active_relation/relations/join_relation_spec.rb b/spec/active_relation/relations/join_relation_spec.rb index 3e60cc4c34..32771428a8 100644 --- a/spec/active_relation/relations/join_relation_spec.rb +++ b/spec/active_relation/relations/join_relation_spec.rb @@ -1,27 +1,27 @@ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper') -describe JoinRelation do +describe ActiveRelation::Relations::Join do before do - @relation1 = TableRelation.new(:foo) - @relation2 = TableRelation.new(:bar) - @predicate = EqualityPredicate.new(@relation1[:id], @relation2[:id]) + @relation1 = ActiveRelation::Relations::Table.new(:foo) + @relation2 = ActiveRelation::Relations::Table.new(:bar) + @predicate = ActiveRelation::Predicates::Equality.new(@relation1[:id], @relation2[:id]) end describe '==' do it 'obtains if the two relations and the predicate are identical' do - JoinRelation.new(@relation1, @relation2, @predicate).should == JoinRelation.new(@relation1, @relation2, @predicate) - JoinRelation.new(@relation1, @relation2, @predicate).should_not == JoinRelation.new(@relation1, @relation1, @predicate) + ActiveRelation::Relations::Join.new("INNER JOIN", @relation1, @relation2, @predicate).should == ActiveRelation::Relations::Join.new("INNER JOIN", @relation1, @relation2, @predicate) + ActiveRelation::Relations::Join.new("INNER JOIN", @relation1, @relation2, @predicate).should_not == ActiveRelation::Relations::Join.new("INNER JOIN", @relation1, @relation1, @predicate) end it 'is commutative on the relations' do - JoinRelation.new(@relation1, @relation2, @predicate).should == JoinRelation.new(@relation2, @relation1, @predicate) + ActiveRelation::Relations::Join.new("INNER JOIN", @relation1, @relation2, @predicate).should == ActiveRelation::Relations::Join.new("INNER JOIN", @relation2, @relation1, @predicate) end end describe '#qualify' do it 'distributes over the relations and predicates' do - InnerJoinRelation.new(@relation1, @relation2, @predicate).qualify. \ - should == InnerJoinRelation.new(@relation1.qualify, @relation2.qualify, @predicate.qualify) + ActiveRelation::Relations::Join.new("INNER JOIN", @relation1, @relation2, @predicate).qualify. \ + should == ActiveRelation::Relations::Join.new("INNER JOIN", @relation1.qualify, @relation2.qualify, @predicate.qualify) end end @@ -31,7 +31,7 @@ describe JoinRelation do end it 'manufactures sql joining the two tables on the predicate, merging the selects' do - InnerJoinRelation.new(@relation1, @relation2, @predicate).to_sql.should be_like(""" + ActiveRelation::Relations::Join.new("INNER JOIN", @relation1, @relation2, @predicate).to_sql.should be_like(""" SELECT `foo`.`name`, `foo`.`id`, `bar`.`name`, `bar`.`foo_id`, `bar`.`id` FROM `foo` INNER JOIN `bar` ON `foo`.`id` = `bar`.`id` diff --git a/spec/active_relation/relations/order_relation_spec.rb b/spec/active_relation/relations/order_relation_spec.rb index 8655780c37..edf2faf455 100644 --- a/spec/active_relation/relations/order_relation_spec.rb +++ b/spec/active_relation/relations/order_relation_spec.rb @@ -1,23 +1,23 @@ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper') -describe OrderRelation do +describe ActiveRelation::Relations::Order do before do - @relation1 = TableRelation.new(:foo) - @relation2 = TableRelation.new(:bar) + @relation1 = ActiveRelation::Relations::Table.new(:foo) + @relation2 = ActiveRelation::Relations::Table.new(:bar) @attribute1 = @relation1[:id] @attribute2 = @relation2[:id] end describe '#qualify' do it "distributes over the relation and attributes" do - OrderRelation.new(@relation1, @attribute1).qualify. \ - should == OrderRelation.new(@relation1.qualify, @attribute1.qualify) + ActiveRelation::Relations::Order.new(@relation1, @attribute1).qualify. \ + should == ActiveRelation::Relations::Order.new(@relation1.qualify, @attribute1.qualify) end end describe '#to_sql' do it "manufactures sql with an order clause" do - OrderRelation.new(@relation1, @attribute1).to_sql.should be_like(""" + ActiveRelation::Relations::Order.new(@relation1, @attribute1).to_sql.should be_like(""" SELECT `foo`.`name`, `foo`.`id` FROM `foo` ORDER BY `foo`.`id` diff --git a/spec/active_relation/relations/projection_relation_spec.rb b/spec/active_relation/relations/projection_relation_spec.rb index 77722a17c5..8ba571e06c 100644 --- a/spec/active_relation/relations/projection_relation_spec.rb +++ b/spec/active_relation/relations/projection_relation_spec.rb @@ -1,31 +1,31 @@ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper') -describe ProjectionRelation do +describe ActiveRelation::Relations::Projection do before do - @relation1 = TableRelation.new(:foo) - @relation2 = TableRelation.new(:bar) + @relation1 = ActiveRelation::Relations::Table.new(:foo) + @relation2 = ActiveRelation::Relations::Table.new(:bar) @attribute1 = @relation1[:id] @attribute2 = @relation2[:id] end 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) + ActiveRelation::Relations::Projection.new(@relation1, @attribute1, @attribute2).should == ActiveRelation::Relations::Projection.new(@relation1, @attribute1, @attribute2) + ActiveRelation::Relations::Projection.new(@relation1, @attribute1).should_not == ActiveRelation::Relations::Projection.new(@relation2, @attribute1) + ActiveRelation::Relations::Projection.new(@relation1, @attribute1).should_not == ActiveRelation::Relations::Projection.new(@relation1, @attribute2) end end describe '#qualify' do it "distributes over teh relation and attributes" do - ProjectionRelation.new(@relation1, @attribute1).qualify. \ - should == ProjectionRelation.new(@relation1.qualify, @attribute1.qualify) + ActiveRelation::Relations::Projection.new(@relation1, @attribute1).qualify. \ + should == ActiveRelation::Relations::Projection.new(@relation1.qualify, @attribute1.qualify) end end describe '#to_sql' do it "manufactures sql with a limited select clause" do - ProjectionRelation.new(@relation1, @attribute1).to_sql.should be_like(""" + ActiveRelation::Relations::Projection.new(@relation1, @attribute1).to_sql.should be_like(""" SELECT `foo`.`id` FROM `foo` """) diff --git a/spec/active_relation/relations/range_relation_spec.rb b/spec/active_relation/relations/range_relation_spec.rb index 67e2b0d164..d4107259aa 100644 --- a/spec/active_relation/relations/range_relation_spec.rb +++ b/spec/active_relation/relations/range_relation_spec.rb @@ -1,9 +1,9 @@ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper') -describe RangeRelation do +describe ActiveRelation::Relations::Range do before do - @relation1 = TableRelation.new(:foo) - @relation2 = TableRelation.new(:bar) + @relation1 = ActiveRelation::Relations::Table.new(:foo) + @relation2 = ActiveRelation::Relations::Table.new(:bar) @range1 = 1..2 @range2 = 4..9 end @@ -18,7 +18,7 @@ describe RangeRelation 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_s.should be_like(""" + ActiveRelation::Relations::Range.new(@relation1, @range2).to_s.should be_like(""" SELECT `foo`.`name`, `foo`.`id` FROM `foo` LIMIT #{range_size} diff --git a/spec/active_relation/relations/relation_spec.rb b/spec/active_relation/relations/relation_spec.rb index 5d7c40a530..caee3bb527 100644 --- a/spec/active_relation/relations/relation_spec.rb +++ b/spec/active_relation/relations/relation_spec.rb @@ -1,40 +1,44 @@ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper') -describe Relation do +describe ActiveRelation::Relations::Base do before do - @relation1 = TableRelation.new(:foo) - @relation2 = TableRelation.new(:bar) - @attribute1 = Attribute.new(@relation1, :id) - @attribute2 = Attribute.new(@relation1, :name) + @relation1 = ActiveRelation::Relations::Table.new(:foo) + @relation2 = ActiveRelation::Relations::Table.new(:bar) + @attribute1 = ActiveRelation::Primitives::Attribute.new(@relation1, :id) + @attribute2 = ActiveRelation::Primitives::Attribute.new(@relation1, :name) end describe '[]' do it "manufactures an attribute when given a symbol" do - @relation1[:id].should be_kind_of(Attribute) + @relation1[:id].should == ActiveRelation::Primitives::Attribute.new(@relation1, :id) end it "manufactures a range relation when given a range" do - @relation1[1..2].should be_kind_of(RangeRelation) + @relation1[1..2].should == ActiveRelation::Relations::Range.new(@relation1, 1..2) end end describe '#include?' do it "manufactures an inclusion predicate" do - @relation1.include?(@attribute1).should be_kind_of(RelationInclusionPredicate) + @relation1.include?(@attribute1).should be_kind_of(ActiveRelation::Predicates::RelationInclusion) end end describe 'read operations' do describe 'joins' do + before do + @predicate = @relation1[:id] == @relation2[:id] + end + describe '<=>' do it "manufactures an inner join operation between those two relations" do - (@relation1 <=> @relation2).should be_kind_of(InnerJoinOperation) + (@relation1 <=> @relation2).on(@predicate).should == ActiveRelation::Relations::Join.new("INNER JOIN", @relation1, @relation2, @predicate) end end describe '<<' do it "manufactures a left outer join operation between those two relations" do - (@relation1 << @relation2).should be_kind_of(LeftOuterJoinOperation) + (@relation1 << @relation2).on(@predicate).should == ActiveRelation::Relations::Join.new("LEFT OUTER JOIN", @relation1, @relation2, @predicate) end end end @@ -45,33 +49,33 @@ describe Relation do end it "manufactures a projection relation" do - @relation1.project(@attribute1, @attribute2).should be_kind_of(ProjectionRelation) + @relation1.project(@attribute1, @attribute2).should be_kind_of(ActiveRelation::Relations::Projection) end end describe '#rename' do it "manufactures a rename relation" do - @relation1.rename(@attribute1, :foo).should be_kind_of(RenameRelation) + @relation1.rename(@attribute1, :foo).should be_kind_of(ActiveRelation::Relations::Rename) end end describe '#select' do before do - @predicate = EqualityPredicate.new(@attribute1, @attribute2) + @predicate = ActiveRelation::Predicates::Equality.new(@attribute1, @attribute2) end it "manufactures a selection relation" do - @relation1.select(@predicate).should be_kind_of(SelectionRelation) + @relation1.select(@predicate).should be_kind_of(ActiveRelation::Relations::Selection) end it "accepts arbitrary strings" do - @relation1.select("arbitrary").should be_kind_of(SelectionRelation) + @relation1.select("arbitrary").should be_kind_of(ActiveRelation::Relations::Selection) end end describe '#order' do it "manufactures an order relation" do - @relation1.order(@attribute1, @attribute2).should be_kind_of(OrderRelation) + @relation1.order(@attribute1, @attribute2).should be_kind_of(ActiveRelation::Relations::Order) end end end @@ -79,13 +83,13 @@ describe Relation do describe 'write operations' do describe '#delete' do it 'manufactures a deletion relation' do - @relation1.delete.should be_kind_of(DeletionRelation) + @relation1.delete.should be_kind_of(ActiveRelation::Relations::Deletion) end end describe '#insert' do it 'manufactures an insertion relation' do - @relation1.insert(record = {:id => 1}).should be_kind_of(InsertionRelation) + @relation1.insert(record = {:id => 1}).should be_kind_of(ActiveRelation::Relations::Insertion) end end end diff --git a/spec/active_relation/relations/rename_relation_spec.rb b/spec/active_relation/relations/rename_relation_spec.rb index 664e9c6145..6fac206ff1 100644 --- a/spec/active_relation/relations/rename_relation_spec.rb +++ b/spec/active_relation/relations/rename_relation_spec.rb @@ -1,15 +1,15 @@ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper') -describe RenameRelation do +describe ActiveRelation::Relations::Rename do before do - @relation = TableRelation.new(:foo) - @renamed_relation = RenameRelation.new(@relation, @relation[:id] => :schmid) + @relation = ActiveRelation::Relations::Table.new(:foo) + @renamed_relation = ActiveRelation::Relations::Rename.new(@relation, @relation[:id] => :schmid) end describe '#initialize' do it "manufactures nested rename relations if multiple renames are provided" do - RenameRelation.new(@relation, @relation[:id] => :humpty, @relation[:name] => :dumpty). \ - should == RenameRelation.new(RenameRelation.new(@relation, @relation[:id] => :humpty), @relation[:name] => :dumpty) + ActiveRelation::Relations::Rename.new(@relation, @relation[:id] => :humpty, @relation[:name] => :dumpty). \ + should == ActiveRelation::Relations::Rename.new(ActiveRelation::Relations::Rename.new(@relation, @relation[:id] => :humpty), @relation[:name] => :dumpty) end it "raises an exception if the alias provided is already used" do @@ -25,7 +25,7 @@ describe RenameRelation do describe '#attributes' do it "manufactures a list of attributes with the renamed attribute aliased" do - RenameRelation.new(@relation, @relation[:id] => :schmid).attributes.should == + ActiveRelation::Relations::Rename.new(@relation, @relation[:id] => :schmid).attributes.should == (@relation.attributes - [@relation[:id]]) + [@relation[:id].alias(:schmid)] end end @@ -45,8 +45,8 @@ describe RenameRelation do describe '#qualify' do it "distributes over the relation and renames" do - RenameRelation.new(@relation, @relation[:id] => :schmid).qualify. \ - should == RenameRelation.new(@relation.qualify, @relation[:id].qualify => :schmid) + ActiveRelation::Relations::Rename.new(@relation, @relation[:id] => :schmid).qualify. \ + should == ActiveRelation::Relations::Rename.new(@relation.qualify, @relation[:id].qualify => :schmid) end end diff --git a/spec/active_relation/relations/selection_relation_spec.rb b/spec/active_relation/relations/selection_relation_spec.rb index ac1f227c67..90dc3169b6 100644 --- a/spec/active_relation/relations/selection_relation_spec.rb +++ b/spec/active_relation/relations/selection_relation_spec.rb @@ -1,30 +1,30 @@ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper') -describe SelectionRelation do +describe ActiveRelation::Relations::Selection 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) + @relation1 = ActiveRelation::Relations::Table.new(:foo) + @relation2 = ActiveRelation::Relations::Table.new(:bar) + @predicate1 = ActiveRelation::Predicates::Equality.new(@relation1[:id], @relation2[:foo_id]) + @predicate2 = ActiveRelation::Predicates::LessThan.new(@relation1[:age], 2) end describe '#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) + ActiveRelation::Relations::Selection.new(@relation1, @predicate1, @predicate2). \ + should == ActiveRelation::Relations::Selection.new(ActiveRelation::Relations::Selection.new(@relation1, @predicate2), @predicate1) end end describe '#qualify' do it "distributes over the relation and predicates" do - SelectionRelation.new(@relation1, @predicate1).qualify. \ - should == SelectionRelation.new(@relation1.qualify, @predicate1.qualify) + ActiveRelation::Relations::Selection.new(@relation1, @predicate1).qualify. \ + should == ActiveRelation::Relations::Selection.new(@relation1.qualify, @predicate1.qualify) end end describe '#to_sql' do it "manufactures sql with where clause conditions" do - SelectionRelation.new(@relation1, @predicate1).to_sql.should be_like(""" + ActiveRelation::Relations::Selection.new(@relation1, @predicate1).to_sql.should be_like(""" SELECT `foo`.`name`, `foo`.`id` FROM `foo` WHERE `foo`.`id` = `bar`.`foo_id` @@ -32,7 +32,7 @@ describe SelectionRelation do end it "allows arbitrary sql" do - SelectionRelation.new(@relation1, "asdf").to_sql.should be_like(""" + ActiveRelation::Relations::Selection.new(@relation1, "asdf").to_sql.should be_like(""" SELECT `foo`.`name`, `foo`.`id` FROM `foo` WHERE asdf diff --git a/spec/active_relation/relations/table_relation_spec.rb b/spec/active_relation/relations/table_relation_spec.rb index 79e9610e1b..62b8e44980 100644 --- a/spec/active_relation/relations/table_relation_spec.rb +++ b/spec/active_relation/relations/table_relation_spec.rb @@ -1,8 +1,8 @@ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper') -describe TableRelation do +describe ActiveRelation::Relations::Table do before do - @relation = TableRelation.new(:users) + @relation = ActiveRelation::Relations::Table.new(:users) end describe '#to_sql' do @@ -22,8 +22,8 @@ describe TableRelation do describe '#qualify' do it 'manufactures a rename relation with all attribute names qualified' do - @relation.qualify.should == RenameRelation.new( - RenameRelation.new(@relation, @relation[:id] => 'users.id'), @relation[:name] => 'users.name' + @relation.qualify.should == ActiveRelation::Relations::Rename.new( + ActiveRelation::Relations::Rename.new(@relation, @relation[:id] => 'users.id'), @relation[:name] => 'users.name' ) end end |