1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 module ActiveRecord
19 class Base
20 def self.find_ids(options={})
21 find_ids_with_associations(options)
22 end
23 end
24
25 module Associations
26 module ClassMethods
27 def find_ids_with_associations(options = {})
28 catch :invalid_query do
29 join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(self, merge_includes(scope(:find, :include), options[:include]), options[:joins])
30 return connection.select_values(construct_ids_finder_sql_with_included_associations(options, join_dependency)).map(&:to_i)
31 end
32 []
33 end
34
35 def construct_ids_finder_sql_with_included_associations(options, join_dependency)
36 scope = scope(:find)
37 sql = "SELECT #{table_name}.id FROM #{(scope && scope[:from]) || options[:from] || quoted_table_name} "
38 sql << join_dependency.join_associations.collect{|join| join.association_join }.join
39
40 add_joins!(sql, options[:joins], scope)
41 add_conditions!(sql, options[:conditions], scope)
42 add_limited_ids_condition!(sql, options, join_dependency) if !using_limitable_reflections?(join_dependency.reflections) && ((scope && scope[:limit]) || options[:limit])
43
44 add_group!(sql, options[:group], options[:having], scope)
45 add_order!(sql, options[:order], scope)
46 add_limit!(sql, options, scope) if using_limitable_reflections?(join_dependency.reflections)
47 add_lock!(sql, options, scope)
48
49 return sanitize_sql(sql)
50 end
51 end
52 end
53 end