Class: Rgviz::ColumnVisitor

Inherits:
Visitor
  • Object
show all
Defined in:
lib/rgviz_rails/executor.rb

Direct Known Subclasses

OrderVisitor, WhereVisitor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(executor) ⇒ ColumnVisitor

Returns a new instance of ColumnVisitor.



431
432
433
434
# File 'lib/rgviz_rails/executor.rb', line 431

def initialize(executor)
  @string = ''
  @executor = executor
end

Instance Attribute Details

#stringObject (readonly)

Returns the value of attribute string.



429
430
431
# File 'lib/rgviz_rails/executor.rb', line 429

def string
  @string
end

Instance Method Details

#<<(string) ⇒ Object



436
437
438
# File 'lib/rgviz_rails/executor.rb', line 436

def <<(string)
  @string += string
end

#escaped_string(str) ⇒ Object



542
543
544
545
# File 'lib/rgviz_rails/executor.rb', line 542

def escaped_string(str)
  str = str.gsub("'", "''")
  "'#{str}'"
end

#visit_aggregate_column(node) ⇒ Object



506
507
508
509
510
511
512
# File 'lib/rgviz_rails/executor.rb', line 506

def visit_aggregate_column(node)
  @string += node.function.to_s
  @string += '('
  node.argument.accept self
  @string += ')'
  false
end

#visit_boolean_column(node) ⇒ Object



467
468
469
# File 'lib/rgviz_rails/executor.rb', line 467

def visit_boolean_column(node)
  @string += node.value ? '1' : '0'
end

#visit_date_column(node) ⇒ Object



471
472
473
# File 'lib/rgviz_rails/executor.rb', line 471

def visit_date_column(node)
  @executor.adapter.visit_date_column node, self
end

#visit_date_time_column(node) ⇒ Object



475
476
477
# File 'lib/rgviz_rails/executor.rb', line 475

def visit_date_time_column(node)
  @executor.adapter.visit_date_time_column node, self
end

#visit_format(node) ⇒ Object



534
535
536
# File 'lib/rgviz_rails/executor.rb', line 534

def visit_format(node)
  false
end

#visit_group_by(node) ⇒ Object



514
515
516
517
518
519
520
# File 'lib/rgviz_rails/executor.rb', line 514

def visit_group_by(node)
  node.columns.each_with_index do |c, i|
    @string += ',' if i > 0
    c.accept self
  end
  false
end

#visit_id_column(node) ⇒ Object



440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/rgviz_rails/executor.rb', line 440

def visit_id_column(node)
  if @executor.virtual_columns && (vc = @executor.virtual_columns[node.name])
    if vc[:gql]
      vc[:gql].accept self
    else
      @string += vc[:sql]
    end
    return
  end

  klass, rails_col, joins = Rgviz::find_rails_col @executor.model_class, node.name
  raise "Unknown column '#{node.name}'" unless rails_col
  @string += ActiveRecord::Base.connection.quote_column_name(klass.table_name)
  @string += '.'
  @string += ActiveRecord::Base.connection.quote_column_name(rails_col.name)

  @executor.add_joins joins
end

#visit_label(node) ⇒ Object



530
531
532
# File 'lib/rgviz_rails/executor.rb', line 530

def visit_label(node)
  false
end

#visit_number_column(node) ⇒ Object



459
460
461
# File 'lib/rgviz_rails/executor.rb', line 459

def visit_number_column(node)
  @string += node.value.to_s
end

#visit_option(node) ⇒ Object



538
539
540
# File 'lib/rgviz_rails/executor.rb', line 538

def visit_option(node)
  false
end

#visit_pivot(node) ⇒ Object



522
523
524
525
526
527
528
# File 'lib/rgviz_rails/executor.rb', line 522

def visit_pivot(node)
  node.columns.each_with_index do |c, i|
    @string += ',' if i > 0
    c.accept self
  end
  false
end

#visit_scalar_function_column(node) ⇒ Object



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/rgviz_rails/executor.rb', line 483

def visit_scalar_function_column(node)
  case node.function
  when ScalarFunctionColumn::Sum, ScalarFunctionColumn::Difference,
       ScalarFunctionColumn::Product, ScalarFunctionColumn::Quotient
    @string += "("
    node.arguments[0].accept self
    @string += node.function.to_s
    node.arguments[1].accept self
    @string += ")"
  when ScalarFunctionColumn::Round
    @string += "round("
    node.arguments[0].accept self
    @string += ")"
  when ScalarFunctionColumn::Floor
    @string += "round("
    node.arguments[0].accept self
    @string += ")"
  else
    @executor.adapter.accept_scalar_function_column(node, self)
  end
  false
end

#visit_string_column(node) ⇒ Object



463
464
465
# File 'lib/rgviz_rails/executor.rb', line 463

def visit_string_column(node)
  @string += escaped_string(node.value)
end

#visit_time_of_day_column(node) ⇒ Object



479
480
481
# File 'lib/rgviz_rails/executor.rb', line 479

def visit_time_of_day_column(node)
  @executor.adapter.visit_time_of_day_column node, self
end