Class: PgCanary::Rules::Scope
- Inherits:
-
Object
- Object
- PgCanary::Rules::Scope
- Defined in:
- lib/pg_canary/rules/scope.rb
Overview
One SELECT scope (top-level statement, CTE, subquery, ...) with its own FROM-clause alias resolution.
Instance Attribute Summary collapse
-
#aliases ⇒ Object
readonly
Returns the value of attribute aliases.
-
#stmt ⇒ Object
readonly
Returns the value of attribute stmt.
Instance Method Summary collapse
-
#initialize(stmt) ⇒ Scope
constructor
A new instance of Scope.
- #limited? ⇒ Boolean
-
#resolve(column_ref) ⇒ Object
Resolves a ColumnRef to [table, column].
-
#sort_items ⇒ Object
=> [PgQuery::SortBy].
-
#tables ⇒ Object
Real table names referenced directly in this scope's FROM clause.
- #where_clause ⇒ Object
Constructor Details
#initialize(stmt) ⇒ Scope
Returns a new instance of Scope.
12 13 14 15 16 |
# File 'lib/pg_canary/rules/scope.rb', line 12 def initialize(stmt) @stmt = stmt @aliases = {} collect_aliases(stmt.from_clause) end |
Instance Attribute Details
#aliases ⇒ Object (readonly)
Returns the value of attribute aliases.
10 11 12 |
# File 'lib/pg_canary/rules/scope.rb', line 10 def aliases @aliases end |
#stmt ⇒ Object (readonly)
Returns the value of attribute stmt.
10 11 12 |
# File 'lib/pg_canary/rules/scope.rb', line 10 def stmt @stmt end |
Instance Method Details
#limited? ⇒ Boolean
27 28 29 |
# File 'lib/pg_canary/rules/scope.rb', line 27 def limited? !stmt.limit_count.nil? end |
#resolve(column_ref) ⇒ Object
Resolves a ColumnRef to [table, column]. Returns nil when the ref cannot be attributed to a real table unambiguously (unknown alias, subquery output, multiple candidate tables for an unqualified ref).
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/pg_canary/rules/scope.rb', line 39 def resolve(column_ref) fields = column_ref.field_names return nil if fields.nil? || fields.empty? column = fields.last if fields.length >= 2 table = @aliases[fields[-2]] table ? [table, column] : nil else tables.length == 1 ? [tables.first, column] : nil end end |
#sort_items ⇒ Object
=> [PgQuery::SortBy]
23 24 25 |
# File 'lib/pg_canary/rules/scope.rb', line 23 def sort_items stmt.sort_clause.map(&:unwrap).grep(PgQuery::SortBy) end |
#tables ⇒ Object
Real table names referenced directly in this scope's FROM clause.
32 33 34 |
# File 'lib/pg_canary/rules/scope.rb', line 32 def tables @aliases.values.compact.uniq end |
#where_clause ⇒ Object
18 19 20 |
# File 'lib/pg_canary/rules/scope.rb', line 18 def where_clause stmt.where_clause end |