rgviz-rails 0.46 → 0.47

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,7 @@ module Rgviz
2
2
  class Executor
3
3
  attr_reader :model_class
4
4
  attr_reader :adapter
5
-
5
+
6
6
  def initialize(model_class, query)
7
7
  @model_class = model_class
8
8
  @query = query
@@ -14,55 +14,58 @@ module Rgviz
14
14
  @original_columns = []
15
15
  case ActiveRecord::Base.connection.adapter_name.downcase
16
16
  when 'sqlite'
17
+ require File.dirname(__FILE__) + '/adapters/sqlite_adapter.rb'
17
18
  @adapter = SqliteAdapter.new
18
19
  when 'mysql'
20
+ require File.dirname(__FILE__) + '/adapters/mysql_adapter.rb'
19
21
  @adapter = MySqlAdapter.new
20
22
  when 'postgresql'
23
+ require File.dirname(__FILE__) + '/adapters/postgresql_adapter.rb'
21
24
  @adapter = PostgreSqlAdapter.new
22
25
  end
23
26
  end
24
-
27
+
25
28
  def execute(options = {})
26
29
  @query = Parser.parse(@query, options) unless @query.kind_of?(Query)
27
-
30
+
28
31
  @table = Table.new
29
32
  @extra_conditions = options[:conditions]
30
-
33
+
31
34
  process_pivot
32
35
  process_labels
33
-
36
+
34
37
  generate_columns
35
38
  generate_conditions
36
39
  generate_group
37
40
  generate_order
38
-
41
+
39
42
  generate_rows
40
-
43
+
41
44
  @table
42
45
  end
43
-
46
+
44
47
  def process_labels
45
48
  return unless @query.labels.present?
46
-
49
+
47
50
  @query.labels.each do |label|
48
51
  @labels[label.column.to_s] = label.label
49
52
  end
50
53
  end
51
-
54
+
52
55
  def process_pivot
53
56
  if @query.pivot
54
57
  @query.pivot.columns.each do |column|
55
58
  @pivots[column.to_s] = true
56
59
  end
57
60
  end
58
-
61
+
59
62
  if @query.group_by
60
63
  @query.group_by.columns.each do |column|
61
64
  @group_bys[column.to_s] = true
62
65
  end
63
66
  end
64
67
  end
65
-
68
+
66
69
  def add_joins(joins)
67
70
  map = @joins
68
71
  joins.each do |join|
@@ -72,14 +75,14 @@ module Rgviz
72
75
  map = map[key]
73
76
  end
74
77
  end
75
-
78
+
76
79
  def generate_columns
77
80
  if @query.select && @query.select.columns.present?
78
81
  # Select the specified columns
79
82
  i = 0
80
83
  @query.select.columns.each do |col|
81
84
  col_to_s = col.to_s
82
-
85
+
83
86
  @table.cols << (Column.new :id => column_id(col, i), :type => column_type(col), :label => column_label(col_to_s))
84
87
  @selects << "(#{column_select(col)}) as c#{i}"
85
88
  @original_columns << col_to_s
@@ -95,26 +98,26 @@ module Rgviz
95
98
  i += 1
96
99
  end
97
100
  end
98
-
101
+
99
102
  # Select pivot columns and group by columns
100
103
  if @query.pivot
101
104
  @max_before_pivot_columns = @original_columns.length
102
-
105
+
103
106
  @query.pivot.columns.each do |col|
104
107
  col_to_s = col.to_s
105
-
108
+
106
109
  @table.cols << (Column.new :id => column_id(col, i), :type => column_type(col), :label => column_label(col_to_s))
107
- @selects << "(#{column_select(col)}) as c#{i}"
110
+ @selects << "(#{column_select(col)}) as c#{i}"
108
111
  @original_columns << col_to_s
109
112
  i += 1
110
113
  end
111
-
114
+
112
115
  @max_original_columns = @original_columns.length
113
-
116
+
114
117
  if @query.group_by
115
118
  @query.group_by.columns.each do |col|
116
119
  col_to_s = col.to_s
117
-
120
+
118
121
  @table.cols << (Column.new :id => column_id(col, i), :type => column_type(col), :label => column_label(col_to_s))
119
122
  @selects << "(#{column_select(col)}) as c#{i}"
120
123
  i += 1
@@ -122,28 +125,28 @@ module Rgviz
122
125
  end
123
126
  end
124
127
  end
125
-
128
+
126
129
  def generate_conditions
127
130
  @conditions = to_string @query.where, WhereVisitor if @query.where
128
131
  end
129
-
132
+
130
133
  def generate_group
131
134
  @group = to_string @query.group_by, ColumnVisitor if @query.group_by
132
135
  pivot = to_string @query.pivot, ColumnVisitor if @query.pivot
133
-
136
+
134
137
  if pivot.present?
135
138
  if @group.present?
136
- @group += ',' + pivot
139
+ @group += ',' + pivot
137
140
  else
138
141
  @group = pivot
139
142
  end
140
143
  end
141
144
  end
142
-
145
+
143
146
  def generate_order
144
147
  @order = to_string @query.order_by, OrderVisitor if @query.order_by
145
148
  end
146
-
149
+
147
150
  def generate_rows
148
151
  conditions = @conditions
149
152
  if @extra_conditions
@@ -151,14 +154,14 @@ module Rgviz
151
154
  if @extra_conditions.kind_of? String
152
155
  conditions = "(#{conditions}) AND #{@extra_conditions}"
153
156
  elsif @extra_conditions.kind_of? Array
154
- conditions = ["(#{conditions}) AND #{@extra_conditions[0]}", *@extra_conditions[1 .. -1]]
157
+ conditions = ["(#{conditions}) AND #{@extra_conditions[0]}", *@extra_conditions[1 .. -1]]
155
158
  end
