From 2e5d94800c2752a310342bbf4aca131ba5cf9034 Mon Sep 17 00:00:00 2001 From: Tim Felgentreff Date: Wed, 8 Dec 2010 18:13:30 +0100 Subject: [PATCH] replace more references to Report namespace with 'engine' method call --- app/models/report/filter/base.rb | 10 +++++---- app/models/report/filter/no_filter.rb | 1 + app/models/report/group_by/base.rb | 2 +- app/models/report/operator.rb | 1 + app/models/report/query_utils.rb | 30 +++++++++++++++++++++++++-- app/models/report/sql_statement.rb | 9 ++++++-- 6 files changed, 44 insertions(+), 9 deletions(-) diff --git a/app/models/report/filter/base.rb b/app/models/report/filter/base.rb index 43aa02af7a..7664a70227 100644 --- a/app/models/report/filter/base.rb +++ b/app/models/report/filter/base.rb @@ -1,6 +1,8 @@ class Report::Filter class Base < Report::Chainable - Report::Operator.load + include Report::CostQuery + + engine::Operator.load inherited_attribute :available_operators, :list => true, :map => :to_operator, @@ -30,8 +32,8 @@ class Report::Filter names.each do |name| dont_inherit :available_operators if skip_inherited_operators.include? name case name - when String, Report::Operator then operators << name.to_operator - when Symbol then operators.push(*Report::Operator.send(name)) + when String, engine::Operator then operators << name.to_operator + when Symbol then operators.push(*engine::Operator.send(name)) else fail "dunno what to do with #{name.inspect}" end end @@ -93,7 +95,7 @@ class Report::Filter end def operator - (@operator || self.class.default_operator || Report::Operator.default_operator).to_operator + (@operator || self.class.default_operator || engine::Operator.default_operator).to_operator end def operator=(value) diff --git a/app/models/report/filter/no_filter.rb b/app/models/report/filter/no_filter.rb index 4922861eb6..c92a3238c1 100644 --- a/app/models/report/filter/no_filter.rb +++ b/app/models/report/filter/no_filter.rb @@ -3,5 +3,6 @@ class Report::Filter::NoFilter < Report::Filter::Base singleton def sql_statement + raise NotImplementedError, "My subclass should have overwritten 'sql_statement'" end end diff --git a/app/models/report/group_by/base.rb b/app/models/report/group_by/base.rb index 966976e0c0..2867546d71 100644 --- a/app/models/report/group_by/base.rb +++ b/app/models/report/group_by/base.rb @@ -37,7 +37,7 @@ class Report::GroupBy def aggregation_mixin sql_aggregation? ? SqlAggregation : RubyAggregation - end + end def initialize(child = nil, optios = {}) super diff --git a/app/models/report/operator.rb b/app/models/report/operator.rb index 323f802f82..72f7d2c8b4 100644 --- a/app/models/report/operator.rb +++ b/app/models/report/operator.rb @@ -189,6 +189,7 @@ class Report::Operator all[name.to_s] ||= super end + #TODO: this should be inheritable by subclasses def self.all @@all_operators ||= {} end diff --git a/app/models/report/query_utils.rb b/app/models/report/query_utils.rb index 6f3418f36d..de403abdb3 100644 --- a/app/models/report/query_utils.rb +++ b/app/models/report/query_utils.rb @@ -1,6 +1,16 @@ module Report::QueryUtils delegate :quoted_false, :quoted_true, :to => "ActiveRecord::Base.connection" + def self.included(base) + @includees ||= [] + @includees << base + end + + def self.send_to_all_includees(mod) + @includees ||= [] + @includees.each {|i| i.send :include, mod } + end + ## # Subclass of Report to be used for constant lookup and such. # It is considered public API to override this method i.e. in Tests. @@ -28,7 +38,21 @@ module Report::QueryUtils # @param [#flatten] *values Ruby collection # @return [String] SQL collection def collection(*values) - "(#{values.flatten.map { |v| "'#{quote_string(v)}'" }.join ", "})" + if values.empty? + "" + else + "(#{values.flatten.map { |v| "'#{quote_string(v)}'" }.join ", "})" + end + end + + ## + # Graceful, internationalized quoted string. + # + # @see quote_string + # @param [Object] str String to quote/translate + # @return [Object] Quoted, translated version + def quoted_label(ident) + "'#{quote_string l(ident)}'" end def quoted_date(date) @@ -74,6 +98,7 @@ module Report::QueryUtils # @return [String] Field name. def field_name_for(arg, default_table = nil) return 'NULL' unless arg + return field_name_for(arg.keys.first, default_table) if arg.is_a? Hash return arg if arg.is_a? String and arg =~ /\.| |\(.*\)/ 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 @@ -87,10 +112,11 @@ module Report::QueryUtils # @param [Object] statement Not sanitized statement. # @return [String] Sanitized statement. def sanitize_sql_for_conditions(statement) - Report.send :sanitize_sql_for_conditions, statement + engine.send :sanitize_sql_for_conditions, statement end ## + # FIXME: This is redmine # Generates string representation for a currency. # # @see CostRate.clean_currency diff --git a/app/models/report/sql_statement.rb b/app/models/report/sql_statement.rb index 6757305f4a..3aded2e7ef 100644 --- a/app/models/report/sql_statement.rb +++ b/app/models/report/sql_statement.rb @@ -150,6 +150,11 @@ class Report::SqlStatement end end + def default_select(value = nil) + @default_select = value if value + @default_select ||= ["*"] + end + ## # @overload select # @return [Array] All fields/statements for select part @@ -165,7 +170,7 @@ class Report::SqlStatement # @param [Array, Hash, String, Symbol, SqlStatement] fields Fields to add to select part # @return [Array] All fields/statements for select part def select(*fields) - return(@select || ["*"]) if fields.empty? + return(@select || default_select) if fields.empty? returning(@select ||= []) do @sql = nil fields.each do |f| @@ -176,7 +181,7 @@ class Report::SqlStatement end when Hash then select f.map { |k,v| "#{field_name_for v} as #{field_name_for k}" } when String, Symbol then @select << field_name_for(f) - when Report::SqlStatement then @select << f.to_s + when engine::SqlStatement then @select << f.to_s else raise ArgumentError, "cannot handle #{f.inspect}" end end