Fix incorrect collapser source being generated due to unicode collapse points
[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 DBIx::Class::_Util 'perlstring';
9
10 use constant HAS_DOR => ( $] < 5.010 ? 0 : 1 );
11
12 use base 'Exporter';
13 our @EXPORT_OK = qw(
14   assemble_simple_parser
15   assemble_collapsing_parser
16 );
17
18 # working title - we are hoping to extract this eventually...
19 our $null_branch_class = 'DBIx::ResultParser::RelatedNullBranch';
20
21 sub assemble_simple_parser {
22   #my ($args) = @_;
23
24   # the non-collapsing assembler is easy
25   # FIXME SUBOPTIMAL there could be a yet faster way to do things here, but
26   # need to try an actual implementation and benchmark it:
27   #
28   # <timbunce_> First setup the nested data structure you want for each row
29   #   Then call bind_col() to alias the row fields into the right place in
30   #   the data structure, then to fetch the data do:
31   # push @rows, dclone($row_data_struct) while ($sth->fetchrow);
32   #
33   my $parser_src = sprintf('$_ = %s for @{$_[0]}', __visit_infmap_simple($_[0]) );
34
35   # change the quoted placeholders to unquoted alias-references
36   $parser_src =~ s/ \' \xFF__VALPOS__(\d+)__\xFF \' /"\$_->[$1]"/gex;
37
38   $parser_src = "  { use strict; use warnings FATAL => 'all';\n$parser_src\n  }";
39 }
40
41 # the simple non-collapsing nested structure recursor
42 sub __visit_infmap_simple {
43   my $args = shift;
44
45   my $my_cols = {};
46   my $rel_cols;
47   for (keys %{$args->{val_index}}) {
48     if ($_ =~ /^ ([^\.]+) \. (.+) /x) {
49       $rel_cols->{$1}{$2} = $args->{val_index}{$_};
50     }
51     else {
52       $my_cols->{$_} = $args->{val_index}{$_};
53     }
54   }
55
56   my @relperl;
57   for my $rel (sort keys %$rel_cols) {
58
59     my $rel_struct = __visit_infmap_simple({ %$args,
60       val_index => $rel_cols->{$rel},
61     });
62
63     if (keys %$my_cols) {
64
65       my $branch_null_checks = join ' && ', map
66         { "( ! defined '\xFF__VALPOS__${_}__\xFF' )" }
67         sort { $a <=> $b } values %{$rel_cols->{$rel}}
68       ;
69
70       if ($args->{prune_null_branches}) {
71         $rel_struct = sprintf ( '( (%s) ? undef : %s )',
72           $branch_null_checks,
73           $rel_struct,
74         );
75       }
76       else {
77         $rel_struct = sprintf ( '( (%s) ? bless( (%s), %s ) : %s )',
78           $branch_null_checks,
79           $rel_struct,
80           perlstring($null_branch_class),
81           $rel_struct,
82         );
83       }
84     }
85
86     push @relperl, sprintf '( %s => %s )',
87       perlstring($rel),
88       $rel_struct,
89     ;
90
91   }
92
93   my $me_struct;
94   $me_struct = __result_struct_to_source($my_cols) if keys %$my_cols;
95
96   if ($args->{hri_style}) {
97     $me_struct =~ s/^ \s* \{ | \} \s* $//gx
98       if $me_struct;
99
100     return sprintf '{ %s }', join (', ', $me_struct||(), @relperl);
101   }
102   else {
103     return sprintf '[%s]', join (',',
104       $me_struct || 'undef',
105       @relperl ? sprintf ('{ %s }', join (',', @relperl)) : (),
106     );
107   }
108 }
109
110 sub assemble_collapsing_parser {
111   my $args = shift;
112
113   # it may get unset further down
114   my $no_rowid_container = $args->{prune_null_branches};
115
116   my ($top_node_key, $top_node_key_assembler);
117
118   if (scalar @{$args->{collapse_map}{-identifying_columns}}) {
119     $top_node_key = join ('', map
120       { "{'\xFF__IDVALPOS__${_}__\xFF'}" }
121       @{$args->{collapse_map}{-identifying_columns}}
122     );
123   }
124   elsif( my @variants = @{$args->{collapse_map}{-identifying_columns_variants}} ) {
125
126     my @path_parts = map { sprintf
127       "( ( defined '\xFF__VALPOS__%d__\xFF' ) && (join qq(\xFF), '', %s, '') )",
128       $_->[0],  # checking just first is enough - one ID defined, all defined
129       ( join ', ', map { "'\xFF__VALPOS__${_}__\xFF'" } @$_ ),
130     } @variants;
131
132     my $virtual_column_idx = (scalar keys %{$args->{val_index}} ) + 1;
133
134     $top_node_key = "{'\xFF__IDVALPOS__${virtual_column_idx}__\xFF'}";
135
136     $top_node_key_assembler = sprintf "'\xFF__IDVALPOS__%d__\xFF' = (%s);",
137       $virtual_column_idx,
138       "\n" . join( "\n  or\n", @path_parts, qq{"\0\$rows_pos\0"} )
139     ;
140
141     $args->{collapse_map} = {
142       %{$args->{collapse_map}},
143       -custom_node_key => $top_node_key,
144     };
145
146     $no_rowid_container = 0;
147   }
148   else {
149     die('Unexpected collapse map contents');
150   }
151
152   my ($data_assemblers, $stats) = __visit_infmap_collapse ($args);
153
154   my @idcol_args = $no_rowid_container ? ('', '') : (
155     ', %cur_row_ids', # only declare the variable if we'll use it
156     join ("\n", map {
157       my $quoted_null_val = qq( "\0NULL\xFF\${rows_pos}\xFF${_}\0" );
158       qq(\$cur_row_ids{$_} = ) . (
159         # in case we prune - we will never hit these undefs
160         $args->{prune_null_branches} ? qq( \$cur_row_data->[$_]; )
161         : HAS_DOR                    ? qq( \$cur_row_data->[$_] // $quoted_null_val; )
162         :                              qq( defined(\$cur_row_data->[$_]) ? \$cur_row_data->[$_] : $quoted_null_val; )
163       )
164     } sort { $a <=> $b } keys %{ $stats->{idcols_seen} } ),
165   );
166
167   my $parser_src = sprintf (<<'EOS', @idcol_args, $top_node_key_assembler||'', $top_node_key, join( "\n", @{$data_assemblers||[]} ) );
168 ### BEGIN LITERAL STRING EVAL
169   my $rows_pos = 0;
170   my ($result_pos, @collapse_idx, $cur_row_data %1$s);
171
172   # this loop is a bit arcane - the rationale is that the passed in
173   # $_[0] will either have only one row (->next) or will have all
174   # rows already pulled in (->all and/or unordered). Given that the
175   # result can be rather large - we reuse the same already allocated
176   # array, since the collapsed prefetch is smaller by definition.
177   # At the end we cut the leftovers away and move on.
178   while ($cur_row_data = (
179     ( $rows_pos >= 0 and $_[0][$rows_pos++] )
180       or
181     ( $_[1] and $rows_pos = -1 and $_[1]->() )
182   ) ) {
183
184     # this code exists only when we are using a cur_row_ids
185     # furthermore the undef checks may or may not be there
186     # depending on whether we prune or not
187     #
188     # due to left joins some of the ids may be NULL/undef, and
189     # won't play well when used as hash lookups
190     # we also need to differentiate NULLs on per-row/per-col basis
191     # (otherwise folding of optional 1:1s will be greatly confused
192 %2$s
193
194     # in the case of an underdefined root - calculate the virtual id (otherwise no code at all)
195 %3$s
196
197     # if we were supplied a coderef - we are collapsing lazily (the set
198     # is ordered properly)
199     # as long as we have a result already and the next result is new we
200     # return the pre-read data and bail
201 $_[1] and $result_pos and ! $collapse_idx[0]%4$s and (unshift @{$_[2]}, $cur_row_data) and last;
202
203     # the rel assemblers
204 %5$s
205
206   }
207
208   $#{$_[0]} = $result_pos - 1; # truncate the passed in array to where we filled it with results
209 ### END LITERAL STRING EVAL
210 EOS
211
212   # !!! note - different var than the one above
213   # change the quoted placeholders to unquoted alias-references
214   $parser_src =~ s/ \' \xFF__VALPOS__(\d+)__\xFF \' /"\$cur_row_data->[$1]"/gex;
215   $parser_src =~ s/
216     \' \xFF__IDVALPOS__(\d+)__\xFF \'
217   /
218     $no_rowid_container ? "\$cur_row_data->[$1]" : "\$cur_row_ids{$1}"
219   /gex;
220
221   $parser_src = "  { use strict; use warnings FATAL => 'all';\n$parser_src\n  }";
222 }
223
224
225 # the collapsing nested structure recursor
226 sub __visit_infmap_collapse {
227   my $args = {%{ shift() }};
228
229   my $cur_node_idx = ${ $args->{-node_idx_counter} ||= \do { my $x = 0} }++;
230
231   my ($my_cols, $rel_cols) = {};
232   for ( keys %{$args->{val_index}} ) {
233     if ($_ =~ /^ ([^\.]+) \. (.+) /x) {
234       $rel_cols->{$1}{$2} = $args->{val_index}{$_};
235     }
236     else {
237       $my_cols->{$_} = $args->{val_index}{$_};
238     }
239   }
240
241
242   if ($args->{hri_style}) {
243     delete $my_cols->{$_} for grep { $rel_cols->{$_} } keys %$my_cols;
244   }
245
246   my $me_struct;
247   $me_struct = __result_struct_to_source($my_cols) if keys %$my_cols;
248
249   $me_struct = sprintf( '[ %s ]', $me_struct||'' )
250     unless $args->{hri_style};
251
252
253   my $node_key = $args->{collapse_map}->{-custom_node_key} || join ('', map
254     { "{'\xFF__IDVALPOS__${_}__\xFF'}" }
255     @{$args->{collapse_map}->{-identifying_columns}}
256   );
257   my $node_idx_slot = sprintf '$collapse_idx[%d]%s', $cur_node_idx, $node_key;
258
259
260   my @src;
261
262   if ($cur_node_idx == 0) {
263     push @src, sprintf( '%s %s $_[0][$result_pos++] = %s;',
264       $node_idx_slot,
265       (HAS_DOR ? '//=' : '||='),
266       $me_struct || '{}',
267     );
268   }
269   else {
270     my $parent_attach_slot = sprintf( '$collapse_idx[%d]%s%s{%s}',
271       @{$args}{qw/-parent_node_idx -parent_node_key/},
272       $args->{hri_style} ? '' : '[1]',
273       perlstring($args->{-node_rel_name}),
274     );
275
276     if ($args->{collapse_map}->{-is_single}) {
277       push @src, sprintf ( '%s %s %s%s;',
278         $parent_attach_slot,
279         (HAS_DOR ? '//=' : '||='),
280         $node_idx_slot,
281         $me_struct ? " = $me_struct" : '',
282       );
283     }
284     else {
285       push @src, sprintf('(! %s) and push @{%s}, %s%s;',
286         $node_idx_slot,
287         $parent_attach_slot,
288         $node_idx_slot,
289         $me_struct ? " = $me_struct" : '',
290       );
291     }
292   }
293
294   my $known_present_ids = { map { $_ => 1 } @{$args->{collapse_map}{-identifying_columns}} };
295   my ($stats, $rel_src);
296
297   for my $rel (sort keys %$rel_cols) {
298
299     my $relinfo = $args->{collapse_map}{$rel};
300
301     ($rel_src, $stats->{$rel}) = __visit_infmap_collapse({ %$args,
302       val_index => $rel_cols->{$rel},
303       collapse_map => $relinfo,
304       -parent_node_idx => $cur_node_idx,
305       -parent_node_key => $node_key,
306       -node_rel_name => $rel,
307     });
308
309     my $rel_src_pos = $#src + 1;
310     push @src, @$rel_src;
311
312     if (
313       $relinfo->{-is_optional}
314         and
315       defined ( my $first_distinct_child_idcol = first
316         { ! $known_present_ids->{$_} }
317         @{$relinfo->{-identifying_columns}}
318       )
319     ) {
320
321       if ($args->{prune_null_branches}) {
322
323         # start of wrap of the entire chain in a conditional
324         splice @src, $rel_src_pos, 0, sprintf "( ! defined %s )\n  ? %s%s{%s} = %s\n  : do {",
325           "'\xFF__VALPOS__${first_distinct_child_idcol}__\xFF'",
326           $node_idx_slot,
327           $args->{hri_style} ? '' : '[1]',
328           perlstring($rel),
329           ($args->{hri_style} && $relinfo->{-is_single}) ? 'undef' : '[]'
330         ;
331
332         # end of wrap
333         push @src, '};'
334       }
335       else {
336
337         splice @src, $rel_src_pos + 1, 0, sprintf ( '(defined %s) or bless (%s[1]{%s}, %s);',
338           "'\xFF__VALPOS__${first_distinct_child_idcol}__\xFF'",
339           $node_idx_slot,
340           perlstring($rel),
341           perlstring($null_branch_class),
342         );
343       }
344     }
345   }
346
347   return (
348     \@src,
349     {
350       idcols_seen => {
351         ( map { %{ $_->{idcols_seen} } } values %$stats ),
352         ( map { $_ => 1 } @{$args->{collapse_map}->{-identifying_columns}} ),
353       }
354     }
355   );
356 }
357
358 sub __result_struct_to_source {
359   sprintf( '{ %s }', join (', ', map
360     { sprintf "%s => '\xFF__VALPOS__%d__\xFF'", perlstring($_), $_[0]{$_} }
361     sort keys %{$_[0]}
362   ));
363 }
364
365 1;