1 package # hide from the pauses
2 DBIx::Class::ResultSource::RowParser::Util;
7 use List::Util 'first';
12 assemble_simple_parser
13 assemble_collapsing_parser
16 # working title - we are hoping to extract this eventually...
17 our $null_branch_class = 'DBIx::ResultParser::RelatedNullBranch';
19 sub assemble_simple_parser {
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:
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);
31 my $parser_src = sprintf('$_ = %s for @{$_[0]}', __visit_infmap_simple($_[0]) );
33 # change the quoted placeholders to unquoted alias-references
34 $parser_src =~ s/ \' \xFF__VALPOS__(\d+)__\xFF \' /"\$_->[$1]"/gex;
36 $parser_src = " { use strict; use warnings FATAL => 'all';\n$parser_src\n }";
39 # the simple non-collapsing nested structure recursor
40 sub __visit_infmap_simple {
45 for (keys %{$args->{val_index}}) {
46 if ($_ =~ /^ ([^\.]+) \. (.+) /x) {
47 $rel_cols->{$1}{$2} = $args->{val_index}{$_};
50 $my_cols->{$_} = $args->{val_index}{$_};
55 for my $rel (sort keys %$rel_cols) {
57 my $rel_struct = __visit_infmap_simple({ %$args,
58 val_index => $rel_cols->{$rel},
63 my $branch_null_checks = join ' && ', map
64 { "( ! defined '\xFF__VALPOS__${_}__\xFF' )" }
65 sort { $a <=> $b } values %{$rel_cols->{$rel}}
68 if ($args->{hri_style}) {
69 $rel_struct = sprintf ( '( (%s) ? undef : %s )',
75 $rel_struct = sprintf ( '( (%s) ? bless( (%s), %s ) : %s )',
78 perlstring($null_branch_class),
84 push @relperl, sprintf '( %s => %s )',
92 $me_struct = __visit_dump({ map { $_ => "\xFF__VALPOS__$my_cols->{$_}__\xFF" } (keys %$my_cols) })
95 if ($args->{hri_style}) {
96 $me_struct =~ s/^ \s* \{ | \} \s* $//gx
99 return sprintf '{ %s }', join (', ', $me_struct||(), @relperl);
102 return sprintf '[%s]', join (',',
103 $me_struct || 'undef',
104 @relperl ? sprintf ('{ %s }', join (',', @relperl)) : (),
109 sub assemble_collapsing_parser {
112 my ($top_node_key, $top_node_key_assembler);
114 if (scalar @{$args->{collapse_map}{-identifying_columns}}) {
115 $top_node_key = join ('', map
116 { "{'\xFF__IDVALPOS__${_}__\xFF'}" }
117 @{$args->{collapse_map}{-identifying_columns}}
120 elsif( my @variants = @{$args->{collapse_map}{-identifying_columns_variants}} ) {
122 my @path_parts = map { sprintf
123 "( ( defined '\xFF__VALPOS__%d__\xFF' ) && (join qq(\xFF), '', %s, '') )",
124 $_->[0], # checking just first is enough - one ID defined, all defined
125 ( join ', ', map { "'\xFF__VALPOS__${_}__\xFF'" } @$_ ),
128 my $virtual_column_idx = (scalar keys %{$args->{val_index}} ) + 1;
130 $top_node_key_assembler = sprintf '$cur_row_ids{%d} = (%s);',
132 "\n" . join( "\n or\n", @path_parts, qq{"\0\$rows_pos\0"} );
134 $top_node_key = sprintf '{$cur_row_ids{%d}}', $virtual_column_idx;
136 $args->{collapse_map} = {
137 %{$args->{collapse_map}},
138 -custom_node_key => $top_node_key,
143 die('Unexpected collapse map contents');
146 my ($data_assemblers, $stats) = __visit_infmap_collapse ($args);
148 my $list_of_idcols = join(', ', sort { $a <=> $b } keys %{ $stats->{idcols_seen} } );
150 my $parser_src = sprintf (<<'EOS', $list_of_idcols, $top_node_key, $top_node_key_assembler||'', join( "\n", @{$data_assemblers||[]} ) );
151 ### BEGIN LITERAL STRING EVAL
152 my ($rows_pos, $result_pos, $cur_row_data, %%cur_row_ids, @collapse_idx, $is_new_res) = (0,0);
153 # this loop is a bit arcane - the rationale is that the passed in
154 # $_[0] will either have only one row (->next) or will have all
155 # rows already pulled in (->all and/or unordered). Given that the
156 # result can be rather large - we reuse the same already allocated
157 # array, since the collapsed prefetch is smaller by definition.
158 # At the end we cut the leftovers away and move on.
159 while ($cur_row_data =
160 ( ( $rows_pos >= 0 and $_[0][$rows_pos++] ) or do { $rows_pos = -1; undef } )
162 ($_[1] and $_[1]->())
164 # due to left joins some of the ids may be NULL/undef, and
165 # won't play well when used as hash lookups
166 # we also need to differentiate NULLs on per-row/per-col basis
167 # (otherwise folding of optional 1:1s will be greatly confused
168 $cur_row_ids{$_} = defined $cur_row_data->[$_] ? $cur_row_data->[$_] : "\0NULL\xFF$rows_pos\xFF$_\0"
171 # maybe(!) cache the top node id calculation
174 $is_new_res = ! $collapse_idx[0]%2$s and (
175 $_[1] and $result_pos and (unshift @{$_[2]}, $cur_row_data) and last
181 $_[0][$result_pos++] = $collapse_idx[0]%2$s
185 splice @{$_[0]}, $result_pos; # truncate the passed in array for cases of collapsing ->all()
186 ### END LITERAL STRING EVAL
189 # !!! note - different var than the one above
190 # change the quoted placeholders to unquoted alias-references
191 $parser_src =~ s/ \' \xFF__VALPOS__(\d+)__\xFF \' /"\$cur_row_data->[$1]"/gex;
192 $parser_src =~ s/ \' \xFF__IDVALPOS__(\d+)__\xFF \' /"\$cur_row_ids{$1}"/gex;
194 $parser_src = " { use strict; use warnings FATAL => 'all';\n$parser_src\n }";
198 # the collapsing nested structure recursor
199 sub __visit_infmap_collapse {
200 my $args = {%{ shift() }};
202 my $cur_node_idx = ${ $args->{-node_idx_counter} ||= \do { my $x = 0} }++;
204 my ($my_cols, $rel_cols) = {};
205 for ( keys %{$args->{val_index}} ) {
206 if ($_ =~ /^ ([^\.]+) \. (.+) /x) {
207 $rel_cols->{$1}{$2} = $args->{val_index}{$_};
210 $my_cols->{$_} = $args->{val_index}{$_};
215 if ($args->{hri_style}) {
216 delete $my_cols->{$_} for grep { $rel_cols->{$_} } keys %$my_cols;
220 $me_struct = __visit_dump({ map { $_ => "\xFF__VALPOS__$my_cols->{$_}__\xFF" } (keys %$my_cols) })
223 $me_struct = sprintf( '[ %s ]', $me_struct||'' )
224 unless $args->{hri_style};
227 my $node_key = $args->{collapse_map}->{-custom_node_key} || join ('', map
228 { "{'\xFF__IDVALPOS__${_}__\xFF'}" }
229 @{$args->{collapse_map}->{-identifying_columns}}
231 my $node_idx_slot = sprintf '$collapse_idx[%d]%s', $cur_node_idx, $node_key;
236 if ($cur_node_idx == 0) {
237 push @src, sprintf( '%s ||= %s;',
243 my $parent_attach_slot = sprintf( '$collapse_idx[%d]%s%s{%s}',
244 @{$args}{qw/-parent_node_idx -parent_node_key/},
245 $args->{hri_style} ? '' : '[1]',
246 perlstring($args->{-node_relname}),
249 if ($args->{collapse_map}->{-is_single}) {
250 push @src, sprintf ( '%s ||= %s%s;',
253 $me_struct ? " ||= $me_struct" : '',
257 push @src, sprintf('(! %s) and push @{%s}, %s%s;',
261 $me_struct ? " = $me_struct" : '',
266 my $known_present_ids = { map { $_ => 1 } @{$args->{collapse_map}{-identifying_columns}} };
267 my ($stats, $rel_src);
269 for my $rel (sort keys %$rel_cols) {
271 my $relinfo = $args->{collapse_map}{$rel};
273 ($rel_src, $stats->{$rel}) = __visit_infmap_collapse({ %$args,
274 val_index => $rel_cols->{$rel},
275 collapse_map => $relinfo,
276 -parent_node_idx => $cur_node_idx,
277 -parent_node_key => $node_key,
278 -node_relname => $rel,
281 my $rel_src_pos = $#src + 1;
282 push @src, @$rel_src;
285 $relinfo->{-is_optional}
287 defined ( my $first_distinct_child_idcol = first
288 { ! $known_present_ids->{$_} }
289 @{$relinfo->{-identifying_columns}}
293 if ($args->{hri_style}) {
295 $src[$rel_src_pos] = sprintf( '%s and %s',
296 "( defined '\xFF__VALPOS__${first_distinct_child_idcol}__\xFF' )",
300 splice @src, $rel_src_pos + 1, 0, sprintf ( '%s{%s} ||= %s;',
303 $relinfo->{-is_single} ? 'undef' : '[]',
308 splice @src, $rel_src_pos + 1, 0, sprintf ( '(defined %s) or bless (%s[1]{%s}, %s);',
309 "'\xFF__VALPOS__${first_distinct_child_idcol}__\xFF'",
312 perlstring($null_branch_class),
322 ( map { %{ $_->{idcols_seen} } } values %$stats ),
323 ( map { $_ => 1 } @{$args->{collapse_map}->{-identifying_columns}} ),
329 # keep our own DD object around so we don't have to fitz with quoting
333 # we actually will be producing functional perl code here,
334 # thus no second-guessing of what these globals might have
335 # been set to. DO NOT CHANGE!
336 ($dumper_obj ||= do {
337 require Data::Dumper;
338 Data::Dumper->new([])
348 ->Indent (0) # faster but harder to read, perhaps leave at 1 ?
349 })->Values ([$_[0]])->Dump;