Class: Bmg::Sql::Processor::Transform

Inherits:
Processor
  • Object
show all
Defined in:
lib/bmg/sql/processor/transform.rb

Defined Under Namespace

Modules: SplitSupported

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transformation, options, builder) ⇒ Transform

module SplitSupported

Raises:



42
43
44
45
46
# File 'lib/bmg/sql/processor/transform.rb', line 42

def initialize(transformation, options, builder)
  raise NotSupportedError unless options.empty?
  super(builder)
  @transformation = transformation
end

Instance Attribute Details

#transformationObject (readonly)

Returns the value of attribute transformation.



47
48
49
# File 'lib/bmg/sql/processor/transform.rb', line 47

def transformation
  @transformation
end

Class Method Details

.split_supported(*args, &bl) ⇒ Object



49
50
51
# File 'lib/bmg/sql/processor/transform.rb', line 49

def self.split_supported(*args, &bl)
  SplitSupported.split_supported(*args, &bl)
end

.sql_expr?(x) ⇒ Boolean

An SQL-able element the transform can emit verbatim: an object that carries its own SQL (the predicate gem's :sql/:sql_literal convention) or a Sequel expression (rendered through Sequel.expr).

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/bmg/sql/processor/transform.rb', line 56

def self.sql_expr?(x)
  x.respond_to?(:sql_literal) || x.respond_to?(:sql) ||
    (defined?(::Sequel::SQL::Expression) && x.is_a?(::Sequel::SQL::Expression))
end

Instance Method Details

#on_select_item(sexpr) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/bmg/sql/processor/transform.rb', line 67

def on_select_item(sexpr)
  as = sexpr.as_name.to_sym
  case t = transformation_for(as)
  when NilClass
    sexpr
  when Class, Array
    sexpr([:select_item,
      func_call_node(sexpr, Array(t).reverse),
      sexpr[2]
    ])
  when Hash
    sexpr([:select_item,
      case_when_node(sexpr, t),
      sexpr[2]
    ])
  else
    # An SQL-able value (e.g. a Sequel expression) carries its own SQL
    # — including the column reference — so it rides through as a
    # literal, which the Sequel translator emits verbatim.
    raise NotSupportedError unless Transform.sql_expr?(t)
    sexpr([:select_item, [:literal, t], sexpr[2]])
  end
end

#on_select_list(sexpr) ⇒ Object



61
62
63
64
65
# File 'lib/bmg/sql/processor/transform.rb', line 61

def on_select_list(sexpr)
  sexpr.each_with_index.map{|child,index|
    index == 0 ? child : apply(child)
  }
end