Class: PgQuery::SummaryParserResult

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_query/summary.rb

Overview

Result of PgQuery.summary.

Where possible this is API compatible with ParserResult, with these caveats:

  • tables_with_details omits the location, inh and relpersistence values that ParserResult reports, since the summary does not carry them
  • filter_columns only covers the WHERE clause of SELECT statements, so unlike ParserResult#filter_columns it does not include JOIN conditions, or the WHERE clause of UPDATE/DELETE statements
  • functions_with_details additionally returns the schema name and the bare function name, which ParserResult does not provide
  • statement_types has no ParserResult equivalent, returns the statement types of the query, e.g. ["SelectStmt"]
  • truncated_query is offered instead of ParserResult#truncate(max_length), since truncation has already happened when the result is returned - it returns nil if no truncate_limit was passed to PgQuery.summary

The underlying protobuf message is available through #protobuf.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, protobuf, warnings = [], truncate_limit = -1)) ⇒ SummaryParserResult

Returns a new instance of SummaryParserResult.



51
52
53
54
55
56
# File 'lib/pg_query/summary.rb', line 51

def initialize(query, protobuf, warnings = [], truncate_limit = -1)
  @query = query
  @protobuf = protobuf
  @warnings = warnings
  @truncate_limit = truncate_limit
end

Instance Attribute Details

#protobuf ⇒ Object (readonly)

Returns the value of attribute protobuf.



49
50
51
# File 'lib/pg_query/summary.rb', line 49

def protobuf
  @protobuf
end

#query ⇒ Object (readonly)

Returns the value of attribute query.



49
50
51
# File 'lib/pg_query/summary.rb', line 49

def query
  @query
end

#warnings ⇒ Object (readonly)

Returns the value of attribute warnings.



49
50
51
# File 'lib/pg_query/summary.rb', line 49

def warnings
  @warnings
end

Instance Method Details

#aliases ⇒ Object



90
91
92
# File 'lib/pg_query/summary.rb', line 90

def aliases
  @aliases ||= @protobuf.aliases.to_h
end

#call_functions ⇒ Object



82
83
84
# File 'lib/pg_query/summary.rb', line 82

def call_functions
  functions_with_details.select { |f| f[:type] == :call }.map { |f| f[:function] }.uniq
end

#cte_names ⇒ Object



86
87
88
# File 'lib/pg_query/summary.rb', line 86

def cte_names
  @cte_names ||= @protobuf.cte_names.to_a.uniq
end

#ddl_functions ⇒ Object



78
79
80
# File 'lib/pg_query/summary.rb', line 78

def ddl_functions
  functions_with_details.select { |f| f[:type] == :ddl }.map { |f| f[:function] }.uniq
end

#ddl_tables ⇒ Object



70
71
72
# File 'lib/pg_query/summary.rb', line 70

def ddl_tables
  tables_with_details.select { |t| t[:type] == :ddl }.map { |t| t[:name] }.uniq
end

#dml_tables ⇒ Object



66
67
68
# File 'lib/pg_query/summary.rb', line 66

def dml_tables
  tables_with_details.select { |t| t[:type] == :dml }.map { |t| t[:name] }.uniq
end

#filter_columns ⇒ Object



116
117
118
119
120
121
122
# File 'lib/pg_query/summary.rb', line 116

def filter_columns
  @filter_columns ||= @protobuf.filter_columns.map do |filter_column|
    table = [filter_column.schema_name, filter_column.table_name].reject(&:empty?).join('.')
    table = nil if table.empty?
    [aliases[table] || table, filter_column.column]
  end.uniq
end

#functions ⇒ Object



74
75
76
# File 'lib/pg_query/summary.rb', line 74

def functions
  functions_with_details.map { |f| f[:function] }.uniq
end

#functions_with_details ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/pg_query/summary.rb', line 105

def functions_with_details
  @functions_with_details ||= @protobuf.functions.map do |function|
    {
      function: function.name,
      type: context_to_type(function.context),
      schemaname: (function.schema_name unless function.schema_name.empty?),
      funcname: function.function_name
    }
  end.uniq
end

#select_tables ⇒ Object



62
63
64
# File 'lib/pg_query/summary.rb', line 62

def select_tables
  tables_with_details.select { |t| t[:type] == :select }.map { |t| t[:name] }.uniq
end

#statement_types ⇒ Object



124
125
126
# File 'lib/pg_query/summary.rb', line 124

def statement_types
  @statement_types ||= @protobuf.statement_types.to_a
end

#tables ⇒ Object



58
59
60
# File 'lib/pg_query/summary.rb', line 58

def tables
  tables_with_details.map { |t| t[:name] }.uniq
end

#tables_with_details ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/pg_query/summary.rb', line 94

def tables_with_details
  @tables_with_details ||= @protobuf.tables.map do |table|
    {
      name: table.name,
      type: context_to_type(table.context),
      schemaname: (table.schema_name unless table.schema_name.empty?),
      relname: table.table_name
    }
  end.uniq
end

#truncated_query ⇒ Object



128
129
130
# File 'lib/pg_query/summary.rb', line 128

def truncated_query
  @protobuf.truncated_query unless @truncate_limit == -1
end