Class: PgExecArrayParams::Rewriters::ResTarget

Inherits:
Node
  • Object
show all
Defined in:
lib/pg_exec_array_params/rewriters/res_target.rb

Constant Summary collapse

VAL =
'val'

Instance Attribute Summary

Attributes inherited from Node

#ref_idx, #value

Instance Method Summary collapse

Methods inherited from Node

#initialize, #process, #to_s

Constructor Details

This class inherits a constructor from PgExecArrayParams::Rewriters::Node

Instance Method Details

#plain_selection?Boolean

handle “select $1” “location”=>7}, “location”=>7} AExpr handles “select $1 + 1”

Returns:

  • (Boolean)


33
34
35
# File 'lib/pg_exec_array_params/rewriters/res_target.rb', line 33

def plain_selection?
  value.key?(VAL) && value[VAL].is_a?(Hash) && value[VAL].key?(PARAM_REF)
end

#refs_atObject



37
38
39
# File 'lib/pg_exec_array_params/rewriters/res_target.rb', line 37

def refs_at
  [value[VAL][PARAM_REF][LOCATION] + 1, value[VAL][PARAM_REF][NUMBER].to_s.size]
end

#rewrite!Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pg_exec_array_params/rewriters/res_target.rb', line 12

def rewrite!
  # puts({value_before: value}.inspect)
  old_ref_idx = value[VAL][PARAM_REF][NUMBER] - 1 # one based
  unless (new_ref_idx = ref_idx[old_ref_idx])
    raise Error.new("No parameter for $#{old_ref_idx + 1}", nil, self)
  end

  if new_ref_idx.is_a?(Array)
    elements = Range.new(*new_ref_idx).map do |param_ref_idx|
      { PARAM_REF => { NUMBER => param_ref_idx } }
    end
    value[VAL] = { 'A_ArrayExpr' => { 'elements' => elements } }
  else
    value[VAL][PARAM_REF][NUMBER] = new_ref_idx
  end
  # puts({value_after_: value, 'ref_idx' => ref_idx}.inspect)
end

#should_rewrite?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/pg_exec_array_params/rewriters/res_target.rb', line 8

def should_rewrite?
  plain_selection?
end