-
Notifications
You must be signed in to change notification settings - Fork 40
/
with_cte.rb
218 lines (177 loc) · 6.03 KB
/
with_cte.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# frozen_string_literal: true
module ActiveRecordExtended
module QueryMethods
module WithCTE
class WithCTE
include ActiveRecordExtended::Utilities::Support
include Enumerable
extend Forwardable
def_delegators :@with_values, :empty?, :blank?, :present?
attr_reader :with_values, :with_keys, :materialized_keys, :not_materialized_keys
# @param [ActiveRecord::Relation] scope
def initialize(scope)
@scope = scope
reset!
end
# @return [Enumerable] Returns the order for which CTE's were imported as.
def each
return to_enum(:each) unless block_given?
with_keys.each do |key|
yield(key, with_values[key])
end
end
alias each_pair each
# @param [Hash, WithCTE] value
def with_values=(value)
reset!
pipe_cte_with!(value)
end
# @return [Boolean]
def materialized_key?(key)
materialized_keys.include?(key.to_sym)
end
# @return [Boolean]
def not_materialized_key?(key)
not_materialized_keys.include?(key.to_sym)
end
# @param [Hash, WithCTE] value
def pipe_cte_with!(value) # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity
return if value.nil? || value.empty?
value.each_pair do |name, expression|
sym_name = name.to_sym
next if with_values.key?(sym_name)
# Ensure we follow FIFO pattern.
# If the parent has similar CTE alias keys, we want to favor the parent's expressions over its children's.
if expression.is_a?(ActiveRecord::Relation) && expression.with_values?
expression.cte = expression.cte.dup if expression.cte
# Add child's materialized keys to the parent
@materialized_keys += expression.cte.materialized_keys
@not_materialized_keys += expression.cte.not_materialized_keys
pipe_cte_with!(expression.cte)
expression.cte.reset!
end
@with_keys |= [sym_name]
@with_values[sym_name] = expression
end
value.reset! if value.is_a?(WithCTE)
end
def reset!
@with_keys = []
@with_values = {}
@materialized_keys = Set.new
@not_materialized_keys = Set.new
end
end
class WithChain
# @param [ActiveRecord::Relation] scope
def initialize(scope)
@scope = scope
@scope.cte ||= WithCTE.new(scope)
end
# @param [Hash, WithCTE] args
def recursive(args)
@scope.tap do |scope|
scope.recursive_value = true
scope.cte.pipe_cte_with!(args)
end
end
# @param [Hash, WithCTE] args
def materialized(args)
@scope.tap do |scope|
args.each_pair do |name, _expression|
sym_name = name.to_sym
raise ArgumentError.new("CTE already set as not_materialized") if scope.cte.not_materialized_key?(sym_name)
scope.cte.materialized_keys << sym_name
end
scope.cte.pipe_cte_with!(args)
end
end
# @param [Hash, WithCTE] args
def not_materialized(args)
@scope.tap do |scope|
args.each_pair do |name, _expression|
sym_name = name.to_sym
raise ArgumentError.new("CTE already set as materialized") if scope.cte.materialized_key?(sym_name)
scope.cte.not_materialized_keys << sym_name
end
scope.cte.pipe_cte_with!(args)
end
end
end
# @return [WithCTE]
def cte
@values[:cte]
end
# @param [WithCTE] cte
def cte=(cte)
raise TypeError.new("Must be a WithCTE class type") unless cte.is_a?(WithCTE)
@values[:cte] = cte
end
# @return [Boolean]
def with_values?
!(cte.nil? || cte.empty?)
end
# @return [Array<Hash>]
def with_values
with_values? ? [cte.with_values] : []
end
# @param [Hash, WithCTE] values
def with_values=(values)
cte.with_values = values
end
# @param [Boolean] value
def recursive_value=(value)
raise ImmutableRelation if @loaded
@values[:recursive] = value
end
# @return [Boolean]
def recursive_value?
!(!@values[:recursive])
end
# @param [Hash, WithCTE] opts
def with(opts = :chain, *rest)
return WithChain.new(spawn) if opts == :chain
opts.blank? ? self : spawn.with!(opts, *rest)
end
# @param [Hash, WithCTE] opts
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
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
private
def add_materialized_modifier(expression, cte, name)
if cte.materialized_key?(name)
Arel.sql("MATERIALIZED #{expression.to_sql}")
elsif cte.not_materialized_key?(name)
Arel.sql("NOT MATERIALIZED #{expression.to_sql}")
else
expression
end
end
end
end
end
ActiveRecord::Relation.prepend(ActiveRecordExtended::QueryMethods::WithCTE)