kanbanworkflowstimelinescrumrubyroadmapproject-planningproject-managementopenprojectangularissue-trackerifcgantt-chartganttbug-trackerboardsbcf
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
228 lines
6.7 KiB
228 lines
6.7 KiB
11 years ago
|
#-- copyright
|
||
5 years ago
|
# OpenProject is an open source project management software.
|
||
|
# Copyright (C) 2012-2020 the OpenProject GmbH
|
||
11 years ago
|
#
|
||
5 years ago
|
# This program is free software; you can redistribute it and/or
|
||
|
# modify it under the terms of the GNU General Public License version 3.
|
||
|
#
|
||
|
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
|
||
|
# Copyright (C) 2006-2017 Jean-Philippe Lang
|
||
|
# Copyright (C) 2010-2013 the ChiliProject Team
|
||
11 years ago
|
#
|
||
|
# This program is free software; you can redistribute it and/or
|
||
|
# modify it under the terms of the GNU General Public License
|
||
5 years ago
|
# as published by the Free Software Foundation; either version 2
|
||
|
# of the License, or (at your option) any later version.
|
||
11 years ago
|
#
|
||
|
# This program is distributed in the hope that it will be useful,
|
||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
# GNU General Public License for more details.
|
||
|
#
|
||
|
# You should have received a copy of the GNU General Public License
|
||
|
# along with this program; if not, write to the Free Software
|
||
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
5 years ago
|
#
|
||
|
# See docs/COPYRIGHT.rdoc for more details.
|
||
11 years ago
|
#++
|
||
|
|
||
14 years ago
|
module Report::QueryUtils
|
||
10 years ago
|
Infinity = 1.0 / 0
|
||
14 years ago
|
include Engine
|
||
14 years ago
|
|
||
10 years ago
|
delegate :quoted_false, :quoted_true, to: 'engine.reporting_connection'
|
||
14 years ago
|
attr_writer :engine
|
||
15 years ago
|
|
||
5 years ago
|
include Costs::NumberHelper
|
||
|
|
||
15 years ago
|
##
|
||
|
# Graceful string quoting.
|
||
|
#
|
||
|
# @param [Object] str String to quote
|
||
|
# @return [Object] Quoted version
|
||
|
def quote_string(str)
|
||
|
return str unless str.respond_to? :to_str
|
||
14 years ago
|
engine.reporting_connection.quote_string(str)
|
||
15 years ago
|
end
|
||
|
|
||
14 years ago
|
def current_language
|
||
|
::I18n.locale
|
||
|
end
|
||
|
|
||
15 years ago
|
##
|
||
|
# Creates a SQL fragment representing a collection/array.
|
||
|
#
|
||
|
# @see quote_string
|
||
|
# @param [#flatten] *values Ruby collection
|
||
|
# @return [String] SQL collection
|
||
|
def collection(*values)
|
||
10 years ago
|
return '' if values.empty?
|
||
11 years ago
|
|
||
10 years ago
|
v = if values.is_a?(Array)
|
||
|
values.flatten.each_with_object([]) do |str, l|
|
||
|
l << split_with_safe_return(str)
|
||
|
end
|
||
|
else
|
||
|
split_with_safe_return(str)
|
||
|
end
|
||
|
|
||
10 years ago
|
"(#{v.flatten.map { |x| "'#{quote_string(x)}'" }.join(', ')})"
|
||
14 years ago
|
end
|
||
|
|
||
10 years ago
|
def split_with_safe_return(str)
|
||
|
# From ruby doc:
|
||
|
# When the input str is empty an empty Array is returned as the string is
|
||
|
# considered to have no fields to split.
|
||
|
str.to_s.empty? ? '' : str.to_s.split(',')
|
||
|
end
|
||
|
|
||
14 years ago
|
##
|
||
|
# Graceful, internationalized quoted string.
|
||
|
#
|
||
|
# @see quote_string
|
||
|
# @param [Object] str String to quote/translate
|
||
|
# @return [Object] Quoted, translated version
|
||
|
def quoted_label(ident)
|
||
14 years ago
|
"'#{quote_string ::I18n.t(ident)}'"
|
||
15 years ago
|
end
|
||
|
|
||
15 years ago
|
def quoted_date(date)
|
||
14 years ago
|
engine.reporting_connection.quoted_date date.to_dateish
|
||
15 years ago
|
end
|
||
|
|
||
15 years ago
|
##
|
||
|
# SQL date quoting.
|
||
|
# @param [Date,Time] date Date to quote.
|
||
|
# @return [String] Quoted date.
|
||
|
def quote_date(date)
|
||
|
"'#{quoted_date date}'"
|
||
|
end
|
||
|
|
||
|
##
|
||
|
# Generate a table name for any object.
|
||
|
#
|
||
|
# @example Table names
|
||
|
# table_name_for Issue # => 'issues'
|
||
|
# table_name_for :issue # => 'issues'
|
||
|
# table_name_for "issue" # => 'issues'
|
||
|
# table_name_for "issues" # => 'issues
|
||
|
#
|
||
|
# @param [#table_name, #to_s] object Object you need the table name for.
|
||
|
# @return [String] The table name.
|
||
|
def table_name_for(object)
|
||
|
return object.table_name if object.respond_to? :table_name
|
||
|
object.to_s.tableize
|
||
|
end
|
||
|
|
||
|
##
|
||
|
# Generate a field name
|
||
|
#
|
||
|
# @example Field names
|
||
|
# field_name_for nil # => 'NULL'
|
||
|
# field_name_for 'foo' # => 'foo'
|
||
|
# field_name_for [Issue, 'project_id'] # => 'issues.project_id'
|
||
|
# field_name_for [:issue, 'project_id'], :entry # => 'issues.project_id'
|
||
|
# field_name_for 'project_id', :entry # => 'entries.project_id'
|
||
|
#
|
||
|
# @param [Array, Object] arg Object to generate field name for.
|
||
|
# @param [Object, optional] default_table Table name to use if no table name is given.
|
||
|
# @return [String] Field name.
|
||
|
def field_name_for(arg, default_table = nil)
|
||
|
return 'NULL' unless arg
|
||
14 years ago
|
return field_name_for(arg.keys.first, default_table) if arg.is_a? Hash
|
||
15 years ago
|
return arg if arg.is_a? String and arg =~ /\.| |\(.*\)/
|
||
15 years ago
|
return table_name_for(arg.first || default_table) + '.' << arg.last.to_s if arg.is_a? Array and arg.size == 2
|
||
|
return arg.to_s unless default_table
|
||
|
field_name_for [default_table, arg]
|
||
|
end
|
||
|
|
||
|
##
|
||
|
# Sanitizes sql condition
|
||
|
#
|
||
|
# @see ActiveRecord::Base#sanitize_sql_for_conditions
|
||
|
# @param [Object] statement Not sanitized statement.
|
||
|
# @return [String] Sanitized statement.
|
||
|
def sanitize_sql_for_conditions(statement)
|
||
14 years ago
|
engine.send :sanitize_sql_for_conditions, statement
|
||
15 years ago
|
end
|
||
|
|
||
|
##
|
||
|
# Generates a SQL case statement.
|
||
|
#
|
||
|
# @example
|
||
|
# switch "#{table}.overridden_costs IS NULL" => [model, :costs], :else => [model, :overridden_costs]
|
||
|
#
|
||
|
# @param [Hash] options Condition => Result.
|
||
|
# @return [String] Case statement.
|
||
|
def switch(options)
|
||
14 years ago
|
desc = "#{__method__} #{options.inspect[1..-2]}".gsub(/(Cost|Time)Entry\([^\)]*\)/, '\1Entry')
|
||
15 years ago
|
options = options.with_indifferent_access
|
||
|
else_part = options.delete :else
|
||
14 years ago
|
"-- #{desc}\n\t" \
|
||
10 years ago
|
"CASE #{options.map { |k, v|
|
||
|
"\n\t\tWHEN #{field_name_for k}\n\t\t" \
|
||
|
"THEN #{field_name_for v}"
|
||
|
}.join(', ')}\n\t\tELSE #{field_name_for else_part}\n\tEND"
|
||
15 years ago
|
end
|
||
|
|
||
14 years ago
|
##
|
||
|
# Converts value with a given behavior, but treats nil differently.
|
||
|
# Params
|
||
|
# - value: the value to convert
|
||
|
# - block (optional) - defines how to convert values which are not nil
|
||
|
# if no block is given, values stay untouched
|
||
5 years ago
|
def convert_unless_nil(value)
|
||
14 years ago
|
if value.nil?
|
||
5 years ago
|
1.0 / 0 # Infinity, which is greater than any string or number
|
||
14 years ago
|
else
|
||
5 years ago
|
yield value
|
||
14 years ago
|
end
|
||
|
end
|
||
|
|
||
14 years ago
|
def map_field(key, value)
|
||
14 years ago
|
case key.to_s
|
||
5 years ago
|
when 'tweek', 'tmonth', 'tweek' then value.to_i
|
||
|
else convert_unless_nil(value, &:to_s)
|
||
14 years ago
|
end
|
||
|
end
|
||
|
|
||
|
def adapter_name
|
||
14 years ago
|
engine.reporting_connection.adapter_name.downcase.to_sym
|
||
14 years ago
|
end
|
||
|
|
||
|
def cache
|
||
|
Report::QueryUtils.cache
|
||
|
end
|
||
|
|
||
14 years ago
|
def compare(first, second)
|
||
|
first = Array(first).flatten
|
||
|
second = Array(second).flatten
|
||
|
first.zip second do |a, b|
|
||
|
return (a <=> b) || (a == Infinity ? 1 : -1) if a != b
|
||
|
end
|
||
|
second.size > first.size ? -1 : 0
|
||
|
end
|
||
|
|
||
6 years ago
|
def typed(type, value, escape = true)
|
||
|
safe_value = escape ? "'#{quote_string value}'" : value
|
||
|
"#{safe_value}::#{type}"
|
||
14 years ago
|
end
|
||
|
|
||
6 years ago
|
def iso_year_week(field, default_table = nil)
|
||
|
field_name = field_name_for(field, default_table)
|
||
15 years ago
|
|
||
6 years ago
|
"(EXTRACT(isoyear from #{field_name})*100 + \n\t\t" \
|
||
|
"EXTRACT(week from #{field_name} - \n\t\t" \
|
||
|
"(EXTRACT(dow FROM #{field_name})::int+6)%7))"
|
||
14 years ago
|
end
|
||
|
|
||
15 years ago
|
def self.cache
|
||
10 years ago
|
@cache ||= Hash.new { |h, k| h[k] = {} }
|
||
15 years ago
|
end
|
||
|
|
||
15 years ago
|
def self.included(klass)
|
||
|
super
|
||
|
klass.extend self
|
||
|
end
|
||
14 years ago
|
end
|