156
159
  else
157
160
  conditions = @extra_conditions
158
161
  end
159
162
  end
160
-
161
- results = @model_class.send :all,
163
+
164
+ results = @model_class.send :all,
162
165
  :select => @selects.join(','),
163
166
  :conditions => conditions,
164
167
  :group => @group,
@@ -166,15 +169,15 @@ module Rgviz
166
169
  :limit => @query.limit,
167
170
  :offset => @query.offset,
168
171
  :joins => @joins
169
-
172
+
170
173
  if @pivots.empty? || results.empty?
171
174
  @table.cols = @table.cols[0 ... @max_before_pivot_columns] if @pivots.present?
172
-
175
+
173
176
  # Simple, just convert the results to a table
174
177
  results.each do |result|
175
178
  row = Row.new
176
179
  @table.rows << row
177
-
180
+
178
181
  i = 0
179
182
  @table.cols.each do |col|
180
183
  hash = {}
@@ -185,26 +188,26 @@ module Rgviz
185
188
  end
186
189
  else
187
190
  # A little more complicated...
188
-
191
+
189
192
  # This is grouping => pivot => [selections]
190
193
  fin = ActiveSupport::OrderedHash.new
191
-
194
+
192
195
  # The uniq pivot values
193
196
  uniq_pivots = []
194
-
197
+
195
198
  # Fill fin and uniq_pivots
196
199
  results.each do |result|
197
200
  # The grouping key of this result
198
201
  grouped_by = []
199
-
202
+
200
203
  # The pivots of this result
201
204
  pivots = []
202
-
205
+
203
206
  # The selections of this result
204
207
  selections = []
205
-
208
+
206
209
  # Fill grouped_by, pivots and selections, as well as uniq_pivots
207
- @table.cols.each_with_index do |col, i|
210
+ @table.cols.each_with_index do |col, i|
208
211
  val = column_value(col, result.send("c#{i}"))
209
212
  if i >= @max_original_columns || @group_bys.include?(@original_columns[i])
210
213
  grouped_by << val
@@ -214,44 +217,44 @@ module Rgviz
214
217
  selections << val
215
218
  end
216
219
  end
217
-
220
+
218
221
  uniq_pivots << pivots unless uniq_pivots.include? pivots
219
-
222
+
220
223
  # Now put all this info into fin
221
224
  fin[grouped_by] = {} unless fin[grouped_by]
222
225
  fin[grouped_by][pivots] = selections
223
226
  end
224
-
227
+
225
228
  # Sort the uniq pivots so the results will be sorted for a human
226
229
  uniq_pivots.sort!
227
-
230
+
228
231
  # Regenerate the columns info: the current info has the values
229
- # we needed to get the info we needed
232
+ # we needed to get the info we needed
230
233
  col_i = 0
231
234
  new_cols = []
232
235
  @original_columns.each_with_index do |original_column, i|
233
236
  break if i >= @max_original_columns
234
-
235
- old_col = @table.cols[i]
237
+
238
+ old_col = @table.cols[i]
236
239
  if @group_bys.include?(original_column)
237
240
  old_col.id = "c#{col_i}"
238
241
  new_cols << @table.cols[i]
239
242
  col_i += 1
240
243
  elsif !@pivots.include?(original_column)
241
244
  uniq_pivots.each do |uniq_pivot|
242
- new_cols << (Column.new :id => "c#{col_i}", :type => old_col.type, :label => "#{uniq_pivot.join(', ')} #{old_col.label}")
245
+ new_cols << (Column.new :id => "c#{col_i}", :type => old_col.type, :label => "#{uniq_pivot.join(', ')} #{old_col.label}")
243
246
  col_i += 1
244
247
  end
245
248
  end
246
249
  end
247
-
250
+
248
251
  @table.cols = new_cols
249
-
252
+
250
253
  # Create the rows
251
254
  fin.each do |key, value|
252
255
  row = Row.new
253
256
  @table.rows << row
254
-
257
+
255
258
  group_i = 0
256
259
  value_i = 0
257
260
  @original_columns.each_with_index do |original_column, i|
@@ -264,7 +267,7 @@ module Rgviz
264
267
  uniq_pivots.each do |uniq_pivot|
265
268
  v = value[uniq_pivot]
266
269
  v = v[value_i] if v
267
-
270
+
268
271
  hash = {}
269
272
  hash[:v] = v unless @query.options && @query.options.no_values
270
273
  row.c << (Cell.new hash)
@@ -275,7 +278,7 @@ module Rgviz
275
278
  end
276
279
  end
277
280
  end
278
-
281
+
279
282
  def column_id(col, i)
280
283
  case col
281
284
  when IdColumn
@@ -284,7 +287,7 @@ module Rgviz
284
287
  "c#{i}"
285
288
  end
286
289
  end
287
-
290
+
288
291
  def column_type(col)
289
292
  case col
290
293
  when IdColumn
@@ -312,96 +315,96 @@ module Rgviz
312
315
  when ScalarFunctionColumn::Upper, ScalarFunctionColumn::Lower, ScalarFunctionColumn::Concat
313
316
  :string
314
317
  else
315
- :number
318
+ :number
316
319
  end
317
320
  when AggregateColumn
318
321
  :number
319
322
  end
320
323
  end
321
-
324
+
322
325
  def column_select(col)
323
326
  to_string col, ColumnVisitor
324
327
  end
325
-
328
+
326
329
  def column_value(col, value)
327
330
  case col.type
328
331
  when :number
329
332
  i = value.to_i
330
- f = value.to_f
333
+ f = value.to_f
331
334
  i == f ? i : f
