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