diff options
author | Emilio Tagua <miloops@gmail.com> | 2009-06-23 17:38:31 -0300 |
---|---|---|
committer | Emilio Tagua <miloops@gmail.com> | 2009-06-23 17:38:31 -0300 |
commit | 02a723f7b5f30b00fc77bc85162598f707c7b682 (patch) | |
tree | f3761e591165cac781455b7fbf27b1ba316a3e90 /activerecord/lib/active_record | |
parent | bc899fd3a37fae7e0024e8f0439f204818aeafd7 (diff) | |
download | rails-02a723f7b5f30b00fc77bc85162598f707c7b682.tar.gz rails-02a723f7b5f30b00fc77bc85162598f707c7b682.tar.bz2 rails-02a723f7b5f30b00fc77bc85162598f707c7b682.zip |
Arel now buils SQL queries for associations. Removed old code and
updated Arel version to support this.
Diffstat (limited to 'activerecord/lib/active_record')
3 files changed, 6 insertions, 67 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 29f58a04e6..97fdd9b1ba 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1604,14 +1604,6 @@ module ActiveRecord return sanitize_sql(arel.to_sql) end - def add_limited_ids_condition!(sql, options, join_dependency) - unless (id_list = select_limited_ids_list(options, join_dependency)).empty? - sql << "#{condition_word(sql)} #{connection.quote_table_name table_name}.#{primary_key} IN (#{id_list}) " - else - throw :invalid_query - end - end - def construct_limited_ids_condition(where, options, join_dependency) unless (id_list = select_limited_ids_list(options, join_dependency)).empty? "#{where.blank? ? '' : ' AND '} #{connection.quote_table_name table_name}.#{primary_key} IN (#{id_list}) " @@ -1630,47 +1622,18 @@ module ActiveRecord end def construct_finder_sql_for_association_limiting(options, join_dependency) - scope = scope(:find) - # Only join tables referenced in order or conditions since this is particularly slow on the pre-query. tables_from_conditions = conditions_tables(options) tables_from_order = order_tables(options) all_tables = tables_from_conditions + tables_from_order - distinct_join_associations = all_tables.uniq.map{|table| + options[:joins] = all_tables.uniq.map {|table| join_dependency.joins_for_table_name(table) - }.flatten.compact.uniq - - order = options[:order] - if scoped_order = (scope && scope[:order]) - order = order ? "#{order}, #{scoped_order}" : scoped_order - end - - is_distinct = !options[:joins].blank? || include_eager_conditions?(options, tables_from_conditions) || include_eager_order?(options, tables_from_order) - sql = "SELECT " - if is_distinct - sql << connection.distinct("#{connection.quote_table_name table_name}.#{primary_key}", order) - else - sql << primary_key - end - sql << " FROM #{connection.quote_table_name table_name} " + }.flatten.compact.uniq.collect { |assoc| assoc.association_join }.join - if is_distinct - sql << distinct_join_associations.collect { |assoc| assoc.association_join }.join - add_joins!(sql, options[:joins], scope) - end - - add_conditions!(sql, options[:conditions], scope) - add_group!(sql, options[:group], options[:having], scope) - - if order && is_distinct - connection.add_order_by_for_association_limiting!(sql, :order => order) - else - add_order!(sql, options[:order], scope) - end - - add_limit!(sql, options, scope) - - return sanitize_sql(sql) + construct_finder_sql(options.merge( + :select => connection.distinct("#{connection.quote_table_name table_name}.#{primary_key}", construct_order(options[:order], scope(:find)).join(",")) + ) + ) end def tables_in_string(string) @@ -1774,10 +1737,6 @@ module ActiveRecord end end - def condition_word(sql) - sql =~ /where/i ? " AND " : "WHERE " - end - def create_extension_modules(association_id, block_extension, extensions) if block_extension extension_module_name = "#{self.to_s.demodulize}#{association_id.to_s.camelize}AssociationExtension" diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index ff63ea3a2e..2f01a303e5 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -403,12 +403,6 @@ module ActiveRecord "DISTINCT #{columns}" end - # ORDER BY clause for the passed order option. - # PostgreSQL overrides this due to its stricter standards compliance. - def add_order_by_for_association_limiting!(sql, options) - sql << " ORDER BY #{options[:order]}" - end - # Adds timestamps (created_at and updated_at) columns to the named table. # ===== Examples # add_timestamps(:suppliers) diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 90a90a0a9b..6b230787c3 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -911,20 +911,6 @@ module ActiveRecord sql << order_columns * ', ' end - # Returns an ORDER BY clause for the passed order option. - # - # PostgreSQL does not allow arbitrary ordering when using DISTINCT ON, so we work around this - # by wrapping the +sql+ string as a sub-select and ordering in that query. - def add_order_by_for_association_limiting!(sql, options) #:nodoc: - return sql if options[:order].blank? - - order = options[:order].split(',').collect { |s| s.strip }.reject(&:blank?) - order.map! { |s| 'DESC' if s =~ /\bdesc$/i } - order = order.zip((0...order.size).to_a).map { |s,i| "id_list.alias_#{i} #{s}" }.join(', ') - - sql.replace "SELECT * FROM (#{sql}) AS id_list ORDER BY #{order}" - end - protected # Returns the version of the connected PostgreSQL version. def postgresql_version |