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 sub assemble_simple_parser {
19 # the non-collapsing assembler is easy
20 # FIXME SUBOPTIMAL there could be a yet faster way to do things here, but
21 # need to try an actual implementation and benchmark it:
23 # <timbunce_> First setup the nested data structure you want for each row
24 # Then call bind_col() to alias the row fields into the right place in
25 # the data structure, then to fetch the data do:
26 # push @rows, dclone($row_data_struct) while ($sth->fetchrow);
28 my $parser_src = sprintf('$_ = %s for @{$_[0]}', __visit_infmap_simple($_[0]) );
30 # change the quoted placeholders to unquoted alias-references
31 $parser_src =~ s/ \' \xFF__VALPOS__(\d+)__\xFF \' /"\$_->[$1]"/gex;
36 # the simple non-collapsing nested structure recursor
37 sub __visit_infmap_simple {
42 for (keys %{$args->{val_index}}) {
43 if ($_ =~ /^ ([^\.]+) \. (.+) /x) {
44 $rel_cols->{$1}{$2} = $args->{val_index}{$_};
47 $my_cols->{$_} = $args->{val_index}{$_};
52 for my $rel (sort keys %$rel_cols) {
54 push @relperl, join ' => ', perlstring($rel), __visit_infmap_simple({ %$args,
55 val_index => $rel_cols->{$rel},
58 if ($args->{prune_null_branches} and keys %$my_cols) {
60 my @branch_null_checks = map
61 { "( ! defined '\xFF__VALPOS__${_}__\xFF' )" }
62 sort { $a <=> $b } values %{$rel_cols->{$rel}}
65 $relperl[-1] = sprintf ( '(%s) ? ( %s => %s ) : ( %s )',
66 join (' && ', @branch_null_checks ),
68 $args->{hri_style} ? 'undef' : '[]',
75 $me_struct = __visit_dump({ map { $_ => "\xFF__VALPOS__$my_cols->{$_}__\xFF" } (keys %$my_cols) })
78 if ($args->{hri_style}) {
79 $me_struct =~ s/^ \s* \{ | \} \s* $//gx
82 return sprintf '{ %s }', join (', ', $me_struct||(), @relperl);
85 return sprintf '[%s]', join (',',
86 $me_struct || 'undef',
87 @relperl ? sprintf ('{ %s }', join (',', @relperl)) : (),
92 sub assemble_collapsing_parser {
95 my ($top_node_key, $top_node_key_assembler);
97 if (scalar @{$args->{collapse_map}{-identifying_columns}}) {
98 $top_node_key = join ('', map
99 { "{'\xFF__IDVALPOS__${_}__\xFF'}" }
100 @{$args->{collapse_map}{-identifying_columns}}
103 elsif( my @variants = @{$args->{collapse_map}{-identifying_columns_variants}} ) {
105 my @path_parts = map { sprintf
106 "( ( defined '\xFF__VALPOS__%d__\xFF' ) && (join qq(\xFF), '', %s, '') )",
107 $_->[0], # checking just first is enough - one ID defined, all defined
108 ( join ', ', map { "'\xFF__VALPOS__${_}__\xFF'" } @$_ ),
111 my $virtual_column_idx = (scalar keys %{$args->{val_index}} ) + 1;
113 $top_node_key_assembler = sprintf '$cur_row_ids{%d} = (%s);',
115 "\n" . join( "\n or\n", @path_parts, qq{"\0\$rows_pos\0"} );
117 $top_node_key = sprintf '{$cur_row_ids{%d}}', $virtual_column_idx;
119 $args->{collapse_map} = {
120 %{$args->{collapse_map}},
121 -custom_node_key => $top_node_key,
126 die('Unexpected collapse map contents');
129 my ($data_assemblers, $stats) = __visit_infmap_collapse ($args);
131 my $list_of_idcols = join(', ', sort { $a <=> $b } keys %{ $stats->{idcols_seen} } );
133 my $parser_src = sprintf (<<'EOS', $list_of_idcols, $top_node_key, $top_node_key_assembler||'', join( "\n", @{$data_assemblers||[]} ) );
134 ### BEGIN LITERAL STRING EVAL
135 my ($rows_pos, $result_pos, $cur_row_data, %%cur_row_ids, @collapse_idx, $is_new_res) = (0,0);
136 # this loop is a bit arcane - the rationale is that the passed in
137 # $_[0] will either have only one row (->next) or will have all
138 # rows already pulled in (->all and/or unordered). Given that the
139 # result can be rather large - we reuse the same already allocated
140 # array, since the collapsed prefetch is smaller by definition.
141 # At the end we cut the leftovers away and move on.
142 while ($cur_row_data =
143 ( ( $rows_pos >= 0 and $_[0][$rows_pos++] ) or do { $rows_pos = -1; undef } )
145 ($_[1] and $_[1]->())
147 # due to left joins some of the ids may be NULL/undef, and
148 # won't play well when used as hash lookups
149 # we also need to differentiate NULLs on per-row/per-col basis
150 # (otherwise folding of optional 1:1s will be greatly confused
151 $cur_row_ids{$_} = defined $cur_row_data->[$_] ? $cur_row_data->[$_] : "\0NULL\xFF$rows_pos\xFF$_\0"
154 # maybe(!) cache the top node id calculation
157 $is_new_res = ! $collapse_idx[0]%2$s and (
158 $_[1] and $result_pos and (unshift @{$_[2]}, $cur_row_data) and last
164 $_[0][$result_pos++] = $collapse_idx[0]%2$s
168 splice @{$_[0]}, $result_pos; # truncate the passed in array for cases of collapsing ->all()
169 ### END LITERAL STRING EVAL
172 # !!! note - different var than the one above
173 # change the quoted placeholders to unquoted alias-references
174 $parser_src =~ s/ \' \xFF__VALPOS__(\d+)__\xFF \' /"\$cur_row_data->[$1]"/gex;
175 $parser_src =~ s/ \' \xFF__IDVALPOS__(\d+)__\xFF \' /"\$cur_row_ids{$1}"/gex;
181 # the collapsing nested structure recursor
182 sub __visit_infmap_collapse {
183 my $args = {%{ shift() }};
185 my $cur_node_idx = ${ $args->{-node_idx_counter} ||= \do { my $x = 0} }++;
187 my ($my_cols, $rel_cols) = {};
188 for ( keys %{$args->{val_index}} ) {
189 if ($_ =~ /^ ([^\.]+) \. (.+) /x) {
190 $rel_cols->{$1}{$2} = $args->{val_index}{$_};
193 $my_cols->{$_} = $args->{val_index}{$_};
198 my $node_key = $args->{collapse_map}->{-custom_node_key} || join ('', map
199 { "{'\xFF__IDVALPOS__${_}__\xFF'}" }
200 @{$args->{collapse_map}->{-identifying_columns}}
205 if ($args->{hri_style}) {
206 delete $my_cols->{$_} for grep { $rel_cols->{$_} } keys %$my_cols;
209 if (keys %$my_cols) {
210 $me_struct = __visit_dump({ map { $_ => "\xFF__VALPOS__$my_cols->{$_}__\xFF" } (keys %$my_cols) });
211 $me_struct = "[ $me_struct ]" unless $args->{hri_style};
214 my $node_idx_slot = sprintf '$collapse_idx[%d]%s', $cur_node_idx, $node_key;
218 if ($cur_node_idx == 0) {
219 push @src, sprintf( '%s ||= %s;',
225 my $parent_attach_slot = sprintf( '$collapse_idx[%d]%s%s{%s}',
226 @{$args}{qw/-parent_node_idx -parent_node_key/},
227 $args->{hri_style} ? '' : '[1]',
228 perlstring($args->{-node_relname}),
231 if ($args->{collapse_map}->{-is_single}) {
232 push @src, sprintf ( '%s ||= %s%s;',
235 $me_struct ? " ||= $me_struct" : '',
239 push @src, sprintf('(! %s) and push @{%s}, %s%s;',
243 $me_struct ? " = $me_struct" : '',
248 my $known_present_ids = { map { $_ => 1 } @{$args->{collapse_map}{-identifying_columns}} };
249 my ($stats, $rel_src);
251 for my $rel (sort keys %$rel_cols) {
253 my $relinfo = $args->{collapse_map}{$rel};
254 if ($args->{collapse_map}{-is_optional}) {
255 $relinfo = { %$relinfo, -is_optional => 1 };
258 ($rel_src, $stats->{$rel}) = __visit_infmap_collapse({ %$args,
259 val_index => $rel_cols->{$rel},
260 collapse_map => $relinfo,
261 -parent_node_idx => $cur_node_idx,
262 -parent_node_key => $node_key,
263 -node_relname => $rel,
266 my $rel_src_pos = $#src + 1;
267 push @src, @$rel_src;
270 $args->{prune_null_branches}
272 $relinfo->{-is_optional}
274 defined ( my $first_distinct_child_idcol = first
275 { ! $known_present_ids->{$_} }
276 @{$relinfo->{-identifying_columns}}
280 $src[$rel_src_pos] = sprintf( '%s and %s',
281 "( defined '\xFF__VALPOS__${first_distinct_child_idcol}__\xFF' )",
285 splice @src, $rel_src_pos + 1, 0, sprintf ( '%s%s{%s} ||= %s;',
287 $args->{hri_style} ? '' : '[1]',
289 $args->{hri_style} && $relinfo->{-is_single} ? 'undef' : '[]',
298 ( map { %{ $_->{idcols_seen} } } values %$stats ),
299 ( map { $_ => 1 } @{$args->{collapse_map}->{-identifying_columns}} ),
305 # keep our own DD object around so we don't have to fitz with quoting
308 # we actually will be producing functional perl code here,
309 # thus no second-guessing of what these globals might have
310 # been set to. DO NOT CHANGE!
311 ($dumper_obj ||= do {
312 require Data::Dumper;
313 Data::Dumper->new([])
323 ->Indent (0) # faster but harder to read, perhaps leave at 1 ?
324 })->Values ([$_[0]])->Dump;