rgviz-rails 0.30 → 0.31

Sign up to get free protection for your applications and to get access to all the features.
@@ -56,6 +56,13 @@ module Rgviz
56
56
  visitor << "lower("
57
57
  node.arguments[0].accept visitor
58
58
  visitor << ")"
59
+ when ScalarFunctionColumn::Concat
60
+ visitor << "concat("
61
+ node.arguments.each_with_index do |arg, i|
62
+ visitor << ", " if i > 0
63
+ arg.accept visitor
64
+ end
65
+ visitor << ")"
59
66
  end
60
67
  end
61
68
  end
@@ -62,6 +62,13 @@ module Rgviz
62
62
  visitor << "lower("
63
63
  node.arguments[0].accept visitor
64
64
  visitor << ")"
65
+ when ScalarFunctionColumn::Concat
66
+ visitor << "("
67
+ node.arguments.each_with_index do |arg, i|
68
+ visitor << " || " if i > 0
69
+ arg.accept visitor
70
+ end
71
+ visitor << ")"
65
72
  end
66
73
  end
67
74
  end
@@ -54,6 +54,13 @@ module Rgviz
54
54
  visitor << "lower("
55
55
  node.arguments[0].accept visitor
56
56
  visitor << ")"
57
+ when ScalarFunctionColumn::Concat
58
+ visitor << "("
59
+ node.arguments.each_with_index do |arg, i|
60
+ visitor << " || " if i > 0
61
+ arg.accept visitor
62
+ end
63
+ visitor << ")"
57
64
  end
58
65
  end
59
66
  end
@@ -6,7 +6,6 @@ module Rgviz
6
6
  def initialize(model_class, query)
7
7
  @model_class = model_class
8
8
  @query = query
9
- @query = Parser.new(query).parse unless query.kind_of?(Query)
10
9
  @selects = []
11
10
  @joins = {}
12
11
  @labels = {}
@@ -24,6 +23,8 @@ module Rgviz
24
23
  end
25
24
 
26
25
  def execute(options = {})
26
+ @query = Parser.parse(@query, options) unless @query.kind_of?(Query)
27
+
27
28
  @table = Table.new
28
29
  @extra_conditions = options[:conditions]
29
30
 
@@ -97,6 +98,8 @@ module Rgviz
97
98
 
98
99
  # Select pivot columns and group by columns
99
100
  if @query.pivot
101
+ @max_before_pivot_columns = @original_columns.length
102
+
100
103
  @query.pivot.columns.each do |col|
101
104
  col_to_s = col.to_s
102
105
 
@@ -164,7 +167,9 @@ module Rgviz
164
167
  :offset => @query.offset,
165
168
  :joins => @joins
166
169
 
167
- if @pivots.empty?
170
+ if @pivots.empty? || results.empty?
171
+ @table.cols = @table.cols[0 ... @max_before_pivot_columns] if @pivots.present?
172
+
168
173
  # Simple, just convert the results to a table
169
174
  results.each do |result|
170
175
  row = Row.new
@@ -175,13 +180,13 @@ module Rgviz
175
180
  hash = {}
176
181
  hash[:v] = column_value(col, result.send("c#{i}")) unless @query.options && @query.options.no_values
177
182
  row.c << Cell.new(hash)
178
- i += 1
183
+ i += 1
179
184
  end
180
185
  end
181
186
  else
182
187
  # A little more complicated...
183
-
184
- # This has the grouping as a key and the pivoted selection values as a value (in a list)
188
+
189
+ # This is grouping => pivot => [selections]
185
190
  fin = ActiveSupport::OrderedHash.new
186
191
 
187
192
  # The uniq pivot values
@@ -189,7 +194,6 @@ module Rgviz
189
194
 
190
195
  # Fill fin and uniq_pivots
191
196
  results.each do |result|
192
-
193
197
  # The grouping key of this result
194
198
  grouped_by = []
195
199
 
@@ -305,7 +309,7 @@ module Rgviz
305
309
  :datetime
306
310
  when ScalarFunctionColumn::ToDate
307
311
  :date
