diff options
author | Bryan Helmkamp <bryan@brynary.com> | 2009-05-17 14:20:29 -0400 |
---|---|---|
committer | Bryan Helmkamp <bryan@brynary.com> | 2009-05-17 14:24:06 -0400 |
commit | bdca9ed42ffea10aa6989ea3ecebedb424fa01ed (patch) | |
tree | 80682aa1c8fe544f1e91ccb8c529e66c8a1ddc94 /lib/arel/relations | |
parent | 2bbf8ca9d2af3ea959a21c3729b4894bc31f088b (diff) | |
download | rails-bdca9ed42ffea10aa6989ea3ecebedb424fa01ed.tar.gz rails-bdca9ed42ffea10aa6989ea3ecebedb424fa01ed.tar.bz2 rails-bdca9ed42ffea10aa6989ea3ecebedb424fa01ed.zip |
moved sql related code to its own engine area
Conflicts:
lib/arel/engine.rb
lib/arel/extensions/object.rb
lib/arel/predicates.rb
lib/arel/primitives/attribute.rb
lib/arel/primitives/expression.rb
lib/arel/primitives/value.rb
lib/arel/relations/operations/join.rb
lib/arel/relations/relation.rb
lib/arel/relations/utilities/externalization.rb
lib/arel/relations/utilities/nil.rb
lib/arel/relations/writes/delete.rb
lib/arel/relations/writes/insert.rb
lib/arel/relations/writes/update.rb
spec/arel/unit/relations/skip_spec.rb
spec/arel/unit/relations/take_spec.rb
spec/spec_helper.rb
Diffstat (limited to 'lib/arel/relations')
-rw-r--r-- | lib/arel/relations/array.rb | 19 | ||||
-rw-r--r-- | lib/arel/relations/operations/alias.rb | 1 | ||||
-rw-r--r-- | lib/arel/relations/operations/join.rb | 16 | ||||
-rw-r--r-- | lib/arel/relations/relation.rb | 32 | ||||
-rw-r--r-- | lib/arel/relations/table.rb | 36 | ||||
-rw-r--r-- | lib/arel/relations/utilities.rb | 2 | ||||
-rw-r--r-- | lib/arel/relations/utilities/externalization.rb | 10 | ||||
-rw-r--r-- | lib/arel/relations/utilities/nil.rb | 3 | ||||
-rw-r--r-- | lib/arel/relations/utilities/recursion.rb | 13 | ||||
-rw-r--r-- | lib/arel/relations/writes/delete.rb | 9 | ||||
-rw-r--r-- | lib/arel/relations/writes/insert.rb | 9 | ||||
-rw-r--r-- | lib/arel/relations/writes/update.rb | 11 |
12 files changed, 1 insertions, 160 deletions
diff --git a/lib/arel/relations/array.rb b/lib/arel/relations/array.rb deleted file mode 100644 index dac65abbc2..0000000000 --- a/lib/arel/relations/array.rb +++ /dev/null @@ -1,19 +0,0 @@ -module Arel - class Array < Relation - include Recursion::BaseCase - - def initialize(array, attribute_names) - @array, @attribute_names = array, attribute_names - end - - def attributes - @attributes ||= @attribute_names.collect do |name| - Attribute.new(self, name.to_sym) - end - end - - def call(connection = nil) - @array.collect { |row| attributes.zip(row).to_hash } - end - end -end
\ No newline at end of file diff --git a/lib/arel/relations/operations/alias.rb b/lib/arel/relations/operations/alias.rb index 8ed33fc597..67837f6a75 100644 --- a/lib/arel/relations/operations/alias.rb +++ b/lib/arel/relations/operations/alias.rb @@ -1,6 +1,5 @@ module Arel class Alias < Compound - include Recursion::BaseCase attributes :relation deriving :initialize alias_method :==, :equal? diff --git a/lib/arel/relations/operations/join.rb b/lib/arel/relations/operations/join.rb index 8fe89358d8..8e19254378 100644 --- a/lib/arel/relations/operations/join.rb +++ b/lib/arel/relations/operations/join.rb @@ -9,22 +9,6 @@ module Arel @join_sql, @relation1, @relation2, @predicates = join_sql, relation1, relation2, predicates end - def table_sql(formatter = Sql::TableReference.new(self)) - relation1.externalize.table_sql(formatter) - end - - def joins(environment, formatter = Sql::TableReference.new(environment)) - @joins ||= begin - this_join = [ - join_sql, - relation2.externalize.table_sql(formatter), - ("ON" unless predicates.blank?), - (ons + relation2.externalize.wheres).collect { |p| p.bind(environment).to_sql(Sql::WhereClause.new(environment)) }.join(' AND ') - ].compact.join(" ") - [relation1.joins(environment), this_join, relation2.joins(environment)].compact.join(" ") - end - end - def attributes @attributes ||= (relation1.externalize.attributes + relation2.externalize.attributes).collect { |a| a.bind(self) } diff --git a/lib/arel/relations/relation.rb b/lib/arel/relations/relation.rb index ef295dfdd7..20badaf165 100644 --- a/lib/arel/relations/relation.rb +++ b/lib/arel/relations/relation.rb @@ -6,32 +6,6 @@ module Arel Session.new end - def count - @count = "COUNT(*) AS count_all" - end - - def to_sql(formatter = Sql::SelectStatement.new(self)) - formatter.select select_sql, self - end - alias_method :to_s, :to_sql - - def select_sql - [ - "SELECT #{@count} #{attributes.collect { |a| a.to_sql(Sql::SelectClause.new(self)) }.join(', ') unless @count}", - "FROM #{table_sql(Sql::TableReference.new(self))}", - (joins(self) unless joins(self).blank? ), - ("WHERE #{wheres .collect { |w| w.to_sql(Sql::WhereClause.new(self)) }.join("\n\tAND ")}" unless wheres.blank? ), - ("GROUP BY #{groupings.collect { |g| g.to_sql(Sql::GroupClause.new(self)) }.join(', ')}" unless groupings.blank? ), - ("ORDER BY #{orders .collect { |o| o.to_sql(Sql::OrderClause.new(self)) }.join(', ')}" unless orders.blank? ), - ("LIMIT #{taken}" unless taken.blank? ), - ("OFFSET #{skipped}" unless skipped.blank? ) - ].compact.join("\n") - end - - def inclusion_predicate_sql - "IN" - end - def call engine.read(self) end @@ -44,10 +18,6 @@ module Arel self end - def christener - @christener ||= Sql::Christener.new - end - module Enumerable include ::Enumerable @@ -155,7 +125,7 @@ module Arel def orders; [] end def inserts; [] end def groupings; [] end - def joins(formatter = nil); nil end + def joins(formatter = nil); nil end # FIXME def taken; nil end def skipped; nil end end diff --git a/lib/arel/relations/table.rb b/lib/arel/relations/table.rb deleted file mode 100644 index 0433abbbb2..0000000000 --- a/lib/arel/relations/table.rb +++ /dev/null @@ -1,36 +0,0 @@ -module Arel - class Table < Relation - include Recursion::BaseCase - - cattr_accessor :engine - attr_reader :name, :engine - hash_on :name - - def initialize(name, engine = Table.engine) - @name, @engine = name.to_s, engine - end - - def attributes - @attributes ||= columns.collect do |column| - Attribute.new(self, column.name.to_sym) - end - end - - def column_for(attribute) - has_attribute?(attribute) and columns.detect { |c| c.name == attribute.name.to_s } - end - - def columns - @columns ||= engine.columns(name, "#{name} Columns") - end - - def reset - @attributes = @columns = nil - end - - def ==(other) - Table === other and - name == other.name - end - end -end diff --git a/lib/arel/relations/utilities.rb b/lib/arel/relations/utilities.rb index 454d455359..fbd949ef0c 100644 --- a/lib/arel/relations/utilities.rb +++ b/lib/arel/relations/utilities.rb @@ -1,5 +1,3 @@ require 'arel/relations/utilities/compound' -require 'arel/relations/utilities/recursion' require 'arel/relations/utilities/nil' require 'arel/relations/utilities/externalization' -require 'arel/relations/utilities/recursion'
\ No newline at end of file diff --git a/lib/arel/relations/utilities/externalization.rb b/lib/arel/relations/utilities/externalization.rb index 3b9b2296dc..bd067f2304 100644 --- a/lib/arel/relations/utilities/externalization.rb +++ b/lib/arel/relations/utilities/externalization.rb @@ -2,24 +2,14 @@ module Arel class Externalization < Compound attributes :relation deriving :initialize, :== - include Recursion::BaseCase def wheres [] end - def table_sql(formatter = Sql::TableReference.new(relation)) - formatter.select relation.select_sql, self - end - def attributes @attributes ||= relation.attributes.collect(&:to_attribute).collect { |a| a.bind(self) } end - - # REMOVEME - def name - relation.name + '_external' - end end class Relation diff --git a/lib/arel/relations/utilities/nil.rb b/lib/arel/relations/utilities/nil.rb index 56cf395d2c..6a9d678c45 100644 --- a/lib/arel/relations/utilities/nil.rb +++ b/lib/arel/relations/utilities/nil.rb @@ -3,8 +3,5 @@ require 'singleton' module Arel class Nil < Relation include Singleton - - def table_sql(formatter = nil); '' end - def name; '' end end end diff --git a/lib/arel/relations/utilities/recursion.rb b/lib/arel/relations/utilities/recursion.rb deleted file mode 100644 index 848b059507..0000000000 --- a/lib/arel/relations/utilities/recursion.rb +++ /dev/null @@ -1,13 +0,0 @@ -module Arel - module Recursion - module BaseCase - def table - self - end - - def table_sql(formatter = Sql::TableReference.new(self)) - formatter.table self - end - end - end -end
\ No newline at end of file diff --git a/lib/arel/relations/writes/delete.rb b/lib/arel/relations/writes/delete.rb index 0a04454e7f..a94067c7fa 100644 --- a/lib/arel/relations/writes/delete.rb +++ b/lib/arel/relations/writes/delete.rb @@ -3,15 +3,6 @@ module Arel attributes :relation deriving :initialize, :== - def to_sql(formatter = nil) - [ - "DELETE", - "FROM #{table_sql}", - ("WHERE #{wheres.collect(&:to_sql).join('\n\tAND ')}" unless wheres.blank? ), - ("LIMIT #{taken}" unless taken.blank? ), - ].compact.join("\n") - end - def call engine.delete(self) end diff --git a/lib/arel/relations/writes/insert.rb b/lib/arel/relations/writes/insert.rb index d05bd9cdc8..6d85e9769a 100644 --- a/lib/arel/relations/writes/insert.rb +++ b/lib/arel/relations/writes/insert.rb @@ -7,15 +7,6 @@ module Arel @relation, @record = relation, record.bind(relation) end - def to_sql(formatter = nil) - [ - "INSERT", - "INTO #{table_sql}", - "(#{record.keys.map { |key| engine.quote_column_name(key.name) }.join(', ')})", - "VALUES (#{record.map { |key, value| key.format(value) }.join(', ')})" - ].join("\n") - end - def call engine.create(self) end diff --git a/lib/arel/relations/writes/update.rb b/lib/arel/relations/writes/update.rb index fd0aadb479..e647218a80 100644 --- a/lib/arel/relations/writes/update.rb +++ b/lib/arel/relations/writes/update.rb @@ -7,17 +7,6 @@ module Arel @relation, @assignments = relation, assignments end - def to_sql(formatter = nil) - [ - "UPDATE #{table_sql} SET", - assignments.collect do |attribute, value| - "#{engine.quote_column_name(attribute.name)} = #{attribute.format(value)}" - end.join(",\n"), - ("WHERE #{wheres.map(&:to_sql).join('\n\tAND ')}" unless wheres.blank? ), - ("LIMIT #{taken}" unless taken.blank? ) - ].join("\n") - end - def call engine.update(self) end |