Commit | Line | Data |
9f98c4b2 |
1 | package # hide from the pauses |
2 | DBIx::Class::ResultSource::RowParser::Util; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
ce556881 |
7 | use List::Util 'first'; |
9f98c4b2 |
8 | use B 'perlstring'; |
9 | |
10 | use base 'Exporter'; |
11 | our @EXPORT_OK = qw( |
12 | assemble_simple_parser |
13 | assemble_collapsing_parser |
14 | ); |
15 | |
52864fbd |
16 | # working title - we are hoping to extract this eventually... |
17 | our $null_branch_class = 'DBIx::ResultParser::RelatedNullBranch'; |
18 | |
9f98c4b2 |
19 | sub assemble_simple_parser { |
20 | #my ($args) = @_; |
21 | |
22 | # the non-collapsing assembler is easy |
23 | # FIXME SUBOPTIMAL there could be a yet faster way to do things here, but |
24 | # need to try an actual implementation and benchmark it: |
25 | # |
26 | # <timbunce_> First setup the nested data structure you want for each row |
27 | # Then call bind_col() to alias the row fields into the right place in |
28 | # the data structure, then to fetch the data do: |
29 | # push @rows, dclone($row_data_struct) while ($sth->fetchrow); |
30 | # |
31 | my $parser_src = sprintf('$_ = %s for @{$_[0]}', __visit_infmap_simple($_[0]) ); |
32 | |
33 | # change the quoted placeholders to unquoted alias-references |
34 | $parser_src =~ s/ \' \xFF__VALPOS__(\d+)__\xFF \' /"\$_->[$1]"/gex; |
35 | |
bdbd2ae8 |
36 | $parser_src = " { use strict; use warnings FATAL => 'all';\n$parser_src\n }"; |
9f98c4b2 |
37 | } |
38 | |
39 | # the simple non-collapsing nested structure recursor |
40 | sub __visit_infmap_simple { |
41 | my $args = shift; |
42 | |
43 | my $my_cols = {}; |
44 | my $rel_cols; |
45 | for (keys %{$args->{val_index}}) { |
46 | if ($_ =~ /^ ([^\.]+) \. (.+) /x) { |
47 | $rel_cols->{$1}{$2} = $args->{val_index}{$_}; |
48 | } |
49 | else { |
50 | $my_cols->{$_} = $args->{val_index}{$_}; |
51 | } |
52 | } |
53 | |
54 | my @relperl; |
55 | for my $rel (sort keys %$rel_cols) { |
56 | |
52864fbd |
57 | my $rel_struct = __visit_infmap_simple({ %$args, |
9f98c4b2 |
58 | val_index => $rel_cols->{$rel}, |
9f98c4b2 |
59 | }); |
60 | |
52864fbd |
61 | if (keys %$my_cols) { |
ce556881 |
62 | |
52864fbd |
63 | my $branch_null_checks = join ' && ', map |
ce556881 |
64 | { "( ! defined '\xFF__VALPOS__${_}__\xFF' )" } |
65 | sort { $a <=> $b } values %{$rel_cols->{$rel}} |
66 | ; |
67 | |
79adc44f |
68 | if ($args->{prune_null_branches}) { |
52864fbd |
69 | $rel_struct = sprintf ( '( (%s) ? undef : %s )', |
70 | $branch_null_checks, |
71 | $rel_struct, |
72 | ); |
73 | } |
74 | else { |
75 | $rel_struct = sprintf ( '( (%s) ? bless( (%s), %s ) : %s )', |
76 | $branch_null_checks, |
77 | $rel_struct, |
78 | perlstring($null_branch_class), |
79 | $rel_struct, |
80 | ); |
81 | } |
ce556881 |
82 | } |
52864fbd |
83 | |
84 | push @relperl, sprintf '( %s => %s )', |
85 | perlstring($rel), |
86 | $rel_struct, |
87 | ; |
88 | |
9f98c4b2 |
89 | } |
90 | |
ce556881 |
91 | my $me_struct; |
a8f62ee0 |
92 | $me_struct = __result_struct_to_source($my_cols) if keys %$my_cols; |
9f98c4b2 |
93 | |
ce556881 |
94 | if ($args->{hri_style}) { |
95 | $me_struct =~ s/^ \s* \{ | \} \s* $//gx |
96 | if $me_struct; |
97 | |
98 | return sprintf '{ %s }', join (', ', $me_struct||(), @relperl); |
99 | } |
100 | else { |
101 | return sprintf '[%s]', join (',', |
102 | $me_struct || 'undef', |
103 | @relperl ? sprintf ('{ %s }', join (',', @relperl)) : (), |
104 | ); |
105 | } |
9f98c4b2 |
106 | } |
107 | |
108 | sub assemble_collapsing_parser { |
109 | my $args = shift; |
110 | |
06b3406d |
111 | # it may get unset further down |
79adc44f |
112 | my $no_rowid_container = $args->{prune_null_branches}; |
06b3406d |
113 | |
9f98c4b2 |
114 | my ($top_node_key, $top_node_key_assembler); |
115 | |
116 | if (scalar @{$args->{collapse_map}{-identifying_columns}}) { |
117 | $top_node_key = join ('', map |
118 | { "{'\xFF__IDVALPOS__${_}__\xFF'}" } |
119 | @{$args->{collapse_map}{-identifying_columns}} |
120 | ); |
121 | } |
122 | elsif( my @variants = @{$args->{collapse_map}{-identifying_columns_variants}} ) { |
123 | |
124 | my @path_parts = map { sprintf |
125 | "( ( defined '\xFF__VALPOS__%d__\xFF' ) && (join qq(\xFF), '', %s, '') )", |
ce556881 |
126 | $_->[0], # checking just first is enough - one ID defined, all defined |
9f98c4b2 |
127 | ( join ', ', map { "'\xFF__VALPOS__${_}__\xFF'" } @$_ ), |
128 | } @variants; |
129 | |
130 | my $virtual_column_idx = (scalar keys %{$args->{val_index}} ) + 1; |
131 | |
7596ddca |
132 | $top_node_key = "{'\xFF__IDVALPOS__${virtual_column_idx}__\xFF'}"; |
9f98c4b2 |
133 | |
7596ddca |
134 | $top_node_key_assembler = sprintf "'\xFF__IDVALPOS__%d__\xFF' = (%s);", |
135 | $virtual_column_idx, |
136 | "\n" . join( "\n or\n", @path_parts, qq{"\0\$rows_pos\0"} ) |
137 | ; |
9f98c4b2 |
138 | |
139 | $args->{collapse_map} = { |
140 | %{$args->{collapse_map}}, |
141 | -custom_node_key => $top_node_key, |
142 | }; |
143 | |
06b3406d |
144 | $no_rowid_container = 0; |
9f98c4b2 |
145 | } |
146 | else { |
147 | die('Unexpected collapse map contents'); |
148 | } |
149 | |
150 | my ($data_assemblers, $stats) = __visit_infmap_collapse ($args); |
151 | |
06b3406d |
152 | my @idcol_args = $no_rowid_container ? ('', '') : ( |
aa1d8a87 |
153 | ', %cur_row_ids', # only declare the variable if we'll use it |
3b4cd124 |
154 | join ("\n", map { |
155 | qq(\$cur_row_ids{$_} = defined(\$cur_row_data->[$_]) ? \$cur_row_data->[$_] : "\0NULL\xFF\$rows_pos\xFF$_\0";) |
156 | } sort { $a <=> $b } keys %{ $stats->{idcols_seen} } ) |
7596ddca |
157 | ); |
9f98c4b2 |
158 | |
7596ddca |
159 | my $parser_src = sprintf (<<'EOS', @idcol_args, $top_node_key_assembler||'', $top_node_key, join( "\n", @{$data_assemblers||[]} ) ); |
9f98c4b2 |
160 | ### BEGIN LITERAL STRING EVAL |
aa1d8a87 |
161 | my $rows_pos = 0; |
162 | my ($result_pos, @collapse_idx, $cur_row_data %1$s); |
163 | |
9f98c4b2 |
164 | # this loop is a bit arcane - the rationale is that the passed in |
165 | # $_[0] will either have only one row (->next) or will have all |
166 | # rows already pulled in (->all and/or unordered). Given that the |
167 | # result can be rather large - we reuse the same already allocated |
168 | # array, since the collapsed prefetch is smaller by definition. |
169 | # At the end we cut the leftovers away and move on. |
3b4cd124 |
170 | while ($cur_row_data = ( |
171 | ( $rows_pos >= 0 and $_[0][$rows_pos++] ) |
172 | or |
173 | ( $_[1] and $rows_pos = -1 and $_[1]->() ) |
174 | ) ) { |
175 | |
7596ddca |
176 | # this code exists only when we are *not* assembling direct to HRI |
177 | # |
9f98c4b2 |
178 | # due to left joins some of the ids may be NULL/undef, and |
179 | # won't play well when used as hash lookups |
180 | # we also need to differentiate NULLs on per-row/per-col basis |
ce556881 |
181 | # (otherwise folding of optional 1:1s will be greatly confused |
3b4cd124 |
182 | %2$s |
9f98c4b2 |
183 | |
7596ddca |
184 | # in the case of an underdefined root - calculate the virtual id (otherwise no code at all) |
3b4cd124 |
185 | %3$s |
9f98c4b2 |
186 | |
aa1d8a87 |
187 | # if we were supplied a coderef - we are collapsing lazily (the set |
188 | # is ordered properly) |
189 | # as long as we have a result already and the next result is new we |
190 | # return the pre-read data and bail |
3b4cd124 |
191 | $_[1] and $result_pos and ! $collapse_idx[0]%4$s and (unshift @{$_[2]}, $cur_row_data) and last; |
9f98c4b2 |
192 | |
193 | # the rel assemblers |
7596ddca |
194 | %5$s |
9f98c4b2 |
195 | |
9f98c4b2 |
196 | } |
197 | |
aa1d8a87 |
198 | $#{$_[0]} = $result_pos - 1; # truncate the passed in array to where we filled it with results |
9f98c4b2 |
199 | ### END LITERAL STRING EVAL |
200 | EOS |
201 | |
202 | # !!! note - different var than the one above |
203 | # change the quoted placeholders to unquoted alias-references |
204 | $parser_src =~ s/ \' \xFF__VALPOS__(\d+)__\xFF \' /"\$cur_row_data->[$1]"/gex; |
06b3406d |
205 | $parser_src =~ s/ |
206 | \' \xFF__IDVALPOS__(\d+)__\xFF \' |
207 | / |
208 | $no_rowid_container ? "\$cur_row_data->[$1]" : "\$cur_row_ids{$1}" |
209 | /gex; |
9f98c4b2 |
210 | |
bdbd2ae8 |
211 | $parser_src = " { use strict; use warnings FATAL => 'all';\n$parser_src\n }"; |
9f98c4b2 |
212 | } |
213 | |
214 | |
215 | # the collapsing nested structure recursor |
216 | sub __visit_infmap_collapse { |
217 | my $args = {%{ shift() }}; |
218 | |
219 | my $cur_node_idx = ${ $args->{-node_idx_counter} ||= \do { my $x = 0} }++; |
220 | |
ce556881 |
221 | my ($my_cols, $rel_cols) = {}; |
9f98c4b2 |
222 | for ( keys %{$args->{val_index}} ) { |
223 | if ($_ =~ /^ ([^\.]+) \. (.+) /x) { |
224 | $rel_cols->{$1}{$2} = $args->{val_index}{$_}; |
225 | } |
226 | else { |
227 | $my_cols->{$_} = $args->{val_index}{$_}; |
228 | } |
229 | } |
230 | |
ce556881 |
231 | |
ce556881 |
232 | if ($args->{hri_style}) { |
233 | delete $my_cols->{$_} for grep { $rel_cols->{$_} } keys %$my_cols; |
234 | } |
9f98c4b2 |
235 | |
52864fbd |
236 | my $me_struct; |
a8f62ee0 |
237 | $me_struct = __result_struct_to_source($my_cols) if keys %$my_cols; |
ce556881 |
238 | |
52864fbd |
239 | $me_struct = sprintf( '[ %s ]', $me_struct||'' ) |
240 | unless $args->{hri_style}; |
241 | |
242 | |
243 | my $node_key = $args->{collapse_map}->{-custom_node_key} || join ('', map |
244 | { "{'\xFF__IDVALPOS__${_}__\xFF'}" } |
245 | @{$args->{collapse_map}->{-identifying_columns}} |
246 | ); |
ce556881 |
247 | my $node_idx_slot = sprintf '$collapse_idx[%d]%s', $cur_node_idx, $node_key; |
9f98c4b2 |
248 | |
52864fbd |
249 | |
9f98c4b2 |
250 | my @src; |
ce556881 |
251 | |
9f98c4b2 |
252 | if ($cur_node_idx == 0) { |
aa1d8a87 |
253 | push @src, sprintf( '%s ||= $_[0][$result_pos++] = %s;', |
9f98c4b2 |
254 | $node_idx_slot, |
aa1d8a87 |
255 | $me_struct || '{}', |
256 | ); |
9f98c4b2 |
257 | } |
9f98c4b2 |
258 | else { |
ce556881 |
259 | my $parent_attach_slot = sprintf( '$collapse_idx[%d]%s%s{%s}', |
260 | @{$args}{qw/-parent_node_idx -parent_node_key/}, |
261 | $args->{hri_style} ? '' : '[1]', |
262 | perlstring($args->{-node_relname}), |
9f98c4b2 |
263 | ); |
ce556881 |
264 | |
265 | if ($args->{collapse_map}->{-is_single}) { |
266 | push @src, sprintf ( '%s ||= %s%s;', |
267 | $parent_attach_slot, |
268 | $node_idx_slot, |
aa1d8a87 |
269 | $me_struct ? " = $me_struct" : '', |
ce556881 |
270 | ); |
271 | } |
272 | else { |
273 | push @src, sprintf('(! %s) and push @{%s}, %s%s;', |
274 | $node_idx_slot, |
275 | $parent_attach_slot, |
276 | $node_idx_slot, |
277 | $me_struct ? " = $me_struct" : '', |
278 | ); |
279 | } |
9f98c4b2 |
280 | } |
281 | |
ce556881 |
282 | my $known_present_ids = { map { $_ => 1 } @{$args->{collapse_map}{-identifying_columns}} }; |
283 | my ($stats, $rel_src); |
284 | |
9f98c4b2 |
285 | for my $rel (sort keys %$rel_cols) { |
286 | |
ce556881 |
287 | my $relinfo = $args->{collapse_map}{$rel}; |
9f98c4b2 |
288 | |
ce556881 |
289 | ($rel_src, $stats->{$rel}) = __visit_infmap_collapse({ %$args, |
9f98c4b2 |
290 | val_index => $rel_cols->{$rel}, |
ce556881 |
291 | collapse_map => $relinfo, |
9f98c4b2 |
292 | -parent_node_idx => $cur_node_idx, |
293 | -parent_node_key => $node_key, |
294 | -node_relname => $rel, |
295 | }); |
296 | |
ce556881 |
297 | my $rel_src_pos = $#src + 1; |
298 | push @src, @$rel_src; |
299 | |
300 | if ( |
ce556881 |
301 | $relinfo->{-is_optional} |
302 | and |
303 | defined ( my $first_distinct_child_idcol = first |
304 | { ! $known_present_ids->{$_} } |
305 | @{$relinfo->{-identifying_columns}} |
306 | ) |
307 | ) { |
308 | |
79adc44f |
309 | if ($args->{prune_null_branches}) { |
ce556881 |
310 | |
7596ddca |
311 | # start of wrap of the entire chain in a conditional |
79adc44f |
312 | splice @src, $rel_src_pos, 0, sprintf "( ! defined %s )\n ? %s%s{%s} = %s\n : do {", |
7596ddca |
313 | "'\xFF__VALPOS__${first_distinct_child_idcol}__\xFF'", |
52864fbd |
314 | $node_idx_slot, |
79adc44f |
315 | $args->{hri_style} ? '' : '[1]', |
52864fbd |
316 | perlstring($rel), |
79adc44f |
317 | ($args->{hri_style} && $relinfo->{-is_single}) ? 'undef' : '[]' |
7596ddca |
318 | ; |
319 | |
320 | # end of wrap |
321 | push @src, '};' |
52864fbd |
322 | } |
323 | else { |
324 | |
325 | splice @src, $rel_src_pos + 1, 0, sprintf ( '(defined %s) or bless (%s[1]{%s}, %s);', |
326 | "'\xFF__VALPOS__${first_distinct_child_idcol}__\xFF'", |
327 | $node_idx_slot, |
328 | perlstring($rel), |
329 | perlstring($null_branch_class), |
330 | ); |
331 | } |
ce556881 |
332 | } |
9f98c4b2 |
333 | } |
334 | |
335 | return ( |
ce556881 |
336 | \@src, |
9f98c4b2 |
337 | { |
338 | idcols_seen => { |
339 | ( map { %{ $_->{idcols_seen} } } values %$stats ), |
340 | ( map { $_ => 1 } @{$args->{collapse_map}->{-identifying_columns}} ), |
341 | } |
342 | } |
343 | ); |
344 | } |
345 | |
a8f62ee0 |
346 | sub __result_struct_to_source { |
347 | sprintf( '{ %s }', join (', ', map |
348 | { sprintf "%s => '\xFF__VALPOS__%d__\xFF'", perlstring($_), $_[0]{$_} } |
349 | sort keys %{$_[0]} |
350 | )); |
9f98c4b2 |
351 | } |
352 | |
353 | 1; |