308
- when ScalarFunctionColumn::Upper, ScalarFunctionColumn::Lower
312
+ when ScalarFunctionColumn::Upper, ScalarFunctionColumn::Lower, ScalarFunctionColumn::Concat
309
313
  :string
310
314
  else
311
315
  :number
data/rails/init.rb CHANGED
@@ -13,15 +13,17 @@ config.after_initialize do
13
13
  else
14
14
  model = hash[:rgviz]
15
15
  conditions = hash[:conditions]
16
+ extensions = hash[:extensions]
16
17
  query = params[:tq]
17
18
  tqx = params[:tqx] || ''
18
19
 
19
20
  tqx = Rgviz::Tqx.parse(tqx)
20
21
 
21
22
  begin
22
- executor = Rgviz::Executor.new model, Rgviz::Parser.new(query).parse
23
+ executor = Rgviz::Executor.new model, query
23
24
  options = {}
24
25
  options[:conditions] = conditions if conditions
26
+ options[:extensions] = extensions if extensions
25
27
  table = executor.execute options
26
28
 
27
29
  yield table if block_given?
@@ -7,13 +7,9 @@ describe Executor do
7
7
  before :each do
8
8
  [Person, City, Country].each &:delete_all
9
9
  end
10
-
11
- def parse(string)
12
- Parser.new(string).parse
13
- end
14
10
 
15
11
  def exec(query, options = {})
16
- exec = Executor.new Person, (parse query)
12
+ exec = Executor.new Person, query
17
13
  exec.execute options
18
14
  end
19
15
 
@@ -25,7 +21,7 @@ describe Executor do
25
21
  date.strftime "%Y-%m-%d"
26
22
  end
27
23
 
28
- def self.it_processes_single_select_column(query, id, type, value, label)
24
+ def self.it_processes_single_select_column(query, id, type, value, label, options = {})
29
25
  it "processes select #{query}" do
30
26
  if block_given?
31
27
  yield
@@ -33,7 +29,7 @@ describe Executor do
33
29
  Person.make
34
30
  end
35
31
 
36
- table = exec "select #{query}"
32
+ table = exec "select #{query}", options
37
33
  table.cols.length.should == 1
38
34
 
39
35
  table.cols[0].id.should == id
@@ -270,6 +266,14 @@ describe Executor do
270
266
  Person.make :name => 'FOO'
271
267
  end
272
268
 
269
+ it_processes_single_select_column "concat(age)", 'c0', :string, '20', "concat(age)", :extensions => true do
270
+ Person.make :age => 20
271
+ end
272
+
273
+ it_processes_single_select_column "concat(name, 'bar')", 'c0', :string, 'foobar', "concat(name, 'bar')", :extensions => true do
274
+ Person.make :name => 'foo'
275
+ end
276
+
273
277
  it_processes_single_select_column "name label name 'my name'", 'name', :string, 'foo', "my name" do
274
278
  Person.make :name => 'foo'
275
279
  end
@@ -393,6 +397,24 @@ describe Executor do
393
397
  end
394
398
  end
395
399
 
400
+ it "processes pivot with no results" do
401
+ Person.make :name => 'Eng', :birthday => '2000-01-12', :age => 10
402
+ Person.make :name => 'Sales', :birthday => '2001-02-12', :age => 20
403
+
404
+ table = exec 'select birthday, sum(age) where 1 = 2 group by month(birthday) pivot name order by name'
405
+
406
+ table.cols.length.should == 2
407
+
408
+ i = 0
409
+ [['birthday', :date, 'birthday'],
410
+ ['c1', :number, 'sum(age)']].each do |id, type, label|
411
+ table.cols[i].id.should == id
412
+ table.cols[i].type.should == type
413
+ table.cols[i].label.should == label
414
+ i += 1
415
+ end
416
+ end
417
+
396
418
  it "processes pivot with group by not in select" do
397
419
  Person.make :name => 'Eng', :birthday => '2000-01-12', :age => 10
398
420
  Person.make :name => 'Sales', :birthday => '2001-02-12', :age => 20
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rgviz-rails
3
3
  version: !ruby/object:Gem::Version
4
- hash: 55
4
+ hash: 53
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 30
9
- version: "0.30"
8
+ - 31
9
+ version: "0.31"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ary Borenszweig