Do not rely on Data::Dumper to produce rolled out hashrefs
[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';
9f98c4b2 8use B 'perlstring';
9
10use base 'Exporter';
11our @EXPORT_OK = qw(
12 assemble_simple_parser
13 assemble_collapsing_parser
14);
15
52864fbd 16# working title - we are hoping to extract this eventually...
17our $null_branch_class = 'DBIx::ResultParser::RelatedNullBranch';
18
9f98c4b2 19sub 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
bdbd2ae8 36 $parser_src = " { use strict; use warnings FATAL => 'all';\n$parser_src\n }";
9f98c4b2 37}
38
39# the simple non-collapsing nested structure recursor
40sub __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
52864fbd 57 my $rel_struct = __visit_infmap_simple({ %$args,
9f98c4b2 58 val_index => $rel_cols->{$rel},
9f98c4b2 59 });
60
52864fbd 61 if (keys %$my_cols) {
ce556881 62
52864fbd 63 my $branch_null_checks = join ' && ', map
ce556881 64 { "( ! defined '\xFF__VALPOS__${_}__\xFF' )" }
65 sort { $a <=> $b } values %{$rel_cols->{$rel}}
66 ;
67
52864fbd 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 }
ce556881 82 }
52864fbd 83
84 push @relperl, sprintf '( %s => %s )',
85 perlstring($rel),
86 $rel_struct,
87 ;
88
9f98c4b2 89 }
90
ce556881 91 my $me_struct;
a8f62ee0 92 $me_struct = __result_struct_to_source($my_cols) if keys %$my_cols;
9f98c4b2 93
ce556881 94 if ($args->{hri_style}) {
95 $me_struct =~ s/^ \s* \{ | \} \s* $//gx
96 if $me_struct;
97
98 return sprintf '{ %s }', join (', ', $me_struct||(), @relperl);
99 }
100 else {
101 return sprintf '[%s]', join (',',
102 $me_struct || 'undef',
103 @relperl ? sprintf ('{ %s }', join (',', @relperl)) : (),
104 );
105 }
9f98c4b2 106}
107
108sub assemble_collapsing_parser {
109 my $args = shift;
110
111 my ($top_node_key, $top_node_key_assembler);
112
113 if (scalar @{$args->{collapse_map}{-identifying_columns}}) {
114 $top_node_key = join ('', map
115 { "{'\xFF__IDVALPOS__${_}__\xFF'}" }
116 @{$args->{collapse_map}{-identifying_columns}}
117 );
118 }
119 elsif( my @variants = @{$args->{collapse_map}{-identifying_columns_variants}} ) {
120
121 my @path_parts = map { sprintf
122 "( ( defined '\xFF__VALPOS__%d__\xFF' ) && (join qq(\xFF), '', %s, '') )",
ce556881 123 $_->[0], # checking just first is enough - one ID defined, all defined
9f98c4b2 124 ( join ', ', map { "'\xFF__VALPOS__${_}__\xFF'" } @$_ ),
125 } @variants;
126
127 my $virtual_column_idx = (scalar keys %{$args->{val_index}} ) + 1;
128
7596ddca 129 $top_node_key = "{'\xFF__IDVALPOS__${virtual_column_idx}__\xFF'}";
9f98c4b2 130
7596ddca 131 $top_node_key_assembler = sprintf "'\xFF__IDVALPOS__%d__\xFF' = (%s);",
132 $virtual_column_idx,
133 "\n" . join( "\n or\n", @path_parts, qq{"\0\$rows_pos\0"} )
134 ;
9f98c4b2 135
136 $args->{collapse_map} = {
137 %{$args->{collapse_map}},
138 -custom_node_key => $top_node_key,
139 };
140
141 }
142 else {
143 die('Unexpected collapse map contents');
144 }
145
146 my ($data_assemblers, $stats) = __visit_infmap_collapse ($args);
147
7596ddca 148 my @idcol_args = $args->{hri_style} ? ('', '') : (
149 '%cur_row_ids, ', # only declare the variable if we'll use it
150
151 sprintf( <<'EOS', join ', ', sort { $a <=> $b } keys %{ $stats->{idcols_seen} } ),
152 $cur_row_ids{$_} = defined($cur_row_data->[$_]) ? $cur_row_data->[$_] : "\0NULL\xFF$rows_pos\xFF$_\0"
153 for (%s);
154EOS
155 );
9f98c4b2 156
7596ddca 157 my $parser_src = sprintf (<<'EOS', @idcol_args, $top_node_key_assembler||'', $top_node_key, join( "\n", @{$data_assemblers||[]} ) );
9f98c4b2 158### BEGIN LITERAL STRING EVAL
7596ddca 159 my ($rows_pos, $result_pos, $cur_row_data,%1$s @collapse_idx, $is_new_res) = (0,0);
9f98c4b2 160 # this loop is a bit arcane - the rationale is that the passed in
161 # $_[0] will either have only one row (->next) or will have all
162 # rows already pulled in (->all and/or unordered). Given that the
163 # result can be rather large - we reuse the same already allocated
164 # array, since the collapsed prefetch is smaller by definition.
165 # At the end we cut the leftovers away and move on.
166 while ($cur_row_data =
167 ( ( $rows_pos >= 0 and $_[0][$rows_pos++] ) or do { $rows_pos = -1; undef } )
168 ||
169 ($_[1] and $_[1]->())
170 ) {
7596ddca 171 # this code exists only when we are *not* assembling direct to HRI
172 #
9f98c4b2 173 # due to left joins some of the ids may be NULL/undef, and
174 # won't play well when used as hash lookups
175 # we also need to differentiate NULLs on per-row/per-col basis
ce556881 176 # (otherwise folding of optional 1:1s will be greatly confused
7596ddca 177 %2$s
9f98c4b2 178
7596ddca 179 # in the case of an underdefined root - calculate the virtual id (otherwise no code at all)
9f98c4b2 180 %3$s
181
7596ddca 182 $is_new_res = ! $collapse_idx[0]%4$s and (
9f98c4b2 183 $_[1] and $result_pos and (unshift @{$_[2]}, $cur_row_data) and last
184 );
185
186 # the rel assemblers
7596ddca 187%5$s
9f98c4b2 188
7596ddca 189 $_[0][$result_pos++] = $collapse_idx[0]%4$s
9f98c4b2 190 if $is_new_res;
191 }
192
193 splice @{$_[0]}, $result_pos; # truncate the passed in array for cases of collapsing ->all()
194### END LITERAL STRING EVAL
195EOS
196
197 # !!! note - different var than the one above
198 # change the quoted placeholders to unquoted alias-references
199 $parser_src =~ s/ \' \xFF__VALPOS__(\d+)__\xFF \' /"\$cur_row_data->[$1]"/gex;
7596ddca 200 $parser_src =~ s/ \' \xFF__IDVALPOS__(\d+)__\xFF \' /$args->{hri_style} ? "\$cur_row_data->[$1]" : "\$cur_row_ids{$1}" /gex;
9f98c4b2 201
bdbd2ae8 202 $parser_src = " { use strict; use warnings FATAL => 'all';\n$parser_src\n }";
9f98c4b2 203}
204
205
206# the collapsing nested structure recursor
207sub __visit_infmap_collapse {
208 my $args = {%{ shift() }};
209
210 my $cur_node_idx = ${ $args->{-node_idx_counter} ||= \do { my $x = 0} }++;
211
ce556881 212 my ($my_cols, $rel_cols) = {};
9f98c4b2 213 for ( keys %{$args->{val_index}} ) {
214 if ($_ =~ /^ ([^\.]+) \. (.+) /x) {
215 $rel_cols->{$1}{$2} = $args->{val_index}{$_};
216 }
217 else {
218 $my_cols->{$_} = $args->{val_index}{$_};
219 }
220 }
221
ce556881 222
ce556881 223 if ($args->{hri_style}) {
224 delete $my_cols->{$_} for grep { $rel_cols->{$_} } keys %$my_cols;
225 }
9f98c4b2 226
52864fbd 227 my $me_struct;
a8f62ee0 228 $me_struct = __result_struct_to_source($my_cols) if keys %$my_cols;
ce556881 229
52864fbd 230 $me_struct = sprintf( '[ %s ]', $me_struct||'' )
231 unless $args->{hri_style};
232
233
234 my $node_key = $args->{collapse_map}->{-custom_node_key} || join ('', map
235 { "{'\xFF__IDVALPOS__${_}__\xFF'}" }
236 @{$args->{collapse_map}->{-identifying_columns}}
237 );
ce556881 238 my $node_idx_slot = sprintf '$collapse_idx[%d]%s', $cur_node_idx, $node_key;
9f98c4b2 239
52864fbd 240
9f98c4b2 241 my @src;
ce556881 242
9f98c4b2 243 if ($cur_node_idx == 0) {
244 push @src, sprintf( '%s ||= %s;',
245 $node_idx_slot,
246 $me_struct,
247 ) if $me_struct;
248 }
9f98c4b2 249 else {
ce556881 250 my $parent_attach_slot = sprintf( '$collapse_idx[%d]%s%s{%s}',
251 @{$args}{qw/-parent_node_idx -parent_node_key/},
252 $args->{hri_style} ? '' : '[1]',
253 perlstring($args->{-node_relname}),
9f98c4b2 254 );
ce556881 255
256 if ($args->{collapse_map}->{-is_single}) {
257 push @src, sprintf ( '%s ||= %s%s;',
258 $parent_attach_slot,
259 $node_idx_slot,
260 $me_struct ? " ||= $me_struct" : '',
261 );
262 }
263 else {
264 push @src, sprintf('(! %s) and push @{%s}, %s%s;',
265 $node_idx_slot,
266 $parent_attach_slot,
267 $node_idx_slot,
268 $me_struct ? " = $me_struct" : '',
269 );
270 }
9f98c4b2 271 }
272
ce556881 273 my $known_present_ids = { map { $_ => 1 } @{$args->{collapse_map}{-identifying_columns}} };
274 my ($stats, $rel_src);
275
9f98c4b2 276 for my $rel (sort keys %$rel_cols) {
277
ce556881 278 my $relinfo = $args->{collapse_map}{$rel};
9f98c4b2 279
ce556881 280 ($rel_src, $stats->{$rel}) = __visit_infmap_collapse({ %$args,
9f98c4b2 281 val_index => $rel_cols->{$rel},
ce556881 282 collapse_map => $relinfo,
9f98c4b2 283 -parent_node_idx => $cur_node_idx,
284 -parent_node_key => $node_key,
285 -node_relname => $rel,
286 });
287
ce556881 288 my $rel_src_pos = $#src + 1;
289 push @src, @$rel_src;
290
291 if (
ce556881 292 $relinfo->{-is_optional}
293 and
294 defined ( my $first_distinct_child_idcol = first
295 { ! $known_present_ids->{$_} }
296 @{$relinfo->{-identifying_columns}}
297 )
298 ) {
299
52864fbd 300 if ($args->{hri_style}) {
ce556881 301
7596ddca 302 # start of wrap of the entire chain in a conditional
303 splice @src, $rel_src_pos, 0, sprintf "( ! defined %s )\n ? %s{%s} = %s\n : do {",
304 "'\xFF__VALPOS__${first_distinct_child_idcol}__\xFF'",
52864fbd 305 $node_idx_slot,
306 perlstring($rel),
7596ddca 307 $relinfo->{-is_single} ? 'undef' : '[]'
308 ;
309
310 # end of wrap
311 push @src, '};'
52864fbd 312 }
313 else {
314
315 splice @src, $rel_src_pos + 1, 0, sprintf ( '(defined %s) or bless (%s[1]{%s}, %s);',
316 "'\xFF__VALPOS__${first_distinct_child_idcol}__\xFF'",
317 $node_idx_slot,
318 perlstring($rel),
319 perlstring($null_branch_class),
320 );
321 }
ce556881 322 }
9f98c4b2 323 }
324
325 return (
ce556881 326 \@src,
9f98c4b2 327 {
328 idcols_seen => {
329 ( map { %{ $_->{idcols_seen} } } values %$stats ),
330 ( map { $_ => 1 } @{$args->{collapse_map}->{-identifying_columns}} ),
331 }
332 }
333 );
334}
335
a8f62ee0 336sub __result_struct_to_source {
337 sprintf( '{ %s }', join (', ', map
338 { sprintf "%s => '\xFF__VALPOS__%d__\xFF'", perlstring($_), $_[0]{$_} }
339 sort keys %{$_[0]}
340 ));
9f98c4b2 341}
342
3431;