332
335
  when :boolean
333
- value == '1' ? true : false
336
+ value == 1 || value == '1' ? true : false
334
337
  else
335
338
  value.to_s
336
339
  end
337
340
  end
338
-
341
+
339
342
  def column_label(string)
340
343
  @labels[string] || string
341
344
  end
342
-
345
+
343
346
  def to_string(node, visitor_class)
344
347
  visitor = visitor_class.new self
345
348
  node.accept visitor
346
349
  visitor.string
347
350
  end
348
-
351
+
349
352
  def rails_column_type(col)
350
353
  case col.type
351
- when :integer
354
+ when :integer, :float, :decimal
352
355
  :number
353
356
  else
354
357
  col.type
355
358
  end
356
359
  end
357
360
  end
358
-
361
+
359
362
  class ColumnVisitor < Rgviz::Visitor
360
363
  attr_reader :string
361
-
364
+
362
365
  def initialize(executor)
363
366
  @string = ''
364
367
  @executor = executor
365
368
  end
366
-
369
+
367
370
  def <<(string)
368
371
  @string += string
369
372
  end
370
-
373
+
371
374
  def visit_id_column(node)
372
375
  klass, rails_col, joins = Rgviz::find_rails_col @executor.model_class, node.name
373
376
  raise "Unknown column '#{node.name}'" unless rails_col
374
377
  @string += ActiveRecord::Base.connection.quote_column_name(klass.table_name)
375
378
  @string += '.'
376
379
  @string += ActiveRecord::Base.connection.quote_column_name(rails_col.name)
377
-
380
+
378
381
  @executor.add_joins joins
379
382
  end
380
-
383
+
381
384
  def visit_number_column(node)
382
385
  @string += node.value.to_s
383
386
  end
384
-
387
+
385
388
  def visit_string_column(node)
386
389
  @string += escaped_string(node.value)
387
390
  end
388
-
391
+
389
392
  def visit_boolean_column(node)
390
393
  @string += node.value ? '1' : '0'
391
394
  end
392
-
395
+
393
396
  def visit_date_column(node)
394
397
  @string += escaped_string(node.value.to_s)
395
398
  end
396
-
399
+
397
400
  def visit_date_time_column(node)
398
401
  @string += escaped_string(node.value.strftime("%Y-%m-%d %H:%M:%S"))
399
402
  end
400
-
403
+
401
404
  def visit_time_of_day_column(node)
402
405
  @string += escaped_string(node.value.strftime("%H:%M:%S"))
403
406
  end
404
-
407
+
405
408
  def visit_scalar_function_column(node)
406
409
  case node.function
407
410
  when ScalarFunctionColumn::Sum, ScalarFunctionColumn::Difference,
@@ -416,7 +419,7 @@ module Rgviz
416
419
  end
417
420
  false
418
421
  end
419
-
422
+
420
423
  def visit_aggregate_column(node)
421
424
  @string += node.function.to_s
422
425
  @string += '('
@@ -424,7 +427,7 @@ module Rgviz
424
427
  @string += ')'
425
428
  false
426
429
  end
427
-
430
+
428
431
  def visit_group_by(node)
429
432
  node.columns.each_with_index do |c, i|
430
433
  @string += ',' if i > 0
@@ -432,7 +435,7 @@ module Rgviz
432
435
  end
433
436
  false
434
437
  end
435
-
438
+
436
439
  def visit_pivot(node)
437
440
  node.columns.each_with_index do |c, i|
438
441
  @string += ',' if i > 0
@@ -440,25 +443,25 @@ module Rgviz
440
443
  end
441
444
  false
442
445
  end
443
-
446
+
444
447
  def visit_label(node)
445
448
  false
446
449
  end
447
-
450
+
448
451
  def visit_format(node)
449
452
  false
450
453
  end
451
-
454
+
452
455
  def visit_option(node)
453
456
  false
454
457
  end
455
-
458
+
456
459
  def escaped_string(str)
457
460
  str = str.gsub("'", "''")
458
461
  "'#{str}'"
459
462
  end
460
463
  end
461
-
464
+
462
465
  class WhereVisitor < ColumnVisitor
463
466
  def visit_logical_expression(node)
464
467
  @string += "("
@@ -469,14 +472,14 @@ module Rgviz
469
472
  @string += ")"
470
473
  false
471
474
  end
472
-
475
+
473
476
  def visit_binary_expression(node)
474
477
  node.left.accept self
475
478
  @string += " #{node.operator} "
476
479
  node.right.accept self
477
480
  false
478
481
  end
479
-
482
+
480
483
  def visit_unary_expression(node)
481
484
  case node.operator
482
485
  when UnaryExpression::Not
@@ -493,7 +496,7 @@ module Rgviz
493
496
  false
494
497
  end
495
498
  end
496
-
499
+
497
500
  class OrderVisitor < ColumnVisitor
498
501
  def visit_order_by(node)
499
502
  node.sorts.each_with_index do |sort, i|
@@ -502,34 +505,34 @@ module Rgviz
502
505
  end
503
506
  false
504
507
  end
505
-
508
+
506
509
  def visit_sort(node)
507
510
  node.column.accept self
508
511
  @string += node.order == Sort::Asc ? ' asc' : ' desc'
509
512
  false
510
513
  end
511
514
  end
512
-
515
+
513
516
  def self.find_rails_col(klass, name)
514
517
  joins = []
515
-
518
+
516
519
  while true
517
520
  col = klass.send(:columns).select{|x| x.name == name}.first
518
521
  return [klass, col, joins] if col
519
-
522
+
520
523
  idx = name.index '_'
521
524
  return nil if not idx
522
-
525
+
523
526
  before = name[0 ... idx]
