a4e2eb5f72312689c44c3bb108c48cce7e7e0388
[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 B 'perlstring';
8
9 use base 'Exporter';
10 our @EXPORT_OK = qw(
11   assemble_simple_parser
12   assemble_collapsing_parser
13 );
14
15 sub assemble_simple_parser {
16   #my ($args) = @_;
17
18   # the non-collapsing assembler is easy
19   # FIXME SUBOPTIMAL there could be a yet faster way to do things here, but
20   # need to try an actual implementation and benchmark it:
21   #
22   # <timbunce_> First setup the nested data structure you want for each row
23   #   Then call bind_col() to alias the row fields into the right place in
24   #   the data structure, then to fetch the data do:
25   # push @rows, dclone($row_data_struct) while ($sth->fetchrow);
26   #
27   my $parser_src = sprintf('$_ = %s for @{$_[0]}', __visit_infmap_simple($_[0]) );
28
29   # change the quoted placeholders to unquoted alias-references
30   $parser_src =~ s/ \' \xFF__VALPOS__(\d+)__\xFF \' /"\$_->[$1]"/gex;
31
32   return $parser_src;
33 }
34
35 # the simple non-collapsing nested structure recursor
36 sub __visit_infmap_simple {
37   my $args = shift;
38
39   my $my_cols = {};
40   my $rel_cols;
41   for (keys %{$args->{val_index}}) {
42     if ($_ =~ /^ ([^\.]+) \. (.+) /x) {
43       $rel_cols->{$1}{$2} = $args->{val_index}{$_};
44     }
45     else {
46       $my_cols->{$_} = $args->{val_index}{$_};
47     }
48   }
49
50   my @relperl;
51   for my $rel (sort keys %$rel_cols) {
52
53     # DISABLEPRUNE
54     #my $optional = $args->{is_optional};
55     #$optional ||= ($args->{rsrc}->relationship_info($rel)->{attrs}{join_type} || '') =~ /^left/i;
56
57     push @relperl, join ' => ', perlstring($rel), __visit_infmap_simple({ %$args,
58       val_index => $rel_cols->{$rel},
59       # DISABLEPRUNE
60       #non_top => 1,
61       #is_optional => $optional,
62     });
63
64     # FIXME SUBOPTIMAL DISABLEPRUNE - disabled to satisfy t/resultset/inflate_result_api.t
65     #if ($optional and my @branch_null_checks = map
66     #  { "(! defined '\xFF__VALPOS__${_}__\xFF')" }
67     #  sort { $a <=> $b } values %{$rel_cols->{$rel}}
68     #) {
69     #  $relperl[-1] = sprintf ( '(%s) ? ( %s => [] ) : ( %s )',
70     #    join (' && ', @branch_null_checks ),
71     #    perlstring($rel),
72     #    $relperl[-1],
73     #  );
74     #}
75   }
76
77   my $me_struct = keys %$my_cols
78     ? __visit_dump({ map { $_ => "\xFF__VALPOS__$my_cols->{$_}__\xFF" } (keys %$my_cols) })
79     : 'undef'
80   ;
81
82   return sprintf '[%s]', join (',',
83     $me_struct,
84     @relperl ? sprintf ('{ %s }', join (',', @relperl)) : (),
85   );
86 }
87
88 sub assemble_collapsing_parser {
89   my $args = shift;
90
91   my ($top_node_key, $top_node_key_assembler);
92
93   if (scalar @{$args->{collapse_map}{-identifying_columns}}) {
94     $top_node_key = join ('', map
95       { "{'\xFF__IDVALPOS__${_}__\xFF'}" }
96       @{$args->{collapse_map}{-identifying_columns}}
97     );
98   }
99   elsif( my @variants = @{$args->{collapse_map}{-identifying_columns_variants}} ) {
100
101     my @path_parts = map { sprintf
102       "( ( defined '\xFF__VALPOS__%d__\xFF' ) && (join qq(\xFF), '', %s, '') )",
103       $_->[0],  # checking just first is enough - one defined, all defined
104       ( join ', ', map { "'\xFF__VALPOS__${_}__\xFF'" } @$_ ),
105     } @variants;
106
107     my $virtual_column_idx = (scalar keys %{$args->{val_index}} ) + 1;
108
109     $top_node_key_assembler = sprintf '$cur_row_ids{%d} = (%s);',
110       $virtual_column_idx,
111       "\n" . join( "\n  or\n", @path_parts, qq{"\0\$rows_pos\0"} );
112
113     $top_node_key = sprintf '{$cur_row_ids{%d}}', $virtual_column_idx;
114
115     $args->{collapse_map} = {
116       %{$args->{collapse_map}},
117       -custom_node_key => $top_node_key,
118     };
119
120   }
121   else {
122     die('Unexpected collapse map contents');
123   }
124
125   my ($data_assemblers, $stats) = __visit_infmap_collapse ($args);
126
127   my $list_of_idcols = join(', ', sort { $a <=> $b } keys %{ $stats->{idcols_seen} } );
128
129   my $parser_src = sprintf (<<'EOS', $list_of_idcols, $top_node_key, $top_node_key_assembler||'', $data_assemblers);
130 ### BEGIN LITERAL STRING EVAL
131   my ($rows_pos, $result_pos, $cur_row_data, %%cur_row_ids, @collapse_idx, $is_new_res) = (0,0);
132
133   # this loop is a bit arcane - the rationale is that the passed in
134   # $_[0] will either have only one row (->next) or will have all
135   # rows already pulled in (->all and/or unordered). Given that the
136   # result can be rather large - we reuse the same already allocated
137   # array, since the collapsed prefetch is smaller by definition.
138   # At the end we cut the leftovers away and move on.
139   while ($cur_row_data =
140     ( ( $rows_pos >= 0 and $_[0][$rows_pos++] ) or do { $rows_pos = -1; undef } )
141       ||
142     ($_[1] and $_[1]->())
143   ) {
144
145     # due to left joins some of the ids may be NULL/undef, and
146     # won't play well when used as hash lookups
147     # we also need to differentiate NULLs on per-row/per-col basis
148     #(otherwise folding of optional 1:1s will be greatly confused
149     $cur_row_ids{$_} = defined $cur_row_data->[$_] ? $cur_row_data->[$_] : "\0NULL\xFF$rows_pos\xFF$_\0"
150       for (%1$s);
151
152     # maybe(!) cache the top node id calculation
153     %3$s
154
155     $is_new_res = ! $collapse_idx[0]%2$s and (
156       $_[1] and $result_pos and (unshift @{$_[2]}, $cur_row_data) and last
157     );
158
159     # the rel assemblers
160     %4$s
161
162     $_[0][$result_pos++] = $collapse_idx[0]%2$s
163       if $is_new_res;
164   }
165
166   splice @{$_[0]}, $result_pos; # truncate the passed in array for cases of collapsing ->all()
167 ### END LITERAL STRING EVAL
168 EOS
169
170   # !!! note - different var than the one above
171   # change the quoted placeholders to unquoted alias-references
172   $parser_src =~ s/ \' \xFF__VALPOS__(\d+)__\xFF \' /"\$cur_row_data->[$1]"/gex;
173   $parser_src =~ s/ \' \xFF__IDVALPOS__(\d+)__\xFF \' /"\$cur_row_ids{$1}"/gex;
174
175   $parser_src;
176 }
177
178
179 # the collapsing nested structure recursor
180 sub __visit_infmap_collapse {
181   my $args = {%{ shift() }};
182
183   my $cur_node_idx = ${ $args->{-node_idx_counter} ||= \do { my $x = 0} }++;
184
185   my ($my_cols, $rel_cols);
186   for ( keys %{$args->{val_index}} ) {
187     if ($_ =~ /^ ([^\.]+) \. (.+) /x) {
188       $rel_cols->{$1}{$2} = $args->{val_index}{$_};
189     }
190     else {
191       $my_cols->{$_} = $args->{val_index}{$_};
192     }
193   }
194
195   my $node_key = $args->{collapse_map}->{-custom_node_key} || join ('', map
196     { "{'\xFF__IDVALPOS__${_}__\xFF'}" }
197     @{$args->{collapse_map}->{-identifying_columns}}
198   );
199
200   my $me_struct = $my_cols
201     ? __visit_dump([{ map { $_ => "\xFF__VALPOS__$my_cols->{$_}__\xFF" } (keys %$my_cols) }])
202     : undef
203   ;
204   my $node_idx_slot = sprintf '$collapse_idx[%d]%s', $cur_node_idx, $node_key;
205
206   my $parent_attach_slot = sprintf( '$collapse_idx[%d]%s[1]{%s}',
207     @{$args}{qw/-parent_node_idx -parent_node_key/},
208     perlstring($args->{-node_relname}),
209   ) if $args->{-node_relname};
210
211   my @src;
212   if ($cur_node_idx == 0) {
213     push @src, sprintf( '%s ||= %s;',
214       $node_idx_slot,
215       $me_struct,
216     ) if $me_struct;
217   }
218   elsif ($args->{collapse_map}->{-is_single}) {
219     push @src, sprintf ( '%s ||= %s%s;',
220       $parent_attach_slot,
221       $node_idx_slot,
222       $me_struct ? " ||= $me_struct" : '',
223     );
224   }
225   else {
226     push @src, sprintf('push @{%s}, %s%s unless %s;',
227       $parent_attach_slot,
228       $node_idx_slot,
229       $me_struct ? " ||= $me_struct" : '',
230       $node_idx_slot,
231     );
232   }
233
234   # DISABLEPRUNE
235   #my $known_defined = { %{ $parent_info->{known_defined} || {} } };
236   #$known_defined->{$_}++ for @{$args->{collapse_map}->{-identifying_columns}};
237   my $stats;
238   for my $rel (sort keys %$rel_cols) {
239
240 #    push @src, sprintf(
241 #      '%s[1]{%s} ||= [];', $node_idx_slot, perlstring($rel)
242 #    ) unless $args->{collapse_map}->{$rel}{-is_single};
243
244     ($src[$#src + 1], $stats->{$rel}) = __visit_infmap_collapse({ %$args,
245       val_index => $rel_cols->{$rel},
246       collapse_map => $args->{collapse_map}{$rel},
247       -parent_node_idx => $cur_node_idx,
248       -parent_node_key => $node_key,
249       -node_relname => $rel,
250     });
251
252     # FIXME SUBOPTIMAL DISABLEPRUNE - disabled to satisfy t/resultset/inflate_result_api.t
253     #if ($args->{collapse_map}->{$rel}{-is_optional} and my @null_checks = map
254     #  { "(! defined '\xFF__IDVALPOS__${_}__\xFF')" }
255     #  sort { $a <=> $b } grep
256     #    { ! $known_defined->{$_} }
257     #    @{$args->{collapse_map}->{$rel}{-identifying_columns}}
258     #) {
259     #  $src[-1] = sprintf( '(%s) or %s',
260     #    join (' || ', @null_checks ),
261     #    $src[-1],
262     #  );
263     #}
264   }
265
266   return (
267     join("\n", @src),
268     {
269       idcols_seen => {
270         ( map { %{ $_->{idcols_seen} } } values %$stats ),
271         ( map { $_ => 1 } @{$args->{collapse_map}->{-identifying_columns}} ),
272       }
273     }
274   );
275 }
276
277 # keep our own DD object around so we don't have to fitz with quoting
278 my $dumper_obj;
279 sub __visit_dump {
280   # we actually will be producing functional perl code here,
281   # thus no second-guessing of what these globals might have
282   # been set to. DO NOT CHANGE!
283   ($dumper_obj ||= do {
284     require Data::Dumper;
285     Data::Dumper->new([])
286       ->Useperl (0)
287       ->Purity (1)
288       ->Pad ('')
289       ->Useqq (0)
290       ->Terse (1)
291       ->Quotekeys (1)
292       ->Deepcopy (0)
293       ->Deparse (0)
294       ->Maxdepth (0)
295       ->Indent (0)  # faster but harder to read, perhaps leave at 1 ?
296   })->Values ([$_[0]])->Dump;
297 }
298
299 1;