diff options
Diffstat (limited to 'lib/arel/engines/sql/relations')
-rw-r--r-- | lib/arel/engines/sql/relations/compiler.rb | 153 | ||||
-rw-r--r-- | lib/arel/engines/sql/relations/table.rb | 100 | ||||
-rw-r--r-- | lib/arel/engines/sql/relations/utilities/nil.rb | 6 |
3 files changed, 0 insertions, 259 deletions
diff --git a/lib/arel/engines/sql/relations/compiler.rb b/lib/arel/engines/sql/relations/compiler.rb deleted file mode 100644 index c8511374d9..0000000000 --- a/lib/arel/engines/sql/relations/compiler.rb +++ /dev/null @@ -1,153 +0,0 @@ -module Arel - module SqlCompiler - class GenericCompiler - attr_reader :relation, :engine - - def initialize(relation) - @relation = relation - @engine = relation.engine - end - - def christener - relation.christener - end - - def select_sql - projections = @relation.projections - if Count === projections.first && projections.size == 1 && - (relation.taken.present? || relation.wheres.present?) && relation.joins(self).blank? - subquery = [ - "SELECT 1 FROM #{relation.from_clauses}", build_clauses - ].join ' ' - query = "SELECT COUNT(*) AS count_id FROM (#{subquery}) AS subquery" - else - query = [ - "SELECT #{relation.select_clauses.join(', ')}", - "FROM #{relation.from_clauses}", - build_clauses - ].compact.join ' ' - end - query - end - - def build_clauses - joins = relation.joins(self) - wheres = relation.where_clauses - groups = relation.group_clauses - havings = relation.having_clauses - orders = relation.order_clauses - - clauses = [ "", - joins, - ("WHERE #{wheres.join(' AND ')}" unless wheres.empty?), - ("GROUP BY #{groups.join(', ')}" unless groups.empty?), - ("HAVING #{havings.join(' AND ')}" unless havings.empty?), - ("ORDER BY #{orders.join(', ')}" unless orders.empty?) - ].compact.join ' ' - - offset = relation.skipped - limit = relation.taken - @engine.connection.add_limit_offset!(clauses, :limit => limit, - :offset => offset) if offset || limit - - clauses << " #{locked}" unless locked.blank? - clauses unless clauses.blank? - end - - def delete_sql - build_query \ - "DELETE", - "FROM #{relation.table_sql}", - ("WHERE #{relation.wheres.collect { |x| x.to_sql }.join(' AND ')}" unless relation.wheres.blank? ), - (add_limit_on_delete(relation.taken) unless relation.taken.blank? ) - end - - def add_limit_on_delete(taken) - "LIMIT #{taken}" - end - - def insert_sql(include_returning = true) - insertion_attributes_values_sql = if relation.record.is_a?(Value) - relation.record.value - else - attributes = relation.record.keys.sort_by do |attribute| - attribute.name.to_s - end - - first = attributes.collect do |key| - @engine.connection.quote_column_name(key.name) - end.join(', ') - - second = attributes.collect do |key| - key.format(relation.record[key]) - end.join(', ') - - build_query "(#{first})", "VALUES (#{second})" - end - - build_query \ - "INSERT", - "INTO #{relation.table_sql}", - insertion_attributes_values_sql, - ("RETURNING #{engine.connection.quote_column_name(primary_key)}" if include_returning && relation.compiler.supports_insert_with_returning?) - end - - def supports_insert_with_returning? - false - end - - def update_sql - build_query \ - "UPDATE #{relation.table_sql} SET", - assignment_sql, - build_update_conditions_sql - end - - protected - - def locked - relation.locked - end - - def build_query(*parts) - parts.compact.join(" ") - end - - def assignment_sql - if relation.assignments.respond_to?(:collect) - attributes = relation.assignments.keys.sort_by do |attribute| - attribute.name.to_s - end - - attributes.map do |attribute| - value = relation.assignments[attribute] - "#{@engine.connection.quote_column_name(attribute.name)} = #{attribute.format(value)}" - end.join(", ") - else - relation.assignments.value - end - end - - def build_update_conditions_sql - conditions = "" - conditions << " WHERE #{relation.wheres.map { |x| x.to_sql }.join(' AND ')}" unless relation.wheres.blank? - conditions << " ORDER BY #{relation.order_clauses.join(', ')}" unless relation.orders.blank? - - taken = relation.taken - unless taken.blank? - conditions = limited_update_conditions(conditions, taken) - end - - conditions - end - - def limited_update_conditions(conditions, taken) - conditions << " LIMIT #{taken}" - quoted_primary_key = @engine.connection.quote_column_name(relation.primary_key) - "WHERE #{quoted_primary_key} IN (SELECT #{quoted_primary_key} FROM #{@engine.connection.quote_table_name relation.table.name} #{conditions})" - end - - end - - end -end diff --git a/lib/arel/engines/sql/relations/table.rb b/lib/arel/engines/sql/relations/table.rb deleted file mode 100644 index b31df7a6d0..0000000000 --- a/lib/arel/engines/sql/relations/table.rb +++ /dev/null @@ -1,100 +0,0 @@ -module Arel - class Table - include Relation, Recursion::BaseCase - - @@engine = nil - @@tables = nil - class << self # FIXME: Do we really need these? - def engine; @@engine; end - def engine= e; @@engine = e; end - - def tables; @@tables; end - def tables= e; @@tables = e; end - end - - attr_reader :name, :engine, :table_alias, :options, :christener - attr_reader :table_exists - alias :table_exists? :table_exists - - def initialize(name, options = {}) - @name = name.to_s - @table_exists = nil - @table_alias = nil - @christener = Sql::Christener.new - @attributes = nil - @matching_attributes = nil - - if options.is_a?(Hash) - @options = options - @engine = options[:engine] || Table.engine - - if options[:as] - as = options[:as].to_s - @table_alias = as unless as == @name - end - else - @engine = options # Table.new('foo', engine) - end - - if @engine.connection - begin - require "arel/engines/sql/compilers/#{@engine.adapter_name.downcase}_compiler" - rescue LoadError - begin - # try to load an externally defined compiler, in case this adapter has defined the compiler on its own. - require "#{@engine.adapter_name.downcase}/arel_compiler" - rescue LoadError - raise "#{@engine.adapter_name} is not supported by Arel." - end - end - - @@tables ||= engine.connection.tables - @table_exists = @@tables.include?(name) || - @engine.connection.table_exists?(name) - end - end - - def as(table_alias) - Table.new(name, options.merge(:as => table_alias)) - end - - def attributes - return @attributes if @attributes - if table_exists? - attrs = columns.collect do |column| - Sql::Attributes.for(column).new(column, self, column.name.to_sym) - end - @attributes = Header.new(attrs) - else - Header.new - end - end - - def column_for(attribute) - has_attribute?(attribute) and columns.detect { |c| c.name == attribute.name.to_s } - end - - def columns - @columns ||= engine.connection.columns(name, "#{name} Columns") - end - - def reset - @columns = nil - @attributes = Header.new([]) - end - - private - def matching_attributes - @matching_attributes ||= Hash[attributes.map { |a| [a.root, true] }] - end - - def has_attribute?(attribute) - matching_attributes.key? attribute.root - end - end -end - -def Table(name, engine = Arel::Table.engine) - Arel::Table.new(name, engine) -end - diff --git a/lib/arel/engines/sql/relations/utilities/nil.rb b/lib/arel/engines/sql/relations/utilities/nil.rb deleted file mode 100644 index 0f7ca5d757..0000000000 --- a/lib/arel/engines/sql/relations/utilities/nil.rb +++ /dev/null @@ -1,6 +0,0 @@ -module Arel - class Nil - def table_sql(formatter = nil); '' end - def name; '' end - end -end |