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