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



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 131

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))
    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:



81
82
83
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 81

def cte
  @values[:cte]
end

#cte=(cte) ⇒ Object

Parameters:

Raises:

  • (TypeError)


86
87
88
89
90
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 86

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)


103
104
105
106
107
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 103

def recursive_value=(value)
  raise ImmutableRelation if @loaded

  @values[:recursive] = value
end

#recursive_value?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 110

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

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

Parameters:

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


115
116
117
118
119
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 115

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

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

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

Parameters:

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


122
123
124
125
126
127
128
129
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 122

def with!(opts = :chain, *_rest)
  return WithChain.new(self) if :chain == opts

  tap do |scope|
    scope.cte ||= WithCTE.new(self)
    scope.cte.pipe_cte_with!(opts)
  end
end

#with_values=(values) ⇒ Object

Parameters:



98
99
100
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 98

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

#with_values?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/active_record_extended/query_methods/with_cte.rb', line 93

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