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.
289 lines
8.4 KiB
289 lines
8.4 KiB
11 years ago
|
#-- copyright
|
||
|
# ReportingEngine
|
||
|
#
|
||
|
# Copyright (C) 2010 - 2014 the OpenProject Foundation (OPF)
|
||
|
#
|
||
|
# This program is free software; you can redistribute it and/or
|
||
|
# modify it under the terms of the GNU General Public License
|
||
|
# version 3.
|
||
|
#
|
||
|
# 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.
|
||
|
#++
|
||
|
|
||
14 years ago
|
class Report::SqlStatement
|
||
14 years ago
|
class Union
|
||
|
attr_accessor :first, :second, :as
|
||
|
def initialize(first, second, as = nil)
|
||
|
@first, @second, @as = first, second, as
|
||
|
end
|
||
|
|
||
|
def to_s
|
||
14 years ago
|
"((\n#{first.gsub("\n", "\n\t")}\n) UNION (\n" \
|
||
|
"#{second.gsub("\n", "\n\t")}\n))#{" AS #{as}" if as}\n"
|
||
14 years ago
|
end
|
||
|
|
||
|
def each_subselect
|
||
|
yield first
|
||
|
yield second
|
||
|
end
|
||
|
|
||
|
def gsub(*args, &block)
|
||
|
to_s.gsub(*args, &block)
|
||
|
end
|
||
|
end
|
||
|
|
||
14 years ago
|
include Report::QueryUtils
|
||
|
|
||
14 years ago
|
##
|
||
|
# Describes the query. This may be used in a sql-comment later.
|
||
14 years ago
|
attr_accessor :desc
|
||
15 years ago
|
|
||
|
##
|
||
|
# Generates new SqlStatement.
|
||
|
#
|
||
|
# @param [String, #to_s] table Table name (or subselect) for from part.
|
||
10 years ago
|
def initialize(table, desc = '')
|
||
14 years ago
|
self.desc = desc
|
||
15 years ago
|
from table
|
||
|
end
|
||
|
|
||
|
##
|
||
|
# Creates a uninon of the caller and the callee.
|
||
|
#
|
||
14 years ago
|
# @param [Report::SqlStatement] other Second part of the union
|
||
15 years ago
|
# @return [String] The sql query.
|
||
|
def union(other, as = nil)
|
||
14 years ago
|
Union.new(self, other, as)
|
||
15 years ago
|
end
|
||
|
|
||
|
##
|
||
|
# Adds sum(..) part to select.
|
||
|
#
|
||
|
# @param [#to_s] field Name of the field to aggregate on
|
||
|
# @param [#to_s] name Name of the result (defaults to sum)
|
||
|
def sum(field, name = :sum, type = :sum)
|
||
15 years ago
|
@sql = nil
|
||
15 years ago
|
return sum({ name => field }, nil, type) unless field.respond_to? :to_hash
|
||
10 years ago
|
field.each { |k, v| field[k] = "#{type}(#{v})" }
|
||
15 years ago
|
select field
|
||
|
end
|
||
|
|
||
|
##
|
||
|
# Adds count(..) part to select.
|
||
|
#
|
||
|
# @param [#to_s] field Name of the field to aggregate on (defaults to *)
|
||
|
# @param [#to_s] name Name of the result (defaults to sum)
|
||
10 years ago
|
def count(field = '*', name = :count)
|
||
15 years ago
|
sum field, name, :count
|
||
|
end
|
||
|
|
||
|
##
|
||
|
# Generates the SQL query.
|
||
|
# Code looks ugly in exchange for pretty output (so one does unterstand those).
|
||
|
#
|
||
|
# @return [String] The query
|
||
|
def to_s
|
||
|
# FIXME I'm ugly
|
||
15 years ago
|
@sql ||= begin
|
||
14 years ago
|
sql = "\n-- BEGIN #{desc}\n" \
|
||
14 years ago
|
"-- DB: #{ActiveRecord::Base.connection.current_database}\n" \
|
||
14 years ago
|
"SELECT\n#{select.map { |e| "\t#{e}" }.join ",\n"}" \
|
||
15 years ago
|
"\nFROM\n\t#{from.gsub("\n", "\n\t")}" \
|
||
14 years ago
|
"\n\t#{joins.map { |e| e.gsub("\n", "\n\t") }.join "\n\t"}" \
|
||
10 years ago
|
"\nWHERE #{where.join ' AND '}\n"
|
||
15 years ago
|
sql << "GROUP BY #{group_by.join ', '}\nORDER BY #{group_by.join ', '}\n" if group_by?
|
||
14 years ago
|
sql << "-- END #{desc}\n"
|
||
|
sql.gsub!('--', '#') if mysql?
|
||
15 years ago
|
sql # << " LIMIT 100"
|
||
15 years ago
|
end
|
||
15 years ago
|
end
|
||
|
|
||
|
##
|
||
|
# @overload from
|
||
|
# Reads the from part.
|
||
|
# @return [#to_s] From part
|
||
|
# @overload from(table)
|
||
|
# Sets the from part.
|
||
|
# @param [#to_s] table
|
||
|
# @param [#to_s] From part
|
||
|
def from(table = nil)
|
||
15 years ago
|
return @from unless table
|
||
|
@sql = nil
|
||
|
@from = table
|
||
15 years ago
|
end
|
||
|
|
||
|
##
|
||
|
# Where conditions. Will be joined together by AND.
|
||
|
#
|
||
|
# @overload where
|
||
|
# Reads the where part
|
||
|
# @return [Array<#to_s>] Where clauses
|
||
|
# @overload where(fields)
|
||
|
# Adds condition to where clause
|
||
|
# @param [Array, Hash, String] fields Parameters passed to sanitize_sql_for_conditions.
|
||
14 years ago
|
# @see Report::QueryUtils#sanitize_sql_for_conditions
|
||
15 years ago
|
def where(fields = nil)
|
||
10 years ago
|
@where ||= ['1=1']
|
||
15 years ago
|
unless fields.nil?
|
||
|
@where << sanitize_sql_for_conditions(fields)
|
||
|
@sql = nil
|
||
|
end
|
||
15 years ago
|
@where
|
||
|
end
|
||
|
|
||
|
##
|
||
|
# @return [Array<String>] List of table joins
|
||
|
def joins
|
||
10 years ago
|
(@joins ||= []).tap(&:uniq!)
|
||
15 years ago
|
end
|
||
|
|
||
|
##
|
||
|
# Adds an "left outer join" (guessing field names) to #joins.
|
||
|
#
|
||
|
# @overload join(name)
|
||
|
# @param [Symbol, String] name Singular table name to join with, will join plural from on table.id = table_id
|
||
|
# @overload join(model)
|
||
|
# @param [#table_name, #model_name] model ActiveRecord model to join with
|
||
|
# @overload join(hash)
|
||
|
# @param [Hash<#to_s => #to_s>] hash Key is singular table name to join with, value is field to join on
|
||
|
# @overload join(*list)
|
||
|
# @param [Array<String,Symbol,Array>] list Will generate join entries (according to guessings described above)
|
||
|
# @see #joins
|
||
|
def join(*list)
|
||
15 years ago
|
@sql = nil
|
||
10 years ago
|
join_syntax = 'LEFT OUTER JOIN %1$s ON %1$s.id = %2$s_id'
|
||
15 years ago
|
list.each do |e|
|
||
|
case e
|
||
10 years ago
|
when Class then joins << (join_syntax % [table_name_for(e), e.lookup_ancestors.last.model_name.to_s.underscore])
|
||
14 years ago
|
when / / then joins << e
|
||
15 years ago
|
when Symbol, String then joins << (join_syntax % [table_name_for(e), e])
|
||
10 years ago
|
when Hash then e.each { |k, v| joins << (join_syntax % [table_name_for(k), field_name_for(v)]) }
|
||
15 years ago
|
when Array then join(*e)
|
||
|
else raise ArgumentError, "cannot join #{e.inspect}"
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
14 years ago
|
def default_select(value = nil)
|
||
|
@default_select = value if value
|
||
10 years ago
|
@default_select ||= ['*']
|
||
14 years ago
|
end
|
||
|
|
||
15 years ago
|
##
|
||
|
# @overload select
|
||
|
# @return [Array<String>] All fields/statements for select part
|
||
|
#
|
||
|
# @overload select(*fields)
|
||
|
# Adds fields to select query.
|
||
|
# @example
|
||
15 years ago
|
# SqlStatement.new.select(some_sql_statement) # [some_sql_statement.to_s]
|
||
|
# SqlStatement.new.select("sum(foo)") # ["sum(foo)"]
|
||
|
# SqlStatement.new.select(:a).select(:b) # ["a", "b"]
|
||
|
# SqlStatement.new.select(:bar => :foo) # ["foo as bar"]
|
||
|
# SqlStatement.new.select(:bar => nil) # ["NULL as bar"]
|
||
15 years ago
|
# @param [Array, Hash, String, Symbol, SqlStatement] fields Fields to add to select part
|
||
|
# @return [Array<String>] All fields/statements for select part
|
||
|
def select(*fields)
|
||
14 years ago
|
return(@select || default_select) if fields.empty?
|
||
14 years ago
|
(@select ||= []).tap do
|
||
15 years ago
|
@sql = nil
|
||
10 years ago
|
fields.reject { |f| never_select.include? f }.each do |f|
|
||
15 years ago
|
case f
|
||
|
when Array
|
||
|
if f.size == 2 and f.first.respond_to? :table_name then select field_name_for(f)
|
||
|
else select(*f)
|
||
|
end
|
||
10 years ago
|
when Hash then select f.map { |k, v| "#{field_name_for v} as #{field_name_for k}" }
|
||
15 years ago
|
when String, Symbol then @select << field_name_for(f)
|
||
14 years ago
|
when engine::SqlStatement then @select << f.to_s
|
||
15 years ago
|
else raise ArgumentError, "cannot handle #{f.inspect}"
|
||
|
end
|
||
|
end
|
||
|
# when doing a union in sql, both subselects must have the same order.
|
||
|
# by sorting here we never ever have to worry about this again, sucker!
|
||
10 years ago
|
@select = @select.uniq.sort_by { |x| x.split(' as ').last }
|
||
15 years ago
|
end
|
||
|
end
|
||
|
|
||
14 years ago
|
def unselect(*fields)
|
||
|
@sql = nil
|
||
|
@select = @select.reject do |field|
|
||
|
fields.find { |f| f == field }
|
||
|
end
|
||
|
end
|
||
|
|
||
14 years ago
|
def never_select(*fields)
|
||
|
(@never_select ||= []).tap do
|
||
|
unless fields.empty?
|
||
|
@never_select += fields
|
||
|
unselect *fields
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
14 years ago
|
##
|
||
|
# Return the names which have been bound through select statements
|
||
|
# @return [Array<String>] All fields for select part
|
||
|
def selects
|
||
10 years ago
|
@select.map { |s| s.split(' as ').last }
|
||
14 years ago
|
end
|
||
|
|
||
15 years ago
|
##
|
||
|
# @overload group_by
|
||
|
# @return [Array<String>] All fields/statements for group by part
|
||
|
#
|
||
|
# @overload group(*fields)
|
||
|
# Adds fields to group by query
|
||
|
# @param [Array, String, Symbol] fields Fields to add
|
||
|
def group_by(*fields)
|
||
15 years ago
|
@sql = nil unless fields.empty?
|
||
14 years ago
|
(@group_by ||= []).tap do
|
||
10 years ago
|
fields.reject { |f| never_group_by.include? f }.each do |e|
|
||
15 years ago
|
if e.is_a? Array and (e.size != 2 or !e.first.respond_to? :table_name)
|
||
|
group_by(*e)
|
||
|
else
|
||
|
@group_by << field_name_for(e)
|
||
|
end
|
||
|
end
|
||
14 years ago
|
@group_by.uniq!
|
||
15 years ago
|
end
|
||
|
end
|
||
|
|
||
14 years ago
|
def group_not_by(*fields)
|
||
|
@sql = nil
|
||
|
@group_by = @group_by.reject do |field|
|
||
|
fields.find { |f| f == field }
|
||
|
end
|
||
|
end
|
||
|
|
||
14 years ago
|
def never_group_by(*fields)
|
||
|
(@never_group_by ||= []).tap do
|
||
|
unless fields.empty?
|
||
|
@never_group_by += fields
|
||
|
group_not_by *fields
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
15 years ago
|
##
|
||
|
# @return [TrueClass, FalseClass] Whether or not to add a group by part.
|
||
|
def group_by?
|
||
|
!group_by.empty?
|
||
|
end
|
||
|
|
||
|
def inspect
|
||
|
"#<SqlStatement: #{to_s.inspect}>"
|
||
|
end
|
||
|
|
||
14 years ago
|
def gsub(*args, &block)
|
||
|
to_s.gsub(*args, &block)
|
||
|
end
|
||
15 years ago
|
end
|