annotate app/models/issue_query.rb @ 1628:9c5f8e24dadc live tip

Quieten this cron script
author Chris Cannam
date Tue, 25 Aug 2020 11:38:49 +0100
parents dffacf8a6908
children
rev   line source
Chris@1464 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@1464 3 #
Chris@1464 4 # This program is free software; you can redistribute it and/or
Chris@1464 5 # modify it under the terms of the GNU General Public License
Chris@1464 6 # as published by the Free Software Foundation; either version 2
Chris@1464 7 # of the License, or (at your option) any later version.
Chris@1464 8 #
Chris@1464 9 # This program is distributed in the hope that it will be useful,
Chris@1464 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1464 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1464 12 # GNU General Public License for more details.
Chris@1464 13 #
Chris@1464 14 # You should have received a copy of the GNU General Public License
Chris@1464 15 # along with this program; if not, write to the Free Software
Chris@1464 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1464 17
Chris@1464 18 class IssueQuery < Query
Chris@1464 19
Chris@1464 20 self.queried_class = Issue
Chris@1464 21
Chris@1464 22 self.available_columns = [
Chris@1464 23 QueryColumn.new(:id, :sortable => "#{Issue.table_name}.id", :default_order => 'desc', :caption => '#', :frozen => true),
Chris@1464 24 QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true),
Chris@1464 25 QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position", :groupable => true),
Chris@1464 26 QueryColumn.new(:parent, :sortable => ["#{Issue.table_name}.root_id", "#{Issue.table_name}.lft ASC"], :default_order => 'desc', :caption => :field_parent_issue),
Chris@1464 27 QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position", :groupable => true),
Chris@1464 28 QueryColumn.new(:priority, :sortable => "#{IssuePriority.table_name}.position", :default_order => 'desc', :groupable => true),
Chris@1464 29 QueryColumn.new(:subject, :sortable => "#{Issue.table_name}.subject"),
Chris@1464 30 QueryColumn.new(:author, :sortable => lambda {User.fields_for_order_statement("authors")}, :groupable => true),
Chris@1464 31 QueryColumn.new(:assigned_to, :sortable => lambda {User.fields_for_order_statement}, :groupable => true),
Chris@1464 32 QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on", :default_order => 'desc'),
Chris@1464 33 QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name", :groupable => true),
Chris@1464 34 QueryColumn.new(:fixed_version, :sortable => lambda {Version.fields_for_order_statement}, :groupable => true),
Chris@1464 35 QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"),
Chris@1464 36 QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"),
Chris@1464 37 QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours"),
Chris@1464 38 QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio", :groupable => true),
Chris@1464 39 QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'),
Chris@1464 40 QueryColumn.new(:closed_on, :sortable => "#{Issue.table_name}.closed_on", :default_order => 'desc'),
Chris@1464 41 QueryColumn.new(:relations, :caption => :label_related_issues),
Chris@1464 42 QueryColumn.new(:description, :inline => false)
Chris@1464 43 ]
Chris@1464 44
Chris@1464 45 scope :visible, lambda {|*args|
Chris@1464 46 user = args.shift || User.current
Chris@1464 47 base = Project.allowed_to_condition(user, :view_issues, *args)
Chris@1464 48 scope = includes(:project).where("#{table_name}.project_id IS NULL OR (#{base})")
Chris@1464 49
Chris@1464 50 if user.admin?
Chris@1464 51 scope.where("#{table_name}.visibility <> ? OR #{table_name}.user_id = ?", VISIBILITY_PRIVATE, user.id)
Chris@1464 52 elsif user.memberships.any?
Chris@1464 53 scope.where("#{table_name}.visibility = ?" +
Chris@1464 54 " OR (#{table_name}.visibility = ? AND #{table_name}.id IN (" +
Chris@1464 55 "SELECT DISTINCT q.id FROM #{table_name} q" +
Chris@1464 56 " INNER JOIN #{table_name_prefix}queries_roles#{table_name_suffix} qr on qr.query_id = q.id" +
Chris@1464 57 " INNER JOIN #{MemberRole.table_name} mr ON mr.role_id = qr.role_id" +
Chris@1464 58 " INNER JOIN #{Member.table_name} m ON m.id = mr.member_id AND m.user_id = ?" +
Chris@1464 59 " WHERE q.project_id IS NULL OR q.project_id = m.project_id))" +
Chris@1464 60 " OR #{table_name}.user_id = ?",
Chris@1464 61 VISIBILITY_PUBLIC, VISIBILITY_ROLES, user.id, user.id)
Chris@1464 62 elsif user.logged?
Chris@1464 63 scope.where("#{table_name}.visibility = ? OR #{table_name}.user_id = ?", VISIBILITY_PUBLIC, user.id)
Chris@1464 64 else
Chris@1464 65 scope.where("#{table_name}.visibility = ?", VISIBILITY_PUBLIC)
Chris@1464 66 end
Chris@1464 67 }
Chris@1464 68
Chris@1464 69 def initialize(attributes=nil, *args)
Chris@1464 70 super attributes
Chris@1464 71 self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} }
Chris@1464 72 end
Chris@1464 73
Chris@1464 74 # Returns true if the query is visible to +user+ or the current user.
Chris@1464 75 def visible?(user=User.current)
Chris@1464 76 return true if user.admin?
Chris@1464 77 return false unless project.nil? || user.allowed_to?(:view_issues, project)
Chris@1464 78 case visibility
Chris@1464 79 when VISIBILITY_PUBLIC
Chris@1464 80 true
Chris@1464 81 when VISIBILITY_ROLES
Chris@1464 82 if project
Chris@1464 83 (user.roles_for_project(project) & roles).any?
Chris@1464 84 else
Chris@1464 85 Member.where(:user_id => user.id).joins(:roles).where(:member_roles => {:role_id => roles.map(&:id)}).any?
Chris@1464 86 end
Chris@1464 87 else
Chris@1464 88 user == self.user
Chris@1464 89 end
Chris@1464 90 end
Chris@1464 91
Chris@1464 92 def is_private?
Chris@1464 93 visibility == VISIBILITY_PRIVATE
Chris@1464 94 end
Chris@1464 95
Chris@1464 96 def is_public?
Chris@1464 97 !is_private?
Chris@1464 98 end
Chris@1464 99
Chris@1464 100 def draw_relations
Chris@1464 101 r = options[:draw_relations]
Chris@1464 102 r.nil? || r == '1'
Chris@1464 103 end
Chris@1464 104
Chris@1464 105 def draw_relations=(arg)
Chris@1464 106 options[:draw_relations] = (arg == '0' ? '0' : nil)
Chris@1464 107 end
Chris@1464 108
Chris@1464 109 def draw_progress_line
Chris@1464 110 r = options[:draw_progress_line]
Chris@1464 111 r == '1'
Chris@1464 112 end
Chris@1464 113
Chris@1464 114 def draw_progress_line=(arg)
Chris@1464 115 options[:draw_progress_line] = (arg == '1' ? '1' : nil)
Chris@1464 116 end
Chris@1464 117
Chris@1464 118 def build_from_params(params)
Chris@1464 119 super
Chris@1464 120 self.draw_relations = params[:draw_relations] || (params[:query] && params[:query][:draw_relations])
Chris@1464 121 self.draw_progress_line = params[:draw_progress_line] || (params[:query] && params[:query][:draw_progress_line])
Chris@1464 122 self
Chris@1464 123 end
Chris@1464 124
Chris@1464 125 def initialize_available_filters
Chris@1464 126 principals = []
Chris@1464 127 subprojects = []
Chris@1464 128 versions = []
Chris@1464 129 categories = []
Chris@1464 130 issue_custom_fields = []
Chris@1464 131
Chris@1464 132 if project
Chris@1464 133 principals += project.principals.sort
Chris@1464 134 unless project.leaf?
Chris@1464 135 subprojects = project.descendants.visible.all
Chris@1464 136 principals += Principal.member_of(subprojects)
Chris@1464 137 end
Chris@1464 138 versions = project.shared_versions.all
Chris@1464 139 categories = project.issue_categories.all
Chris@1464 140 issue_custom_fields = project.all_issue_custom_fields
Chris@1464 141 else
Chris@1464 142 if all_projects.any?
Chris@1464 143 principals += Principal.member_of(all_projects)
Chris@1464 144 end
Chris@1517 145 versions = Version.visible.where(:sharing => 'system').all
Chris@1464 146 issue_custom_fields = IssueCustomField.where(:is_for_all => true)
Chris@1464 147 end
Chris@1464 148 principals.uniq!
Chris@1464 149 principals.sort!
Chris@1464 150 users = principals.select {|p| p.is_a?(User)}
Chris@1464 151
Chris@1464 152 add_available_filter "status_id",
Chris@1517 153 :type => :list_status, :values => IssueStatus.sorted.collect{|s| [s.name, s.id.to_s] }
Chris@1464 154
Chris@1464 155 if project.nil?
Chris@1464 156 project_values = []
Chris@1464 157 if User.current.logged? && User.current.memberships.any?
Chris@1464 158 project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"]
Chris@1464 159 end
Chris@1464 160 project_values += all_projects_values
Chris@1464 161 add_available_filter("project_id",
Chris@1464 162 :type => :list, :values => project_values
Chris@1464 163 ) unless project_values.empty?
Chris@1464 164 end
Chris@1464 165
Chris@1464 166 add_available_filter "tracker_id",
Chris@1464 167 :type => :list, :values => trackers.collect{|s| [s.name, s.id.to_s] }
Chris@1464 168 add_available_filter "priority_id",
Chris@1464 169 :type => :list, :values => IssuePriority.all.collect{|s| [s.name, s.id.to_s] }
Chris@1464 170
Chris@1464 171 author_values = []
Chris@1464 172 author_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
Chris@1464 173 author_values += users.collect{|s| [s.name, s.id.to_s] }
Chris@1464 174 add_available_filter("author_id",
Chris@1464 175 :type => :list, :values => author_values
Chris@1464 176 ) unless author_values.empty?
Chris@1464 177
Chris@1464 178 assigned_to_values = []
Chris@1464 179 assigned_to_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
Chris@1464 180 assigned_to_values += (Setting.issue_group_assignment? ?
Chris@1464 181 principals : users).collect{|s| [s.name, s.id.to_s] }
Chris@1464 182 add_available_filter("assigned_to_id",
Chris@1464 183 :type => :list_optional, :values => assigned_to_values
Chris@1464 184 ) unless assigned_to_values.empty?
Chris@1464 185
Chris@1464 186 group_values = Group.all.collect {|g| [g.name, g.id.to_s] }
Chris@1464 187 add_available_filter("member_of_group",
Chris@1464 188 :type => :list_optional, :values => group_values
Chris@1464 189 ) unless group_values.empty?
Chris@1464 190
Chris@1464 191 role_values = Role.givable.collect {|r| [r.name, r.id.to_s] }
Chris@1464 192 add_available_filter("assigned_to_role",
Chris@1464 193 :type => :list_optional, :values => role_values
Chris@1464 194 ) unless role_values.empty?
Chris@1464 195
Chris@1464 196 if versions.any?
Chris@1464 197 add_available_filter "fixed_version_id",
Chris@1464 198 :type => :list_optional,
Chris@1464 199 :values => versions.sort.collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s] }
Chris@1464 200 end
Chris@1464 201
Chris@1464 202 if categories.any?
Chris@1464 203 add_available_filter "category_id",
Chris@1464 204 :type => :list_optional,
Chris@1464 205 :values => categories.collect{|s| [s.name, s.id.to_s] }
Chris@1464 206 end
Chris@1464 207
Chris@1464 208 add_available_filter "subject", :type => :text
Chris@1464 209 add_available_filter "created_on", :type => :date_past
Chris@1464 210 add_available_filter "updated_on", :type => :date_past
Chris@1464 211 add_available_filter "closed_on", :type => :date_past
Chris@1464 212 add_available_filter "start_date", :type => :date
Chris@1464 213 add_available_filter "due_date", :type => :date
Chris@1464 214 add_available_filter "estimated_hours", :type => :float
Chris@1464 215 add_available_filter "done_ratio", :type => :integer
Chris@1464 216
Chris@1464 217 if User.current.allowed_to?(:set_issues_private, nil, :global => true) ||
Chris@1464 218 User.current.allowed_to?(:set_own_issues_private, nil, :global => true)
Chris@1464 219 add_available_filter "is_private",
Chris@1464 220 :type => :list,
Chris@1464 221 :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]]
Chris@1464 222 end
Chris@1464 223
Chris@1464 224 if User.current.logged?
Chris@1464 225 add_available_filter "watcher_id",
Chris@1464 226 :type => :list, :values => [["<< #{l(:label_me)} >>", "me"]]
Chris@1464 227 end
Chris@1464 228
Chris@1464 229 if subprojects.any?
Chris@1464 230 add_available_filter "subproject_id",
Chris@1464 231 :type => :list_subprojects,
Chris@1464 232 :values => subprojects.collect{|s| [s.name, s.id.to_s] }
Chris@1464 233 end
Chris@1464 234
Chris@1464 235 add_custom_fields_filters(issue_custom_fields)
Chris@1464 236
Chris@1464 237 add_associations_custom_fields_filters :project, :author, :assigned_to, :fixed_version
Chris@1464 238
Chris@1464 239 IssueRelation::TYPES.each do |relation_type, options|
Chris@1464 240 add_available_filter relation_type, :type => :relation, :label => options[:name]
Chris@1464 241 end
Chris@1464 242
Chris@1464 243 Tracker.disabled_core_fields(trackers).each {|field|
Chris@1464 244 delete_available_filter field
Chris@1464 245 }
Chris@1464 246 end
Chris@1464 247
Chris@1464 248 def available_columns
Chris@1464 249 return @available_columns if @available_columns
Chris@1464 250 @available_columns = self.class.available_columns.dup
Chris@1464 251 @available_columns += (project ?
Chris@1464 252 project.all_issue_custom_fields :
Chris@1464 253 IssueCustomField
Chris@1464 254 ).visible.collect {|cf| QueryCustomFieldColumn.new(cf) }
Chris@1464 255
Chris@1464 256 if User.current.allowed_to?(:view_time_entries, project, :global => true)
Chris@1464 257 index = nil
Chris@1464 258 @available_columns.each_with_index {|column, i| index = i if column.name == :estimated_hours}
Chris@1464 259 index = (index ? index + 1 : -1)
Chris@1464 260 # insert the column after estimated_hours or at the end
Chris@1464 261 @available_columns.insert index, QueryColumn.new(:spent_hours,
Chris@1464 262 :sortable => "COALESCE((SELECT SUM(hours) FROM #{TimeEntry.table_name} WHERE #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id), 0)",
Chris@1464 263 :default_order => 'desc',
Chris@1464 264 :caption => :label_spent_time
Chris@1464 265 )
Chris@1464 266 end
Chris@1464 267
Chris@1464 268 if User.current.allowed_to?(:set_issues_private, nil, :global => true) ||
Chris@1464 269 User.current.allowed_to?(:set_own_issues_private, nil, :global => true)
Chris@1464 270 @available_columns << QueryColumn.new(:is_private, :sortable => "#{Issue.table_name}.is_private")
Chris@1464 271 end
Chris@1464 272
Chris@1464 273 disabled_fields = Tracker.disabled_core_fields(trackers).map {|field| field.sub(/_id$/, '')}
Chris@1464 274 @available_columns.reject! {|column|
Chris@1464 275 disabled_fields.include?(column.name.to_s)
Chris@1464 276 }
Chris@1464 277
Chris@1464 278 @available_columns
Chris@1464 279 end
Chris@1464 280
Chris@1464 281 def default_columns_names
Chris@1464 282 @default_columns_names ||= begin
Chris@1464 283 default_columns = Setting.issue_list_default_columns.map(&:to_sym)
Chris@1464 284
Chris@1464 285 project.present? ? default_columns : [:project] | default_columns
Chris@1464 286 end
Chris@1464 287 end
Chris@1464 288
Chris@1464 289 # Returns the issue count
Chris@1464 290 def issue_count
Chris@1464 291 Issue.visible.joins(:status, :project).where(statement).count
Chris@1464 292 rescue ::ActiveRecord::StatementInvalid => e
Chris@1464 293 raise StatementInvalid.new(e.message)
Chris@1464 294 end
Chris@1464 295
Chris@1464 296 # Returns the issue count by group or nil if query is not grouped
Chris@1464 297 def issue_count_by_group
Chris@1464 298 r = nil
Chris@1464 299 if grouped?
Chris@1464 300 begin
Chris@1464 301 # Rails3 will raise an (unexpected) RecordNotFound if there's only a nil group value
Chris@1464 302 r = Issue.visible.
Chris@1464 303 joins(:status, :project).
Chris@1464 304 where(statement).
Chris@1464 305 joins(joins_for_order_statement(group_by_statement)).
Chris@1464 306 group(group_by_statement).
Chris@1464 307 count
Chris@1464 308 rescue ActiveRecord::RecordNotFound
Chris@1464 309 r = {nil => issue_count}
Chris@1464 310 end
Chris@1464 311 c = group_by_column
Chris@1464 312 if c.is_a?(QueryCustomFieldColumn)
Chris@1464 313 r = r.keys.inject({}) {|h, k| h[c.custom_field.cast_value(k)] = r[k]; h}
Chris@1464 314 end
Chris@1464 315 end
Chris@1464 316 r
Chris@1464 317 rescue ::ActiveRecord::StatementInvalid => e
Chris@1464 318 raise StatementInvalid.new(e.message)
Chris@1464 319 end
Chris@1464 320
Chris@1464 321 # Returns the issues
Chris@1464 322 # Valid options are :order, :offset, :limit, :include, :conditions
Chris@1464 323 def issues(options={})
Chris@1464 324 order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?)
Chris@1464 325
Chris@1464 326 scope = Issue.visible.
Chris@1464 327 joins(:status, :project).
Chris@1464 328 where(statement).
Chris@1464 329 includes(([:status, :project] + (options[:include] || [])).uniq).
Chris@1464 330 where(options[:conditions]).
Chris@1464 331 order(order_option).
Chris@1464 332 joins(joins_for_order_statement(order_option.join(','))).
Chris@1464 333 limit(options[:limit]).
Chris@1464 334 offset(options[:offset])
Chris@1464 335
Chris@1517 336 scope = scope.preload(:custom_values)
Chris@1517 337 if has_column?(:author)
Chris@1517 338 scope = scope.preload(:author)
Chris@1464 339 end
Chris@1464 340
Chris@1464 341 issues = scope.all
Chris@1464 342
Chris@1464 343 if has_column?(:spent_hours)
Chris@1464 344 Issue.load_visible_spent_hours(issues)
Chris@1464 345 end
Chris@1464 346 if has_column?(:relations)
Chris@1464 347 Issue.load_visible_relations(issues)
Chris@1464 348 end
Chris@1464 349 issues
Chris@1464 350 rescue ::ActiveRecord::StatementInvalid => e
Chris@1464 351 raise StatementInvalid.new(e.message)
Chris@1464 352 end
Chris@1464 353
Chris@1464 354 # Returns the issues ids
Chris@1464 355 def issue_ids(options={})
Chris@1464 356 order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?)
Chris@1464 357
Chris@1464 358 Issue.visible.
Chris@1464 359 joins(:status, :project).
Chris@1464 360 where(statement).
Chris@1464 361 includes(([:status, :project] + (options[:include] || [])).uniq).
Chris@1464 362 where(options[:conditions]).
Chris@1464 363 order(order_option).
Chris@1464 364 joins(joins_for_order_statement(order_option.join(','))).
Chris@1464 365 limit(options[:limit]).
Chris@1464 366 offset(options[:offset]).
Chris@1464 367 find_ids
Chris@1464 368 rescue ::ActiveRecord::StatementInvalid => e
Chris@1464 369 raise StatementInvalid.new(e.message)
Chris@1464 370 end
Chris@1464 371
Chris@1464 372 # Returns the journals
Chris@1464 373 # Valid options are :order, :offset, :limit
Chris@1464 374 def journals(options={})
Chris@1464 375 Journal.visible.
Chris@1464 376 joins(:issue => [:project, :status]).
Chris@1464 377 where(statement).
Chris@1464 378 order(options[:order]).
Chris@1464 379 limit(options[:limit]).
Chris@1464 380 offset(options[:offset]).
Chris@1464 381 preload(:details, :user, {:issue => [:project, :author, :tracker, :status]}).
Chris@1464 382 all
Chris@1464 383 rescue ::ActiveRecord::StatementInvalid => e
Chris@1464 384 raise StatementInvalid.new(e.message)
Chris@1464 385 end
Chris@1464 386
Chris@1464 387 # Returns the versions
Chris@1464 388 # Valid options are :conditions
Chris@1464 389 def versions(options={})
Chris@1464 390 Version.visible.
Chris@1464 391 where(project_statement).
Chris@1464 392 where(options[:conditions]).
Chris@1464 393 includes(:project).
Chris@1464 394 all
Chris@1464 395 rescue ::ActiveRecord::StatementInvalid => e
Chris@1464 396 raise StatementInvalid.new(e.message)
Chris@1464 397 end
Chris@1464 398
Chris@1464 399 def sql_for_watcher_id_field(field, operator, value)
Chris@1464 400 db_table = Watcher.table_name
Chris@1464 401 "#{Issue.table_name}.id #{ operator == '=' ? 'IN' : 'NOT IN' } (SELECT #{db_table}.watchable_id FROM #{db_table} WHERE #{db_table}.watchable_type='Issue' AND " +
Chris@1464 402 sql_for_field(field, '=', value, db_table, 'user_id') + ')'
Chris@1464 403 end
Chris@1464 404
Chris@1464 405 def sql_for_member_of_group_field(field, operator, value)
Chris@1464 406 if operator == '*' # Any group
Chris@1464 407 groups = Group.all
Chris@1464 408 operator = '=' # Override the operator since we want to find by assigned_to
Chris@1464 409 elsif operator == "!*"
Chris@1464 410 groups = Group.all
Chris@1464 411 operator = '!' # Override the operator since we want to find by assigned_to
Chris@1464 412 else
Chris@1517 413 groups = Group.where(:id => value).all
Chris@1464 414 end
Chris@1464 415 groups ||= []
Chris@1464 416
Chris@1464 417 members_of_groups = groups.inject([]) {|user_ids, group|
Chris@1464 418 user_ids + group.user_ids + [group.id]
Chris@1464 419 }.uniq.compact.sort.collect(&:to_s)
Chris@1464 420
Chris@1464 421 '(' + sql_for_field("assigned_to_id", operator, members_of_groups, Issue.table_name, "assigned_to_id", false) + ')'
Chris@1464 422 end
Chris@1464 423
Chris@1464 424 def sql_for_assigned_to_role_field(field, operator, value)
Chris@1464 425 case operator
Chris@1464 426 when "*", "!*" # Member / Not member
Chris@1464 427 sw = operator == "!*" ? 'NOT' : ''
Chris@1464 428 nl = operator == "!*" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : ''
Chris@1464 429 "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}" +
Chris@1464 430 " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id))"
Chris@1464 431 when "=", "!"
Chris@1464 432 role_cond = value.any? ?
Chris@1464 433 "#{MemberRole.table_name}.role_id IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" :
Chris@1464 434 "1=0"
Chris@1464 435
Chris@1464 436 sw = operator == "!" ? 'NOT' : ''
Chris@1464 437 nl = operator == "!" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : ''
Chris@1464 438 "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}, #{MemberRole.table_name}" +
Chris@1464 439 " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id AND #{Member.table_name}.id = #{MemberRole.table_name}.member_id AND #{role_cond}))"
Chris@1464 440 end
Chris@1464 441 end
Chris@1464 442
Chris@1464 443 def sql_for_is_private_field(field, operator, value)
Chris@1464 444 op = (operator == "=" ? 'IN' : 'NOT IN')
Chris@1464 445 va = value.map {|v| v == '0' ? connection.quoted_false : connection.quoted_true}.uniq.join(',')
Chris@1464 446
Chris@1464 447 "#{Issue.table_name}.is_private #{op} (#{va})"
Chris@1464 448 end
Chris@1464 449
Chris@1464 450 def sql_for_relations(field, operator, value, options={})
Chris@1464 451 relation_options = IssueRelation::TYPES[field]
Chris@1464 452 return relation_options unless relation_options
Chris@1464 453
Chris@1464 454 relation_type = field
Chris@1464 455 join_column, target_join_column = "issue_from_id", "issue_to_id"
Chris@1464 456 if relation_options[:reverse] || options[:reverse]
Chris@1464 457 relation_type = relation_options[:reverse] || relation_type
Chris@1464 458 join_column, target_join_column = target_join_column, join_column
Chris@1464 459 end
Chris@1464 460
Chris@1464 461 sql = case operator
Chris@1464 462 when "*", "!*"
Chris@1464 463 op = (operator == "*" ? 'IN' : 'NOT IN')
Chris@1464 464 "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name} WHERE #{IssueRelation.table_name}.relation_type = '#{connection.quote_string(relation_type)}')"
Chris@1464 465 when "=", "!"
Chris@1464 466 op = (operator == "=" ? 'IN' : 'NOT IN')
Chris@1464 467 "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name} WHERE #{IssueRelation.table_name}.relation_type = '#{connection.quote_string(relation_type)}' AND #{IssueRelation.table_name}.#{target_join_column} = #{value.first.to_i})"
Chris@1464 468 when "=p", "=!p", "!p"
Chris@1464 469 op = (operator == "!p" ? 'NOT IN' : 'IN')
Chris@1464 470 comp = (operator == "=!p" ? '<>' : '=')
Chris@1464 471 "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name}, #{Issue.table_name} relissues WHERE #{IssueRelation.table_name}.relation_type = '#{connection.quote_string(relation_type)}' AND #{IssueRelation.table_name}.#{target_join_column} = relissues.id AND relissues.project_id #{comp} #{value.first.to_i})"
Chris@1464 472 end
Chris@1464 473
Chris@1464 474 if relation_options[:sym] == field && !options[:reverse]
Chris@1464 475 sqls = [sql, sql_for_relations(field, operator, value, :reverse => true)]
Chris@1464 476 sql = sqls.join(["!", "!*", "!p"].include?(operator) ? " AND " : " OR ")
Chris@1464 477 end
Chris@1464 478 "(#{sql})"
Chris@1464 479 end
Chris@1464 480
Chris@1464 481 IssueRelation::TYPES.keys.each do |relation_type|
Chris@1464 482 alias_method "sql_for_#{relation_type}_field".to_sym, :sql_for_relations
Chris@1464 483 end
Chris@1464 484 end