Module: ActiveRecordExtended::QueryMethods::WithCTE

Defined in:
lib/active_record_extended/query_methods/with_cte.rb

Defined Under Namespace

Classes: WithCTE, WithChain

Instance Method Summary collapse

Instance Method Details

#build_with(arel) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 178

def build_with(arel)
  return unless with_values?

  cte_statements = cte.map do |name, expression|
    grouped_expression = cte.generate_grouping(expression)
    cte_name           = cte.to_arel_sql(cte.double_quote(name.to_s))
    grouped_expression = add_materialized_modifier(grouped_expression, cte, name)

    Arel::Nodes::As.new(cte_name, grouped_expression)
  end

  if recursive_value?
    arel.with(:recursive, cte_statements)
  else
    arel.with(cte_statements)
  end
end

#cteWithCTE

Returns:



123
124
125
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 123

def cte
  @values[:cte]
end

#cte=(cte) ⇒ Object

Parameters:

Raises:

  • (TypeError)


128
129
130
131
132
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 128

def cte=(cte)
  raise TypeError.new("Must be a WithCTE class type") unless cte.is_a?(WithCTE)

  @values[:cte] = cte
end

#recursive_value=(value) ⇒ Object

Parameters:

  • value (Boolean)

Raises:

  • (ImmutableRelation)


145
146
147
148
149
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 145

def recursive_value=(value)
  raise ImmutableRelation if @loaded

  @values[:recursive] = value
end

#recursive_value?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 152

def recursive_value?
  !(!@values[:recursive])
end

#with(opts = :chain, *rest) ⇒ Object

Parameters:

  • opts (Hash, WithCTE) (defaults to: :chain)


157
158
159
160
161
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 157

def with(opts = :chain, *rest)
  return WithChain.new(spawn) if opts == :chain

  opts.blank? ? self : spawn.with!(opts, *rest)
end

#with!(opts = :chain, *rest) ⇒ Object

Parameters:

  • opts (Hash, WithCTE) (defaults to: :chain)


164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 164

def with!(opts = :chain, *rest)
  case opts
  when :chain
    WithChain.new(self)
  when :recursive
    WithChain.new(self).recursive(*rest)
  else
    tap do |scope|
      scope.cte ||= WithCTE.new(self)
      scope.cte.pipe_cte_with!(opts)
    end
  end
end

#with_values=(values) ⇒ Object

Parameters:



140
141
142
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 140

def with_values=(values)
  cte.with_values = values
end

#with_values?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 135

def with_values?
  !(cte.nil? || cte.empty?)
end