Add EOL test
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultSetColumn.pm
CommitLineData
2bb7b40b 1package DBIx::Class::ResultSetColumn;
1a58752c 2
2bb7b40b 3use strict;
4use warnings;
1a58752c 5
2bb7b40b 6use base 'DBIx::Class';
1a58752c 7
722c0140 8use Carp::Clan qw/^DBIx::Class/;
1a58752c 9use DBIx::Class::Exception;
66521001 10use List::Util;
2bb7b40b 11
12=head1 NAME
13
14 DBIx::Class::ResultSetColumn - helpful methods for messing
15 with a single column of the resultset
16
17=head1 SYNOPSIS
18
19 $rs = $schema->resultset('CD')->search({ artist => 'Tool' });
20 $rs_column = $rs->get_column('year');
21 $max_year = $rs_column->max; #returns latest year
22
23=head1 DESCRIPTION
24
eb98561c 25A convenience class used to perform operations on a specific column of
26a resultset.
2bb7b40b 27
28=cut
29
30=head1 METHODS
31
32=head2 new
33
34 my $obj = DBIx::Class::ResultSetColumn->new($rs, $column);
35
eb98561c 36Creates a new resultset column object from the resultset and column
37passed as params. Used internally by L<DBIx::Class::ResultSet/get_column>.
2bb7b40b 38
39=cut
40
41sub new {
42 my ($class, $rs, $column) = @_;
43 $class = ref $class if ref $class;
7ae9706c 44
45 $rs->throw_exception("column must be supplied") unless $column;
46
d8dbe471 47 my $orig_attrs = $rs->_resolved_attrs;
48 my $new_parent_rs = $rs->search_rs;
7ae9706c 49
50 # prefetch causes additional columns to be fetched, but we can not just make a new
51 # rs via the _resolved_attrs trick - we need to retain the separation between
bed3a173 52 # +select/+as and select/as. At the same time we want to preserve any joins that the
53 # prefetch would otherwise generate.
d8dbe471 54
55 my $new_attrs = $new_parent_rs->{attrs} ||= {};
56 $new_attrs->{join} = $rs->_merge_attr( delete $new_attrs->{join}, delete $new_attrs->{prefetch} );
b6e85b48 57
152002c4 58 # If $column can be found in the 'as' list of the parent resultset, use the
59 # corresponding element of its 'select' list (to keep any custom column
60 # definition set up with 'select' or '+select' attrs), otherwise use $column
61 # (to create a new column definition on-the-fly).
7ae9706c 62
d8dbe471 63 my $as_list = $orig_attrs->{as} || [];
64 my $select_list = $orig_attrs->{select} || [];
b6e85b48 65 my $as_index = List::Util::first { ($as_list->[$_] || "") eq $column } 0..$#$as_list;
b6e85b48 66 my $select = defined $as_index ? $select_list->[$as_index] : $column;
67
d8dbe471 68 # {collapse} would mean a has_many join was injected, which in turn means
722c0140 69 # we need to group *IF WE CAN* (only if the column in question is unique)
d8dbe471 70 if (!$new_attrs->{group_by} && keys %{$orig_attrs->{collapse}}) {
71
72 # scan for a constraint that would contain our column only - that'd be proof
73 # enough it is unique
74 my $constraints = { $rs->result_source->unique_constraints };
75 for my $constraint_columns ( values %$constraints ) {
76
77 next unless @$constraint_columns == 1;
78
79 my $col = $constraint_columns->[0];
80 my $fqcol = join ('.', $new_attrs->{alias}, $col);
81
82 if ($col eq $select or $fqcol eq $select) {
83 $new_attrs->{group_by} = [ $select ];
722c0140 84 delete $new_attrs->{distinct}; # it is ignored when group_by is present
d8dbe471 85 last;
86 }
87 }
722c0140 88
89 if (!$new_attrs->{group_by}) {
90 carp (
91 "Attempting to retrieve non-unique column '$column' on a resultset containing "
92 . 'one-to-many joins will return duplicate results.'
93 );
94 }
d8dbe471 95 }
96
b6e85b48 97 my $new = bless { _select => $select, _as => $column, _parent_resultset => $new_parent_rs }, $class;
2bb7b40b 98 return $new;
99}
100
6dfbe2f8 101=head2 as_query
658fa250 102
103=over 4
104
428a645e 105=item Arguments: none
658fa250 106
4dc99a01 107=item Return Value: \[ $sql, @bind ]
658fa250 108
109=back
110
111Returns the SQL query and bind vars associated with the invocant.
112
03834f77 113This is generally used as the RHS for a subquery.
c7a9d102 114
115=cut
116
0f6fc705 117sub as_query { return shift->_resultset->as_query(@_) }
c7a9d102 118
2bb7b40b 119=head2 next
120
121=over 4
122
123=item Arguments: none
124
125=item Return Value: $value
126
127=back
128
eb98561c 129Returns the next value of the column in the resultset (or C<undef> if
130there is none).
2bb7b40b 131
eb98561c 132Much like L<DBIx::Class::ResultSet/next> but just returning the
133one value.
2bb7b40b 134
135=cut
136
137sub next {
138 my $self = shift;
b7c79955 139
140 # using cursor so we don't inflate anything
66521001 141 my ($row) = $self->_resultset->cursor->next;
b7c79955 142
2bb7b40b 143 return $row;
144}
145
146=head2 all
147
148=over 4
149
150=item Arguments: none
151
152=item Return Value: @values
153
154=back
155
eb98561c 156Returns all values of the column in the resultset (or C<undef> if
157there are none).
2bb7b40b 158
eb98561c 159Much like L<DBIx::Class::ResultSet/all> but returns values rather
160than row objects.
2bb7b40b 161
162=cut
163
164sub all {
165 my $self = shift;
b7c79955 166
167 # using cursor so we don't inflate anything
66521001 168 return map { $_->[0] } $self->_resultset->cursor->all;
169}
170
171=head2 reset
172
173=over 4
174
175=item Arguments: none
176
177=item Return Value: $self
178
179=back
180
181Resets the underlying resultset's cursor, so you can iterate through the
182elements of the column again.
183
184Much like L<DBIx::Class::ResultSet/reset>.
185
186=cut
187
188sub reset {
189 my $self = shift;
190 $self->_resultset->cursor->reset;
b7c79955 191 return $self;
66521001 192}
193
194=head2 first
195
196=over 4
197
198=item Arguments: none
199
200=item Return Value: $value
201
202=back
203
204Resets the underlying resultset and returns the next value of the column in the
205resultset (or C<undef> if there is none).
206
207Much like L<DBIx::Class::ResultSet/first> but just returning the one value.
208
209=cut
210
211sub first {
212 my $self = shift;
b7c79955 213
214 # using cursor so we don't inflate anything
215 $self->_resultset->cursor->reset;
01dc6781 216 my ($row) = $self->_resultset->cursor->next;
b7c79955 217
66521001 218 return $row;
2bb7b40b 219}
220
4e55c3ae 221=head2 single
222
223=over 4
224
225=item Arguments: none
226
227=item Return Value: $value
228
229=back
230
231Much like L<DBIx::Class::ResultSet/single> fetches one and only one column
232value using the cursor directly. If additional rows are present a warning
233is issued before discarding the cursor.
234
235=cut
236
237sub single {
238 my $self = shift;
239
240 my $attrs = $self->_resultset->_resolved_attrs;
241 my ($row) = $self->_resultset->result_source->storage->select_single(
242 $attrs->{from}, $attrs->{select}, $attrs->{where}, $attrs
243 );
244
245 return $row;
246}
247
2bb7b40b 248=head2 min
249
250=over 4
251
252=item Arguments: none
253
254=item Return Value: $lowest_value
255
256=back
257
eb98561c 258 my $first_year = $year_col->min();
259
260Wrapper for ->func. Returns the lowest value of the column in the
261resultset (or C<undef> if there are none).
2bb7b40b 262
263=cut
264
265sub min {
6b051e14 266 return shift->func('MIN');
2bb7b40b 267}
268
4fa7bc22 269=head2 min_rs
270
271=over 4
272
273=item Arguments: none
274
275=item Return Value: $resultset
276
277=back
278
279 my $rs = $year_col->min_rs();
280
281Wrapper for ->func_rs for function MIN().
282
283=cut
284
285sub min_rs { return shift->func_rs('MIN') }
286
2bb7b40b 287=head2 max
288
289=over 4
290
291=item Arguments: none
292
293=item Return Value: $highest_value
294
295=back
296
eb98561c 297 my $last_year = $year_col->max();
298
299Wrapper for ->func. Returns the highest value of the column in the
300resultset (or C<undef> if there are none).
2bb7b40b 301
302=cut
303
304sub max {
6b051e14 305 return shift->func('MAX');
2bb7b40b 306}
307
4fa7bc22 308=head2 max_rs
309
310=over 4
311
312=item Arguments: none
313
314=item Return Value: $resultset
315
316=back
317
318 my $rs = $year_col->max_rs();
319
320Wrapper for ->func_rs for function MAX().
321
322=cut
323
324sub max_rs { return shift->func_rs('MAX') }
325
2bb7b40b 326=head2 sum
327
328=over 4
329
330=item Arguments: none
331
332=item Return Value: $sum_of_values
333
334=back
335
eb98561c 336 my $total = $prices_col->sum();
337
338Wrapper for ->func. Returns the sum of all the values in the column of
339the resultset. Use on varchar-like columns at your own risk.
2bb7b40b 340
341=cut
342
343sub sum {
6b051e14 344 return shift->func('SUM');
2bb7b40b 345}
346
4fa7bc22 347=head2 sum_rs
348
349=over 4
350
351=item Arguments: none
352
353=item Return Value: $resultset
354
355=back
356
357 my $rs = $year_col->sum_rs();
358
359Wrapper for ->func_rs for function SUM().
360
361=cut
362
363sub sum_rs { return shift->func_rs('SUM') }
364
2bb7b40b 365=head2 func
366
367=over 4
368
369=item Arguments: $function
370
371=item Return Value: $function_return_value
372
373=back
374
e8419341 375 $rs = $schema->resultset("CD")->search({});
376 $length = $rs->get_column('title')->func('LENGTH');
2bb7b40b 377
eb98561c 378Runs a query using the function on the column and returns the
379value. Produces the following SQL:
380
381 SELECT LENGTH( title ) FROM cd me
2bb7b40b 382
383=cut
384
385sub func {
6b051e14 386 my ($self,$function) = @_;
4fa7bc22 387 my $cursor = $self->func_rs($function)->cursor;
d4daee7b 388
5d62876f 389 if( wantarray ) {
390 return map { $_->[ 0 ] } $cursor->all;
391 }
392
393 return ( $cursor->next )[ 0 ];
2bb7b40b 394}
395
4fa7bc22 396=head2 func_rs
397
398=over 4
399
400=item Arguments: $function
401
402=item Return Value: $resultset
403
404=back
405
406Creates the resultset that C<func()> uses to run its query.
407
408=cut
409
410sub func_rs {
411 my ($self,$function) = @_;
412 return $self->{_parent_resultset}->search(
413 undef, {
414 select => {$function => $self->{_select}},
415 as => [$self->{_as}],
416 },
417 );
418}
419
5d1fc7dc 420=head2 throw_exception
421
422See L<DBIx::Class::Schema/throw_exception> for details.
d4daee7b 423
5d1fc7dc 424=cut
d4daee7b 425
5d1fc7dc 426sub throw_exception {
427 my $self=shift;
1a58752c 428
5d1fc7dc 429 if (ref $self && $self->{_parent_resultset}) {
1a58752c 430 $self->{_parent_resultset}->throw_exception(@_);
431 }
432 else {
433 DBIx::Class::Exception->throw(@_);
5d1fc7dc 434 }
435}
436
b6e85b48 437# _resultset
438#
439# Arguments: none
440#
441# Return Value: $resultset
442#
443# $year_col->_resultset->next
444#
445# Returns the underlying resultset. Creates it from the parent resultset if
446# necessary.
b7c79955 447#
66521001 448sub _resultset {
449 my $self = shift;
450
451 return $self->{_resultset} ||= $self->{_parent_resultset}->search(undef,
452 {
453 select => [$self->{_select}],
454 as => [$self->{_as}]
455 }
456 );
457}
458
2bb7b40b 4591;
460
461=head1 AUTHORS
462
463Luke Saunders <luke.saunders@gmail.com>
464
eb98561c 465Jess Robinson
466
2bb7b40b 467=head1 LICENSE
468
469You may distribute this code under the same terms as Perl itself.
470
471=cut