Fix incorrect collapser source being generated due to unicode collapse points
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultSource / RowParser / Util.pm
CommitLineData
9f98c4b2 1package # hide from the pauses
2 DBIx::Class::ResultSource::RowParser::Util;
3
4use strict;
5use warnings;
6
ce556881 7use List::Util 'first';
01b25f12 8use DBIx::Class::_Util 'perlstring';
9f98c4b2 9
cd784aab 10use constant HAS_DOR => ( $] < 5.010 ? 0 : 1 );
11
9f98c4b2 12use base 'Exporter';
13our @EXPORT_OK = qw(
14 assemble_simple_parser
15 assemble_collapsing_parser
16);
17
52864fbd 18# working title - we are hoping to extract this eventually...
19our $null_branch_class = 'DBIx::ResultParser::RelatedNullBranch';
20
9f98c4b2 21sub 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
bdbd2ae8 38 $parser_src = " { use strict; use warnings FATAL => 'all';\n$parser_src\n }";
9f98c4b2 39}
40
41# the simple non-collapsing nested structure recursor
42sub __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
52864fbd 59 my $rel_struct = __visit_infmap_simple({ %$args,
9f98c4b2 60 val_index => $rel_cols->{$rel},
9f98c4b2 61 });
62
52864fbd 63 if (keys %$my_cols) {
ce556881 64
52864fbd 65 my $branch_null_checks = join ' && ', map
ce556881 66 { "( ! defined '\xFF__VALPOS__${_}__\xFF' )" }
67 sort { $a <=> $b } values %{$rel_cols->{$rel}}
68 ;
69
79adc44f 70 if ($args->{prune_null_branches}) {
52864fbd 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 }
ce556881 84 }
52864fbd 85
86 push @relperl, sprintf '( %s => %s )',
87 perlstring($rel),
88 $rel_struct,
89 ;
90
9f98c4b2 91 }
92
ce556881 93 my $me_struct;
a8f62ee0 94 $me_struct = __result_struct_to_source($my_cols) if keys %$my_cols;
9f98c4b2 95
ce556881 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 }
9f98c4b2 108}
109
110sub assemble_collapsing_parser {
111 my $args = shift;
112
06b3406d 113 # it may get unset further down
79adc44f 114 my $no_rowid_container = $args->{prune_null_branches};
06b3406d 115
9f98c4b2 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, '') )",
ce556881 128 $_->[0], # checking just first is enough - one ID defined, all defined
9f98c4b2 129 ( join ', ', map { "'\xFF__VALPOS__${_}__\xFF'" } @$_ ),
130 } @variants;
131
132 my $virtual_column_idx = (scalar keys %{$args->{val_index}} ) + 1;
133
7596ddca 134 $top_node_key = "{'\xFF__IDVALPOS__${virtual_column_idx}__\xFF'}";
9f98c4b2 135
7596ddca 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 ;
9f98c4b2 140
141 $args->{collapse_map} = {
142 %{$args->{collapse_map}},
143 -custom_node_key => $top_node_key,
144 };
145
06b3406d 146 $no_rowid_container = 0;
9f98c4b2 147 }
148 else {
149 die('Unexpected collapse map contents');
150 }
151
152 my ($data_assemblers, $stats) = __visit_infmap_collapse ($args);
153
06b3406d 154 my @idcol_args = $no_rowid_container ? ('', '') : (
aa1d8a87 155 ', %cur_row_ids', # only declare the variable if we'll use it
2fdeef65 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} } ),
7596ddca 165 );
9f98c4b2 166
7596ddca 167 my $parser_src = sprintf (<<'EOS', @idcol_args, $top_node_key_assembler||'', $top_node_key, join( "\n", @{$data_assemblers||[]} ) );
9f98c4b2 168### BEGIN LITERAL STRING EVAL
aa1d8a87 169 my $rows_pos = 0;
170 my ($result_pos, @collapse_idx, $cur_row_data %1$s);
171
9f98c4b2 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.
3b4cd124 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
5e6d06f4 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
7596ddca 187 #
9f98c4b2 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
ce556881 191 # (otherwise folding of optional 1:1s will be greatly confused
3b4cd124 192%2$s
9f98c4b2 193
7596ddca 194 # in the case of an underdefined root - calculate the virtual id (otherwise no code at all)
3b4cd124 195%3$s
9f98c4b2 196
aa1d8a87 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
3b4cd124 201$_[1] and $result_pos and ! $collapse_idx[0]%4$s and (unshift @{$_[2]}, $cur_row_data) and last;
9f98c4b2 202
203 # the rel assemblers
7596ddca 204%5$s
9f98c4b2 205
9f98c4b2 206 }
207
aa1d8a87 208 $#{$_[0]} = $result_pos - 1; # truncate the passed in array to where we filled it with results
9f98c4b2 209### END LITERAL STRING EVAL
210EOS
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;
06b3406d 215 $parser_src =~ s/
216 \' \xFF__IDVALPOS__(\d+)__\xFF \'
217 /
218 $no_rowid_container ? "\$cur_row_data->[$1]" : "\$cur_row_ids{$1}"
219 /gex;
9f98c4b2 220
bdbd2ae8 221 $parser_src = " { use strict; use warnings FATAL => 'all';\n$parser_src\n }";
9f98c4b2 222}
223
224
225# the collapsing nested structure recursor
226sub __visit_infmap_collapse {
227 my $args = {%{ shift() }};
228
229 my $cur_node_idx = ${ $args->{-node_idx_counter} ||= \do { my $x = 0} }++;
230
ce556881 231 my ($my_cols, $rel_cols) = {};
9f98c4b2 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
ce556881 241
ce556881 242 if ($args->{hri_style}) {
243 delete $my_cols->{$_} for grep { $rel_cols->{$_} } keys %$my_cols;
244 }
9f98c4b2 245
52864fbd 246 my $me_struct;
a8f62ee0 247 $me_struct = __result_struct_to_source($my_cols) if keys %$my_cols;
ce556881 248
52864fbd 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 );
ce556881 257 my $node_idx_slot = sprintf '$collapse_idx[%d]%s', $cur_node_idx, $node_key;
9f98c4b2 258
52864fbd 259
9f98c4b2 260 my @src;
ce556881 261
9f98c4b2 262 if ($cur_node_idx == 0) {
cd784aab 263 push @src, sprintf( '%s %s $_[0][$result_pos++] = %s;',
9f98c4b2 264 $node_idx_slot,
cd784aab 265 (HAS_DOR ? '//=' : '||='),
aa1d8a87 266 $me_struct || '{}',
267 );
9f98c4b2 268 }
9f98c4b2 269 else {
ce556881 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]',
a5f5e470 273 perlstring($args->{-node_rel_name}),
9f98c4b2 274 );
ce556881 275
276 if ($args->{collapse_map}->{-is_single}) {
cd784aab 277 push @src, sprintf ( '%s %s %s%s;',
ce556881 278 $parent_attach_slot,
cd784aab 279 (HAS_DOR ? '//=' : '||='),
ce556881 280 $node_idx_slot,
aa1d8a87 281 $me_struct ? " = $me_struct" : '',
ce556881 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 }
9f98c4b2 292 }
293
ce556881 294 my $known_present_ids = { map { $_ => 1 } @{$args->{collapse_map}{-identifying_columns}} };
295 my ($stats, $rel_src);
296
9f98c4b2 297 for my $rel (sort keys %$rel_cols) {
298
ce556881 299 my $relinfo = $args->{collapse_map}{$rel};
9f98c4b2 300
ce556881 301 ($rel_src, $stats->{$rel}) = __visit_infmap_collapse({ %$args,
9f98c4b2 302 val_index => $rel_cols->{$rel},
ce556881 303 collapse_map => $relinfo,
9f98c4b2 304 -parent_node_idx => $cur_node_idx,
305 -parent_node_key => $node_key,
a5f5e470 306 -node_rel_name => $rel,
9f98c4b2 307 });
308
ce556881 309 my $rel_src_pos = $#src + 1;
310 push @src, @$rel_src;
311
312 if (
ce556881 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
79adc44f 321 if ($args->{prune_null_branches}) {
ce556881 322
7596ddca 323 # start of wrap of the entire chain in a conditional
79adc44f 324 splice @src, $rel_src_pos, 0, sprintf "( ! defined %s )\n ? %s%s{%s} = %s\n : do {",
7596ddca 325 "'\xFF__VALPOS__${first_distinct_child_idcol}__\xFF'",
52864fbd 326 $node_idx_slot,
79adc44f 327 $args->{hri_style} ? '' : '[1]',
52864fbd 328 perlstring($rel),
79adc44f 329 ($args->{hri_style} && $relinfo->{-is_single}) ? 'undef' : '[]'
7596ddca 330 ;
331
332 # end of wrap
333 push @src, '};'
52864fbd 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 }
ce556881 344 }
9f98c4b2 345 }
346
347 return (
ce556881 348 \@src,
9f98c4b2 349 {
350 idcols_seen => {
351 ( map { %{ $_->{idcols_seen} } } values %$stats ),
352 ( map { $_ => 1 } @{$args->{collapse_map}->{-identifying_columns}} ),
353 }
354 }
355 );
356}
357
a8f62ee0 358sub __result_struct_to_source {
359 sprintf( '{ %s }', join (', ', map
360 { sprintf "%s => '\xFF__VALPOS__%d__\xFF'", perlstring($_), $_[0]{$_} }
361 sort keys %{$_[0]}
362 ));
9f98c4b2 363}
364
3651;