4d833d36f5eaa06c7edd098288aba5e0f2ee6ec1
[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 # working title - we are hoping to extract this eventually...
17 our $null_branch_class = 'DBIx::ResultParser::RelatedNullBranch';
18
19 sub assemble_simple_parser {
20   #my ($args) = @_;
21
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:
25   #
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);
30   #
31   my $parser_src = sprintf('$_ = %s for @{$_[0]}', __visit_infmap_simple($_[0]) );
32
33   # change the quoted placeholders to unquoted alias-references
34   $parser_src =~ s/ \' \xFF__VALPOS__(\d+)__\xFF \' /"\$_->[$1]"/gex;
35
36   $parser_src = "  { use strict; use warnings FATAL => 'all';\n$parser_src\n  }";
37 }
38
39 # the simple non-collapsing nested structure recursor
40 sub __visit_infmap_simple {
41   my $args = shift;
42
43   my $my_cols = {};
44   my $rel_cols;
45   for (keys %{$args->{val_index}}) {
46     if ($_ =~ /^ ([^\.]+) \. (.+) /x) {
47       $rel_cols->{$1}{$2} = $args->{val_index}{$_};
48     }
49     else {
50       $my_cols->{$_} = $args->{val_index}{$_};
51     }
52   }
53
54   my @relperl;
55   for my $rel (sort keys %$rel_cols) {
56
57     my $rel_struct = __visit_infmap_simple({ %$args,
58       val_index => $rel_cols->{$rel},
59     });
60
61     if (keys %$my_cols) {
62
63       my $branch_null_checks = join ' && ', map
64         { "( ! defined '\xFF__VALPOS__${_}__\xFF' )" }
65         sort { $a <=> $b } values %{$rel_cols->{$rel}}
66       ;
67
68       if ($args->{hri_style}) {
69         $rel_struct = sprintf ( '( (%s) ? undef : %s )',
70           $branch_null_checks,
71           $rel_struct,
72         );
73       }
74       else {
75         $rel_struct = sprintf ( '( (%s) ? bless( (%s), %s ) : %s )',
76           $branch_null_checks,
77           $rel_struct,
78           perlstring($null_branch_class),
79           $rel_struct,
80         );
81       }
82     }
83
84     push @relperl, sprintf '( %s => %s )',
85       perlstring($rel),
86       $rel_struct,
87     ;
88
89   }
90
91   my $me_struct;
92   $me_struct = __visit_dump({ map { $_ => "\xFF__VALPOS__$my_cols->{$_}__\xFF" } (keys %$my_cols) })
93     if keys %$my_cols;
94
95   if ($args->{hri_style}) {
96     $me_struct =~ s/^ \s* \{ | \} \s* $//gx
97       if $me_struct;
98
99     return sprintf '{ %s }', join (', ', $me_struct||(), @relperl);
100   }
101   else {
102     return sprintf '[%s]', join (',',
103       $me_struct || 'undef',
104       @relperl ? sprintf ('{ %s }', join (',', @relperl)) : (),
105     );
106   }
107 }
108
109 sub assemble_collapsing_parser {
110   my $args = shift;
111
112   my ($top_node_key, $top_node_key_assembler);
113
114   if (scalar @{$args->{collapse_map}{-identifying_columns}}) {
115     $top_node_key = join ('', map
116       { "{'\xFF__IDVALPOS__${_}__\xFF'}" }
117       @{$args->{collapse_map}{-identifying_columns}}
118     );
119   }
120   elsif( my @variants = @{$args->{collapse_map}{-identifying_columns_variants}} ) {
121
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'" } @$_ ),
126     } @variants;
127
128     my $virtual_column_idx = (scalar keys %{$args->{val_index}} ) + 1;
129
130     $top_node_key = "{'\xFF__IDVALPOS__${virtual_column_idx}__\xFF'}";
131
132     $top_node_key_assembler = sprintf "'\xFF__IDVALPOS__%d__\xFF' = (%s);",
133       $virtual_column_idx,
134       "\n" . join( "\n  or\n", @path_parts, qq{"\0\$rows_pos\0"} )
135     ;
136
137     $args->{collapse_map} = {
138       %{$args->{collapse_map}},
139       -custom_node_key => $top_node_key,
140     };
141
142   }
143   else {
144     die('Unexpected collapse map contents');
145   }
146
147   my ($data_assemblers, $stats) = __visit_infmap_collapse ($args);
148
149   my @idcol_args = $args->{hri_style} ? ('', '') : (
150     '%cur_row_ids, ', # only declare the variable if we'll use it
151
152     sprintf( <<'EOS', join ', ', sort { $a <=> $b } keys %{ $stats->{idcols_seen} } ),
153   $cur_row_ids{$_} = defined($cur_row_data->[$_]) ? $cur_row_data->[$_] : "\0NULL\xFF$rows_pos\xFF$_\0"
154     for (%s);
155 EOS
156   );
157
158   my $parser_src = sprintf (<<'EOS', @idcol_args, $top_node_key_assembler||'', $top_node_key, join( "\n", @{$data_assemblers||[]} ) );
159 ### BEGIN LITERAL STRING EVAL
160   my ($rows_pos, $result_pos, $cur_row_data,%1$s @collapse_idx, $is_new_res) = (0,0);
161   # this loop is a bit arcane - the rationale is that the passed in
162   # $_[0] will either have only one row (->next) or will have all
163   # rows already pulled in (->all and/or unordered). Given that the
164   # result can be rather large - we reuse the same already allocated
165   # array, since the collapsed prefetch is smaller by definition.
166   # At the end we cut the leftovers away and move on.
167   while ($cur_row_data =
168     ( ( $rows_pos >= 0 and $_[0][$rows_pos++] ) or do { $rows_pos = -1; undef } )
169       ||
170     ($_[1] and $_[1]->())
171   ) {
172     # this code exists only when we are *not* assembling direct to HRI
173     #
174     # due to left joins some of the ids may be NULL/undef, and
175     # won't play well when used as hash lookups
176     # we also need to differentiate NULLs on per-row/per-col basis
177     # (otherwise folding of optional 1:1s will be greatly confused
178     %2$s
179
180     # in the case of an underdefined root - calculate the virtual id (otherwise no code at all)
181     %3$s
182
183     $is_new_res = ! $collapse_idx[0]%4$s and (
184       $_[1] and $result_pos and (unshift @{$_[2]}, $cur_row_data) and last
185     );
186
187     # the rel assemblers
188 %5$s
189
190     $_[0][$result_pos++] = $collapse_idx[0]%4$s
191       if $is_new_res;
192   }
193
194   splice @{$_[0]}, $result_pos; # truncate the passed in array for cases of collapsing ->all()
195 ### END LITERAL STRING EVAL
196 EOS
197
198   # !!! note - different var than the one above
199   # change the quoted placeholders to unquoted alias-references
200   $parser_src =~ s/ \' \xFF__VALPOS__(\d+)__\xFF \' /"\$cur_row_data->[$1]"/gex;
201   $parser_src =~ s/ \' \xFF__IDVALPOS__(\d+)__\xFF \' /$args->{hri_style} ? "\$cur_row_data->[$1]" : "\$cur_row_ids{$1}" /gex;
202
203   $parser_src = "  { use strict; use warnings FATAL => 'all';\n$parser_src\n  }";
204 }
205
206
207 # the collapsing nested structure recursor
208 sub __visit_infmap_collapse {
209   my $args = {%{ shift() }};
210
211   my $cur_node_idx = ${ $args->{-node_idx_counter} ||= \do { my $x = 0} }++;
212
213   my ($my_cols, $rel_cols) = {};
214   for ( keys %{$args->{val_index}} ) {
215     if ($_ =~ /^ ([^\.]+) \. (.+) /x) {
216       $rel_cols->{$1}{$2} = $args->{val_index}{$_};
217     }
218     else {
219       $my_cols->{$_} = $args->{val_index}{$_};
220     }
221   }
222
223
224   if ($args->{hri_style}) {
225     delete $my_cols->{$_} for grep { $rel_cols->{$_} } keys %$my_cols;
226   }
227
228   my $me_struct;
229   $me_struct = __visit_dump({ map { $_ => "\xFF__VALPOS__$my_cols->{$_}__\xFF" } (keys %$my_cols) })
230     if keys %$my_cols;
231
232   $me_struct = sprintf( '[ %s ]', $me_struct||'' )
233     unless $args->{hri_style};
234
235
236   my $node_key = $args->{collapse_map}->{-custom_node_key} || join ('', map
237     { "{'\xFF__IDVALPOS__${_}__\xFF'}" }
238     @{$args->{collapse_map}->{-identifying_columns}}
239   );
240   my $node_idx_slot = sprintf '$collapse_idx[%d]%s', $cur_node_idx, $node_key;
241
242
243   my @src;
244
245   if ($cur_node_idx == 0) {
246     push @src, sprintf( '%s ||= %s;',
247       $node_idx_slot,
248       $me_struct,
249     ) if $me_struct;
250   }
251   else {
252     my $parent_attach_slot = sprintf( '$collapse_idx[%d]%s%s{%s}',
253       @{$args}{qw/-parent_node_idx -parent_node_key/},
254       $args->{hri_style} ? '' : '[1]',
255       perlstring($args->{-node_relname}),
256     );
257
258     if ($args->{collapse_map}->{-is_single}) {
259       push @src, sprintf ( '%s ||= %s%s;',
260         $parent_attach_slot,
261         $node_idx_slot,
262         $me_struct ? " ||= $me_struct" : '',
263       );
264     }
265     else {
266       push @src, sprintf('(! %s) and push @{%s}, %s%s;',
267         $node_idx_slot,
268         $parent_attach_slot,
269         $node_idx_slot,
270         $me_struct ? " = $me_struct" : '',
271       );
272     }
273   }
274
275   my $known_present_ids = { map { $_ => 1 } @{$args->{collapse_map}{-identifying_columns}} };
276   my ($stats, $rel_src);
277
278   for my $rel (sort keys %$rel_cols) {
279
280     my $relinfo = $args->{collapse_map}{$rel};
281
282     ($rel_src, $stats->{$rel}) = __visit_infmap_collapse({ %$args,
283       val_index => $rel_cols->{$rel},
284       collapse_map => $relinfo,
285       -parent_node_idx => $cur_node_idx,
286       -parent_node_key => $node_key,
287       -node_relname => $rel,
288     });
289
290     my $rel_src_pos = $#src + 1;
291     push @src, @$rel_src;
292
293     if (
294       $relinfo->{-is_optional}
295         and
296       defined ( my $first_distinct_child_idcol = first
297         { ! $known_present_ids->{$_} }
298         @{$relinfo->{-identifying_columns}}
299       )
300     ) {
301
302       if ($args->{hri_style}) {
303
304         # start of wrap of the entire chain in a conditional
305         splice @src, $rel_src_pos, 0, sprintf "( ! defined %s )\n  ? %s{%s} = %s\n  : do {",
306           "'\xFF__VALPOS__${first_distinct_child_idcol}__\xFF'",
307           $node_idx_slot,
308           perlstring($rel),
309           $relinfo->{-is_single} ? 'undef' : '[]'
310         ;
311
312         # end of wrap
313         push @src, '};'
314       }
315       else {
316
317         splice @src, $rel_src_pos + 1, 0, sprintf ( '(defined %s) or bless (%s[1]{%s}, %s);',
318           "'\xFF__VALPOS__${first_distinct_child_idcol}__\xFF'",
319           $node_idx_slot,
320           perlstring($rel),
321           perlstring($null_branch_class),
322         );
323       }
324     }
325   }
326
327   return (
328     \@src,
329     {
330       idcols_seen => {
331         ( map { %{ $_->{idcols_seen} } } values %$stats ),
332         ( map { $_ => 1 } @{$args->{collapse_map}->{-identifying_columns}} ),
333       }
334     }
335   );
336 }
337
338 # keep our own DD object around so we don't have to fitz with quoting
339 my $dumper_obj;
340 sub __visit_dump {
341
342   # we actually will be producing functional perl code here,
343   # thus no second-guessing of what these globals might have
344   # been set to. DO NOT CHANGE!
345   ($dumper_obj ||= do {
346     require Data::Dumper;
347     Data::Dumper->new([])
348       ->Useperl (0)
349       ->Purity (1)
350       ->Pad ('')
351       ->Useqq (0)
352       ->Terse (1)
353       ->Quotekeys (1)
354       ->Deepcopy (0)
355       ->Deparse (0)
356       ->Maxdepth (0)
357       ->Indent (0)  # faster but harder to read, perhaps leave at 1 ?
358   })->Values ([$_[0]])->Dump;
359 }
360
361 1;