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