1 package # hide from the pauses
2 DBIx::Class::ResultSource::RowParser::Util;
11 assemble_simple_parser
12 assemble_collapsing_parser
15 sub assemble_simple_parser {
18 # the non-collapsing assembler is easy
19 # FIXME SUBOPTIMAL there could be a yet faster way to do things here, but
20 # need to try an actual implementation and benchmark it:
22 # <timbunce_> First setup the nested data structure you want for each row
23 # Then call bind_col() to alias the row fields into the right place in
24 # the data structure, then to fetch the data do:
25 # push @rows, dclone($row_data_struct) while ($sth->fetchrow);
27 my $parser_src = sprintf('$_ = %s for @{$_[0]}', __visit_infmap_simple($_[0]) );
29 # change the quoted placeholders to unquoted alias-references
30 $parser_src =~ s/ \' \xFF__VALPOS__(\d+)__\xFF \' /"\$_->[$1]"/gex;
35 # the simple non-collapsing nested structure recursor
36 sub __visit_infmap_simple {
41 for (keys %{$args->{val_index}}) {
42 if ($_ =~ /^ ([^\.]+) \. (.+) /x) {
43 $rel_cols->{$1}{$2} = $args->{val_index}{$_};
46 $my_cols->{$_} = $args->{val_index}{$_};
51 for my $rel (sort keys %$rel_cols) {
54 #my $optional = $args->{is_optional};
55 #$optional ||= ($args->{rsrc}->relationship_info($rel)->{attrs}{join_type} || '') =~ /^left/i;
57 push @relperl, join ' => ', perlstring($rel), __visit_infmap_simple({ %$args,
58 val_index => $rel_cols->{$rel},
61 #is_optional => $optional,
64 # FIXME SUBOPTIMAL DISABLEPRUNE - disabled to satisfy t/resultset/inflate_result_api.t
65 #if ($optional and my @branch_null_checks = map
66 # { "(! defined '\xFF__VALPOS__${_}__\xFF')" }
67 # sort { $a <=> $b } values %{$rel_cols->{$rel}}
69 # $relperl[-1] = sprintf ( '(%s) ? ( %s => [] ) : ( %s )',
70 # join (' && ', @branch_null_checks ),
77 my $me_struct = keys %$my_cols
78 ? __visit_dump({ map { $_ => "\xFF__VALPOS__$my_cols->{$_}__\xFF" } (keys %$my_cols) })
82 return sprintf '[%s]', join (',',
84 @relperl ? sprintf ('{ %s }', join (',', @relperl)) : (),
88 sub assemble_collapsing_parser {
91 my ($top_node_key, $top_node_key_assembler);
93 if (scalar @{$args->{collapse_map}{-identifying_columns}}) {
94 $top_node_key = join ('', map
95 { "{'\xFF__IDVALPOS__${_}__\xFF'}" }
96 @{$args->{collapse_map}{-identifying_columns}}
99 elsif( my @variants = @{$args->{collapse_map}{-identifying_columns_variants}} ) {
101 my @path_parts = map { sprintf
102 "( ( defined '\xFF__VALPOS__%d__\xFF' ) && (join qq(\xFF), '', %s, '') )",
103 $_->[0], # checking just first is enough - one defined, all defined
104 ( join ', ', map { "'\xFF__VALPOS__${_}__\xFF'" } @$_ ),
107 my $virtual_column_idx = (scalar keys %{$args->{val_index}} ) + 1;
109 $top_node_key_assembler = sprintf '$cur_row_ids{%d} = (%s);',
111 "\n" . join( "\n or\n", @path_parts, qq{"\0\$rows_pos\0"} );
113 $top_node_key = sprintf '{$cur_row_ids{%d}}', $virtual_column_idx;
115 $args->{collapse_map} = {
116 %{$args->{collapse_map}},
117 -custom_node_key => $top_node_key,
122 die('Unexpected collapse map contents');
125 my ($data_assemblers, $stats) = __visit_infmap_collapse ($args);
127 my $list_of_idcols = join(', ', sort { $a <=> $b } keys %{ $stats->{idcols_seen} } );
129 my $parser_src = sprintf (<<'EOS', $list_of_idcols, $top_node_key, $top_node_key_assembler||'', $data_assemblers);
130 ### BEGIN LITERAL STRING EVAL
131 my ($rows_pos, $result_pos, $cur_row_data, %%cur_row_ids, @collapse_idx, $is_new_res) = (0,0);
133 # this loop is a bit arcane - the rationale is that the passed in
134 # $_[0] will either have only one row (->next) or will have all
135 # rows already pulled in (->all and/or unordered). Given that the
136 # result can be rather large - we reuse the same already allocated
137 # array, since the collapsed prefetch is smaller by definition.
138 # At the end we cut the leftovers away and move on.
139 while ($cur_row_data =
140 ( ( $rows_pos >= 0 and $_[0][$rows_pos++] ) or do { $rows_pos = -1; undef } )
142 ($_[1] and $_[1]->())
145 # due to left joins some of the ids may be NULL/undef, and
146 # won't play well when used as hash lookups
147 # we also need to differentiate NULLs on per-row/per-col basis
148 #(otherwise folding of optional 1:1s will be greatly confused
149 $cur_row_ids{$_} = defined $cur_row_data->[$_] ? $cur_row_data->[$_] : "\0NULL\xFF$rows_pos\xFF$_\0"
152 # maybe(!) cache the top node id calculation
155 $is_new_res = ! $collapse_idx[0]%2$s and (
156 $_[1] and $result_pos and (unshift @{$_[2]}, $cur_row_data) and last
162 $_[0][$result_pos++] = $collapse_idx[0]%2$s
166 splice @{$_[0]}, $result_pos; # truncate the passed in array for cases of collapsing ->all()
167 ### END LITERAL STRING EVAL
170 # !!! note - different var than the one above
171 # change the quoted placeholders to unquoted alias-references
172 $parser_src =~ s/ \' \xFF__VALPOS__(\d+)__\xFF \' /"\$cur_row_data->[$1]"/gex;
173 $parser_src =~ s/ \' \xFF__IDVALPOS__(\d+)__\xFF \' /"\$cur_row_ids{$1}"/gex;
179 # the collapsing nested structure recursor
180 sub __visit_infmap_collapse {
181 my $args = {%{ shift() }};
183 my $cur_node_idx = ${ $args->{-node_idx_counter} ||= \do { my $x = 0} }++;
185 my ($my_cols, $rel_cols);
186 for ( keys %{$args->{val_index}} ) {
187 if ($_ =~ /^ ([^\.]+) \. (.+) /x) {
188 $rel_cols->{$1}{$2} = $args->{val_index}{$_};
191 $my_cols->{$_} = $args->{val_index}{$_};
195 my $node_key = $args->{collapse_map}->{-custom_node_key} || join ('', map
196 { "{'\xFF__IDVALPOS__${_}__\xFF'}" }
197 @{$args->{collapse_map}->{-identifying_columns}}
200 my $me_struct = $my_cols
201 ? __visit_dump([{ map { $_ => "\xFF__VALPOS__$my_cols->{$_}__\xFF" } (keys %$my_cols) }])
204 my $node_idx_slot = sprintf '$collapse_idx[%d]%s', $cur_node_idx, $node_key;
206 my $parent_attach_slot = sprintf( '$collapse_idx[%d]%s[1]{%s}',
207 @{$args}{qw/-parent_node_idx -parent_node_key/},
208 perlstring($args->{-node_relname}),
209 ) if $args->{-node_relname};
212 if ($cur_node_idx == 0) {
213 push @src, sprintf( '%s ||= %s;',
218 elsif ($args->{collapse_map}->{-is_single}) {
219 push @src, sprintf ( '%s ||= %s%s;',
222 $me_struct ? " ||= $me_struct" : '',
226 push @src, sprintf('push @{%s}, %s%s unless %s;',
229 $me_struct ? " ||= $me_struct" : '',
235 #my $known_defined = { %{ $parent_info->{known_defined} || {} } };
236 #$known_defined->{$_}++ for @{$args->{collapse_map}->{-identifying_columns}};
238 for my $rel (sort keys %$rel_cols) {
240 # push @src, sprintf(
241 # '%s[1]{%s} ||= [];', $node_idx_slot, perlstring($rel)
242 # ) unless $args->{collapse_map}->{$rel}{-is_single};
244 ($src[$#src + 1], $stats->{$rel}) = __visit_infmap_collapse({ %$args,
245 val_index => $rel_cols->{$rel},
246 collapse_map => $args->{collapse_map}{$rel},
247 -parent_node_idx => $cur_node_idx,
248 -parent_node_key => $node_key,
249 -node_relname => $rel,
252 # FIXME SUBOPTIMAL DISABLEPRUNE - disabled to satisfy t/resultset/inflate_result_api.t
253 #if ($args->{collapse_map}->{$rel}{-is_optional} and my @null_checks = map
254 # { "(! defined '\xFF__IDVALPOS__${_}__\xFF')" }
255 # sort { $a <=> $b } grep
256 # { ! $known_defined->{$_} }
257 # @{$args->{collapse_map}->{$rel}{-identifying_columns}}
259 # $src[-1] = sprintf( '(%s) or %s',
260 # join (' || ', @null_checks ),
270 ( map { %{ $_->{idcols_seen} } } values %$stats ),
271 ( map { $_ => 1 } @{$args->{collapse_map}->{-identifying_columns}} ),
277 # keep our own DD object around so we don't have to fitz with quoting
280 # we actually will be producing functional perl code here,
281 # thus no second-guessing of what these globals might have
282 # been set to. DO NOT CHANGE!
283 ($dumper_obj ||= do {
284 require Data::Dumper;
285 Data::Dumper->new([])
295 ->Indent (0) # faster but harder to read, perhaps leave at 1 ?
296 })->Values ([$_[0]])->Dump;