Switch out one more reliance on connect_info
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultSetColumn.pm
CommitLineData
2bb7b40b 1package DBIx::Class::ResultSetColumn;
2use strict;
3use warnings;
4use base 'DBIx::Class';
5
6=head1 NAME
7
8 DBIx::Class::ResultSetColumn - helpful methods for messing
9 with a single column of the resultset
10
11=head1 SYNOPSIS
12
13 $rs = $schema->resultset('CD')->search({ artist => 'Tool' });
14 $rs_column = $rs->get_column('year');
15 $max_year = $rs_column->max; #returns latest year
16
17=head1 DESCRIPTION
18
eb98561c 19A convenience class used to perform operations on a specific column of
20a resultset.
2bb7b40b 21
22=cut
23
24=head1 METHODS
25
26=head2 new
27
28 my $obj = DBIx::Class::ResultSetColumn->new($rs, $column);
29
eb98561c 30Creates a new resultset column object from the resultset and column
31passed as params. Used internally by L<DBIx::Class::ResultSet/get_column>.
2bb7b40b 32
33=cut
34
35sub new {
36 my ($class, $rs, $column) = @_;
37 $class = ref $class if ref $class;
3f6cc7e4 38 my $new_parent_rs = $rs->search_rs; # we don't want to mess up the original, so clone it
39 $new_parent_rs->{attrs}->{prefetch} = undef; # prefetch causes additional columns to be fetched
40 my $new = bless { _column => $column, _parent_resultset => $new_parent_rs }, $class;
6b051e14 41 $new->throw_exception("column must be supplied") unless $column;
2bb7b40b 42 return $new;
43}
44
45=head2 next
46
47=over 4
48
49=item Arguments: none
50
51=item Return Value: $value
52
53=back
54
eb98561c 55Returns the next value of the column in the resultset (or C<undef> if
56there is none).
2bb7b40b 57
eb98561c 58Much like L<DBIx::Class::ResultSet/next> but just returning the
59one value.
2bb7b40b 60
61=cut
62
63sub next {
64 my $self = shift;
2bb7b40b 65 $self->{_resultset} = $self->{_parent_resultset}->search(undef, {select => [$self->{_column}], as => [$self->{_column}]}) unless ($self->{_resultset});
66 my ($row) = $self->{_resultset}->cursor->next;
67 return $row;
68}
69
70=head2 all
71
72=over 4
73
74=item Arguments: none
75
76=item Return Value: @values
77
78=back
79
eb98561c 80Returns all values of the column in the resultset (or C<undef> if
81there are none).
2bb7b40b 82
eb98561c 83Much like L<DBIx::Class::ResultSet/all> but returns values rather
84than row objects.
2bb7b40b 85
86=cut
87
88sub all {
89 my $self = shift;
90 return map {$_->[0]} $self->{_parent_resultset}->search(undef, {select => [$self->{_column}], as => [$self->{_column}]})->cursor->all;
91}
92
93=head2 min
94
95=over 4
96
97=item Arguments: none
98
99=item Return Value: $lowest_value
100
101=back
102
eb98561c 103 my $first_year = $year_col->min();
104
105Wrapper for ->func. Returns the lowest value of the column in the
106resultset (or C<undef> if there are none).
2bb7b40b 107
108=cut
109
110sub min {
6b051e14 111 return shift->func('MIN');
2bb7b40b 112}
113
114=head2 max
115
116=over 4
117
118=item Arguments: none
119
120=item Return Value: $highest_value
121
122=back
123
eb98561c 124 my $last_year = $year_col->max();
125
126Wrapper for ->func. Returns the highest value of the column in the
127resultset (or C<undef> if there are none).
2bb7b40b 128
129=cut
130
131sub max {
6b051e14 132 return shift->func('MAX');
2bb7b40b 133}
134
135=head2 sum
136
137=over 4
138
139=item Arguments: none
140
141=item Return Value: $sum_of_values
142
143=back
144
eb98561c 145 my $total = $prices_col->sum();
146
147Wrapper for ->func. Returns the sum of all the values in the column of
148the resultset. Use on varchar-like columns at your own risk.
2bb7b40b 149
150=cut
151
152sub sum {
6b051e14 153 return shift->func('SUM');
2bb7b40b 154}
155
156=head2 func
157
158=over 4
159
160=item Arguments: $function
161
162=item Return Value: $function_return_value
163
164=back
165
e8419341 166 $rs = $schema->resultset("CD")->search({});
167 $length = $rs->get_column('title')->func('LENGTH');
2bb7b40b 168
eb98561c 169Runs a query using the function on the column and returns the
170value. Produces the following SQL:
171
172 SELECT LENGTH( title ) FROM cd me
2bb7b40b 173
174=cut
175
176sub func {
6b051e14 177 my ($self,$function) = @_;
5d62876f 178 my $cursor = $self->{_parent_resultset}->search(undef, {select => {$function => $self->{_column}}, as => [$self->{_column}]})->cursor;
179
180 if( wantarray ) {
181 return map { $_->[ 0 ] } $cursor->all;
182 }
183
184 return ( $cursor->next )[ 0 ];
2bb7b40b 185}
186
1871;
188
189=head1 AUTHORS
190
191Luke Saunders <luke.saunders@gmail.com>
192
eb98561c 193Jess Robinson
194
2bb7b40b 195=head1 LICENSE
196
197You may distribute this code under the same terms as Perl itself.
198
199=cut