Module: PgCanary::PgQueryRefinement::Impl

Defined in:
lib/pg_canary/pg_query_refinement.rb

Class Method Summary collapse

Class Method Details

.each_child(node, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pg_canary/pg_query_refinement.rb', line 34

def each_child(node, &block)
  node.class.descriptor.each do |field|
    next unless field.type == :message

    value = node[field.name]
    next if value.nil?

    if value.is_a?(Google::Protobuf::RepeatedField)
      value.each(&block)
    else
      yield value
    end
  end
end

.string_values(nodes) ⇒ Object



61
62
63
# File 'lib/pg_canary/pg_query_refinement.rb', line 61

def string_values(nodes)
  nodes.map { |n| unwrap(n) }.grep(PgQuery::String).map(&:sval)
end

.strip_casts(node) ⇒ Object



17
18
19
20
21
# File 'lib/pg_canary/pg_query_refinement.rb', line 17

def strip_casts(node)
  node = unwrap(node)
  node = unwrap(node.arg) while node.is_a?(PgQuery::TypeCast)
  node
end

.unwrap(node) ⇒ Object



10
11
12
13
14
15
# File 'lib/pg_canary/pg_query_refinement.rb', line 10

def unwrap(node)
  return nil if node.nil?
  return node unless node.is_a?(PgQuery::Node)

  node.node ? node.inner : nil
end

.walk(node, prune: nil) {|node| ... } ⇒ Object

Yields:

  • (node)


23
24
25
26
27
28
29
30
31
32
# File 'lib/pg_canary/pg_query_refinement.rb', line 23

def walk(node, prune: nil, &block)
  node = unwrap(node)
  return if node.nil?
  return unless node.is_a?(Google::Protobuf::MessageExts)

  yield node
  return if prune&.call(node)

  each_child(node) { |child| walk(child, prune: prune, &block) }
end

.walk_scope(node) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pg_canary/pg_query_refinement.rb', line 49

def walk_scope(node, &)
  root_seen = false
  walk(node, prune: lambda { |msg|
    if root_seen
      msg.is_a?(PgQuery::SelectStmt)
    else
      root_seen = true
      false
    end
  }, &)
end