Permalink
Browse files

Arel now buils SQL queries for associations. Removed old code and

updated Arel version to support this.
  • Loading branch information...
1 parent bc899fd commit 02a723f7b5f30b00fc77bc85162598f707c7b682 @miloops miloops committed Jun 23, 2009
@@ -1604,14 +1604,6 @@ def construct_finder_sql_with_included_associations(options, join_dependency)
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 @@ def select_limited_ids_list(options, join_dependency)
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 @@ def add_association_callbacks(association_name, options)
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"
@@ -403,12 +403,6 @@ def distinct(columns, order_by)
"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)
@@ -911,20 +911,6 @@ def distinct(columns, order_by) #:nodoc:
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
2 arel

0 comments on commit 02a723f

Please sign in to comment.