Commit | Line | Data |
2bb7b40b |
1 | package DBIx::Class::ResultSetColumn; |
1a58752c |
2 | |
2bb7b40b |
3 | use strict; |
4 | use warnings; |
1a58752c |
5 | |
2bb7b40b |
6 | use base 'DBIx::Class'; |
70c28808 |
7 | use DBIx::Class::Carp; |
a580a227 |
8 | use DBIx::Class::_Util 'fail_on_internal_call'; |
64c50e81 |
9 | use namespace::clean; |
94244987 |
10 | |
2bb7b40b |
11 | =head1 NAME |
12 | |
13 | DBIx::Class::ResultSetColumn - helpful methods for messing |
14 | with a single column of the resultset |
15 | |
16 | =head1 SYNOPSIS |
17 | |
18 | $rs = $schema->resultset('CD')->search({ artist => 'Tool' }); |
19 | $rs_column = $rs->get_column('year'); |
20 | $max_year = $rs_column->max; #returns latest year |
21 | |
22 | =head1 DESCRIPTION |
23 | |
eb98561c |
24 | A convenience class used to perform operations on a specific column of |
25 | a resultset. |
2bb7b40b |
26 | |
27 | =cut |
28 | |
29 | =head1 METHODS |
30 | |
31 | =head2 new |
32 | |
33 | my $obj = DBIx::Class::ResultSetColumn->new($rs, $column); |
34 | |
eb98561c |
35 | Creates a new resultset column object from the resultset and column |
36 | passed as params. Used internally by L<DBIx::Class::ResultSet/get_column>. |
2bb7b40b |
37 | |
38 | =cut |
39 | |
40 | sub new { |
41 | my ($class, $rs, $column) = @_; |
42 | $class = ref $class if ref $class; |
7ae9706c |
43 | |
66e33361 |
44 | $rs->throw_exception('column must be supplied') unless $column; |
7ae9706c |
45 | |
d8dbe471 |
46 | my $orig_attrs = $rs->_resolved_attrs; |
9b8930f4 |
47 | my $alias = $rs->current_source_alias; |
2b3fc408 |
48 | my $rsrc = $rs->result_source; |
472d7df3 |
49 | |
50 | # If $column can be found in the 'as' list of the parent resultset, use the |
51 | # corresponding element of its 'select' list (to keep any custom column |
52 | # definition set up with 'select' or '+select' attrs), otherwise use $column |
53 | # (to create a new column definition on-the-fly). |
54 | my $as_list = $orig_attrs->{as} || []; |
55 | my $select_list = $orig_attrs->{select} || []; |
87b12551 |
56 | my ($as_index) = grep { ($as_list->[$_] || "") eq $column } 0..$#$as_list; |
472d7df3 |
57 | my $select = defined $as_index ? $select_list->[$as_index] : $column; |
58 | |
aca094b4 |
59 | my $colmap; |
2b3fc408 |
60 | for ($rsrc->columns, $column) { |
61 | if ($_ =~ /^ \Q$alias\E \. ([^\.]+) $ /x) { |
62 | $colmap->{$_} = $1; |
63 | } |
64 | elsif ($_ !~ /\./) { |
65 | $colmap->{"$alias.$_"} = $_; |
66 | $colmap->{$_} = $_; |
67 | } |
68 | } |
69 | |
aca094b4 |
70 | my $new_parent_rs; |
472d7df3 |
71 | # analyze the order_by, and see if it is done over a function/nonexistentcolumn |
72 | # if this is the case we will need to wrap a subquery since the result of RSC |
73 | # *must* be a single column select |
472d7df3 |
74 | if ( |
75 | scalar grep |
2b3fc408 |
76 | { ! exists $colmap->{$_->[0]} } |
77 | ( $rsrc->schema->storage->_extract_order_criteria ($orig_attrs->{order_by} ) ) |
472d7df3 |
78 | ) { |
5716e003 |
79 | # nuke the prefetch before collapsing to sql |
aca094b4 |
80 | my $subq_rs = $rs->search_rs; |
37aafa2e |
81 | $subq_rs->{attrs}{join} = $subq_rs->_merge_joinpref_attr( $subq_rs->{attrs}{join}, delete $subq_rs->{attrs}{prefetch} ); |
9b8930f4 |
82 | $new_parent_rs = $subq_rs->as_subselect_rs; |
472d7df3 |
83 | } |
84 | |
85 | $new_parent_rs ||= $rs->search_rs; |
66e33361 |
86 | my $new_attrs = $new_parent_rs->{attrs} ||= {}; |
87 | |
7ae9706c |
88 | # prefetch causes additional columns to be fetched, but we can not just make a new |
89 | # rs via the _resolved_attrs trick - we need to retain the separation between |
bed3a173 |
90 | # +select/+as and select/as. At the same time we want to preserve any joins that the |
91 | # prefetch would otherwise generate. |
37aafa2e |
92 | $new_attrs->{join} = $rs->_merge_joinpref_attr( $new_attrs->{join}, delete $new_attrs->{prefetch} ); |
b6e85b48 |
93 | |
d8dbe471 |
94 | # {collapse} would mean a has_many join was injected, which in turn means |
722c0140 |
95 | # we need to group *IF WE CAN* (only if the column in question is unique) |
f1693c0d |
96 | if (!$orig_attrs->{group_by} && $orig_attrs->{collapse}) { |
d8dbe471 |
97 | |
2b3fc408 |
98 | if ($colmap->{$select} and $rsrc->_identifying_column_set([$colmap->{$select}])) { |
99 | $new_attrs->{group_by} = [ $select ]; |
1e4f9fb3 |
100 | delete @{$new_attrs}{qw(distinct _grouped_by_distinct)}; # it is ignored when group_by is present |
d8dbe471 |
101 | } |
2b3fc408 |
102 | else { |
722c0140 |
103 | carp ( |
104 | "Attempting to retrieve non-unique column '$column' on a resultset containing " |
105 | . 'one-to-many joins will return duplicate results.' |
106 | ); |
107 | } |
d8dbe471 |
108 | } |
109 | |
722274d0 |
110 | return bless { |
111 | _select => $select, |
aca094b4 |
112 | _as => $column, |
722274d0 |
113 | _parent_resultset => $new_parent_rs |
114 | }, $class; |
2bb7b40b |
115 | } |
116 | |
6dfbe2f8 |
117 | =head2 as_query |
658fa250 |
118 | |
119 | =over 4 |
120 | |
428a645e |
121 | =item Arguments: none |
658fa250 |
122 | |
00c12490 |
123 | =item Return Value: \[ $sql, L<@bind_values|DBIx::Class::ResultSet/DBIC BIND VALUES> ] |
658fa250 |
124 | |
125 | =back |
126 | |
127 | Returns the SQL query and bind vars associated with the invocant. |
128 | |
03834f77 |
129 | This is generally used as the RHS for a subquery. |
c7a9d102 |
130 | |
131 | =cut |
132 | |
0f6fc705 |
133 | sub as_query { return shift->_resultset->as_query(@_) } |
c7a9d102 |
134 | |
2bb7b40b |
135 | =head2 next |
136 | |
137 | =over 4 |
138 | |
139 | =item Arguments: none |
140 | |
141 | =item Return Value: $value |
142 | |
143 | =back |
144 | |
eb98561c |
145 | Returns the next value of the column in the resultset (or C<undef> if |
146 | there is none). |
2bb7b40b |
147 | |
8273e845 |
148 | Much like L<DBIx::Class::ResultSet/next> but just returning the |
eb98561c |
149 | one value. |
2bb7b40b |
150 | |
151 | =cut |
152 | |
153 | sub next { |
c5340bfa |
154 | #my $self = shift; |
b7c79955 |
155 | |
156 | # using cursor so we don't inflate anything |
c5340bfa |
157 | ($_[0]->_resultset->cursor->next)[0]; |
2bb7b40b |
158 | } |
159 | |
160 | =head2 all |
161 | |
162 | =over 4 |
163 | |
164 | =item Arguments: none |
165 | |
166 | =item Return Value: @values |
167 | |
168 | =back |
169 | |
eb98561c |
170 | Returns all values of the column in the resultset (or C<undef> if |
171 | there are none). |
2bb7b40b |
172 | |
eb98561c |
173 | Much like L<DBIx::Class::ResultSet/all> but returns values rather |
fb13a49f |
174 | than result objects. |
2bb7b40b |
175 | |
176 | =cut |
177 | |
178 | sub all { |
c5340bfa |
179 | #my $self = shift; |
b7c79955 |
180 | |
181 | # using cursor so we don't inflate anything |
c5340bfa |
182 | map { $_->[0] } $_[0]->_resultset->cursor->all; |
66521001 |
183 | } |
184 | |
185 | =head2 reset |
186 | |
187 | =over 4 |
188 | |
189 | =item Arguments: none |
190 | |
191 | =item Return Value: $self |
192 | |
193 | =back |
194 | |
195 | Resets the underlying resultset's cursor, so you can iterate through the |
196 | elements of the column again. |
197 | |
198 | Much like L<DBIx::Class::ResultSet/reset>. |
199 | |
200 | =cut |
201 | |
202 | sub reset { |
c5340bfa |
203 | #my $self = shift; |
204 | |
205 | $_[0]->_resultset->reset; |
206 | $_[0]; |
66521001 |
207 | } |
208 | |
209 | =head2 first |
210 | |
211 | =over 4 |
212 | |
213 | =item Arguments: none |
214 | |
215 | =item Return Value: $value |
216 | |
217 | =back |
218 | |
219 | Resets the underlying resultset and returns the next value of the column in the |
220 | resultset (or C<undef> if there is none). |
221 | |
222 | Much like L<DBIx::Class::ResultSet/first> but just returning the one value. |
223 | |
224 | =cut |
225 | |
c5340bfa |
226 | sub first :DBIC_method_is_indirect_sugar { |
227 | DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and fail_on_internal_call; |
b7c79955 |
228 | |
229 | # using cursor so we don't inflate anything |
c5340bfa |
230 | my $cursor = $_[0]->_resultset->cursor; |
231 | $cursor->reset; |
232 | ($cursor->next)[0]; |
2bb7b40b |
233 | } |
234 | |
4e55c3ae |
235 | =head2 single |
236 | |
237 | =over 4 |
238 | |
239 | =item Arguments: none |
240 | |
241 | =item Return Value: $value |
242 | |
243 | =back |
244 | |
245 | Much like L<DBIx::Class::ResultSet/single> fetches one and only one column |
246 | value using the cursor directly. If additional rows are present a warning |
247 | is issued before discarding the cursor. |
248 | |
249 | =cut |
250 | |
251 | sub single { |
c5340bfa |
252 | #my $self = shift; |
4e55c3ae |
253 | |
c5340bfa |
254 | my $rs = $_[0]->_resultset; |
4e55c3ae |
255 | |
c5340bfa |
256 | my $attrs = $rs->_resolved_attrs; |
257 | ($rs->result_source->schema->storage->select_single( |
258 | $attrs->{from}, $attrs->{select}, $attrs->{where}, $attrs |
259 | ))[0]; |
4e55c3ae |
260 | } |
261 | |
2bb7b40b |
262 | =head2 min |
263 | |
264 | =over 4 |
265 | |
266 | =item Arguments: none |
267 | |
268 | =item Return Value: $lowest_value |
269 | |
270 | =back |
271 | |
eb98561c |
272 | my $first_year = $year_col->min(); |
273 | |
274 | Wrapper for ->func. Returns the lowest value of the column in the |
275 | resultset (or C<undef> if there are none). |
2bb7b40b |
276 | |
277 | =cut |
278 | |
1b822bd3 |
279 | sub min :DBIC_method_is_indirect_sugar { |
e5053694 |
280 | DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and fail_on_internal_call; |
281 | $_[0]->func('MIN'); |
2bb7b40b |
282 | } |
283 | |
4fa7bc22 |
284 | =head2 min_rs |
285 | |
286 | =over 4 |
287 | |
288 | =item Arguments: none |
289 | |
fb13a49f |
290 | =item Return Value: L<$resultset|DBIx::Class::ResultSet> |
4fa7bc22 |
291 | |
292 | =back |
293 | |
294 | my $rs = $year_col->min_rs(); |
295 | |
296 | Wrapper for ->func_rs for function MIN(). |
297 | |
298 | =cut |
299 | |
1b822bd3 |
300 | sub min_rs :DBIC_method_is_indirect_sugar { |
e5053694 |
301 | DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and fail_on_internal_call; |
302 | $_[0]->func_rs('MIN') |
303 | } |
4fa7bc22 |
304 | |
2bb7b40b |
305 | =head2 max |
306 | |
307 | =over 4 |
308 | |
309 | =item Arguments: none |
310 | |
311 | =item Return Value: $highest_value |
312 | |
313 | =back |
314 | |
eb98561c |
315 | my $last_year = $year_col->max(); |
316 | |
317 | Wrapper for ->func. Returns the highest value of the column in the |
318 | resultset (or C<undef> if there are none). |
2bb7b40b |
319 | |
320 | =cut |
321 | |
1b822bd3 |
322 | sub max :DBIC_method_is_indirect_sugar { |
e5053694 |
323 | DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and fail_on_internal_call; |
324 | $_[0]->func('MAX'); |
2bb7b40b |
325 | } |
326 | |
4fa7bc22 |
327 | =head2 max_rs |
328 | |
329 | =over 4 |
330 | |
331 | =item Arguments: none |
332 | |
fb13a49f |
333 | =item Return Value: L<$resultset|DBIx::Class::ResultSet> |
4fa7bc22 |
334 | |
335 | =back |
336 | |
337 | my $rs = $year_col->max_rs(); |
338 | |
339 | Wrapper for ->func_rs for function MAX(). |
340 | |
341 | =cut |
342 | |
1b822bd3 |
343 | sub max_rs :DBIC_method_is_indirect_sugar { |
e5053694 |
344 | DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and fail_on_internal_call; |
345 | $_[0]->func_rs('MAX') |
346 | } |
4fa7bc22 |
347 | |
2bb7b40b |
348 | =head2 sum |
349 | |
350 | =over 4 |
351 | |
352 | =item Arguments: none |
353 | |
354 | =item Return Value: $sum_of_values |
355 | |
356 | =back |
357 | |
eb98561c |
358 | my $total = $prices_col->sum(); |
359 | |
360 | Wrapper for ->func. Returns the sum of all the values in the column of |
361 | the resultset. Use on varchar-like columns at your own risk. |
2bb7b40b |
362 | |
363 | =cut |
364 | |
1b822bd3 |
365 | sub sum :DBIC_method_is_indirect_sugar { |
e5053694 |
366 | DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and fail_on_internal_call; |
367 | $_[0]->func('SUM'); |
2bb7b40b |
368 | } |
369 | |
4fa7bc22 |
370 | =head2 sum_rs |
371 | |
372 | =over 4 |
373 | |
374 | =item Arguments: none |
375 | |
fb13a49f |
376 | =item Return Value: L<$resultset|DBIx::Class::ResultSet> |
4fa7bc22 |
377 | |
378 | =back |
379 | |
380 | my $rs = $year_col->sum_rs(); |
381 | |
382 | Wrapper for ->func_rs for function SUM(). |
383 | |
384 | =cut |
385 | |
1b822bd3 |
386 | sub sum_rs :DBIC_method_is_indirect_sugar { |
e5053694 |
387 | DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and fail_on_internal_call; |
388 | $_[0]->func_rs('SUM') |
389 | } |
4fa7bc22 |
390 | |
2bb7b40b |
391 | =head2 func |
392 | |
393 | =over 4 |
394 | |
395 | =item Arguments: $function |
396 | |
397 | =item Return Value: $function_return_value |
398 | |
399 | =back |
400 | |
e8419341 |
401 | $rs = $schema->resultset("CD")->search({}); |
402 | $length = $rs->get_column('title')->func('LENGTH'); |
2bb7b40b |
403 | |
eb98561c |
404 | Runs a query using the function on the column and returns the |
405 | value. Produces the following SQL: |
406 | |
407 | SELECT LENGTH( title ) FROM cd me |
2bb7b40b |
408 | |
409 | =cut |
410 | |
c5340bfa |
411 | sub func :DBIC_method_is_indirect_sugar{ |
412 | DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and fail_on_internal_call; |
413 | |
414 | #my ($self,$function) = @_; |
415 | my $cursor = $_[0]->func_rs($_[1])->cursor; |
d4daee7b |
416 | |
a580a227 |
417 | wantarray |
418 | ? map { $_->[ 0 ] } $cursor->all |
419 | : ( $cursor->next )[ 0 ] |
420 | ; |
2bb7b40b |
421 | } |
422 | |
4fa7bc22 |
423 | =head2 func_rs |
424 | |
425 | =over 4 |
426 | |
427 | =item Arguments: $function |
428 | |
fb13a49f |
429 | =item Return Value: L<$resultset|DBIx::Class::ResultSet> |
4fa7bc22 |
430 | |
431 | =back |
432 | |
433 | Creates the resultset that C<func()> uses to run its query. |
434 | |
435 | =cut |
436 | |
437 | sub func_rs { |
438 | my ($self,$function) = @_; |
3214abc7 |
439 | |
440 | my $rs = $self->{_parent_resultset}; |
441 | my $select = $self->{_select}; |
442 | |
443 | # wrap a grouped rs |
444 | if ($rs->_resolved_attrs->{group_by}) { |
445 | $select = $self->{_as}; |
446 | $rs = $rs->as_subselect_rs; |
447 | } |
448 | |
6c5aa1fb |
449 | # FIXME - remove at some point in the future (2018-ish) |
450 | wantarray |
451 | and |
452 | carp_unique( |
453 | 'Starting with DBIC@0.082900 func_rs() always returns a ResultSet ' |
454 | . 'instance regardless of calling context. Please force scalar() context to ' |
455 | . 'silence this warning' |
a580a227 |
456 | ); |
6c5aa1fb |
457 | |
458 | $rs->search_rs( undef, { |
3214abc7 |
459 | columns => { $self->{_as} => { $function => $select } } |
460 | } ); |
4fa7bc22 |
461 | } |
462 | |
5d1fc7dc |
463 | =head2 throw_exception |
464 | |
465 | See L<DBIx::Class::Schema/throw_exception> for details. |
d4daee7b |
466 | |
8273e845 |
467 | =cut |
d4daee7b |
468 | |
5d1fc7dc |
469 | sub throw_exception { |
f9080e45 |
470 | my $self = shift; |
1a58752c |
471 | |
5d1fc7dc |
472 | if (ref $self && $self->{_parent_resultset}) { |
1a58752c |
473 | $self->{_parent_resultset}->throw_exception(@_); |
474 | } |
475 | else { |
476 | DBIx::Class::Exception->throw(@_); |
5d1fc7dc |
477 | } |
478 | } |
479 | |
b6e85b48 |
480 | # _resultset |
481 | # |
482 | # Arguments: none |
483 | # |
484 | # Return Value: $resultset |
485 | # |
486 | # $year_col->_resultset->next |
487 | # |
488 | # Returns the underlying resultset. Creates it from the parent resultset if |
489 | # necessary. |
b7c79955 |
490 | # |
66521001 |
491 | sub _resultset { |
492 | my $self = shift; |
493 | |
722274d0 |
494 | return $self->{_resultset} ||= do { |
495 | |
496 | my $select = $self->{_select}; |
497 | |
498 | if ($self->{_parent_resultset}{attrs}{distinct}) { |
499 | my $alias = $self->{_parent_resultset}->current_source_alias; |
500 | my $rsrc = $self->{_parent_resultset}->result_source; |
501 | my %cols = map { $_ => 1, "$alias.$_" => 1 } $rsrc->columns; |
502 | |
503 | unless( $cols{$select} ) { |
504 | carp_unique( |
505 | 'Use of distinct => 1 while selecting anything other than a column ' |
885c3dbd |
506 | . 'declared on the primary ResultSource is deprecated (you selected ' |
507 | . "'$self->{_as}') - please supply an explicit group_by instead" |
722274d0 |
508 | ); |
509 | |
510 | # collapse the selector to a literal so that it survives the distinct parse |
511 | # if it turns out to be an aggregate - at least the user will get a proper exception |
512 | # instead of silent drop of the group_by altogether |
e5053694 |
513 | $select = \[ $rsrc->schema->storage->sql_maker->_recurse_fields($select) ]; |
722274d0 |
514 | } |
66521001 |
515 | } |
722274d0 |
516 | |
367eaf50 |
517 | $self->{_parent_resultset}->search_rs(undef, { |
722274d0 |
518 | columns => { $self->{_as} => $select } |
519 | }); |
520 | }; |
66521001 |
521 | } |
522 | |
a2bd3796 |
523 | =head1 FURTHER QUESTIONS? |
2bb7b40b |
524 | |
a2bd3796 |
525 | Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>. |
eb98561c |
526 | |
a2bd3796 |
527 | =head1 COPYRIGHT AND LICENSE |
2bb7b40b |
528 | |
a2bd3796 |
529 | This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE> |
530 | by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can |
531 | redistribute it and/or modify it under the same terms as the |
532 | L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>. |
2bb7b40b |
533 | |
534 | =cut |
a2bd3796 |
535 | |
536 | 1; |
537 | |