524
527
  name = name[idx + 1 .. -1]
525
-
528
+
526
529
  assoc = klass.send :reflect_on_association, before.to_sym
527
530
  raise "Unknown association #{before}" unless assoc
528
531
  klass = assoc.klass
529
532
  joins << assoc
530
533
  end
531
534
  end
532
-
535
+
533
536
  class NotSupported < ::Exception
534
537
  end
535
538
  end
data/lib/rgviz_rails.rb CHANGED
@@ -2,6 +2,3 @@ require 'rgviz_rails/init.rb'
2
2
  require 'rgviz_rails/executor.rb'
3
3
  require 'rgviz_rails/js_renderer.rb'
4
4
  require 'rgviz_rails/tqx.rb'
5
- require 'rgviz_rails/adapters/mysql_adapter.rb'
6
- require 'rgviz_rails/adapters/postgresql_adapter.rb'
7
- require 'rgviz_rails/adapters/sqlite_adapter.rb'
@@ -1,5 +1,5 @@
1
1
  require File.dirname(__FILE__) + '/../spec_helper'
2
- require 'rgviz'
2
+ #require 'rgviz'
3
3
 
4
4
  include Rgviz
5
5
 
@@ -7,20 +7,20 @@ describe Executor do
7
7
  before :each do
8
8
  [Person, City, Country].each &:delete_all
9
9
  end
10
-
10
+
11
11
  def exec(query, options = {})
12
12
  exec = Executor.new Person, query
13
13
  exec.execute options
14
14
  end
15
-
15
+
16
16
  def format_datetime(date)
17
17
  date.strftime "%Y-%m-%d %H:%M:%S"
18
18
  end
19
-
19
+
20
20
  def format_date(date)
21
21
  date.strftime "%Y-%m-%d"
22
22
  end
23
-
23
+
24
24
  def self.it_processes_single_select_column(query, id, type, value, label, options = {})
25
25
  it "processes select #{query}" do
26
26
  if block_given?
@@ -28,27 +28,27 @@ describe Executor do
28
28
  else
29
29
  Person.make
30
30
  end
31
-
31
+
32
32
  table = exec "select #{query}", options
33
33
  table.cols.length.should == 1
34
-
34
+
35
35
  table.cols[0].id.should == id
36
36
  table.cols[0].type.should == type
37
37
  table.cols[0].label.should == label
38
-
38
+
39
39
  table.rows.length.should == 1
40
40
  table.rows[0].c.length.should == 1
41
-
41
+
42
42
  table.rows[0].c[0].v.should == value
43
43
  end
44
44
  end
45
45
 
46
46
  it "processes select *" do
47
47
  p = Person.make
48
-
48
+
49
49
  table = exec 'select *'
50
50
  table.cols.length.should == 7
51
-
51
+
52
52
  i = 0
