old_sql 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,6 +4,12 @@ OldSql.setup do |config|
4
4
  # The title of the Report Selection View.
5
5
  config.report_select_page_title = 'Old SQL Reports'
6
6
 
7
+ # Determines whether the values for the report will be rounded.
8
+ config.round_report_values = true
9
+
10
+ # The precision to round all values to if rounding is enabled.
11
+ config.rounding_precision = 2
12
+
7
13
  # Configure the default report view. This setting will be used unless overridden
8
14
  # in config/old_sql/reports.yml.
9
15
  config.default_report_view = "jqgrid"
@@ -1,6 +1,26 @@
1
1
  # Old SQL:
2
- # Example Report Design document.
2
+ # Example Report Design Document
3
+ #
4
+ # Text not in quotes are column names from the SQL query
5
+ # Quoted text are labels that will appear in the report.
6
+ # Expressions like ( id + id ) * 10 below must be single
7
+ # space delimited. So (id+id) will not parse correctly.
8
+ #
9
+ # NaN and Infinity are returned as zero.
10
+ #
11
+ # Comments are lines beginning with a hash #. They are
12
+ # stripped when this document is parsed.
13
+ #
14
+ # Values (either columns or expressions) are rounded by default.
15
+ # To disable this set round_report_values to false in
16
+ # config/initializers/old_sql.rb. You can also change the
17
+ # precision, which is 2 by default.
3
18
  #
4
19
  id,name
5
20
  "Example Math Operation",id + id
6
- "Totals","..."
21
+ "Totals","..."
22
+ "Expression",( id + id ) * 10
23
+ "Divide by Zero 1",0 / 0
24
+ "Divide by Zero 2",1.0 / 0
25
+ "Divide by Zero 3",0.0 / 0.0
26
+ "Rounding",200.0 / 43.0
@@ -8,7 +8,7 @@ module OldSql
8
8
  module ReportProcessor
9
9
  class Base
10
10
 
11
- ROUND_PRECISION = 2
11
+ ROUND_PRECISION = OldSql.rounding_precision
12
12
 
13
13
  def execute_query(report_sql,start_date,end_date,query_vars,design=nil,sub_processor=nil)
14
14
  vars = {:start_date => start_date, :end_date => end_date}
@@ -94,7 +94,11 @@ module OldSql
94
94
  end
95
95
  else
96
96
  if cd.type == OldSql::ReportDesign::CellData::COLUMN
97
- report_row << @rec[cd.data]
97
+ result = @rec[cd.data]
98
+ if OldSql.round_report_values
99
+ result = round(result.to_f, OldSql.rounding_precision)
100
+ end
101
+ report_row << result
98
102
  elsif cd.type == OldSql::ReportDesign::CellData::LABEL
99
103
  report_row << cd.data.gsub(/"/,"")
100
104
  end
@@ -108,11 +112,10 @@ module OldSql
108
112
  end
109
113
 
110
114
  def eval_expression expression
111
- result = "0.0"
115
+ result = 0.0
112
116
  begin
113
117
  Rails.logger.debug "Evalutating Expression: #{expression}"
114
118
  result = eval(expression)
115
- Rails.logger.debug "EXPR RESULT #{result.to_s}"
116
119
  rescue ZeroDivisionError => e
117
120
  Rails.logger.error e
118
121
  rescue Exception => e
@@ -124,14 +127,20 @@ module OldSql
124
127
  end
125
128
 
126
129
  if result == "Infinity" || result == "NaN"
127
- result = "0.0"
130
+ result = 0.0
131
+ elsif OldSql.round_report_values
132
+ result = round(result.to_f, OldSql.rounding_precision)
128
133
  end
129
134
 
130
- puts "Result of the expression: #{result}"
131
- Rails.logger.debug "Result of the expression: #{result}"
135
+ Rails.logger.debug "Expression result: #{result}"
132
136
 
133
137
  result
134
138
  end
139
+
140
+ def round(value, precision = ROUND_PRECISION)
141
+ factor = 10.0**precision
142
+ (value*factor).round / factor
143
+ end
135
144
 
136
145
  def new_data(page=1, total=1, records=1)
137
146
  @id = 0
data/lib/old_sql.rb CHANGED
@@ -10,10 +10,13 @@ module OldSql
10
10
  mattr_accessor :default_report_view
11
11
  @@default_report_view = 'jqgrid'
12
12
 
13
- # The default report view. This setting will be used unless overridden in
14
- # config/old_sql/reports.yml.
15
- mattr_accessor :default_report_view
16
- @@default_report_view = 'jqgrid'
13
+ # Determines whether the values for the report will be rounded.
14
+ mattr_accessor :round_report_values
15
+ @@round_report_values = true
16
+
17
+ # The precision to round all values to if rounding is enabled.
18
+ mattr_accessor :rounding_precision
19
+ @@rounding_precision = 2
17
20
 
18
21
  # Width of the jqGrid component in the jqGrid report view.
19
22
  mattr_accessor :jqgrid_width
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: old_sql
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.1.0
5
+ version: 1.2.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Eddie Gonzales
@@ -200,7 +200,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
200
200
  requirements:
201
201
  - - ">="
202
202
  - !ruby/object:Gem::Version
203
- hash: -2514440904525593455
203
+ hash: 1929929110961270162
204
204
  segments:
205
205
  - 0
206
206
  version: "0"