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