53
53
  [['id', :number], ['name', :string], ['age', :number], ['birthday', :date],
54
54
  ['created_at', :datetime], ['updated_at', :datetime],
@@ -58,12 +58,12 @@ describe Executor do
58
58
  table.cols[i].label.should == id
59
59
  i += 1
60
60
  end
61
-
61
+
62
62
  table.rows.length.should == 1
63
63
  table.rows[0].c.length.should == 7
64
-
64
+
65
65
  i = 0
66
- [p.id, p.name, p.age, format_date(p.birthday),
66
+ [p.id, p.name, p.age, format_date(p.birthday),
67
67
  format_datetime(p.created_at), format_datetime(p.updated_at), p.city.id].each do |val|
68
68
  table.rows[0].c[i].v.should == val
69
69
  i += 1
@@ -73,16 +73,16 @@ describe Executor do
73
73
  it_processes_single_select_column 'name', 'name', :string, 'foo', 'name' do
74
74
  Person.make :name => 'foo'
75
75
  end
76
-
76
+
77
77
  it_processes_single_select_column '1', 'c0', :number, 1, '1'
78
78
  it_processes_single_select_column '1.2', 'c0', :number, 1.2, '1.2'
79
79
  it_processes_single_select_column '"hello"', 'c0', :string, 'hello', "'hello'"
80
80
  it_processes_single_select_column 'false', 'c0', :boolean, false, 'false'
81
81
  it_processes_single_select_column 'true', 'c0', :boolean, true, 'true'
82
- it_processes_single_select_column 'date "2010-01-02"', 'c0', :date, '2010-01-02', "date '2010-01-02'"
82
+ it_processes_single_select_column 'date "2010-01-02"', 'c0', :date, '2010-01-02', "date '2010-01-02'"
83
83
  it_processes_single_select_column 'datetime "2010-01-02 10:11:12"', 'c0', :datetime, '2010-01-02 10:11:12', "datetime '2010-01-02 10:11:12'"
84
84
  it_processes_single_select_column 'timeofday "10:11:12"', 'c0', :timeofday, '10:11:12', "timeofday '10:11:12'"
85
-
85
+
86
86
  it_processes_single_select_column '1 + 2', 'c0', :number, 3, '1 + 2'
87
87
  it_processes_single_select_column '3 - 2', 'c0', :number, 1, '3 - 2'
88
88
  it_processes_single_select_column '2 * 3', 'c0', :number, 6, '2 * 3'
@@ -90,71 +90,71 @@ describe Executor do
90
90
  it_processes_single_select_column '3 * age', 'c0', :number, 60, '3 * age' do
91
91
  Person.make :age => 20
92
92
  end
93
-
93
+
94
94
  it_processes_single_select_column 'sum(age)', 'c0', :number, 6, 'sum(age)' do
95
95
  [1, 2, 3].each{|i| Person.make :age => i}
96
96
  end
97
-
97
+
98
98
  it_processes_single_select_column 'avg(age)', 'c0', :number, 30, 'avg(age)' do
99
99
  [10, 20, 60].each{|i| Person.make :age => i}
100
100
  end
101
-
101
+
102
102
  it_processes_single_select_column 'count(age)', 'c0', :number, 3, 'count(age)' do
103
103
  3.times{|i| Person.make}
104
104
  end
105
-
105
+
106
106
  it_processes_single_select_column 'max(age)', 'c0', :number, 3, 'max(age)' do
107
107
  [1, 2, 3].each{|i| Person.make :age => i}
108
108
  end
109
-
109
+
110
110
  it_processes_single_select_column 'min(age)', 'c0', :number, 1, 'min(age)' do
111
111
  [1, 2, 3].each{|i| Person.make :age => i}
112
112
  end
113
-
113
+
114
114
  it_processes_single_select_column 'age where age > 2', 'age', :number, 3, 'age' do
115
115
  [1, 2, 3].each{|i| Person.make :age => i}
116
116
  end
117
-
117
+
118
118
  it_processes_single_select_column 'age where age > 2 and age <= 3', 'age', :number, 3, 'age' do
119
119
  [1, 2, 3, 4, 5].each{|i| Person.make :age => i}
120
120
  end
121
-
121
+
122
122
  it_processes_single_select_column 'name where age is null', 'name', :string, 'b', 'name' do
123
123
  Person.make :age => 1, :name => 'a'
124
124
  Person.make :age => nil, :name => 'b'
125
125
  end
126
-
126
+
127
127
  it_processes_single_select_column "age where city_name = 'Laos' and year(birthday) = '2010'", 'age', :number, 1, 'age' do
128
128
  Person.make :age => 1, :name => 'a', :city => City.make(:name => 'Laos'), :birthday => '2010-01-01'
129
129
  end
130
-
130
+
131
131
  it_processes_single_select_column 'name where age is not null', 'name', :string, 'a', 'name' do
132
132
  Person.make :age => 1, :name => 'a'
133
133
  Person.make :age => nil, :name => 'b'
134
134
  end
135
-
135
+
136
136
  it "processes group by" do
137
137
  Person.make :name => 'one', :age => 1
138
138
  Person.make :name => 'one', :age => 2
139
139
  Person.make :name => 'two', :age => 3
140
140
  Person.make :name => 'two', :age => 4
141
-
141
+
142
142
  table = exec 'select max(age) group by name order by name'
143
-
143
+
144
144
  table.rows.length.should == 2
145
145
  table.rows[0].c.length.should == 1
146
146
  table.rows[0].c[0].v.should == 2
147
147
  table.rows[1].c.length.should == 1
148
148
  table.rows[1].c[0].v.should == 4
149
149
  end
150
-
150
+
151
151
  it "processes order by" do
152
152
  Person.make :age => 1
153
153
  Person.make :age => 3
154
154
  Person.make :age => 2
155
-
155
+
156
156
  table = exec 'select age order by age desc'
157
-
157
+
158
158
  table.rows.length.should == 3
159
159
  table.rows[0].c.length.should == 1
160
160
  table.rows[0].c[0].v.should == 3
@@ -163,153 +163,153 @@ describe Executor do
163
163
  table.rows[2].c.length.should == 1
164
164
  table.rows[2].c[0].v.should == 1
165
165
  end
166
-
166
+
167
167
  it_processes_single_select_column 'age where age > 3 order by age limit 1', 'age', :number, 4, 'age' do
168
168
  [1, 2, 3, 4, 5].each{|i| Person.make :age => i}
169
169
  end
170
-
170
+
171
171
  it_processes_single_select_column 'age where age > 3 order by age limit 1 offset 1', 'age', :number, 5, 'age' do
172
172
  [1, 2, 3, 4, 5].each{|i| Person.make :age => i}
173
173
  end
174
-
174
+
175
175
  it_processes_single_select_column 'city_name', 'city_name', :string, 'Buenos Aires', 'city_name' do
176
- Person.make :city => City.make(:name => 'Buenos Aires')
176
+ Person.make :city => City.make(:name => 'Buenos Aires')
177
177
  end
178
-
178
+
179
179
  it_processes_single_select_column 'city_country_name', 'city_country_name', :string, 'Argentina', 'city_country_name' do
180
- Person.make :city => City.make(:country => Country.make(:name => 'Argentina'))
180
+ Person.make :city => City.make(:country => Country.make(:name => 'Argentina'))
181
181
  end
182
-
182
+
183
183
  it_processes_single_select_column 'city_country_name group by city_country_name', 'city_country_name', :string, 'Argentina', 'city_country_name' do
184
- Person.make :city => City.make(:country => Country.make(:name => 'Argentina'))
184
+ Person.make :city => City.make(:country => Country.make(:name => 'Argentina'))
185
185
  end
186
-
186
+
187
187
  it "processes with conditions as string" do
188
188
  Person.make :age => 1
189
189
  Person.make :age => 2
190
190
  Person.make :age => 3
191
-
191
+
192
192
  table = exec 'select age', :conditions => 'age = 2'
193
-
193
+
194
194
  table.rows.length.should == 1
195
195
  table.rows[0].c.length.should == 1
196
196
  table.rows[0].c[0].v.should == 2
197
197
  end
198
-
198
+
199
199
  it "processes with conditions as string and another filter" do
200
200
  Person.make :age => 1
201
201
  Person.make :age => 2
202
202
  Person.make :age => 3
203
-
203
+
204
204
  table = exec 'select age where age > 1', :conditions => 'age < 3'
205
-
205
+
206
206
  table.rows.length.should == 1
207
207
  table.rows[0].c.length.should == 1
208
208
  table.rows[0].c[0].v.should == 2
209
209
  end
210
-
210
+
211
211
  it "processes with conditions as array" do
212
212
  Person.make :age => 1
213
213
  Person.make :age => 2
214
214
  Person.make :age => 3
215
-
215
+
216
216
  table = exec 'select age', :conditions => ['age = ?', 2]
217
-
217
+
218
218
  table.rows.length.should == 1
219
219
  table.rows[0].c.length.should == 1
220
220
  table.rows[0].c[0].v.should == 2
221
221
  end
222
-
222
+
223
223
  it "processes with conditions as array and another filter" do
224
224
  Person.make :age => 1
225
225
  Person.make :age => 2
226
226
  Person.make :age => 3
227
-
227
+
228
228
  table = exec 'select age where age > 1', :conditions => ['age < ?', 3]
229
-
229
+
230
230
  table.rows.length.should == 1
231
231
  table.rows[0].c.length.should == 1
232
232
  table.rows[0].c[0].v.should == 2
233
233
  end
234
-
235
- [['year', 2006], ['month', 5], ['day', 2],
234
+
235
+ [['year', 2006], ['month', 5], ['day', 2],
236
236
  ['hour', 3], ['minute', 4], ['second', 9],
237
237
  ['dayOfWeek', 3]].each do |str, val|
238
238
  it_processes_single_select_column "#{str}(created_at)", 'c0', :number, val, "#{str}(created_at)" do
239
- Person.make :created_at => Time.parse('2006-05-02 3:04:09')
239
+ Person.make :created_at => Time.parse('2006-05-02 3:04:09')
240
240
  end
241
241
  end
242
-
242
+
243
243
  # it_processes_single_select_column "quarter(created_at)", 'c0', :number, 2, 'quarter(created_at)' do
244
- # Person.make :created_at => Time.parse('2006-05-02 3:04:09')
244
+ # Person.make :created_at => Time.parse('2006-05-02 3:04:09')
245
245
  # end
246
-
246
+
247
247
  it_processes_single_select_column "dateDiff(date '2008-03-13', date '2008-03-10')", 'c0', :number, 3, "dateDiff(date '2008-03-13', date '2008-03-10')"
248
-
248
+
249
249
  # it_processes_single_select_column "now()", 'c0', :datetime, Time.now.utc.strftime("%Y-%m-%d %H:%M:%S"), "now()" do
250
- # Person.make :created_at => Time.parse('2006-05-02 3:04:09')
250
+ # Person.make :created_at => Time.parse('2006-05-02 3:04:09')
251
251
  # end
252
252
 
253
253
  it_processes_single_select_column "toDate('2008-03-13')", 'c0', :date, Date.parse('2008-03-13').to_s, "toDate('2008-03-13')"
254
-
254
+
255
255
  it_processes_single_select_column "toDate(created_at)", 'c0', :date, Date.parse('2008-03-13').to_s, "toDate(created_at)" do
256
- Person.make :created_at => Time.parse('2008-03-13 3:04:09')
256
+ Person.make :created_at => Time.parse('2008-03-13 3:04:09')
257
257
  end
258
-
258
+
259
259
  # it_processes_single_select_column "toDate(1234567890000)", 'c0', :date, Date.parse('2009-02-13').to_s, "toDate(1234567890000)"
260
-
260
+
261
261
  it_processes_single_select_column "upper(name)", 'c0', :string, 'FOO', "upper(name)" do
262
262
  Person.make :name => 'foo'
263
263
  end
264
-
264
+
265
265
  it_processes_single_select_column "lower(name)", 'c0', :string, 'foo', "lower(name)" do
266
266
  Person.make :name => 'FOO'
267
267
  end
268
-
268
+
269
269
  it_processes_single_select_column "concat(age)", 'c0', :string, '20', "concat(age)", :extensions => true do
270
270
  Person.make :age => 20
271
271
  end
272
-
272
+
273
273
  it_processes_single_select_column "concat(name, 'bar')", 'c0', :string, 'foobar', "concat(name, 'bar')", :extensions => true do
274
274
  Person.make :name => 'foo'
275
275
  end
276
-
276
+
277
277
  it_processes_single_select_column "name label name 'my name'", 'name', :string, 'foo', "my name" do
278
278
  Person.make :name => 'foo'
279
279
  end
280
-
280
+
281
281
  it_processes_single_select_column "1 + 2 label 1 + 2 'my name'", 'c0', :number, 3, "my name"
282
-
282
+
283
283
  it_processes_single_select_column "sum(age) label sum(age) 'my name'", 'c0', :number, 2, "my name" do
284
284
  Person.make :age => 2
285
285
  end
286
-
286
+
287
287
  it_processes_single_select_column "1 options no_values", 'c0', :number, nil, "1"
288
-
288
+
289
289
  it "processes pivot" do
290
290
  Person.make :name => 'Eng', :birthday => '2000-01-12', :age => 1000
291
- Person.make :name => 'Eng', :birthday => '2000-01-12', :age => 500
291
+ Person.make :name => 'Eng', :birthday => '2000-01-12', :age => 500
292
292
  Person.make :name => 'Eng', :birthday => '2000-01-13', :age => 600
293
293
  Person.make :name => 'Sales', :birthday => '2000-01-12', :age => 400
294
294
  Person.make :name => 'Sales', :birthday => '2000-01-12', :age => 350
295
295
  Person.make :name => 'Marketing', :birthday => '2000-01-13', :age => 800
296
-
296
+
297
297
  table = exec 'select name, sum(age) group by name pivot birthday order by name'
298
-
298
+
299
299
  table.cols.length.should == 3
300
-
300
+
301
301
  i = 0
302
302
  [['c0', :string, 'name'],
303
- ['c1', :number, '2000-01-12 sum(age)'],
303
+ ['c1', :number, '2000-01-12 sum(age)'],
304
304
  ['c2', :number, '2000-01-13 sum(age)']].each do |id, type, label|
305
305
  table.cols[i].id.should == id
306
306
  table.cols[i].type.should == type
307
307
  table.cols[i].label.should == label
308
308
  i += 1
309
309
  end
310
-
310
+
311
311
  table.rows.length.should == 3
312
-
312
+
313
313
  i = 0
314
314
  [['Eng', 1500, 600],
315
315
  ['Marketing', nil, 800],
@@ -321,21 +321,21 @@ describe Executor do
321
321
  i += 1
322
322
  end
323
323
  end
324
-
324
+
325
325
  it "processes pivot2" do
326
326
  Person.make :name => 'Eng', :birthday => '2000-01-12', :age => 1000
327
- Person.make :name => 'Eng', :birthday => '2000-01-12', :age => 500
327
+ Person.make :name => 'Eng', :birthday => '2000-01-12', :age => 500
328
328
  Person.make :name => 'Eng', :birthday => '2000-01-13', :age => 600
329
329
  Person.make :name => 'Sales', :birthday => '2000-01-12', :age => 400
330
330
  Person.make :name => 'Sales', :birthday => '2000-01-12', :age => 350
331
331
  Person.make :name => 'Marketing', :birthday => '2000-01-13', :age => 800
332
-
332
+
333
333
  table = exec 'select sum(age), name group by name pivot birthday order by name'
334
-
334
+
335
335
  table.cols.length.should == 3
336
-
336
+
337
337
  i = 0
338
- [['c0', :number, '2000-01-12 sum(age)'],
338
+ [['c0', :number, '2000-01-12 sum(age)'],
339
339
  ['c1', :number, '2000-01-13 sum(age)'],
340
340
  ['c2', :string, 'name']].each do |id, type, label|
341
341
  table.cols[i].id.should == id
@@ -343,9 +343,9 @@ describe Executor do
343
343
  table.cols[i].label.should == label
344
344
  i += 1
345
345
  end
346
-
346
+
347
347
  table.rows.length.should == 3
348
-
348
+
349
349
  i = 0
350
350
  [[1500, 600, 'Eng'],
351
351
  [nil, 800, 'Marketing'],
@@ -357,15 +357,15 @@ describe Executor do
357
357
  i += 1
358
358
  end
359
359
  end
360
-
360
+
361
361
  it "processes pivot3" do
362
362
  Person.make :name => 'Eng', :birthday => '2000-01-12', :age => 10
363
363
  Person.make :name => 'Eng', :birthday => '2001-02-12', :age => 10
364
-
364
+
365
365
  table = exec 'select name, sum(age) group by name pivot year(birthday), month(birthday)'
366
-
366
+
367
367
  table.cols.length.should == 3
368
-
368
+
369
369
  i = 0
370
370
  [['Eng', 10, 10]].each do |values|
371
371
  table.rows[i].c.length.should == 3
@@ -375,15 +375,15 @@ describe Executor do
375
375
  i += 1
376
376
  end
377
377
  end
378
-
378
+
379
379
  it "processes pivot4" do
380
380
  Person.make :name => 'Eng', :birthday => '2000-01-12', :age => 10
381
381
  Person.make :name => 'Sales', :birthday => '2001-02-12', :age => 20
382
-
382
+
383
383
  table = exec 'select birthday, month(birthday), sum(age) group by month(birthday) pivot name order by name'
384
-
384
+
385
385
  table.cols.length.should == 5
386
-
386
+
387
387
  i = 0
388
388
  [
389
389
  ['2000-01-12', nil, 1, 10, nil],
@@ -396,17 +396,51 @@ describe Executor do
396
396
  i += 1
397
397
  end
398
398
  end
399
-
399
+
400
+ it "processes pivot without group by" do
401
+ Person.make :name => 'Eng', :birthday => '2000-01-12', :age => 1000
402
+ Person.make :name => 'Eng', :birthday => '2000-01-12', :age => 500
403
+ Person.make :name => 'Eng', :birthday => '2000-01-13', :age => 600
404
+ Person.make :name => 'Sales', :birthday => '2000-01-12', :age => 400
405
+ Person.make :name => 'Sales', :birthday => '2000-01-12', :age => 350
406
+ Person.make :name => 'Marketing', :birthday => '2000-01-13', :age => 800
407
+
408
+ table = exec 'select sum(age) pivot name order by name'
409
+
410
+ table.cols.length.should == 3
411
+
412
+ i = 0
413
+ [['c0', :number, 'Eng sum(age)'],
414
+ ['c1', :number, 'Marketing sum(age)'],
415
+ ['c2', :number, 'Sales sum(age)']].each do |id, type, label|
416
+ table.cols[i].id.should == id
417
+ table.cols[i].type.should == type
418
+ table.cols[i].label.should == label
419
+ i += 1
420
+ end
421
+
422
+ table.rows.length.should == 1
423
+
424
+ i = 0
425
+ [[2100, 800, 750]].each do |values|
426
+ table.rows[i].c.length.should == 3
427
+ values.each_with_index do |v, j|
428
+ table.rows[i].c[j].v.should == v
429
+ end
430
+ i += 1
431
+ end
432
+ end
433
+
400
434
  it "processes pivot with no results" do
401
435
  Person.make :name => 'Eng', :birthday => '2000-01-12', :age => 10
402
436
  Person.make :name => 'Sales', :birthday => '2001-02-12', :age => 20
403
-
437
+
404
438
  table = exec 'select birthday, sum(age) where 1 = 2 group by month(birthday) pivot name order by name'
405
-
439
+
406
440
  table.cols.length.should == 2
407
-
441
+
408
442
  i = 0
409
- [['birthday', :date, 'birthday'],
443
+ [['birthday', :date, 'birthday'],
410
444
  ['c1', :number, 'sum(age)']].each do |id, type, label|
411
445
  table.cols[i].id.should == id
412
446
  table.cols[i].type.should == type
@@ -414,15 +448,15 @@ describe Executor do
414
448
  i += 1
415
449
  end
416
450
  end
417
-
451
+
418
452
  it "processes pivot with group by not in select" do
419
453
  Person.make :name => 'Eng', :birthday => '2000-01-12', :age => 10
420
454
  Person.make :name => 'Sales', :birthday => '2001-02-12', :age => 20
421
-
455
+
422
456
  table = exec 'select birthday, sum(age) group by month(birthday) pivot name order by name'
423
-
457
+
424
458
  table.cols.length.should == 4
425
-
459
+
426
460
  i = 0
427
461
  [
428
462
  ['2000-01-12', nil, 10, nil],
@@ -435,5 +469,5 @@ describe Executor do
435
469
  i += 1
436
470
  end
437
471
  end
438
-
472
+
439
473
  end
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  $:.unshift(File.dirname(__FILE__) + '/../lib')
2
2
 
3
3
  require 'rubygems'
4
- require 'spec'
5
4
  require 'logger'
6
5
 
7
6
  require 'active_record'
@@ -40,10 +39,11 @@ require File.dirname(__FILE__) + '/models/country'
40
39
 
41
40
  require File.dirname(__FILE__) + '/blueprints'
42
41
 
43
- require 'rgviz'
44
- require 'rgviz_rails'
42
+ require File.dirname(__FILE__) + '/../../rgviz/lib/rgviz'
43
+ #require 'rgviz'
44
+ require 'rgviz_rails/executor'
45
45
 
46
46
  RAILS_ENV = 'test'
47
47
 
48
48
  # Add this directory so the ActiveSupport autoloading works
49
- ActiveSupport::Dependencies.load_paths << File.dirname(__FILE__)
49
+ #ActiveSupport::Dependencies.load_paths << File.dirname(__FILE__)
metadata CHANGED
@@ -1,27 +1,36 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rgviz-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.46"
4
+ hash: 85
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 47
9
+ version: "0.47"
5
10
  platform: ruby
6
11
  authors:
7
- - Ary Borenszweig
12
+ - Ary Borenszweig
8
13
  autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-12-30 00:00:00 -02:00
17
+ date: 2011-07-07 00:00:00 -03:00
13
18
  default_executable:
14
19
  dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: rgviz
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: "0"
24
- version:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rgviz
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
25
34
  description:
26
35
  email: aborenszweig@manas.com.ar
27
36
  executables: []
@@ -29,26 +38,26 @@ executables: []
29
38
  extensions: []
30
39
 
31
40
  extra_rdoc_files:
32
- - README.rdoc
41
+ - README.rdoc
33
42
  files:
34
- - lib/rgviz_rails.rb
35
- - lib/rgviz_rails/executor.rb
36
- - lib/rgviz_rails/js_renderer.rb
37
- - lib/rgviz_rails/tqx.rb
38
- - lib/rgviz_rails/view_helper.rb
39
- - lib/rgviz_rails/adapters/mysql_adapter.rb
40
- - lib/rgviz_rails/adapters/postgresql_adapter.rb
41
- - lib/rgviz_rails/adapters/sqlite_adapter.rb
42
- - lib/rgviz_rails/init.rb
43
- - rails/init.rb
44
- - spec/blueprints.rb
45
- - spec/spec.opts
46
- - spec/spec_helper.rb
47
- - spec/models/city.rb
48
- - spec/models/country.rb
49
- - spec/models/person.rb
50
- - spec/rgviz/executor_spec.rb
51
- - README.rdoc
43
+ - lib/rgviz_rails.rb
44
+ - lib/rgviz_rails/executor.rb
45
+ - lib/rgviz_rails/js_renderer.rb
46
+ - lib/rgviz_rails/tqx.rb
47
+ - lib/rgviz_rails/view_helper.rb
48
+ - lib/rgviz_rails/adapters/mysql_adapter.rb
49
+ - lib/rgviz_rails/adapters/postgresql_adapter.rb
50
+ - lib/rgviz_rails/adapters/sqlite_adapter.rb
51
+ - lib/rgviz_rails/init.rb
52
+ - rails/init.rb
53
+ - spec/blueprints.rb
54
+ - spec/spec.opts
55
+ - spec/spec_helper.rb
56
+ - spec/models/city.rb
57
+ - spec/models/country.rb
58
+ - spec/models/person.rb
59
+ - spec/rgviz/executor_spec.rb
60
+ - README.rdoc
52
61
  has_rdoc: true
53
62
  homepage: http://code.google.com/p/rgviz-rails
54
63
  licenses: []
@@ -57,23 +66,29 @@ post_install_message:
57
66
  rdoc_options: []
58
67
 
59
68
  require_paths:
60
- - lib
69
+ - lib
61
70
  required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
62
72
  requirements:
63
- - - ">="
64
- - !ruby/object:Gem::Version
65
- version: "0"
66
- version:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
67
79
  required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
68
81
  requirements:
69
- - - ">="
70
- - !ruby/object:Gem::Version
71
- version: "0"
72
- version:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
73
88
  requirements: []
74
89
 
75
90
  rubyforge_project:
76
- rubygems_version: 1.3.5
91
+ rubygems_version: 1.4.2
77
92
  signing_key:
78
93
  specification_version: 3
79
94
  summary: rgviz for rails