Reintroduce conditional null-branch pruning and add direct-to-HRI option
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultSource / RowParser / Util.pm
1 package # hide from the pauses
2   DBIx::Class::ResultSource::RowParser::Util;
3
4 use strict;
5 use warnings;
6
7 use List::Util 'first';
8 use B 'perlstring';
9
10 use base 'Exporter';
11 our @EXPORT_OK = qw(
12   assemble_simple_parser
13   assemble_collapsing_parser
14 );
15
16 sub assemble_simple_parser {
17   #my ($args) = @_;
18
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:
22   #
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);
27   #
28   my $parser_src = sprintf('$_ = %s for @{$_[0]}', __visit_infmap_simple($_[0]) );
29
30   # change the quoted placeholders to unquoted alias-references
31   $parser_src =~ s/ \' \xFF__VALPOS__(\d+)__\xFF \' /"\$_->[$1]"/gex;
32
33   return $parser_src;
34 }
35
36 # the simple non-collapsing nested structure recursor
37 sub __visit_infmap_simple {
38   my $args = shift;
39
40   my $my_cols = {};
41   my $rel_cols;
42   for (keys %{$args->{val_index}}) {
43     if ($_ =~ /^ ([^\.]+) \. (.+) /x) {
44       $rel_cols->{$1}{$2} = $args->{val_index}{$_};
45     }
46     else {
47       $my_cols->{$_} = $args->{val_index}{$_};
48     }
49   }
50
51   my @relperl;
52   for my $rel (sort keys %$rel_cols) {
53
54     push @relperl, join ' => ', perlstring($rel), __visit_infmap_simple({ %$args,
55       val_index => $rel_cols->{$rel},
56     });
57
58     if ($args->{prune_null_branches} and keys %$my_cols) {
59
60       my @branch_null_checks = map
61         { "( ! defined '\xFF__VALPOS__${_}__\xFF' )" }
62         sort { $a <=> $b } values %{$rel_cols->{$rel}}
63       ;
64
65       $relperl[-1] = sprintf ( '(%s) ? ( %s => %s ) : ( %s )',
66         join (' && ', @branch_null_checks ),
67         perlstring($rel),
68         $args->{hri_style} ? 'undef' : '[]',
69         $relperl[-1],
70       );
71     }
72   }
73
74   my $me_struct;
75   $me_struct = __visit_dump({ map { $_ => "\xFF__VALPOS__$my_cols->{$_}__\xFF" } (keys %$my_cols) })
76     if keys %$my_cols;
77
78   if ($args->{hri_style}) {
79     $me_struct =~ s/^ \s* \{ | \} \s* $//gx
80       if $me_struct;
81
82     return sprintf '{ %s }', join (', ', $me_struct||(), @relperl);
83   }
84   else {
85     return sprintf '[%s]', join (',',
86       $me_struct || 'undef',
87       @relperl ? sprintf ('{ %s }', join (',', @relperl)) : (),
88     );
89   }
90 }
91
92 sub assemble_collapsing_parser {
93   my $args = shift;
94
95   my ($top_node_key, $top_node_key_assembler);
96
97   if (scalar @{$args->{collapse_map}{-identifying_columns}}) {
98     $top_node_key = join ('', map
99       { "{'\xFF__IDVALPOS__${_}__\xFF'}" }
100       @{$args->{collapse_map}{-identifying_columns}}
101     );
102   }
103   elsif( my @variants = @{$args->{collapse_map}{-identifying_columns_variants}} ) {
104
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'" } @$_ ),
109     } @variants;
110
111     my $virtual_column_idx = (scalar keys %{$args->{val_index}} ) + 1;
112
113     $top_node_key_assembler = sprintf '$cur_row_ids{%d} = (%s);',
114       $virtual_column_idx,
115       "\n" . join( "\n  or\n", @path_parts, qq{"\0\$rows_pos\0"} );
116
117     $top_node_key = sprintf '{$cur_row_ids{%d}}', $virtual_column_idx;
118
119     $args->{collapse_map} = {
120       %{$args->{collapse_map}},
121       -custom_node_key => $top_node_key,
122     };
123
124   }
125   else {
126     die('Unexpected collapse map contents');
127   }
128
129   my ($data_assemblers, $stats) = __visit_infmap_collapse ($args);
130
131   my $list_of_idcols = join(', ', sort { $a <=> $b } keys %{ $stats->{idcols_seen} } );
132
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 } )
144       ||
145     ($_[1] and $_[1]->())
146   ) {
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"
152       for (%1$s);
153
154     # maybe(!) cache the top node id calculation
155     %3$s
156
157     $is_new_res = ! $collapse_idx[0]%2$s and (
158       $_[1] and $result_pos and (unshift @{$_[2]}, $cur_row_data) and last
159     );
160
161     # the rel assemblers
162     %4$s
163
164     $_[0][$result_pos++] = $collapse_idx[0]%2$s
165       if $is_new_res;
166   }
167
168   splice @{$_[0]}, $result_pos; # truncate the passed in array for cases of collapsing ->all()
169 ### END LITERAL STRING EVAL
170 EOS
171
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;
176
177   $parser_src;
178 }
179
180
181 # the collapsing nested structure recursor
182 sub __visit_infmap_collapse {
183   my $args = {%{ shift() }};
184
185   my $cur_node_idx = ${ $args->{-node_idx_counter} ||= \do { my $x = 0} }++;
186
187   my ($my_cols, $rel_cols) = {};
188   for ( keys %{$args->{val_index}} ) {
189     if ($_ =~ /^ ([^\.]+) \. (.+) /x) {
190       $rel_cols->{$1}{$2} = $args->{val_index}{$_};
191     }
192     else {
193       $my_cols->{$_} = $args->{val_index}{$_};
194     }
195   }
196
197
198   my $node_key = $args->{collapse_map}->{-custom_node_key} || join ('', map
199     { "{'\xFF__IDVALPOS__${_}__\xFF'}" }
200     @{$args->{collapse_map}->{-identifying_columns}}
201   );
202
203   my $me_struct;
204
205   if ($args->{hri_style}) {
206     delete $my_cols->{$_} for grep { $rel_cols->{$_} } keys %$my_cols;
207   }
208
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};
212   }
213
214   my $node_idx_slot = sprintf '$collapse_idx[%d]%s', $cur_node_idx, $node_key;
215
216   my @src;
217
218   if ($cur_node_idx == 0) {
219     push @src, sprintf( '%s ||= %s;',
220       $node_idx_slot,
221       $me_struct,
222     ) if $me_struct;
223   }
224   else {
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}),
229     );
230
231     if ($args->{collapse_map}->{-is_single}) {
232       push @src, sprintf ( '%s ||= %s%s;',
233         $parent_attach_slot,
234         $node_idx_slot,
235         $me_struct ? " ||= $me_struct" : '',
236       );
237     }
238     else {
239       push @src, sprintf('(! %s) and push @{%s}, %s%s;',
240         $node_idx_slot,
241         $parent_attach_slot,
242         $node_idx_slot,
243         $me_struct ? " = $me_struct" : '',
244       );
245     }
246   }
247
248   my $known_present_ids = { map { $_ => 1 } @{$args->{collapse_map}{-identifying_columns}} };
249   my ($stats, $rel_src);
250
251   for my $rel (sort keys %$rel_cols) {
252
253     my $relinfo = $args->{collapse_map}{$rel};
254     if ($args->{collapse_map}{-is_optional}) {
255       $relinfo = { %$relinfo, -is_optional => 1 };
256     }
257
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,
264     });
265
266     my $rel_src_pos = $#src + 1;
267     push @src, @$rel_src;
268
269     if (
270       $args->{prune_null_branches}
271         and
272       $relinfo->{-is_optional}
273         and
274       defined ( my $first_distinct_child_idcol = first
275         { ! $known_present_ids->{$_} }
276         @{$relinfo->{-identifying_columns}}
277       )
278     ) {
279
280       $src[$rel_src_pos] = sprintf( '%s and %s',
281         "( defined '\xFF__VALPOS__${first_distinct_child_idcol}__\xFF' )",
282         $src[$rel_src_pos],
283       );
284
285       splice @src, $rel_src_pos + 1, 0, sprintf ( '%s%s{%s} ||= %s;',
286         $node_idx_slot,
287         $args->{hri_style} ? '' : '[1]',
288         perlstring($rel),
289         $args->{hri_style} && $relinfo->{-is_single} ? 'undef' : '[]',
290       );
291     }
292   }
293
294   return (
295     \@src,
296     {
297       idcols_seen => {
298         ( map { %{ $_->{idcols_seen} } } values %$stats ),
299         ( map { $_ => 1 } @{$args->{collapse_map}->{-identifying_columns}} ),
300       }
301     }
302   );
303 }
304
305 # keep our own DD object around so we don't have to fitz with quoting
306 my $dumper_obj;
307 sub __visit_dump {
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([])
314       ->Useperl (0)
315       ->Purity (1)
316       ->Pad ('')
317       ->Useqq (0)
318       ->Terse (1)
319       ->Quotekeys (1)
320       ->Deepcopy (0)
321       ->Deparse (0)
322       ->Maxdepth (0)
323       ->Indent (0)  # faster but harder to read, perhaps leave at 1 ?
324   })->Values ([$_[0]])->Dump;
325 }
326
327 1;