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