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