Class: Torque::PostgreSQL::Arel::Nodes::FromItem

Inherits:
Arel::Nodes::Binary
  • Object
show all
Defined in:
lib/torque/postgresql/arel/nodes.rb,
sig/torque/postgresql/arel/nodes.rbs

Overview

An aliased FROM item. definitions maps a column name to the SQL type a function has to be told to return, columns are the plain names the alias exposes, ordinality names the appended counter, and lateral is only needed when the source is a subquery

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, name, columns: nil, definitions: nil, ordinality: nil, lateral: false) ⇒ FromItem

Returns a new instance of FromItem.

Parameters:

  • source (arel_node)
  • name (column_ref)
  • columns: (Array[column_ref], column_ref, nil) (defaults to: nil)
  • definitions: (Hash[column_ref, String?], nil) (defaults to: nil)
  • ordinality: (column_ref, nil) (defaults to: nil)
  • lateral: (Boolean) (defaults to: false)


101
102
103
104
105
106
107
108
# File 'lib/torque/postgresql/arel/nodes.rb', line 101

def initialize(source, name, columns: nil, definitions: nil, ordinality: nil, lateral: false)
  @columns = Array.wrap(columns).presence&.freeze
  @definitions = definitions.presence&.freeze
  @ordinality = ordinality&.to_s
  @lateral = lateral

  super(source, name.to_s)
end

Instance Attribute Details

#columns ⇒ Array[column_ref]? (readonly)

Returns the value of attribute columns.

Returns:

  • (Array[column_ref], nil)


97
98
99
# File 'lib/torque/postgresql/arel/nodes.rb', line 97

def columns
  @columns
end

#definitions ⇒ Hash[column_ref, String?]? (readonly)

Returns the value of attribute definitions.

Returns:

  • (Hash[column_ref, String?], nil)


97
98
99
# File 'lib/torque/postgresql/arel/nodes.rb', line 97

def definitions
  @definitions
end

#lateral ⇒ Boolean (readonly)

Returns the value of attribute lateral.

Returns:

  • (Boolean)


97
98
99
# File 'lib/torque/postgresql/arel/nodes.rb', line 97

def lateral
  @lateral
end

#ordinality ⇒ String? (readonly)

Returns the value of attribute ordinality.

Returns:

  • (String, nil)


97
98
99
# File 'lib/torque/postgresql/arel/nodes.rb', line 97

def ordinality
  @ordinality
end

Instance Method Details

#alias_columns ⇒ Hash[column_ref, String?]

What the alias exposes, as name => type, where a nil type means the name stands on its own. Types are only spelled out here when they have not been moved into the ROWS FROM source.

The list is all or nothing, because PostgreSQL matches it positionally: a partial list silently renames the columns it does reach instead of naming the ones it does not

Returns:

  • (Hash[column_ref, String?])


124
125
126
127
128
129
130
131
132
# File 'lib/torque/postgresql/arel/nodes.rb', line 124

def alias_columns
  return {} if columns.nil? && definitions.nil?

  plain = rows_from? ? definitions.keys : columns
  result = plain.nil? ? definitions : plain.to_h { |name| [name.to_s, nil] }

  return result if ordinality.blank?
  result.merge(ordinality => nil)
end

#rows_from? ⇒ Boolean

The definitions can only be moved into the source when the counter forces the ROWS FROM form, since PostgreSQL refuses a column definition list next to WITH ORDINALITY

Returns:

  • (Boolean)


113
114
115
# File 'lib/torque/postgresql/arel/nodes.rb', line 113

def rows_from?
  ordinality.present? && definitions.present?
end