| 1 | package Jifty::DBI::Collection; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 13 | ▶ | use Scalar::Defer qw/lazy/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 13 | ▶ | use Scalar::Util qw/weaken/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | use overload ( | ||||||||||||||||||||||||||||||||||||||||||
| 8 | '@{}' => \&items_array_ref, | ||||||||||||||||||||||||||||||||||||||||||
| 9 | '<>' => \&next, | ||||||||||||||||||||||||||||||||||||||||||
| 10 | 435 | ▶ | bool => sub {shift}, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 13 | fallback => 1 | |||||||||||||||||||||||||||||||||||||||||
| 12 | 13 | ▶ | ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | Jifty::DBI::Collection - Encapsulate SQL queries and rows in simple | ||||||||||||||||||||||||||||||||||||||||||
| 17 | perl objects | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | use Jifty::DBI::Collection; | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | package My::ThingCollection; | ||||||||||||||||||||||||||||||||||||||||||
| 24 | use base qw/Jifty::DBI::Collection/; | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | package My::Thing; | ||||||||||||||||||||||||||||||||||||||||||
| 27 | use Jifty::DBI::Schema; | ||||||||||||||||||||||||||||||||||||||||||
| 28 | use Jifty::DBI::Record schema { | ||||||||||||||||||||||||||||||||||||||||||
| 29 | column column_1 => type is 'text'; | ||||||||||||||||||||||||||||||||||||||||||
| 30 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | package main; | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | use Jifty::DBI::Handle; | ||||||||||||||||||||||||||||||||||||||||||
| 35 | my $handle = Jifty::DBI::Handle->new(); | ||||||||||||||||||||||||||||||||||||||||||
| 36 | $handle->connect( driver => 'SQLite', database => "my_test_db" ); | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | my $collection = My::ThingCollection->new( handle => $handle ); | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | $collection->limit( column => "column_1", value => "matchstring" ); | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | while ( my $record = $collection->next ) { | ||||||||||||||||||||||||||||||||||||||||||
| 43 | print $record->id; | ||||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | This module provides an object-oriented mechanism for retrieving and | ||||||||||||||||||||||||||||||||||||||||||
| 49 | updating data in a DBI-accessible database. | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | In order to use this module, you should create a subclass of | ||||||||||||||||||||||||||||||||||||||||||
| 52 | L<Jifty::DBI::Collection> and a subclass of L<Jifty::DBI::Record> for | ||||||||||||||||||||||||||||||||||||||||||
| 53 | each table that you wish to access. (See the documentation of | ||||||||||||||||||||||||||||||||||||||||||
| 54 | L<Jifty::DBI::Record> for more information on subclassing it.) | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | Your L<Jifty::DBI::Collection> subclass must override L</new_item>, | ||||||||||||||||||||||||||||||||||||||||||
| 57 | and probably should override at least L</_init> also; at the very | ||||||||||||||||||||||||||||||||||||||||||
| 58 | least, L</_init> should probably call L</_handle> and L</_table> to | ||||||||||||||||||||||||||||||||||||||||||
| 59 | set the database handle (a L<Jifty::DBI::Handle> object) and table | ||||||||||||||||||||||||||||||||||||||||||
| 60 | name for the class -- see the L</SYNOPSIS> for an example. | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 13 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 13 | ▶ | use Data::Page; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 13 | ▶ | use Clone; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 13 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 13 | ▶ | use base qw/Class::Accessor::Fast/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 13 | __PACKAGE__->mk_accessors(qw/pager prefetch_related derived _handle _is_limited rows_per_page/); | |||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | Creates a new L<Jifty::DBI::Collection> object and immediately calls | ||||||||||||||||||||||||||||||||||||||||||
| 78 | L</_init> with the same parameters that were passed to L</new>. If | ||||||||||||||||||||||||||||||||||||||||||
| 79 | you haven't overridden L<_init> in your subclass, this means that you | ||||||||||||||||||||||||||||||||||||||||||
| 80 | should pass in a L<Jifty::DBI::Handle> (or one of its subclasses) like | ||||||||||||||||||||||||||||||||||||||||||
| 81 | this: | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | my $collection = My::Jifty::DBI::Subclass->new( handle => $handle ); | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | However, if your subclass overrides L</_init> you do not need to take | ||||||||||||||||||||||||||||||||||||||||||
| 86 | a handle argument, as long as your subclass takes care of calling the | ||||||||||||||||||||||||||||||||||||||||||
| 87 | L</_handle> method somehow. This is useful if you want all of your | ||||||||||||||||||||||||||||||||||||||||||
| 88 | L<Jifty::DBI> objects to use a shared global handle and don't want to | ||||||||||||||||||||||||||||||||||||||||||
| 89 | have to explicitly pass it in each time, for example. | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 230 | ▶ | my $proto = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 230 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 230 | my $self = {}; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 230 | bless( $self, $class ); | |||||||||||||||||||||||||||||||||||||||||
| 98 | 230 | ▶ | $self->record_class( $proto->record_class ) if ref $proto; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 230 | $self->_init(@_); | |||||||||||||||||||||||||||||||||||||||||
| 100 | 230 | return ($self); | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | =head2 _init | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | This method is called by L<new> with whatever arguments were passed to | ||||||||||||||||||||||||||||||||||||||||||
| 106 | L</new>. By default, it takes a C<Jifty::DBI::Handle> object as a | ||||||||||||||||||||||||||||||||||||||||||
| 107 | C<handle> argument and calls L</_handle> with that. | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | sub _init { | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 230 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_init: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 230 | my %args = ( | |||||||||||||||||||||||||||||||||||||||||
| 114 | handle => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 115 | derived => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 116 | @_ | ||||||||||||||||||||||||||||||||||||||||||
| 117 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 230 | ▶ | $self->_handle( $args{'handle'} ) if ( $args{'handle'} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 230 | ▶ | $self->derived( $args{'derived'} ) if ( $args{'derived'} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 230 | $self->table( $self->record_class->table() ); | |||||||||||||||||||||||||||||||||||||||||
| 121 | 230 | $self->clean_slate(%args); | |||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | sub _init_pager { | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 296 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_init_pager: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 296 | return $self->pager( Data::Page->new(0, 10, 1) ); | |||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | =head2 clean_slate | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | This completely erases all the data in the object. It's useful if a | ||||||||||||||||||||||||||||||||||||||||||
| 132 | subclass is doing funky stuff to keep track of a search and wants to | ||||||||||||||||||||||||||||||||||||||||||
| 133 | reset the object's data without losing its own data; it's probably | ||||||||||||||||||||||||||||||||||||||||||
| 134 | cleaner to accomplish that in a different way, though. | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | sub clean_slate { | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 296 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clean_slate: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 296 | my %args = (@_); | |||||||||||||||||||||||||||||||||||||||||
| 141 | 296 | $self->redo_search(); | |||||||||||||||||||||||||||||||||||||||||
| 142 | 296 | $self->_init_pager(); | |||||||||||||||||||||||||||||||||||||||||
| 143 | 296 | $self->{'itemscount'} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 144 | 296 | $self->{'tables'} = ""; | |||||||||||||||||||||||||||||||||||||||||
| 145 | 296 | $self->{'auxillary_tables'} = ""; | |||||||||||||||||||||||||||||||||||||||||
| 146 | 296 | $self->{'where_clause'} = ""; | |||||||||||||||||||||||||||||||||||||||||
| 147 | 296 | $self->{'limit_clause'} = ""; | |||||||||||||||||||||||||||||||||||||||||
| 148 | 296 | $self->{'order'} = ""; | |||||||||||||||||||||||||||||||||||||||||
| 149 | 296 | $self->{'alias_count'} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 150 | 296 | $self->{'first_row'} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 296 | delete $self->{$_} for qw( | |||||||||||||||||||||||||||||||||||||||||
| 153 | items | ||||||||||||||||||||||||||||||||||||||||||
| 154 | joins | ||||||||||||||||||||||||||||||||||||||||||
| 155 | raw_rows | ||||||||||||||||||||||||||||||||||||||||||
| 156 | count_all | ||||||||||||||||||||||||||||||||||||||||||
| 157 | subclauses | ||||||||||||||||||||||||||||||||||||||||||
| 158 | restrictions | ||||||||||||||||||||||||||||||||||||||||||
| 159 | _open_parens | ||||||||||||||||||||||||||||||||||||||||||
| 160 | criteria_count | ||||||||||||||||||||||||||||||||||||||||||
| 161 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | 296 | $self->rows_per_page(0); | |||||||||||||||||||||||||||||||||||||||||
| 164 | 296 | $self->implicit_clauses(%args); | |||||||||||||||||||||||||||||||||||||||||
| 165 | 296 | $self->_is_limited(0); | |||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | =head2 implicit_clauses | ||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | Called by L</clean_slate> to set up any implicit clauses that the | ||||||||||||||||||||||||||||||||||||||||||
| 171 | collection B<always> has. Defaults to doing nothing. Is passed the | ||||||||||||||||||||||||||||||||||||||||||
| 172 | paramhash passed into L</new>. | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | ▶ | sub implicit_clauses { } | |||||||||||||||||||||||||||||||||||||||||
|
Subroutine
implicit_clauses: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | =head2 _handle [DBH] | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | Get or set this object's L<Jifty::DBI::Handle> object. | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | =head2 _do_search | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | This internal private method actually executes the search on the | ||||||||||||||||||||||||||||||||||||||||||
| 187 | database; it is called automatically the first time that you actually | ||||||||||||||||||||||||||||||||||||||||||
| 188 | need results (such as a call to L</next>). | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | sub _do_search { | ||||||||||||||||||||||||||||||||||||||||||
| 193 | 48 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_do_search: called CC=25 SCAR=32.9
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | 48 | my $query_string = $self->build_select_query(); | |||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | # If we're about to redo the search, we need an empty set of items | ||||||||||||||||||||||||||||||||||||||||||
| 198 | 48 | delete $self->{'items'}; | |||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | 48 | my $records = $self->_handle->simple_query($query_string); | |||||||||||||||||||||||||||||||||||||||||
| 201 | 48 | ▶ | return 0 unless $records; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 202 | 48 | my @names = @{ $records->{NAME_lc} }; | |||||||||||||||||||||||||||||||||||||||||
| 203 | 48 | my $data = {}; | |||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | 48 | ▶ | my @tables = map { $_->{alias} } values %{ $self->prefetch_related || {} }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | 48 | ▶ | unless ( @tables ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 208 | 45 | ▶ | while ( my $row = $records->fetchrow_hashref() ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | $row->{ substr($_, 5) } = delete $row->{ $_ } | ||||||||||||||||||||||||||||||||||||||||||
| 210 | 130 | foreach grep rindex($_, "main_", 0) == 0, keys %$row; | |||||||||||||||||||||||||||||||||||||||||
| 211 | 130 | my $item = $self->new_item; | |||||||||||||||||||||||||||||||||||||||||
| 212 | 130 | $item->load_from_hash($row, fast => 1); | |||||||||||||||||||||||||||||||||||||||||
| 213 | 130 | $self->add_record($item); | |||||||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||||||
| 215 | 45 | ▶ | if ( $records->err ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | $self->{'must_redo_search'} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | 45 | return $self->_record_count; | |||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | 3 | my @order; | |||||||||||||||||||||||||||||||||||||||||
| 223 | 3 | my $i = 1; | |||||||||||||||||||||||||||||||||||||||||
| 224 | 3 | ▶ | while ( my $base_row = $records->fetchrow_hashref() ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | 18 | my $main_pkey = $base_row->{ $names[0] }; | |||||||||||||||||||||||||||||||||||||||||
| 226 | 18 | ▶ | $main_pkey = 'unique-'.$i++ if $self->{group_by}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | 18 | ▶ | push @order, $main_pkey | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$order[0] and $order[-1] eq $main_pkey
MC/DC100%2/2
$order[0] and $order[-1] eq $main_pkey
$order[0]
$order[-1] eq $main_pkey
Truth table100%3/3
A and B
A= $order[0]B= $order[-1] eq $main_pkey
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | unless ( $order[0] && $order[-1] eq $main_pkey ); | ||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | # let's chop the row into subrows; | ||||||||||||||||||||||||||||||||||||||||||
| 231 | 18 | foreach my $table ('main', @tables) { | |||||||||||||||||||||||||||||||||||||||||
| 232 | 36 | my %tmp = (); | |||||||||||||||||||||||||||||||||||||||||
| 233 | 36 | for my $k( grep rindex($_, $table ."_", 0) == 0, keys %$base_row ) { | |||||||||||||||||||||||||||||||||||||||||
| 234 | 90 | $tmp{ substr($k, length($table)+1) } = $base_row->{ $k }; | |||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||
| 236 | 36 | ▶ | $data->{$main_pkey}{$table}{ $base_row->{ $table . '_id' } || $main_pkey } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$$base_row{$table . '_id'} || $main_pkey
MC/DC0%0/2
$$base_row{$table . '_id'} || $main_pkey
$$base_row{$table . '_id'}
$main_pkey
Truth table33%1/3
A || B
A= $$base_row{$table . '_id'}B= $main_pkey
| |||||||||||||||||||||||||||||||||||||||||||
| 237 | = \%tmp if keys %tmp; | ||||||||||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | 3 | foreach my $row_id (@order) { | |||||||||||||||||||||||||||||||||||||||||
| 242 | 10 | my $item; | |||||||||||||||||||||||||||||||||||||||||
| 243 | 10 | foreach my $row ( values %{ $data->{$row_id}->{'main'} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 244 | 10 | $item = $self->new_item(); | |||||||||||||||||||||||||||||||||||||||||
| 245 | 10 | $item->load_from_hash($row, fast => 1); | |||||||||||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||||||||||
| 247 | 10 | foreach my $alias ( grep { $_ ne 'main' } keys %{ $data->{$row_id} } ) | |||||||||||||||||||||||||||||||||||||||||
| 248 | { | ||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||
| 250 | 10 | my $related_rows = $data->{$row_id}->{$alias}; | |||||||||||||||||||||||||||||||||||||||||
| 251 | 10 | my ( $class, $col_name ) | |||||||||||||||||||||||||||||||||||||||||
| 252 | = $self->class_and_column_for_alias($alias); | ||||||||||||||||||||||||||||||||||||||||||
| 253 | 10 | ▶ | next unless $class; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | 11 | my @rows = sort { $a->{id} <=> $b->{id} } | |||||||||||||||||||||||||||||||||||||||||
| 256 | 10 | grep { $_->{id} } values %$related_rows; | |||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||
| 258 | 10 | ▶ | if ( $class->isa('Jifty::DBI::Collection') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 259 | 4 | my $collection = $class->new( $self->_new_collection_args, | |||||||||||||||||||||||||||||||||||||||||
| 260 | derived => 1 ); | ||||||||||||||||||||||||||||||||||||||||||
| 261 | 4 | foreach my $row (@rows) { | |||||||||||||||||||||||||||||||||||||||||
| 262 | 12 | my $entry = $collection->new_item; | |||||||||||||||||||||||||||||||||||||||||
| 263 | 12 | $entry->load_from_hash($row, fast => 1); | |||||||||||||||||||||||||||||||||||||||||
| 264 | 12 | $collection->add_record($entry); | |||||||||||||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | 4 | $item->prefetched( $col_name => $collection ); | |||||||||||||||||||||||||||||||||||||||||
| 268 | } elsif ( $class->isa('Jifty::DBI::Record') ) { | ||||||||||||||||||||||||||||||||||||||||||
| 269 | 6 | ▶ | warn "Multiple rows returned for $class in prefetch" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 270 | if @rows > 1; | ||||||||||||||||||||||||||||||||||||||||||
| 271 | 6 | my $entry = $class->new( $self->_new_record_args ); | |||||||||||||||||||||||||||||||||||||||||
| 272 | 6 | ▶ | $entry->load_from_hash( shift(@rows), fast => 1 ) if @rows; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 273 | 6 | $item->prefetched( $col_name => $entry ); | |||||||||||||||||||||||||||||||||||||||||
| 274 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | Carp::cluck( | |||||||||||||||||||||||||||||||||||||||||
| 276 | "Asked to prefetch $alias as a $class. Don't know how to handle $class" | ||||||||||||||||||||||||||||||||||||||||||
| 277 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||||||
| 280 | 10 | $self->add_record($item); | |||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||
| 283 | 3 | ▶ | if ( $records->err ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | $self->{'must_redo_search'} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | 3 | return $self->_record_count; | |||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | sub _new_record_args { | ||||||||||||||||||||||||||||||||||||||||||
| 291 | 192 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_new_record_args: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 292 | 192 | return ( handle => $self->_handle ); | |||||||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | sub _new_collection_args { | ||||||||||||||||||||||||||||||||||||||||||
| 296 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_new_collection_args: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 297 | 5 | return ( handle => $self->_handle ); | |||||||||||||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||
| 300 | =head2 add_record RECORD | ||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | Adds a record object to this collection. | ||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | This method automatically sets our "must redo search" flag to 0 and our "we have limits" flag to 1. | ||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | Without those two flags, counting the number of items wouldn't work. | ||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||
| 310 | sub add_record { | ||||||||||||||||||||||||||||||||||||||||||
| 311 | 152 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_record: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 312 | 152 | my $record = shift; | |||||||||||||||||||||||||||||||||||||||||
| 313 | 152 | $self->_is_limited(1); | |||||||||||||||||||||||||||||||||||||||||
| 314 | 152 | $self->{'must_redo_search'} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 315 | 152 | push @{ $self->{'items'} }, $record; | |||||||||||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||
| 318 | =head2 _record_count | ||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||
| 320 | This private internal method returns the number of | ||||||||||||||||||||||||||||||||||||||||||
| 321 | L<Jifty::DBI::Record> objects saved as a result of the last query. | ||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||
| 323 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||
| 325 | sub _record_count { | ||||||||||||||||||||||||||||||||||||||||||
| 326 | 240 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_record_count: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 327 | 240 | ▶ | return 0 unless defined $self->{'items'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 328 | 220 | return scalar @{ $self->{'items'} }; | |||||||||||||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||
| 331 | =head2 _do_count | ||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | This internal private method actually executes a counting operation on | ||||||||||||||||||||||||||||||||||||||||||
| 334 | the database; it is used by L</count> and L</count_all>. | ||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||
| 338 | sub _do_count { | ||||||||||||||||||||||||||||||||||||||||||
| 339 | 78 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_do_count: called CC=5 SCAR=16.7
| |||||||||||||||||||||||||||||||||||||||||||
| 340 | 78 | ▶ | my $all = shift || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 0
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||
| 342 | 78 | my $query_string = $self->build_select_count_query(); | |||||||||||||||||||||||||||||||||||||||||
| 343 | 78 | my $records = $self->_handle->simple_query($query_string); | |||||||||||||||||||||||||||||||||||||||||
| 344 | 78 | ▶ | return 0 unless $records; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||
| 346 | 78 | my @row = $records->fetchrow_array(); | |||||||||||||||||||||||||||||||||||||||||
| 347 | 78 | ▶ | return 0 if $records->err; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||
| 349 | 78 | ▶ | $self->{ $all ? 'count_all' : 'raw_rows' } = $row[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||
| 351 | 78 | return ( $row[0] ); | |||||||||||||||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||
| 354 | =head2 _apply_limits STATEMENTREF | ||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||
| 356 | This routine takes a reference to a scalar containing an SQL | ||||||||||||||||||||||||||||||||||||||||||
| 357 | statement. It massages the statement to limit the returned rows to | ||||||||||||||||||||||||||||||||||||||||||
| 358 | only C<< $self->rows_per_page >> rows, skipping C<< $self->first_row >> | ||||||||||||||||||||||||||||||||||||||||||
| 359 | rows. (That is, if rows are numbered starting from 0, row number | ||||||||||||||||||||||||||||||||||||||||||
| 360 | C<< $self->first_row >> will be the first row returned.) Note that it | ||||||||||||||||||||||||||||||||||||||||||
| 361 | probably makes no sense to set these variables unless you are also | ||||||||||||||||||||||||||||||||||||||||||
| 362 | enforcing an ordering on the rows (with L</order_by>, say). | ||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||
| 364 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||
| 366 | sub _apply_limits { | ||||||||||||||||||||||||||||||||||||||||||
| 367 | 246 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_apply_limits: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 368 | 246 | my $statementref = shift; | |||||||||||||||||||||||||||||||||||||||||
| 369 | 246 | $self->_handle->apply_limits( $statementref, $self->rows_per_page, | |||||||||||||||||||||||||||||||||||||||||
| 370 | $self->first_row ); | ||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||
| 374 | =head2 _distinct_query STATEMENTREF | ||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||
| 376 | This routine takes a reference to a scalar containing an SQL | ||||||||||||||||||||||||||||||||||||||||||
| 377 | statement. It massages the statement to ensure a distinct result set | ||||||||||||||||||||||||||||||||||||||||||
| 378 | is returned. | ||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||
| 380 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||
| 382 | sub _distinct_query { | ||||||||||||||||||||||||||||||||||||||||||
| 383 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_distinct_query: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 384 | 5 | my $statementref = shift; | |||||||||||||||||||||||||||||||||||||||||
| 385 | 5 | $self->_handle->distinct_query( $statementref, $self ); | |||||||||||||||||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||
| 388 | =head2 _build_joins | ||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||
| 390 | Build up all of the joins we need to perform this query. | ||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||
| 392 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||
| 394 | sub _build_joins { | ||||||||||||||||||||||||||||||||||||||||||
| 395 | 328 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_joins: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||
| 397 | 328 | return ( $self->_handle->_build_joins($self) ); | |||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||
| 401 | =head2 _is_joined | ||||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||
| 403 | Returns true if this collection will be joining multiple tables | ||||||||||||||||||||||||||||||||||||||||||
| 404 | together. | ||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||
| 406 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||
| 408 | sub _is_joined { | ||||||||||||||||||||||||||||||||||||||||||
| 409 | 333 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_joined: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 410 | 333 | ▶ | if ( $self->{'joins'} && keys %{ $self->{'joins'} } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"joins"} and keys %{$$self{"joins"};}
MC/DC100%2/2
$self->{"joins"} and keys %{$$self{"joins"};}
$self->{"joins"}
keys %{$$self{"joins"};}
Truth table100%3/3
A and B
A= $self->{"joins"}B= keys %{$$self{"joins"};}
| |||||||||||||||||||||||||||||||||||||||||||
| 411 | 19 | return (1); | |||||||||||||||||||||||||||||||||||||||||
| 412 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 413 | 314 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||
| 417 | =head2 _is_distinctly_joined | ||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||
| 419 | Returns true if this collection is joining multiple table, but is | ||||||||||||||||||||||||||||||||||||||||||
| 420 | joining other table's distinct fields, hence resulting in distinct | ||||||||||||||||||||||||||||||||||||||||||
| 421 | resultsets. The behaviour is undefined if called on a non-joining | ||||||||||||||||||||||||||||||||||||||||||
| 422 | collection. | ||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||
| 424 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||
| 426 | sub _is_distinctly_joined { | ||||||||||||||||||||||||||||||||||||||||||
| 427 | 18 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_distinctly_joined: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 428 | 18 | ▶ | if ( $self->{'joins'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 429 | 18 | for ( values %{ $self->{'joins'} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 430 | 18 | ▶ | return 0 unless $_->{is_distinct}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||
| 433 | 4 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 434 | } | ||||||||||||||||||||||||||||||||||||||||||
| 435 | } | ||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | =head2 _is_limited | ||||||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||||||
| 439 | If we've limited down this search, return true. Otherwise, return | ||||||||||||||||||||||||||||||||||||||||||
| 440 | false. | ||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||
| 442 | C<1> means "we have limits" | ||||||||||||||||||||||||||||||||||||||||||
| 443 | C<-1> means "we should return all rows. We want no where clause" | ||||||||||||||||||||||||||||||||||||||||||
| 444 | C<0> means "no limits have been applied yet. | ||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||
| 446 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||
| 448 | =head2 build_select_query | ||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||
| 450 | Builds a query string for a "SELECT rows from Tables" statement for | ||||||||||||||||||||||||||||||||||||||||||
| 451 | this collection | ||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||
| 453 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||
| 455 | sub build_select_query { | ||||||||||||||||||||||||||||||||||||||||||
| 456 | 246 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
build_select_query: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||
| 458 | 246 | ▶ | return "" if $self->derived; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||
| 460 | # The initial SELECT or SELECT DISTINCT is decided later | ||||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||||
| 462 | 246 | my $query_string = $self->_build_joins . " "; | |||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||
| 464 | 246 | ▶ | if ( $self->_is_limited ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 465 | 246 | $query_string .= $self->_where_clause . " "; | |||||||||||||||||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||||||||||||||||
| 467 | 246 | ▶ | if ( $self->distinct_required ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||
| 469 | # DISTINCT query only required for multi-table selects | ||||||||||||||||||||||||||||||||||||||||||
| 470 | 5 | $self->_distinct_query( \$query_string ); | |||||||||||||||||||||||||||||||||||||||||
| 471 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 472 | 241 | $query_string | |||||||||||||||||||||||||||||||||||||||||
| 473 | = "SELECT " . $self->query_columns . " FROM $query_string"; | ||||||||||||||||||||||||||||||||||||||||||
| 474 | 241 | $query_string .= $self->_group_clause; | |||||||||||||||||||||||||||||||||||||||||
| 475 | 241 | $query_string .= $self->_order_clause; | |||||||||||||||||||||||||||||||||||||||||
| 476 | } | ||||||||||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||||||||||
| 478 | 246 | $self->_apply_limits( \$query_string ); | |||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||
| 480 | 246 | return ($query_string) | |||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||
| 482 | } | ||||||||||||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||||||||||||
| 484 | =head2 query_columns | ||||||||||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||||||||||
| 486 | The columns that the query would load for result items. By default | ||||||||||||||||||||||||||||||||||||||||||
| 487 | it's everything. | ||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||
| 489 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||||||
| 491 | sub query_columns { | ||||||||||||||||||||||||||||||||||||||||||
| 492 | 246 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
query_columns: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||||||||||
| 494 | 246 | my @cols = (); | |||||||||||||||||||||||||||||||||||||||||
| 495 | 246 | ▶ | if ( $self->{columns} and @{ $self->{columns} } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"columns"} and @{$self->{"columns"};}
MC/DC50%1/2
$self->{"columns"} and @{$self->{"columns"};}
$self->{"columns"}
@{$self->{"columns"};}
Truth table66%2/3
A and B
A= $self->{"columns"}B= @{$self->{"columns"};}
| |||||||||||||||||||||||||||||||||||||||||||
| 496 | 3 | push @cols, @{ $self->{columns} }; | |||||||||||||||||||||||||||||||||||||||||
| 497 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 498 | 243 | push @cols, $self->_qualified_record_columns( 'main' => $self->record_class ); | |||||||||||||||||||||||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||||||||||||||||||||||
| 500 | 246 | ▶ | my %prefetch_related = %{ $self->prefetch_related || {} }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 501 | 246 | foreach my $alias ( keys %prefetch_related ) { | |||||||||||||||||||||||||||||||||||||||||
| 502 | 3 | my $class = $prefetch_related{$alias}{class}; | |||||||||||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||||||||||||
| 504 | 3 | my $reference; | |||||||||||||||||||||||||||||||||||||||||
| 505 | 3 | ▶ | if ( $class->isa('Jifty::DBI::Collection') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 506 | 2 | $reference = $class->record_class; | |||||||||||||||||||||||||||||||||||||||||
| 507 | } elsif ( $class->isa('Jifty::DBI::Record') ) { | ||||||||||||||||||||||||||||||||||||||||||
| 508 | 1 | $reference = $class; | |||||||||||||||||||||||||||||||||||||||||
| 509 | } | ||||||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||||||
| 511 | 3 | my $only_cols = $prefetch_related{$alias}{columns}; | |||||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||||||
| 513 | 3 | push @cols, $self->_qualified_record_columns( $alias => $reference, $only_cols ); | |||||||||||||||||||||||||||||||||||||||||
| 514 | } | ||||||||||||||||||||||||||||||||||||||||||
| 515 | 246 | return CORE::join( ', ', @cols ); | |||||||||||||||||||||||||||||||||||||||||
| 516 | } | ||||||||||||||||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||||||||||||||||
| 518 | =head2 class_and_column_for_alias | ||||||||||||||||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||||||||||||||||
| 520 | Takes the alias you've assigned to a prefetched related | ||||||||||||||||||||||||||||||||||||||||||
| 521 | object. Returns the class of the column we've declared that alias | ||||||||||||||||||||||||||||||||||||||||||
| 522 | prefetches. | ||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||
| 524 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||||||||||
| 526 | sub class_and_column_for_alias { | ||||||||||||||||||||||||||||||||||||||||||
| 527 | 10 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
class_and_column_for_alias: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 528 | 10 | my $alias = shift; | |||||||||||||||||||||||||||||||||||||||||
| 529 | 10 | ▶ | my %prefetch = %{ $self->prefetch_related || {} }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 530 | 10 | my $related = $prefetch{$alias}; | |||||||||||||||||||||||||||||||||||||||||
| 531 | 10 | ▶ | return unless $related; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||||||||||||
| 533 | 10 | return $related->{class}, $related->{name}; | |||||||||||||||||||||||||||||||||||||||||
| 534 | } | ||||||||||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||||||||
| 536 | sub _qualified_record_columns { | ||||||||||||||||||||||||||||||||||||||||||
| 537 | 246 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_qualified_record_columns: called CC=3 SCAR=12.2
| |||||||||||||||||||||||||||||||||||||||||||
| 538 | 246 | my $alias = shift; | |||||||||||||||||||||||||||||||||||||||||
| 539 | 246 | my $item = shift; | |||||||||||||||||||||||||||||||||||||||||
| 540 | 246 | my $only_cols = shift; | |||||||||||||||||||||||||||||||||||||||||
| 541 | 246 | ▶ | my @columns = map { $_->name } grep { !$_->virtual && !$_->computed } $item->columns; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$_->virtual || $_->computed
MC/DC50%1/2
$_->virtual || $_->computed
$_->virtual
$_->computed
Truth table66%2/3
A || B
A= $_->virtualB= $_->computed
| |||||||||||||||||||||||||||||||||||||||||||
| 542 | 246 | ▶ | if ($only_cols) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 543 | 0 | my %wanted = map { +($_ => 1) } @{ $only_cols }; | |||||||||||||||||||||||||||||||||||||||||
| 544 | 0 | @columns = grep { $wanted{$_} } @columns; | |||||||||||||||||||||||||||||||||||||||||
| 545 | } | ||||||||||||||||||||||||||||||||||||||||||
| 546 | 246 | return map {$alias ."." . $_ ." as ". $alias ."_". $_} @columns | |||||||||||||||||||||||||||||||||||||||||
| 547 | } | ||||||||||||||||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||||||||||||||||
| 549 | =head2 prefetch PARAMHASH | ||||||||||||||||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||||||||||||||||
| 551 | Prefetches properties of a related table, in the same query. Possible | ||||||||||||||||||||||||||||||||||||||||||
| 552 | keys in the paramhash are: | ||||||||||||||||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||||||||||||||||
| 554 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||||||||||||
| 556 | =item name | ||||||||||||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||||||||||||
| 558 | This argument is required; it specifies the name of the collection or | ||||||||||||||||||||||||||||||||||||||||||
| 559 | record that is to be prefetched. If the name matches a column with a | ||||||||||||||||||||||||||||||||||||||||||
| 560 | C<refers_to> relationship, the other arguments can be inferred, and | ||||||||||||||||||||||||||||||||||||||||||
| 561 | this is the only parameter which needs to be passed. | ||||||||||||||||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||||||||||||||||
| 563 | It is possible to pass values for C<name> which are not real columns | ||||||||||||||||||||||||||||||||||||||||||
| 564 | in the model; these, while they won't be accessible by calling | ||||||||||||||||||||||||||||||||||||||||||
| 565 | C<< $record-> I<columnname> >> on records in this collection, will | ||||||||||||||||||||||||||||||||||||||||||
| 566 | still be accessible by calling C<< $record->prefetched( I<columnname> ) >>. | ||||||||||||||||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||||||||||||||||
| 568 | =item reference | ||||||||||||||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||||||||||||
| 570 | Specifies the series of column names to traverse to extract the | ||||||||||||||||||||||||||||||||||||||||||
| 571 | information. For instance, if groups referred to multiple users, and | ||||||||||||||||||||||||||||||||||||||||||
| 572 | users referred to multiple phone numbers, then providing | ||||||||||||||||||||||||||||||||||||||||||
| 573 | C<users.phones> would do the two necessary joins to produce a phone | ||||||||||||||||||||||||||||||||||||||||||
| 574 | collection for all users in each group. | ||||||||||||||||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||||||||||||||||
| 576 | This option defaults to the name, and is irrelevant if an C<alias> is | ||||||||||||||||||||||||||||||||||||||||||
| 577 | provided. | ||||||||||||||||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||||||||||||||||
| 579 | =item alias | ||||||||||||||||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||||||||||||||||
| 581 | Specifies an alias which has already been joined to this collection as | ||||||||||||||||||||||||||||||||||||||||||
| 582 | the source of the prefetched data. C<class> will also need to be | ||||||||||||||||||||||||||||||||||||||||||
| 583 | specified. | ||||||||||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||||||||||
| 585 | =item class | ||||||||||||||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||||||||||||||
| 587 | Specifies the class of the data to preload. This is only necessary if | ||||||||||||||||||||||||||||||||||||||||||
| 588 | C<alias> is provided, and C<name> is not the name of a column which | ||||||||||||||||||||||||||||||||||||||||||
| 589 | provides C<refers_to> information. | ||||||||||||||||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||||||||||||||||
| 591 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||||||||||||
| 593 | For backwards compatibility, C<prefetch> can instead be called with | ||||||||||||||||||||||||||||||||||||||||||
| 594 | C<alias> and C<name> as its two arguments, instead of a paramhash. | ||||||||||||||||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||||||||||||||||
| 596 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||||||||||||
| 598 | sub prefetch { | ||||||||||||||||||||||||||||||||||||||||||
| 599 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
prefetch: called CC=13 SCAR=27.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||||||||||||
| 601 | # Back-compat | ||||||||||||||||||||||||||||||||||||||||||
| 602 | 3 | ▶ | if ( @_ and $self->{joins}{ $_[0] } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ and $self->{"joins"}{$_[0]}
MC/DC50%1/2
@_ and $self->{"joins"}{$_[0]}
@_
$self->{"joins"}{$_[0]}
Truth table66%2/3
A and B
A= @_B= $self->{"joins"}{$_[0]}
| |||||||||||||||||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||||||||||||||||
| 604 | # First argument appears to be an alias | ||||||||||||||||||||||||||||||||||||||||||
| 605 | 1 | @_ = ( alias => $_[0], name => $_[1] ); | |||||||||||||||||||||||||||||||||||||||||
| 606 | } | ||||||||||||||||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||||||||||||||||
| 608 | 3 | my %args = ( | |||||||||||||||||||||||||||||||||||||||||
| 609 | alias => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 610 | name => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 611 | class => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 612 | reference => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 613 | columns => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 614 | @_, | ||||||||||||||||||||||||||||||||||||||||||
| 615 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||||||||||||||||
| 617 | die "Must at least provide name to prefetch" | ||||||||||||||||||||||||||||||||||||||||||
| 618 | 3 | ▶ | unless $args{name}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||||||||||||||||
| 620 | # Reference defaults to name | ||||||||||||||||||||||||||||||||||||||||||
| 621 | 3 | ▶ | $args{reference} ||= $args{name}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$args{"reference"} ||= $args{"name"}
MC/DC0%0/2
$args{"reference"} ||= $args{"name"}
$args{"reference"}
$args{"name"}
Truth table33%1/3
A ||= B
A= $args{"reference"}B= $args{"name"}
| |||||||||||||||||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||||||||||||||||
| 623 | # If we don't have an alias, do the join | ||||||||||||||||||||||||||||||||||||||||||
| 624 | 3 | ▶ | if ( not $args{alias} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 625 | my ( $class, @columns ) | ||||||||||||||||||||||||||||||||||||||||||
| 626 | 2 | = $self->find_class( split /\./, $args{reference} ); | |||||||||||||||||||||||||||||||||||||||||
| 627 | 2 | $args{class} = ref $class; | |||||||||||||||||||||||||||||||||||||||||
| 628 | 2 | ( $args{alias} ) = $self->resolve_join(@columns); | |||||||||||||||||||||||||||||||||||||||||
| 629 | } | ||||||||||||||||||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||||||||||||||||
| 631 | 3 | ▶ | if ( not $args{class} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||||||||||||||||
| 633 | # Check the column | ||||||||||||||||||||||||||||||||||||||||||
| 634 | 1 | my $column = $self->record_class->column( $args{name} ); | |||||||||||||||||||||||||||||||||||||||||
| 635 | 1 | ▶ | $args{class} = $column->refers_to if $column; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||||||||||||||||
| 637 | 1 | ▶ | die "Don't know class" unless $args{class}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 638 | } | ||||||||||||||||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||||||||||||||||
| 640 | # Check that the class is a Jifty::DBI::Record or Jifty::DBI::Collection | ||||||||||||||||||||||||||||||||||||||||||
| 641 | 3 | ▶ | unless ( UNIVERSAL::isa( $args{class}, "Jifty::DBI::Record" ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
UNIVERSAL::isa($args{"class"}, "Jifty::DBI::Record") or UNIVERSAL::isa($args{"class"}, "Jifty::DBI::Collection")
MC/DC0%0/2
UNIVERSAL::isa($args{"class"}, "Jifty::DBI::Record") or UNIVERSAL::isa($args{"class"}, "Jifty::DBI::Collection")
UNIVERSAL::isa($args{"class"}, "Jifty::DBI::Record")
UNIVERSAL::isa($args{"class"}, "Jifty::DBI::Collection")
Truth table66%2/3
A or B
A= UNIVERSAL::isa($args{"class"}, "Jifty::DBI::Record")B= UNIVERSAL::isa($args{"class"}, "Jifty::DBI::Collection")
| |||||||||||||||||||||||||||||||||||||||||||
| 642 | or UNIVERSAL::isa( $args{class}, "Jifty::DBI::Collection" ) ) | ||||||||||||||||||||||||||||||||||||||||||
| 643 | { | ||||||||||||||||||||||||||||||||||||||||||
| 644 | 0 | warn | |||||||||||||||||||||||||||||||||||||||||
| 645 | "Class ($args{class}) isn't a Jifty::DBI::Record or Jifty::DBI::Collection"; | ||||||||||||||||||||||||||||||||||||||||||
| 646 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 647 | } | ||||||||||||||||||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||||||||||||||||
| 649 | 3 | ▶ | $self->prefetch_related( {} ) unless $self->prefetch_related; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 650 | 3 | $self->prefetch_related->{ $args{alias} } = {}; | |||||||||||||||||||||||||||||||||||||||||
| 651 | $self->prefetch_related->{ $args{alias} }{$_} = $args{$_} | ||||||||||||||||||||||||||||||||||||||||||
| 652 | 3 | for qw/alias class name columns/; | |||||||||||||||||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||||||||||||||||
| 654 | # Return the alias, in case we made it | ||||||||||||||||||||||||||||||||||||||||||
| 655 | 3 | return $args{alias}; | |||||||||||||||||||||||||||||||||||||||||
| 656 | } | ||||||||||||||||||||||||||||||||||||||||||
| 657 | |||||||||||||||||||||||||||||||||||||||||||
| 658 | =head2 find_column NAMES | ||||||||||||||||||||||||||||||||||||||||||
| 659 | |||||||||||||||||||||||||||||||||||||||||||
| 660 | Tales a chained list of column names, where all but the last element | ||||||||||||||||||||||||||||||||||||||||||
| 661 | is the name of a column on the previous class which refers to the next | ||||||||||||||||||||||||||||||||||||||||||
| 662 | collection or record. Returns a list of L<Jifty::DBI::Column> objects | ||||||||||||||||||||||||||||||||||||||||||
| 663 | for the list. | ||||||||||||||||||||||||||||||||||||||||||
| 664 | |||||||||||||||||||||||||||||||||||||||||||
| 665 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||||||||||||||||
| 667 | sub find_column { | ||||||||||||||||||||||||||||||||||||||||||
| 668 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
find_column: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 669 | 0 | my @names = @_; | |||||||||||||||||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||||||||||||||||
| 671 | 0 | my $last = pop @names; | |||||||||||||||||||||||||||||||||||||||||
| 672 | 0 | my ( $class, @columns ) = $self->find_class(@names); | |||||||||||||||||||||||||||||||||||||||||
| 673 | 0 | ▶ | $class = $class->record_class | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 674 | if UNIVERSAL::isa( $class, "Jifty::DBI::Collection" ); | ||||||||||||||||||||||||||||||||||||||||||
| 675 | 0 | my $column = $class->column($last); | |||||||||||||||||||||||||||||||||||||||||
| 676 | 0 | ▶ | die "$class has no column '$last'" unless $column; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 677 | 0 | return @columns, $column; | |||||||||||||||||||||||||||||||||||||||||
| 678 | } | ||||||||||||||||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||||||||||||||||
| 680 | =head2 find_class NAMES | ||||||||||||||||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||||||||||||||||
| 682 | Tales a chained list of column names, where each element is the name | ||||||||||||||||||||||||||||||||||||||||||
| 683 | of a column on the previous class which refers to the next collection | ||||||||||||||||||||||||||||||||||||||||||
| 684 | or record. Returns an instance of the ending class, followed by the | ||||||||||||||||||||||||||||||||||||||||||
| 685 | list of L<Jifty::DBI::Column> objects traversed to get there. | ||||||||||||||||||||||||||||||||||||||||||
| 686 | |||||||||||||||||||||||||||||||||||||||||||
| 687 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||||||||||||||||
| 689 | sub find_class { | ||||||||||||||||||||||||||||||||||||||||||
| 690 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
find_class: called CC=6 SCAR=18.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 691 | 2 | my @names = @_; | |||||||||||||||||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||||||||||||||||
| 693 | 2 | my @res; | |||||||||||||||||||||||||||||||||||||||||
| 694 | 2 | my $object = $self; | |||||||||||||||||||||||||||||||||||||||||
| 695 | 2 | my $itemclass = $self->record_class; | |||||||||||||||||||||||||||||||||||||||||
| 696 | 2 | ▶ | while ( my $name = shift @names ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 697 | 2 | my $column = $itemclass->column($name); | |||||||||||||||||||||||||||||||||||||||||
| 698 | 2 | ▶ | die "$itemclass has no column '$name'" unless $column; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 699 | |||||||||||||||||||||||||||||||||||||||||||
| 700 | 2 | push @res, $column; | |||||||||||||||||||||||||||||||||||||||||
| 701 | |||||||||||||||||||||||||||||||||||||||||||
| 702 | 2 | my $classname = $column->refers_to; | |||||||||||||||||||||||||||||||||||||||||
| 703 | 2 | ▶ | unless ($classname) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 704 | 0 | die "column '$name' of $itemclass is not a reference"; | |||||||||||||||||||||||||||||||||||||||||
| 705 | } | ||||||||||||||||||||||||||||||||||||||||||
| 706 | |||||||||||||||||||||||||||||||||||||||||||
| 707 | 2 | ▶ | if ( UNIVERSAL::isa( $classname, 'Jifty::DBI::Collection' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 708 | 1 | $object = $classname->new( $self->_new_collection_args ); | |||||||||||||||||||||||||||||||||||||||||
| 709 | 1 | $itemclass = $object->record_class; | |||||||||||||||||||||||||||||||||||||||||
| 710 | } elsif ( UNIVERSAL::isa( $classname, 'Jifty::DBI::Record' ) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 711 | 1 | $object = $classname->new( $self->_new_record_args ); | |||||||||||||||||||||||||||||||||||||||||
| 712 | 1 | $itemclass = $classname; | |||||||||||||||||||||||||||||||||||||||||
| 713 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 714 | 0 | die | |||||||||||||||||||||||||||||||||||||||||
| 715 | "Column '$name' refers to '$classname' which is not record or collection"; | ||||||||||||||||||||||||||||||||||||||||||
| 716 | } | ||||||||||||||||||||||||||||||||||||||||||
| 717 | } | ||||||||||||||||||||||||||||||||||||||||||
| 718 | |||||||||||||||||||||||||||||||||||||||||||
| 719 | 2 | return $object, @res; | |||||||||||||||||||||||||||||||||||||||||
| 720 | } | ||||||||||||||||||||||||||||||||||||||||||
| 721 | |||||||||||||||||||||||||||||||||||||||||||
| 722 | =head2 resolve_join COLUMNS | ||||||||||||||||||||||||||||||||||||||||||
| 723 | |||||||||||||||||||||||||||||||||||||||||||
| 724 | Takes a chained list of L<Jifty::DBI::Column> objects, and performs | ||||||||||||||||||||||||||||||||||||||||||
| 725 | the requisite joins to join all of them. Returns the alias of the | ||||||||||||||||||||||||||||||||||||||||||
| 726 | last join. | ||||||||||||||||||||||||||||||||||||||||||
| 727 | |||||||||||||||||||||||||||||||||||||||||||
| 728 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 729 | |||||||||||||||||||||||||||||||||||||||||||
| 730 | sub resolve_join { | ||||||||||||||||||||||||||||||||||||||||||
| 731 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
resolve_join: called CC=7 SCAR=20.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 732 | 2 | my @chain = @_; | |||||||||||||||||||||||||||||||||||||||||
| 733 | |||||||||||||||||||||||||||||||||||||||||||
| 734 | 2 | my $last_alias = 'main'; | |||||||||||||||||||||||||||||||||||||||||
| 735 | |||||||||||||||||||||||||||||||||||||||||||
| 736 | 2 | foreach my $column (@chain) { | |||||||||||||||||||||||||||||||||||||||||
| 737 | 2 | my $name = $column->name; | |||||||||||||||||||||||||||||||||||||||||
| 738 | |||||||||||||||||||||||||||||||||||||||||||
| 739 | 2 | my $classname = $column->refers_to; | |||||||||||||||||||||||||||||||||||||||||
| 740 | 2 | ▶ | unless ($classname) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 741 | 0 | die "column '$name' of is not a reference"; | |||||||||||||||||||||||||||||||||||||||||
| 742 | } | ||||||||||||||||||||||||||||||||||||||||||
| 743 | |||||||||||||||||||||||||||||||||||||||||||
| 744 | 2 | ▶ | if ( UNIVERSAL::isa( $classname, 'Jifty::DBI::Collection' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 745 | 1 | my $right_alias = $self->new_alias($classname->record_class); | |||||||||||||||||||||||||||||||||||||||||
| 746 | 1 | ▶ | $self->join( | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$column->by || 'id'
MC/DC0%0/1
$column->by || 'id'
$column->by
Truth table50%1/2
A
A= $column->by
| |||||||||||||||||||||||||||||||||||||||||||
| 747 | type => 'left', | ||||||||||||||||||||||||||||||||||||||||||
| 748 | alias1 => $last_alias, | ||||||||||||||||||||||||||||||||||||||||||
| 749 | column1 => 'id', | ||||||||||||||||||||||||||||||||||||||||||
| 750 | alias2 => $right_alias, | ||||||||||||||||||||||||||||||||||||||||||
| 751 | column2 => $column->by || 'id', | ||||||||||||||||||||||||||||||||||||||||||
| 752 | is_distinct => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 753 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 754 | 1 | $last_alias = $right_alias; | |||||||||||||||||||||||||||||||||||||||||
| 755 | } elsif ( UNIVERSAL::isa( $classname, 'Jifty::DBI::Record' ) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 756 | 1 | my $right_alias = $self->new_alias($classname); | |||||||||||||||||||||||||||||||||||||||||
| 757 | 1 | ▶ | $self->join( | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$column->by || 'id'
MC/DC0%0/1
$column->by || 'id'
$column->by
Truth table50%1/2
A
A= $column->by
| |||||||||||||||||||||||||||||||||||||||||||
| 758 | type => 'left', | ||||||||||||||||||||||||||||||||||||||||||
| 759 | alias1 => $last_alias, | ||||||||||||||||||||||||||||||||||||||||||
| 760 | column1 => $name, | ||||||||||||||||||||||||||||||||||||||||||
| 761 | alias2 => $right_alias, | ||||||||||||||||||||||||||||||||||||||||||
| 762 | column2 => $column->by || 'id', | ||||||||||||||||||||||||||||||||||||||||||
| 763 | is_distinct => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 764 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 765 | 1 | $last_alias = $right_alias; | |||||||||||||||||||||||||||||||||||||||||
| 766 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 767 | 0 | die | |||||||||||||||||||||||||||||||||||||||||
| 768 | "Column '$name' refers to '$classname' which is not record or collection"; | ||||||||||||||||||||||||||||||||||||||||||
| 769 | } | ||||||||||||||||||||||||||||||||||||||||||
| 770 | } | ||||||||||||||||||||||||||||||||||||||||||
| 771 | 2 | return $last_alias; | |||||||||||||||||||||||||||||||||||||||||
| 772 | } | ||||||||||||||||||||||||||||||||||||||||||
| 773 | |||||||||||||||||||||||||||||||||||||||||||
| 774 | =head2 distinct_required | ||||||||||||||||||||||||||||||||||||||||||
| 775 | |||||||||||||||||||||||||||||||||||||||||||
| 776 | Returns true if Jifty::DBI expects that this result set will end up | ||||||||||||||||||||||||||||||||||||||||||
| 777 | with repeated rows and should be "condensed" down to a single row for | ||||||||||||||||||||||||||||||||||||||||||
| 778 | each unique primary key. | ||||||||||||||||||||||||||||||||||||||||||
| 779 | |||||||||||||||||||||||||||||||||||||||||||
| 780 | Out of the box, this method returns true if you've joined to another table. | ||||||||||||||||||||||||||||||||||||||||||
| 781 | To add additional logic, feel free to override this method in your subclass. | ||||||||||||||||||||||||||||||||||||||||||
| 782 | |||||||||||||||||||||||||||||||||||||||||||
| 783 | XXX TODO: it should be possible to create a better heuristic than the simple | ||||||||||||||||||||||||||||||||||||||||||
| 784 | "is it joined?" question we're asking now. Something along the lines of "are we | ||||||||||||||||||||||||||||||||||||||||||
| 785 | joining this table to something that is not the other table's primary key" | ||||||||||||||||||||||||||||||||||||||||||
| 786 | |||||||||||||||||||||||||||||||||||||||||||
| 787 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 788 | |||||||||||||||||||||||||||||||||||||||||||
| 789 | sub distinct_required { | ||||||||||||||||||||||||||||||||||||||||||
| 790 | 324 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
distinct_required: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 791 | 324 | ▶ | return ( $self->_is_joined ? !$self->_is_distinctly_joined : 0 ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 792 | } | ||||||||||||||||||||||||||||||||||||||||||
| 793 | |||||||||||||||||||||||||||||||||||||||||||
| 794 | =head2 build_select_count_query | ||||||||||||||||||||||||||||||||||||||||||
| 795 | |||||||||||||||||||||||||||||||||||||||||||
| 796 | Builds a SELECT statement to find the number of rows this collection | ||||||||||||||||||||||||||||||||||||||||||
| 797 | would find. | ||||||||||||||||||||||||||||||||||||||||||
| 798 | |||||||||||||||||||||||||||||||||||||||||||
| 799 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 800 | |||||||||||||||||||||||||||||||||||||||||||
| 801 | sub build_select_count_query { | ||||||||||||||||||||||||||||||||||||||||||
| 802 | 78 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
build_select_count_query: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 803 | |||||||||||||||||||||||||||||||||||||||||||
| 804 | 78 | ▶ | return "" if $self->derived; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 805 | |||||||||||||||||||||||||||||||||||||||||||
| 806 | 78 | my $query_string = $self->_build_joins . " "; | |||||||||||||||||||||||||||||||||||||||||
| 807 | |||||||||||||||||||||||||||||||||||||||||||
| 808 | 78 | ▶ | if ( $self->_is_limited ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 809 | 78 | $query_string .= $self->_where_clause . " "; | |||||||||||||||||||||||||||||||||||||||||
| 810 | } | ||||||||||||||||||||||||||||||||||||||||||
| 811 | |||||||||||||||||||||||||||||||||||||||||||
| 812 | # DISTINCT query only required for multi-table selects | ||||||||||||||||||||||||||||||||||||||||||
| 813 | 78 | ▶ | if ( $self->distinct_required or $self->prefetch_related ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->distinct_required or $self->prefetch_related
MC/DC100%2/2
$self->distinct_required or $self->prefetch_related
$self->distinct_required
$self->prefetch_related
Truth table100%3/3
A or B
A= $self->distinct_requiredB= $self->prefetch_related
| |||||||||||||||||||||||||||||||||||||||||||
| 814 | 11 | $query_string = $self->_handle->distinct_count( \$query_string ); | |||||||||||||||||||||||||||||||||||||||||
| 815 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 816 | 67 | $query_string = "SELECT count(main.id) FROM " . $query_string; | |||||||||||||||||||||||||||||||||||||||||
| 817 | } | ||||||||||||||||||||||||||||||||||||||||||
| 818 | |||||||||||||||||||||||||||||||||||||||||||
| 819 | 78 | return ($query_string); | |||||||||||||||||||||||||||||||||||||||||
| 820 | } | ||||||||||||||||||||||||||||||||||||||||||
| 821 | |||||||||||||||||||||||||||||||||||||||||||
| 822 | =head2 do_search | ||||||||||||||||||||||||||||||||||||||||||
| 823 | |||||||||||||||||||||||||||||||||||||||||||
| 824 | C<Jifty::DBI::Collection> usually does searches "lazily". That is, it | ||||||||||||||||||||||||||||||||||||||||||
| 825 | does a C<SELECT COUNT> or a C<SELECT> on the fly the first time you ask | ||||||||||||||||||||||||||||||||||||||||||
| 826 | for results that would need one or the other. Sometimes, you need to | ||||||||||||||||||||||||||||||||||||||||||
| 827 | display a count of results found before you iterate over a collection, | ||||||||||||||||||||||||||||||||||||||||||
| 828 | but you know you're about to do that too. To save a bit of wear and tear | ||||||||||||||||||||||||||||||||||||||||||
| 829 | on your database, call C<do_search> before that C<count>. | ||||||||||||||||||||||||||||||||||||||||||
| 830 | |||||||||||||||||||||||||||||||||||||||||||
| 831 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 832 | |||||||||||||||||||||||||||||||||||||||||||
| 833 | sub do_search { | ||||||||||||||||||||||||||||||||||||||||||
| 834 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_search: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 835 | 0 | ▶ | return if $self->derived; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 836 | 0 | ▶ | $self->_do_search() if $self->{'must_redo_search'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 837 | |||||||||||||||||||||||||||||||||||||||||||
| 838 | } | ||||||||||||||||||||||||||||||||||||||||||
| 839 | |||||||||||||||||||||||||||||||||||||||||||
| 840 | =head2 next | ||||||||||||||||||||||||||||||||||||||||||
| 841 | |||||||||||||||||||||||||||||||||||||||||||
| 842 | Returns the next row from the set as an object of the type defined by | ||||||||||||||||||||||||||||||||||||||||||
| 843 | sub new_item. When the complete set has been iterated through, | ||||||||||||||||||||||||||||||||||||||||||
| 844 | returns undef and resets the search such that the following call to | ||||||||||||||||||||||||||||||||||||||||||
| 845 | L</next> will start over with the first item retrieved from the | ||||||||||||||||||||||||||||||||||||||||||
| 846 | database. | ||||||||||||||||||||||||||||||||||||||||||
| 847 | |||||||||||||||||||||||||||||||||||||||||||
| 848 | You may also call this method via the built-in iterator syntax. | ||||||||||||||||||||||||||||||||||||||||||
| 849 | The two lines below are equivalent: | ||||||||||||||||||||||||||||||||||||||||||
| 850 | |||||||||||||||||||||||||||||||||||||||||||
| 851 | while ($_ = $collection->next) { ... } | ||||||||||||||||||||||||||||||||||||||||||
| 852 | |||||||||||||||||||||||||||||||||||||||||||
| 853 | while (<$collection>) { ... } | ||||||||||||||||||||||||||||||||||||||||||
| 854 | |||||||||||||||||||||||||||||||||||||||||||
| 855 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 856 | |||||||||||||||||||||||||||||||||||||||||||
| 857 | sub next { | ||||||||||||||||||||||||||||||||||||||||||
| 858 | 83 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
next: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 859 | |||||||||||||||||||||||||||||||||||||||||||
| 860 | 83 | my $item = $self->peek; | |||||||||||||||||||||||||||||||||||||||||
| 861 | |||||||||||||||||||||||||||||||||||||||||||
| 862 | 83 | ▶ | if ( $self->{'itemscount'} < $self->_record_count ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 863 | 65 | $self->{'itemscount'}++; | |||||||||||||||||||||||||||||||||||||||||
| 864 | } else { #we've gone through the whole list. reset the count. | ||||||||||||||||||||||||||||||||||||||||||
| 865 | 18 | $self->goto_first_item(); | |||||||||||||||||||||||||||||||||||||||||
| 866 | } | ||||||||||||||||||||||||||||||||||||||||||
| 867 | |||||||||||||||||||||||||||||||||||||||||||
| 868 | 83 | return ($item); | |||||||||||||||||||||||||||||||||||||||||
| 869 | } | ||||||||||||||||||||||||||||||||||||||||||
| 870 | |||||||||||||||||||||||||||||||||||||||||||
| 871 | =head2 peek | ||||||||||||||||||||||||||||||||||||||||||
| 872 | |||||||||||||||||||||||||||||||||||||||||||
| 873 | Exactly the same as next, only it doesn't move the iterator. | ||||||||||||||||||||||||||||||||||||||||||
| 874 | |||||||||||||||||||||||||||||||||||||||||||
| 875 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 876 | |||||||||||||||||||||||||||||||||||||||||||
| 877 | sub peek { | ||||||||||||||||||||||||||||||||||||||||||
| 878 | 91 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
peek: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 879 | |||||||||||||||||||||||||||||||||||||||||||
| 880 | 91 | ▶ | return (undef) unless ( $self->_is_limited ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 881 | |||||||||||||||||||||||||||||||||||||||||||
| 882 | 83 | ▶ | $self->_do_search() if $self->{'must_redo_search'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 883 | |||||||||||||||||||||||||||||||||||||||||||
| 884 | 83 | ▶ | if ( $self->{'itemscount'} < $self->_record_count ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 885 | { #return the next item | ||||||||||||||||||||||||||||||||||||||||||
| 886 | 67 | my $item = ( $self->{'items'}[ $self->{'itemscount'} ] ); | |||||||||||||||||||||||||||||||||||||||||
| 887 | 67 | return ($item); | |||||||||||||||||||||||||||||||||||||||||
| 888 | } else { #no more items! | ||||||||||||||||||||||||||||||||||||||||||
| 889 | 16 | return (undef); | |||||||||||||||||||||||||||||||||||||||||
| 890 | } | ||||||||||||||||||||||||||||||||||||||||||
| 891 | } | ||||||||||||||||||||||||||||||||||||||||||
| 892 | |||||||||||||||||||||||||||||||||||||||||||
| 893 | =head2 goto_first_item | ||||||||||||||||||||||||||||||||||||||||||
| 894 | |||||||||||||||||||||||||||||||||||||||||||
| 895 | Starts the recordset counter over from the first item. The next time | ||||||||||||||||||||||||||||||||||||||||||
| 896 | you call L</next>, you'll get the first item returned by the database, | ||||||||||||||||||||||||||||||||||||||||||
| 897 | as if you'd just started iterating through the result set. | ||||||||||||||||||||||||||||||||||||||||||
| 898 | |||||||||||||||||||||||||||||||||||||||||||
| 899 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 900 | |||||||||||||||||||||||||||||||||||||||||||
| 901 | sub goto_first_item { | ||||||||||||||||||||||||||||||||||||||||||
| 902 | 59 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
goto_first_item: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 903 | 59 | $self->goto_item(0); | |||||||||||||||||||||||||||||||||||||||||
| 904 | } | ||||||||||||||||||||||||||||||||||||||||||
| 905 | |||||||||||||||||||||||||||||||||||||||||||
| 906 | =head2 goto_item | ||||||||||||||||||||||||||||||||||||||||||
| 907 | |||||||||||||||||||||||||||||||||||||||||||
| 908 | Takes an integer, n. Sets the record counter to n. the next time you | ||||||||||||||||||||||||||||||||||||||||||
| 909 | call L</next>, you'll get the nth item. | ||||||||||||||||||||||||||||||||||||||||||
| 910 | |||||||||||||||||||||||||||||||||||||||||||
| 911 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 912 | |||||||||||||||||||||||||||||||||||||||||||
| 913 | sub goto_item { | ||||||||||||||||||||||||||||||||||||||||||
| 914 | 65 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
goto_item: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 915 | 65 | my $item = shift; | |||||||||||||||||||||||||||||||||||||||||
| 916 | 65 | $self->{'itemscount'} = $item; | |||||||||||||||||||||||||||||||||||||||||
| 917 | } | ||||||||||||||||||||||||||||||||||||||||||
| 918 | |||||||||||||||||||||||||||||||||||||||||||
| 919 | =head2 first | ||||||||||||||||||||||||||||||||||||||||||
| 920 | |||||||||||||||||||||||||||||||||||||||||||
| 921 | Returns the first item | ||||||||||||||||||||||||||||||||||||||||||
| 922 | |||||||||||||||||||||||||||||||||||||||||||
| 923 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 924 | |||||||||||||||||||||||||||||||||||||||||||
| 925 | sub first { | ||||||||||||||||||||||||||||||||||||||||||
| 926 | 35 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
first: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 927 | 35 | $self->goto_first_item(); | |||||||||||||||||||||||||||||||||||||||||
| 928 | 35 | return ( $self->next ); | |||||||||||||||||||||||||||||||||||||||||
| 929 | } | ||||||||||||||||||||||||||||||||||||||||||
| 930 | |||||||||||||||||||||||||||||||||||||||||||
| 931 | =head2 last | ||||||||||||||||||||||||||||||||||||||||||
| 932 | |||||||||||||||||||||||||||||||||||||||||||
| 933 | Returns the last item | ||||||||||||||||||||||||||||||||||||||||||
| 934 | |||||||||||||||||||||||||||||||||||||||||||
| 935 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 936 | |||||||||||||||||||||||||||||||||||||||||||
| 937 | sub last { | ||||||||||||||||||||||||||||||||||||||||||
| 938 | 6 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
last: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 939 | 6 | $self->goto_item( ( $self->count ) - 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 940 | 6 | return ( $self->next ); | |||||||||||||||||||||||||||||||||||||||||
| 941 | } | ||||||||||||||||||||||||||||||||||||||||||
| 942 | |||||||||||||||||||||||||||||||||||||||||||
| 943 | =head2 distinct_column_values | ||||||||||||||||||||||||||||||||||||||||||
| 944 | |||||||||||||||||||||||||||||||||||||||||||
| 945 | Takes a column name and returns distinct values of the column. | ||||||||||||||||||||||||||||||||||||||||||
| 946 | Only values in the current collection are returned. | ||||||||||||||||||||||||||||||||||||||||||
| 947 | |||||||||||||||||||||||||||||||||||||||||||
| 948 | Optional arguments are C<max> and C<sort> to limit number of | ||||||||||||||||||||||||||||||||||||||||||
| 949 | values returned and it makes sense to sort results. | ||||||||||||||||||||||||||||||||||||||||||
| 950 | |||||||||||||||||||||||||||||||||||||||||||
| 951 | $col->distinct_column_values('column'); | ||||||||||||||||||||||||||||||||||||||||||
| 952 | |||||||||||||||||||||||||||||||||||||||||||
| 953 | $col->distinct_column_values(column => 'column'); | ||||||||||||||||||||||||||||||||||||||||||
| 954 | |||||||||||||||||||||||||||||||||||||||||||
| 955 | $col->distinct_column_values('column', max => 10, sort => 'asc'); | ||||||||||||||||||||||||||||||||||||||||||
| 956 | |||||||||||||||||||||||||||||||||||||||||||
| 957 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 958 | |||||||||||||||||||||||||||||||||||||||||||
| 959 | sub distinct_column_values { | ||||||||||||||||||||||||||||||||||||||||||
| 960 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
distinct_column_values: called CC=12 SCAR=25.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 961 | 4 | ▶ | my %args = ( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 962 | column => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 963 | sort => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 964 | max => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 965 | @_%2 ? (column => @_) : (@_) | ||||||||||||||||||||||||||||||||||||||||||
| 966 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 967 | |||||||||||||||||||||||||||||||||||||||||||
| 968 | 4 | ▶ | return () if $self->derived; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 969 | |||||||||||||||||||||||||||||||||||||||||||
| 970 | 4 | my $query_string = $self->_build_joins; | |||||||||||||||||||||||||||||||||||||||||
| 971 | 4 | ▶ | if ( $self->_is_limited ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 972 | 0 | $query_string .= ' '. $self->_where_clause . " "; | |||||||||||||||||||||||||||||||||||||||||
| 973 | } | ||||||||||||||||||||||||||||||||||||||||||
| 974 | |||||||||||||||||||||||||||||||||||||||||||
| 975 | 4 | my $column = 'main.'. $args{'column'}; | |||||||||||||||||||||||||||||||||||||||||
| 976 | 4 | $query_string = 'SELECT DISTINCT '. $column .' FROM '. $query_string; | |||||||||||||||||||||||||||||||||||||||||
| 977 | |||||||||||||||||||||||||||||||||||||||||||
| 978 | 4 | ▶ | if ( $args{'sort'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 979 | $query_string .= ' ORDER BY '. $column | ||||||||||||||||||||||||||||||||||||||||||
| 980 | 3 | ▶ | .' '. ($args{'sort'} =~ /^des/i ? 'DESC' : 'ASC'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 981 | } | ||||||||||||||||||||||||||||||||||||||||||
| 982 | |||||||||||||||||||||||||||||||||||||||||||
| 983 | 4 | ▶ | my $sth = $self->_handle->simple_query( $query_string ) or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 984 | 4 | my $value; | |||||||||||||||||||||||||||||||||||||||||
| 985 | 4 | ▶ | $sth->bind_col(1, \$value) or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 986 | 4 | my @col; | |||||||||||||||||||||||||||||||||||||||||
| 987 | 4 | ▶ | if ($args{max}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 988 | 1 | ▶ | push @col, $value while 0 < $args{max}-- && $sth->fetch; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
0 < $args{"max"}-- and $sth->fetch
MC/DC50%1/2
0 < $args{"max"}-- and $sth->fetch
0 < $args{"max"}--
$sth->fetch
Truth table66%2/3
A and B
A= 0 < $args{"max"}--B= $sth->fetch
| |||||||||||||||||||||||||||||||||||||||||||
| 989 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 990 | 3 | ▶ | push @col, $value while $sth->fetch; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 991 | } | ||||||||||||||||||||||||||||||||||||||||||
| 992 | 4 | return @col; | |||||||||||||||||||||||||||||||||||||||||
| 993 | } | ||||||||||||||||||||||||||||||||||||||||||
| 994 | |||||||||||||||||||||||||||||||||||||||||||
| 995 | =head2 items_array_ref | ||||||||||||||||||||||||||||||||||||||||||
| 996 | |||||||||||||||||||||||||||||||||||||||||||
| 997 | Return a reference to an array containing all objects found by this | ||||||||||||||||||||||||||||||||||||||||||
| 998 | search. | ||||||||||||||||||||||||||||||||||||||||||
| 999 | |||||||||||||||||||||||||||||||||||||||||||
| 1000 | You may also call this method via the built-in array dereference syntax. | ||||||||||||||||||||||||||||||||||||||||||
| 1001 | The two lines below are equivalent: | ||||||||||||||||||||||||||||||||||||||||||
| 1002 | |||||||||||||||||||||||||||||||||||||||||||
| 1003 | for (@{$collection->items_array_ref}) { ... } | ||||||||||||||||||||||||||||||||||||||||||
| 1004 | |||||||||||||||||||||||||||||||||||||||||||
| 1005 | for (@$collection) { ... } | ||||||||||||||||||||||||||||||||||||||||||
| 1006 | |||||||||||||||||||||||||||||||||||||||||||
| 1007 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1008 | |||||||||||||||||||||||||||||||||||||||||||
| 1009 | sub items_array_ref { | ||||||||||||||||||||||||||||||||||||||||||
| 1010 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
items_array_ref: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1011 | |||||||||||||||||||||||||||||||||||||||||||
| 1012 | # If we're not limited, return an empty array | ||||||||||||||||||||||||||||||||||||||||||
| 1013 | 8 | ▶ | return [] unless $self->_is_limited; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1014 | |||||||||||||||||||||||||||||||||||||||||||
| 1015 | # Do a search if we need to. | ||||||||||||||||||||||||||||||||||||||||||
| 1016 | 6 | ▶ | $self->_do_search() if $self->{'must_redo_search'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1017 | |||||||||||||||||||||||||||||||||||||||||||
| 1018 | # If we've got any items in the array, return them. Otherwise, | ||||||||||||||||||||||||||||||||||||||||||
| 1019 | # return an empty array | ||||||||||||||||||||||||||||||||||||||||||
| 1020 | 6 | ▶ | return ( $self->{'items'} || [] ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'items'} || []
MC/DC0%0/1
$self->{'items'} || []
$self->{'items'}
Truth table50%1/2
A
A= $self->{'items'}
| |||||||||||||||||||||||||||||||||||||||||||
| 1021 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1022 | |||||||||||||||||||||||||||||||||||||||||||
| 1023 | =head2 new_item | ||||||||||||||||||||||||||||||||||||||||||
| 1024 | |||||||||||||||||||||||||||||||||||||||||||
| 1025 | Should return a new object of the correct type for the current collection. | ||||||||||||||||||||||||||||||||||||||||||
| 1026 | L</record_class> method is used to determine class of the object. | ||||||||||||||||||||||||||||||||||||||||||
| 1027 | |||||||||||||||||||||||||||||||||||||||||||
| 1028 | Each record class at least once is loaded using require. This method is | ||||||||||||||||||||||||||||||||||||||||||
| 1029 | called each time a record fetched so load attempts are cached to avoid | ||||||||||||||||||||||||||||||||||||||||||
| 1030 | penalties. If you're sure that all record classes are loaded before | ||||||||||||||||||||||||||||||||||||||||||
| 1031 | first use then you can override this method. | ||||||||||||||||||||||||||||||||||||||||||
| 1032 | |||||||||||||||||||||||||||||||||||||||||||
| 1033 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1034 | |||||||||||||||||||||||||||||||||||||||||||
| 1035 | 13 | { my %cache = (); | |||||||||||||||||||||||||||||||||||||||||
| 1036 | sub new_item { | ||||||||||||||||||||||||||||||||||||||||||
| 1037 | 185 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new_item: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1038 | 185 | my $class = $self->record_class(); | |||||||||||||||||||||||||||||||||||||||||
| 1039 | |||||||||||||||||||||||||||||||||||||||||||
| 1040 | 185 | ▶ | die "Jifty::DBI::Collection needs to be subclassed; override new_item\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1041 | unless $class; | ||||||||||||||||||||||||||||||||||||||||||
| 1042 | |||||||||||||||||||||||||||||||||||||||||||
| 1043 | 185 | ▶ | unless ( exists $cache{$class} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1044 | 9 | $class->require; | |||||||||||||||||||||||||||||||||||||||||
| 1045 | 9 | $cache{$class} = undef; | |||||||||||||||||||||||||||||||||||||||||
| 1046 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1047 | 185 | return $class->new( $self->_new_record_args ); | |||||||||||||||||||||||||||||||||||||||||
| 1048 | } } | ||||||||||||||||||||||||||||||||||||||||||
| 1049 | |||||||||||||||||||||||||||||||||||||||||||
| 1050 | =head2 record_class | ||||||||||||||||||||||||||||||||||||||||||
| 1051 | |||||||||||||||||||||||||||||||||||||||||||
| 1052 | Returns the record class which this is a collection of; override this | ||||||||||||||||||||||||||||||||||||||||||
| 1053 | to subclass. Or, pass it the name of a class as an argument after | ||||||||||||||||||||||||||||||||||||||||||
| 1054 | creating a C<Jifty::DBI::Collection> object to create an 'anonymous' | ||||||||||||||||||||||||||||||||||||||||||
| 1055 | collection class. | ||||||||||||||||||||||||||||||||||||||||||
| 1056 | |||||||||||||||||||||||||||||||||||||||||||
| 1057 | If you haven't specified a record class, this returns a best guess at | ||||||||||||||||||||||||||||||||||||||||||
| 1058 | the name of the record class for this collection. | ||||||||||||||||||||||||||||||||||||||||||
| 1059 | |||||||||||||||||||||||||||||||||||||||||||
| 1060 | It uses a simple heuristic to determine the record class name -- It | ||||||||||||||||||||||||||||||||||||||||||
| 1061 | chops "Collection" or "s" off its own name. If you want to name your | ||||||||||||||||||||||||||||||||||||||||||
| 1062 | records and collections differently, go right ahead, but don't say we | ||||||||||||||||||||||||||||||||||||||||||
| 1063 | didn't warn you. | ||||||||||||||||||||||||||||||||||||||||||
| 1064 | |||||||||||||||||||||||||||||||||||||||||||
| 1065 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1066 | |||||||||||||||||||||||||||||||||||||||||||
| 1067 | 13 | sub record_class { | |||||||||||||||||||||||||||||||||||||||||
| 1068 | 948 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
record_class: called CC=9 SCAR=22.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1069 | 948 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 1070 | 2 | ▶ | $self->{record_class} = shift if (@_); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1071 | $self->{record_class} = ref $self->{record_class} | ||||||||||||||||||||||||||||||||||||||||||
| 1072 | 2 | ▶ | if ref $self->{record_class}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1073 | ▶ | } elsif ( not ref $self or not $self->{record_class} ) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
ref $self && $self->{'record_class'}
MC/DC100%2/2
ref $self && $self->{'record_class'}
ref $self
$self->{'record_class'}
Truth table100%3/3
A && B
A= ref $selfB= $self->{'record_class'}
| |||||||||||||||||||||||||||||||||||||||||||
| 1074 | 231 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table66%2/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||||||||||||||||
| 1075 | 231 | ▶ | $class =~ s/(?<!:)(Collection|s)$// | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1076 | || die "Can't guess record class from $class"; | ||||||||||||||||||||||||||||||||||||||||||
| 1077 | 231 | ▶ | return $class unless ref $self; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1078 | 228 | $self->{record_class} = $class; | |||||||||||||||||||||||||||||||||||||||||
| 1079 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1080 | 945 | return $self->{record_class}; | |||||||||||||||||||||||||||||||||||||||||
| 1081 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1082 | |||||||||||||||||||||||||||||||||||||||||||
| 1083 | =head2 redo_search | ||||||||||||||||||||||||||||||||||||||||||
| 1084 | |||||||||||||||||||||||||||||||||||||||||||
| 1085 | Takes no arguments. Tells Jifty::DBI::Collection that the next time | ||||||||||||||||||||||||||||||||||||||||||
| 1086 | it is asked for a record, it should re-execute the query. | ||||||||||||||||||||||||||||||||||||||||||
| 1087 | |||||||||||||||||||||||||||||||||||||||||||
| 1088 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1089 | |||||||||||||||||||||||||||||||||||||||||||
| 1090 | sub redo_search { | ||||||||||||||||||||||||||||||||||||||||||
| 1091 | 597 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
redo_search: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1092 | 597 | $self->{'must_redo_search'} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 1093 | 597 | delete $self->{$_} for qw(items raw_rows count_all); | |||||||||||||||||||||||||||||||||||||||||
| 1094 | 597 | $self->{'itemscount'} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 1095 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1096 | |||||||||||||||||||||||||||||||||||||||||||
| 1097 | =head2 unlimit | ||||||||||||||||||||||||||||||||||||||||||
| 1098 | |||||||||||||||||||||||||||||||||||||||||||
| 1099 | Unlimit clears all restrictions on this collection and resets | ||||||||||||||||||||||||||||||||||||||||||
| 1100 | it to a "default" pristine state. Note, in particular, that | ||||||||||||||||||||||||||||||||||||||||||
| 1101 | this means C<unlimit> will erase ordering and grouping | ||||||||||||||||||||||||||||||||||||||||||
| 1102 | metadata. To find all rows without resetting this metadata, | ||||||||||||||||||||||||||||||||||||||||||
| 1103 | use the C<find_all_rows> method. | ||||||||||||||||||||||||||||||||||||||||||
| 1104 | |||||||||||||||||||||||||||||||||||||||||||
| 1105 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1106 | |||||||||||||||||||||||||||||||||||||||||||
| 1107 | sub unlimit { | ||||||||||||||||||||||||||||||||||||||||||
| 1108 | 7 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unlimit: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1109 | |||||||||||||||||||||||||||||||||||||||||||
| 1110 | 7 | $self->clean_slate(); | |||||||||||||||||||||||||||||||||||||||||
| 1111 | 7 | $self->_is_limited(-1); | |||||||||||||||||||||||||||||||||||||||||
| 1112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1113 | |||||||||||||||||||||||||||||||||||||||||||
| 1114 | =head2 find_all_rows | ||||||||||||||||||||||||||||||||||||||||||
| 1115 | |||||||||||||||||||||||||||||||||||||||||||
| 1116 | C<find_all_rows> instructs this collection class to return all rows in | ||||||||||||||||||||||||||||||||||||||||||
| 1117 | the table. (It removes the WHERE clause from your query). | ||||||||||||||||||||||||||||||||||||||||||
| 1118 | |||||||||||||||||||||||||||||||||||||||||||
| 1119 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1120 | |||||||||||||||||||||||||||||||||||||||||||
| 1121 | sub find_all_rows { | ||||||||||||||||||||||||||||||||||||||||||
| 1122 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
find_all_rows: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1123 | 0 | $self->_is_limited(-1); | |||||||||||||||||||||||||||||||||||||||||
| 1124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1125 | |||||||||||||||||||||||||||||||||||||||||||
| 1126 | =head2 limit | ||||||||||||||||||||||||||||||||||||||||||
| 1127 | |||||||||||||||||||||||||||||||||||||||||||
| 1128 | Takes a hash of parameters with the following keys: | ||||||||||||||||||||||||||||||||||||||||||
| 1129 | |||||||||||||||||||||||||||||||||||||||||||
| 1130 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 1131 | |||||||||||||||||||||||||||||||||||||||||||
| 1132 | =item table | ||||||||||||||||||||||||||||||||||||||||||
| 1133 | |||||||||||||||||||||||||||||||||||||||||||
| 1134 | Can be set to something different than this table if a join is | ||||||||||||||||||||||||||||||||||||||||||
| 1135 | wanted (that means we can't do recursive joins as for now). | ||||||||||||||||||||||||||||||||||||||||||
| 1136 | |||||||||||||||||||||||||||||||||||||||||||
| 1137 | =item alias | ||||||||||||||||||||||||||||||||||||||||||
| 1138 | |||||||||||||||||||||||||||||||||||||||||||
| 1139 | Unless alias is set, the join criteria will be taken from EXT_LINKcolumn | ||||||||||||||||||||||||||||||||||||||||||
| 1140 | and INT_LINKcolumn and added to the criteria. If alias is set, new | ||||||||||||||||||||||||||||||||||||||||||
| 1141 | criteria about the foreign table will be added. | ||||||||||||||||||||||||||||||||||||||||||
| 1142 | |||||||||||||||||||||||||||||||||||||||||||
| 1143 | =item column | ||||||||||||||||||||||||||||||||||||||||||
| 1144 | |||||||||||||||||||||||||||||||||||||||||||
| 1145 | Column to be checked against. | ||||||||||||||||||||||||||||||||||||||||||
| 1146 | |||||||||||||||||||||||||||||||||||||||||||
| 1147 | =item value | ||||||||||||||||||||||||||||||||||||||||||
| 1148 | |||||||||||||||||||||||||||||||||||||||||||
| 1149 | Should always be set and will always be quoted. If the value is a | ||||||||||||||||||||||||||||||||||||||||||
| 1150 | subclass of Jifty::DBI::Object, the value will be interpreted to be | ||||||||||||||||||||||||||||||||||||||||||
| 1151 | the object's id. | ||||||||||||||||||||||||||||||||||||||||||
| 1152 | |||||||||||||||||||||||||||||||||||||||||||
| 1153 | =item operator | ||||||||||||||||||||||||||||||||||||||||||
| 1154 | |||||||||||||||||||||||||||||||||||||||||||
| 1155 | operator is the SQL operator to use for this phrase. Possible choices include: | ||||||||||||||||||||||||||||||||||||||||||
| 1156 | |||||||||||||||||||||||||||||||||||||||||||
| 1157 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 1158 | |||||||||||||||||||||||||||||||||||||||||||
| 1159 | =item "=" | ||||||||||||||||||||||||||||||||||||||||||
| 1160 | |||||||||||||||||||||||||||||||||||||||||||
| 1161 | =item "!=" | ||||||||||||||||||||||||||||||||||||||||||
| 1162 | |||||||||||||||||||||||||||||||||||||||||||
| 1163 | Any other standard SQL comparison operators that your underlying | ||||||||||||||||||||||||||||||||||||||||||
| 1164 | database supports are also valid. | ||||||||||||||||||||||||||||||||||||||||||
| 1165 | |||||||||||||||||||||||||||||||||||||||||||
| 1166 | =item "LIKE" | ||||||||||||||||||||||||||||||||||||||||||
| 1167 | |||||||||||||||||||||||||||||||||||||||||||
| 1168 | =item "NOT LIKE" | ||||||||||||||||||||||||||||||||||||||||||
| 1169 | |||||||||||||||||||||||||||||||||||||||||||
| 1170 | =item "MATCHES" | ||||||||||||||||||||||||||||||||||||||||||
| 1171 | |||||||||||||||||||||||||||||||||||||||||||
| 1172 | MATCHES is like LIKE, except it surrounds the value with % signs. | ||||||||||||||||||||||||||||||||||||||||||
| 1173 | |||||||||||||||||||||||||||||||||||||||||||
| 1174 | =item "starts_with" | ||||||||||||||||||||||||||||||||||||||||||
| 1175 | |||||||||||||||||||||||||||||||||||||||||||
| 1176 | starts_with is like LIKE, except it only appends a % at the end of the string | ||||||||||||||||||||||||||||||||||||||||||
| 1177 | |||||||||||||||||||||||||||||||||||||||||||
| 1178 | =item "ends_with" | ||||||||||||||||||||||||||||||||||||||||||
| 1179 | |||||||||||||||||||||||||||||||||||||||||||
| 1180 | ends_with is like LIKE, except it prepends a % to the beginning of the string | ||||||||||||||||||||||||||||||||||||||||||
| 1181 | |||||||||||||||||||||||||||||||||||||||||||
| 1182 | =item "IN" | ||||||||||||||||||||||||||||||||||||||||||
| 1183 | |||||||||||||||||||||||||||||||||||||||||||
| 1184 | IN matches a column within a set of values. The value specified in the limit | ||||||||||||||||||||||||||||||||||||||||||
| 1185 | should be an array reference of values. | ||||||||||||||||||||||||||||||||||||||||||
| 1186 | |||||||||||||||||||||||||||||||||||||||||||
| 1187 | =item "IS" | ||||||||||||||||||||||||||||||||||||||||||
| 1188 | |||||||||||||||||||||||||||||||||||||||||||
| 1189 | =item "IS NOT" | ||||||||||||||||||||||||||||||||||||||||||
| 1190 | |||||||||||||||||||||||||||||||||||||||||||
| 1191 | This is useful for when you wish to match columns that contain NULL (or ones that don't). Use this operator and a value of "NULL". | ||||||||||||||||||||||||||||||||||||||||||
| 1192 | |||||||||||||||||||||||||||||||||||||||||||
| 1193 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 1194 | |||||||||||||||||||||||||||||||||||||||||||
| 1195 | =item escape | ||||||||||||||||||||||||||||||||||||||||||
| 1196 | |||||||||||||||||||||||||||||||||||||||||||
| 1197 | If you need to escape wildcard characters (usually _ or %) in the value *explicitly* with | ||||||||||||||||||||||||||||||||||||||||||
| 1198 | "ESCAPE", set the escape character here. Note that backslashes may require special treatment | ||||||||||||||||||||||||||||||||||||||||||
| 1199 | (e.g. Postgres dislikes \ or \\ in queries unless we use the E'' syntax). | ||||||||||||||||||||||||||||||||||||||||||
| 1200 | |||||||||||||||||||||||||||||||||||||||||||
| 1201 | =item entry_aggregator | ||||||||||||||||||||||||||||||||||||||||||
| 1202 | |||||||||||||||||||||||||||||||||||||||||||
| 1203 | Can be AND or OR (or anything else valid to aggregate two clauses in SQL) | ||||||||||||||||||||||||||||||||||||||||||
| 1204 | |||||||||||||||||||||||||||||||||||||||||||
| 1205 | =item case_sensitive | ||||||||||||||||||||||||||||||||||||||||||
| 1206 | |||||||||||||||||||||||||||||||||||||||||||
| 1207 | on some databases, such as postgres, setting case_sensitive to 1 will make | ||||||||||||||||||||||||||||||||||||||||||
| 1208 | this search case sensitive. Note that this flag is ignored if the column | ||||||||||||||||||||||||||||||||||||||||||
| 1209 | is numeric. | ||||||||||||||||||||||||||||||||||||||||||
| 1210 | |||||||||||||||||||||||||||||||||||||||||||
| 1211 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 1212 | |||||||||||||||||||||||||||||||||||||||||||
| 1213 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1214 | |||||||||||||||||||||||||||||||||||||||||||
| 1215 | sub limit { | ||||||||||||||||||||||||||||||||||||||||||
| 1216 | 279 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
limit: called CC=52 SCAR=43.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1217 | 279 | my %args = ( | |||||||||||||||||||||||||||||||||||||||||
| 1218 | table => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 1219 | alias => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 1220 | column => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 1221 | value => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 1222 | quote_value => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 1223 | entry_aggregator => 'or', | ||||||||||||||||||||||||||||||||||||||||||
| 1224 | case_sensitive => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 1225 | operator => '=', | ||||||||||||||||||||||||||||||||||||||||||
| 1226 | escape => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 1227 | subclause => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 1228 | leftjoin => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 1229 | @_ # get the real argumentlist | ||||||||||||||||||||||||||||||||||||||||||
| 1230 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1231 | |||||||||||||||||||||||||||||||||||||||||||
| 1232 | 279 | ▶ | return if $self->derived; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1233 | |||||||||||||||||||||||||||||||||||||||||||
| 1234 | #If we're performing a left join, we really want the alias to be the | ||||||||||||||||||||||||||||||||||||||||||
| 1235 | #left join criterion. | ||||||||||||||||||||||||||||||||||||||||||
| 1236 | |||||||||||||||||||||||||||||||||||||||||||
| 1237 | 279 | ▶ | if ( ( defined $args{'leftjoin'} ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $args{"leftjoin"} and not defined $args{"alias"}
MC/DC0%0/2
defined $args{"leftjoin"} and not defined $args{"alias"}
defined $args{"leftjoin"}
not defined $args{"alias"}
Truth table33%1/3
A and not(B)
A= defined $args{"leftjoin"}B= not defined $args{"alias"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1238 | && ( not defined $args{'alias'} ) ) | ||||||||||||||||||||||||||||||||||||||||||
| 1239 | { | ||||||||||||||||||||||||||||||||||||||||||
| 1240 | 0 | $args{'alias'} = $args{'leftjoin'}; | |||||||||||||||||||||||||||||||||||||||||
| 1241 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1242 | |||||||||||||||||||||||||||||||||||||||||||
| 1243 | # {{{ if there's no alias set, we need to set it | ||||||||||||||||||||||||||||||||||||||||||
| 1244 | |||||||||||||||||||||||||||||||||||||||||||
| 1245 | 279 | ▶ | unless ( defined $args{'alias'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1246 | |||||||||||||||||||||||||||||||||||||||||||
| 1247 | #if the table we're looking at is the same as the main table | ||||||||||||||||||||||||||||||||||||||||||
| 1248 | 271 | ▶ | if ( !defined $args{'table'} || $args{'table'} eq $self->table ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $args{"table"} or $args{"table"} eq $self->table
MC/DC0%0/2
not defined $args{"table"} or $args{"table"} eq $self->table
not defined $args{"table"}
$args{"table"} eq $self->table
Truth table33%1/3
not(A) or B
A= not defined $args{"table"}B= $args{"table"} eq $self->table
| |||||||||||||||||||||||||||||||||||||||||||
| 1249 | |||||||||||||||||||||||||||||||||||||||||||
| 1250 | # TODO this code assumes no self joins on that table. | ||||||||||||||||||||||||||||||||||||||||||
| 1251 | # if someone can name a case where we'd want to do that, | ||||||||||||||||||||||||||||||||||||||||||
| 1252 | # I'll change it. | ||||||||||||||||||||||||||||||||||||||||||
| 1253 | |||||||||||||||||||||||||||||||||||||||||||
| 1254 | 271 | $args{'alias'} = 'main'; | |||||||||||||||||||||||||||||||||||||||||
| 1255 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1256 | |||||||||||||||||||||||||||||||||||||||||||
| 1257 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1258 | 0 | $args{'alias'} = $self->new_alias( $args{'table'} ); | |||||||||||||||||||||||||||||||||||||||||
| 1259 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1260 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1261 | |||||||||||||||||||||||||||||||||||||||||||
| 1262 | # }}} | ||||||||||||||||||||||||||||||||||||||||||
| 1263 | |||||||||||||||||||||||||||||||||||||||||||
| 1264 | # $column_obj is undefined when the table2 argument to the join is a table | ||||||||||||||||||||||||||||||||||||||||||
| 1265 | # name and not a collection model class. In that case, the class key | ||||||||||||||||||||||||||||||||||||||||||
| 1266 | # doesn't exist for the join. | ||||||||||||||||||||||||||||||||||||||||||
| 1267 | my $class | ||||||||||||||||||||||||||||||||||||||||||
| 1268 | = $self->{joins}{ $args{alias} } | ||||||||||||||||||||||||||||||||||||||||||
| 1269 | && $self->{joins}{ $args{alias} }{class} | ||||||||||||||||||||||||||||||||||||||||||
| 1270 | ? $self->{joins}{ $args{alias} }{class} | ||||||||||||||||||||||||||||||||||||||||||
| 1271 | 279 | ▶ | ->new( $self->_new_collection_args ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"joins"}{$args{"alias"}} and $self->{"joins"}{$args{"alias"}}{"class"}
MC/DC0%0/2
$self->{"joins"}{$args{"alias"}} and $self->{"joins"}{$args{"alias"}}{"class"}
$self->{"joins"}{$args{"alias"}}
$self->{"joins"}{$args{"alias"}}{"class"}
Truth table66%2/3
A and B
A= $self->{"joins"}{$args{"alias"}}B= $self->{"joins"}{$args{"alias"}}{"class"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1272 | : $self; | ||||||||||||||||||||||||||||||||||||||||||
| 1273 | 279 | my $column_obj = $class->record_class->column( $args{column} ); | |||||||||||||||||||||||||||||||||||||||||
| 1274 | |||||||||||||||||||||||||||||||||||||||||||
| 1275 | $self->new_item->_apply_input_filters( | ||||||||||||||||||||||||||||||||||||||||||
| 1276 | column => $column_obj, | ||||||||||||||||||||||||||||||||||||||||||
| 1277 | value_ref => \$args{'value'}, | ||||||||||||||||||||||||||||||||||||||||||
| 1278 | 279 | ▶ | ) if $column_obj && $column_obj->encode_on_select && $args{operator} !~ /IS/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$column_obj and $column_obj->encode_on_select and not $args{"operator"} =~ /IS/
$column_obj and $column_obj->encode_on_select
MC/DC66%2/3
$column_obj and $column_obj->encode_on_select and not $args{"operator"} =~ /IS/
$column_obj
$column_obj->encode_on_select
not $args{"operator"} =~ /IS/
Truth table75%3/4
A and B and not(C)
A= $column_objB= $column_obj->encode_on_selectC= not $args{"operator"} =~ /IS/
| |||||||||||||||||||||||||||||||||||||||||||
| 1279 | |||||||||||||||||||||||||||||||||||||||||||
| 1280 | # Ensure that the column has nothing fishy going on. We can't | ||||||||||||||||||||||||||||||||||||||||||
| 1281 | # simply check $column_obj's truth because joins mostly join by | ||||||||||||||||||||||||||||||||||||||||||
| 1282 | # table name, not class, and we don't track table_name -> class. | ||||||||||||||||||||||||||||||||||||||||||
| 1283 | 279 | ▶ | if ($args{column} =~ /\W/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1284 | 0 | warn "Possible SQL injection on column '$args{column}' in limit at @{[join(',',(caller)[1,2])]}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 1285 | 0 | %args = ( | |||||||||||||||||||||||||||||||||||||||||
| 1286 | %args, | ||||||||||||||||||||||||||||||||||||||||||
| 1287 | column => 'id', | ||||||||||||||||||||||||||||||||||||||||||
| 1288 | operator => '<', | ||||||||||||||||||||||||||||||||||||||||||
| 1289 | value => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 1290 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1291 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1292 | 279 | ▶ | if ($args{operator} !~ /^(=|<|>|!=|<>|<=|>= | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1293 | |(NOT\s*)?LIKE | ||||||||||||||||||||||||||||||||||||||||||
| 1294 | |(NOT\s*)?(STARTS|ENDS)_?WITH | ||||||||||||||||||||||||||||||||||||||||||
| 1295 | |(NOT\s*)?MATCHES | ||||||||||||||||||||||||||||||||||||||||||
| 1296 | |IS(\s*NOT)? | ||||||||||||||||||||||||||||||||||||||||||
| 1297 | |IN)$/ix) { | ||||||||||||||||||||||||||||||||||||||||||
| 1298 | 0 | warn "Unknown operator '$args{operator}' in limit at @{[join(',',(caller)[1,2])]}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 1299 | 0 | %args = ( | |||||||||||||||||||||||||||||||||||||||||
| 1300 | %args, | ||||||||||||||||||||||||||||||||||||||||||
| 1301 | column => 'id', | ||||||||||||||||||||||||||||||||||||||||||
| 1302 | operator => '<', | ||||||||||||||||||||||||||||||||||||||||||
| 1303 | value => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 1304 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1305 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1306 | |||||||||||||||||||||||||||||||||||||||||||
| 1307 | |||||||||||||||||||||||||||||||||||||||||||
| 1308 | # Set this to the name of the column and the alias, unless we've been | ||||||||||||||||||||||||||||||||||||||||||
| 1309 | # handed a subclause name | ||||||||||||||||||||||||||||||||||||||||||
| 1310 | my $qualified_column | ||||||||||||||||||||||||||||||||||||||||||
| 1311 | = $args{'alias'} | ||||||||||||||||||||||||||||||||||||||||||
| 1312 | ? $args{'alias'} . "." . $args{'column'} | ||||||||||||||||||||||||||||||||||||||||||
| 1313 | 279 | ▶ | : $args{'column'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1314 | 279 | ▶ | my $clause_id = $args{'subclause'} || $qualified_column; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$args{'subclause'} || $qualified_column
MC/DC0%0/2
$args{'subclause'} || $qualified_column
$args{'subclause'}
$qualified_column
Truth table66%2/3
A || B
A= $args{'subclause'}B= $qualified_column
| |||||||||||||||||||||||||||||||||||||||||||
| 1315 | |||||||||||||||||||||||||||||||||||||||||||
| 1316 | |||||||||||||||||||||||||||||||||||||||||||
| 1317 | # make passing in an object DTRT | ||||||||||||||||||||||||||||||||||||||||||
| 1318 | 279 | my $value_ref = ref( $args{value} ); | |||||||||||||||||||||||||||||||||||||||||
| 1319 | 279 | ▶ | if ($value_ref) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1320 | 31 | ▶ | if ( ( $value_ref ne 'ARRAY' ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$value_ref ne "ARRAY" and $args{"value"}->isa("Jifty::DBI::Record")
MC/DC50%1/2
$value_ref ne "ARRAY" and $args{"value"}->isa("Jifty::DBI::Record")
$value_ref ne "ARRAY"
$args{"value"}->isa("Jifty::DBI::Record")
Truth table66%2/3
A and B
A= $value_ref ne "ARRAY"B= $args{"value"}->isa("Jifty::DBI::Record")
| |||||||||||||||||||||||||||||||||||||||||||
| 1321 | && $args{value}->isa('Jifty::DBI::Record') ) | ||||||||||||||||||||||||||||||||||||||||||
| 1322 | { | ||||||||||||||||||||||||||||||||||||||||||
| 1323 | 2 | ▶ | my $by = (defined $column_obj and defined $column_obj->by) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $column_obj and defined $column_obj->by
MC/DC0%0/2
defined $column_obj and defined $column_obj->by
defined $column_obj
defined $column_obj->by
Truth table33%1/3
A and B
A= defined $column_objB= defined $column_obj->by
| |||||||||||||||||||||||||||||||||||||||||||
| 1324 | ? $column_obj->by | ||||||||||||||||||||||||||||||||||||||||||
| 1325 | : 'id'; | ||||||||||||||||||||||||||||||||||||||||||
| 1326 | 2 | $args{value} = $args{value}->$by; | |||||||||||||||||||||||||||||||||||||||||
| 1327 | } elsif ( $value_ref eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 1328 | |||||||||||||||||||||||||||||||||||||||||||
| 1329 | # Don't modify the original reference, it isn't polite | ||||||||||||||||||||||||||||||||||||||||||
| 1330 | 29 | $args{value} = [ @{ $args{value} } ]; | |||||||||||||||||||||||||||||||||||||||||
| 1331 | map { | ||||||||||||||||||||||||||||||||||||||||||
| 1332 | 62 | ▶ | my $by = (defined $column_obj and defined $column_obj->by) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $column_obj and defined $column_obj->by
MC/DC50%1/2
defined $column_obj and defined $column_obj->by
defined $column_obj
defined $column_obj->by
Truth table66%2/3
A and B
A= defined $column_objB= defined $column_obj->by
| |||||||||||||||||||||||||||||||||||||||||||
| 1333 | ? $column_obj->by | ||||||||||||||||||||||||||||||||||||||||||
| 1334 | : 'id'; | ||||||||||||||||||||||||||||||||||||||||||
| 1335 | 62 | ▶ | $_ = ( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $_ and $_->isa("Jifty::DBI::Record")
MC/DC50%1/2
ref $_ and $_->isa("Jifty::DBI::Record")
ref $_
$_->isa("Jifty::DBI::Record")
Truth table66%2/3
A and B
A= ref $_B= $_->isa("Jifty::DBI::Record")
| |||||||||||||||||||||||||||||||||||||||||||
| 1336 | ( ref $_ && $_->isa('Jifty::DBI::Record') ) | ||||||||||||||||||||||||||||||||||||||||||
| 1337 | ? ( $_->$by ) | ||||||||||||||||||||||||||||||||||||||||||
| 1338 | : $_ | ||||||||||||||||||||||||||||||||||||||||||
| 1339 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 1340 | 29 | } @{ $args{value} }; | |||||||||||||||||||||||||||||||||||||||||
| 1341 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1342 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1343 | |||||||||||||||||||||||||||||||||||||||||||
| 1344 | #since we're changing the search criteria, we need to redo the search | ||||||||||||||||||||||||||||||||||||||||||
| 1345 | 279 | $self->redo_search(); | |||||||||||||||||||||||||||||||||||||||||
| 1346 | |||||||||||||||||||||||||||||||||||||||||||
| 1347 | #If it's a like, we supply the %s around the search term | ||||||||||||||||||||||||||||||||||||||||||
| 1348 | 279 | ▶ | if ( $args{'operator'} =~ /MATCHES/i ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 1349 | 25 | $args{'value'} = "%" . $args{'value'} . "%"; | |||||||||||||||||||||||||||||||||||||||||
| 1350 | } elsif ( $args{'operator'} =~ /STARTS_?WITH/i ) { | ||||||||||||||||||||||||||||||||||||||||||
| 1351 | 17 | $args{'value'} = $args{'value'} . "%"; | |||||||||||||||||||||||||||||||||||||||||
| 1352 | } elsif ( $args{'operator'} =~ /ENDS_?WITH/i ) { | ||||||||||||||||||||||||||||||||||||||||||
| 1353 | 17 | $args{'value'} = "%" . $args{'value'}; | |||||||||||||||||||||||||||||||||||||||||
| 1354 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1355 | 279 | $args{'operator'} =~ s/(?:MATCHES|ENDS_?WITH|STARTS_?WITH)/LIKE/i; | |||||||||||||||||||||||||||||||||||||||||
| 1356 | |||||||||||||||||||||||||||||||||||||||||||
| 1357 | # Force the value to NULL (non-quoted) if the operator is IS. | ||||||||||||||||||||||||||||||||||||||||||
| 1358 | 279 | ▶ | if ($args{'operator'} =~ /^IS(\s*NOT)?$/i) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1359 | 54 | $args{'quote_value'} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 1360 | 54 | $args{'value'} = 'NULL'; | |||||||||||||||||||||||||||||||||||||||||
| 1361 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1362 | |||||||||||||||||||||||||||||||||||||||||||
| 1363 | # Quote the value | ||||||||||||||||||||||||||||||||||||||||||
| 1364 | 279 | ▶ | if ( $args{'quote_value'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1365 | 223 | ▶ | if ( $value_ref eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1366 | 29 | map { $_ = $self->_handle->quote_value($_) } @{ $args{'value'} }; | |||||||||||||||||||||||||||||||||||||||||
| 1367 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1368 | 194 | $args{'value'} = $self->_handle->quote_value( $args{'value'} ); | |||||||||||||||||||||||||||||||||||||||||
| 1369 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1370 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1371 | |||||||||||||||||||||||||||||||||||||||||||
| 1372 | 279 | ▶ | if ( $args{'escape'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1373 | 4 | $args{'escape'} = 'ESCAPE ' . $self->_handle->quote_value( $args{escape} ); | |||||||||||||||||||||||||||||||||||||||||
| 1374 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1375 | |||||||||||||||||||||||||||||||||||||||||||
| 1376 | # If we're trying to get a leftjoin restriction, lets set | ||||||||||||||||||||||||||||||||||||||||||
| 1377 | # $restriction to point there. otherwise, lets construct normally | ||||||||||||||||||||||||||||||||||||||||||
| 1378 | |||||||||||||||||||||||||||||||||||||||||||
| 1379 | 279 | my $restriction; | |||||||||||||||||||||||||||||||||||||||||
| 1380 | 279 | ▶ | if ( $args{'leftjoin'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1381 | $restriction | ||||||||||||||||||||||||||||||||||||||||||
| 1382 | 0 | ▶ | = $self->{'joins'}{ $args{'leftjoin'} }{'criteria'}{$clause_id} | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"joins"}{$args{"leftjoin"}}{"criteria"}{$clause_id} ||= []
MC/DC0%0/1
$self->{"joins"}{$args{"leftjoin"}}{"criteria"}{$clause_id} ||= []
$self->{"joins"}{$args{"leftjoin"}}{"criteria"}{$clause_id}
Truth table0%0/2
A
A= $self->{"joins"}{$args{"leftjoin"}}{"criteria"}{$clause_id}
| |||||||||||||||||||||||||||||||||||||||||||
| 1383 | ||= []; | ||||||||||||||||||||||||||||||||||||||||||
| 1384 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1385 | 279 | ▶ | $restriction = $self->{'restrictions'}{$clause_id} ||= []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"restrictions"}{$clause_id} ||= []
MC/DC100%1/1
$self->{"restrictions"}{$clause_id} ||= []
$self->{"restrictions"}{$clause_id}
Truth table100%2/2
A
A= $self->{"restrictions"}{$clause_id}
| |||||||||||||||||||||||||||||||||||||||||||
| 1386 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1387 | |||||||||||||||||||||||||||||||||||||||||||
| 1388 | # If it's a new value or we're overwriting this sort of restriction, | ||||||||||||||||||||||||||||||||||||||||||
| 1389 | |||||||||||||||||||||||||||||||||||||||||||
| 1390 | 279 | ▶ | if ( defined $args{'value'} && $args{'quote_value'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $args{"value"} and $args{"quote_value"}
MC/DC50%1/2
defined $args{"value"} and $args{"quote_value"}
defined $args{"value"}
$args{"quote_value"}
Truth table66%2/3
A and B
A= defined $args{"value"}B= $args{"quote_value"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1391 | 223 | my $case_sensitive = 0; | |||||||||||||||||||||||||||||||||||||||||
| 1392 | 223 | ▶ | if ( defined $args{'case_sensitive'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 1393 | 114 | $case_sensitive = $args{'case_sensitive'}; | |||||||||||||||||||||||||||||||||||||||||
| 1394 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1395 | elsif ( $column_obj ) { | ||||||||||||||||||||||||||||||||||||||||||
| 1396 | 103 | $case_sensitive = $column_obj->case_sensitive; | |||||||||||||||||||||||||||||||||||||||||
| 1397 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1398 | # don't worry about case for numeric columns_in_db | ||||||||||||||||||||||||||||||||||||||||||
| 1399 | # only be case insensitive when we KNOW it's a text | ||||||||||||||||||||||||||||||||||||||||||
| 1400 | 223 | ▶ | if ( $column_obj && !$case_sensitive && !$column_obj->is_string ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$column_obj and not $case_sensitive and not $column_obj->is_string
$column_obj and not $case_sensitive
MC/DC100%3/3
$column_obj and not $case_sensitive and not $column_obj->is_string
$column_obj
not $case_sensitive
not $column_obj->is_string
Truth table100%4/4
A and not(B) and not(C)
A= $column_objB= not $case_sensitiveC= not $column_obj->is_string
| |||||||||||||||||||||||||||||||||||||||||||
| 1401 | 68 | $case_sensitive = 1; | |||||||||||||||||||||||||||||||||||||||||
| 1402 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1403 | |||||||||||||||||||||||||||||||||||||||||||
| 1404 | 223 | ▶ | if ( !$case_sensitive && $self->_handle->case_sensitive ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $case_sensitive and $self->_handle->case_sensitive
MC/DC50%1/2
not $case_sensitive and $self->_handle->case_sensitive
not $case_sensitive
$self->_handle->case_sensitive
Truth table66%2/3
not(A) and B
A= not $case_sensitiveB= $self->_handle->case_sensitive
| |||||||||||||||||||||||||||||||||||||||||||
| 1405 | ( $qualified_column, $args{'operator'}, $args{'value'} ) | ||||||||||||||||||||||||||||||||||||||||||
| 1406 | = $self->_handle->_make_clause_case_insensitive( | ||||||||||||||||||||||||||||||||||||||||||
| 1407 | 67 | $qualified_column, $args{'operator'}, $args{'value'} ); | |||||||||||||||||||||||||||||||||||||||||
| 1408 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1409 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1410 | |||||||||||||||||||||||||||||||||||||||||||
| 1411 | 279 | ▶ | if ( $value_ref eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1412 | croak | ||||||||||||||||||||||||||||||||||||||||||
| 1413 | 'Limits with an array ref are only allowed with operator \'IN\' or \'=\'' | ||||||||||||||||||||||||||||||||||||||||||
| 1414 | 29 | ▶ | unless $args{'operator'} =~ /^(IN|=)$/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1415 | 29 | $args{'value'} = '( ' . join( ',', @{ $args{'value'} } ) . ' )'; | |||||||||||||||||||||||||||||||||||||||||
| 1416 | 29 | $args{'operator'} = 'IN'; | |||||||||||||||||||||||||||||||||||||||||
| 1417 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1418 | |||||||||||||||||||||||||||||||||||||||||||
| 1419 | my $clause = { | ||||||||||||||||||||||||||||||||||||||||||
| 1420 | column => $qualified_column, | ||||||||||||||||||||||||||||||||||||||||||
| 1421 | operator => $args{'operator'}, | ||||||||||||||||||||||||||||||||||||||||||
| 1422 | value => $args{'value'}, | ||||||||||||||||||||||||||||||||||||||||||
| 1423 | 279 | escape => $args{'escape'}, | |||||||||||||||||||||||||||||||||||||||||
| 1424 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 1425 | |||||||||||||||||||||||||||||||||||||||||||
| 1426 | # Juju because this should come _AFTER_ the EA | ||||||||||||||||||||||||||||||||||||||||||
| 1427 | 279 | my @prefix; | |||||||||||||||||||||||||||||||||||||||||
| 1428 | 279 | ▶ | if ( $self->{'_open_parens'}{$clause_id} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1429 | 1 | @prefix = ('(') x delete $self->{'_open_parens'}{$clause_id}; | |||||||||||||||||||||||||||||||||||||||||
| 1430 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1431 | |||||||||||||||||||||||||||||||||||||||||||
| 1432 | 279 | ▶ | if ( lc( $args{'entry_aggregator'} || "" ) eq 'none' || !@$restriction ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition60%3/5
lc($args{"entry_aggregator"} || "") eq "none" or not @$restriction
$args{'entry_aggregator'} || ''
MC/DC0%0/2
lc($args{"entry_aggregator"} || "") eq "none" or not @$restriction
$args{'entry_aggregator'}
not @$restriction
Truth table0%0/3
A or not(B)
A= $args{'entry_aggregator'}B= not @$restriction
| |||||||||||||||||||||||||||||||||||||||||||
| 1433 | 274 | @$restriction = ( @prefix, $clause ); | |||||||||||||||||||||||||||||||||||||||||
| 1434 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1435 | 5 | push @$restriction, $args{'entry_aggregator'}, @prefix, $clause; | |||||||||||||||||||||||||||||||||||||||||
| 1436 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1437 | |||||||||||||||||||||||||||||||||||||||||||
| 1438 | # We're now limited. people can do searches. | ||||||||||||||||||||||||||||||||||||||||||
| 1439 | |||||||||||||||||||||||||||||||||||||||||||
| 1440 | 279 | $self->_is_limited(1); | |||||||||||||||||||||||||||||||||||||||||
| 1441 | |||||||||||||||||||||||||||||||||||||||||||
| 1442 | 279 | ▶ | if ( defined( $args{'alias'} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1443 | 279 | return ( $args{'alias'} ); | |||||||||||||||||||||||||||||||||||||||||
| 1444 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1445 | 0 | return (1); | |||||||||||||||||||||||||||||||||||||||||
| 1446 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1447 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1448 | |||||||||||||||||||||||||||||||||||||||||||
| 1449 | =head2 open_paren CLAUSE | ||||||||||||||||||||||||||||||||||||||||||
| 1450 | |||||||||||||||||||||||||||||||||||||||||||
| 1451 | Places an open parenthesis at the current location in the given C<CLAUSE>. | ||||||||||||||||||||||||||||||||||||||||||
| 1452 | Note that this can be used for Deep Magic, and has a high likelihood | ||||||||||||||||||||||||||||||||||||||||||
| 1453 | of allowing you to construct malformed SQL queries. Its interface | ||||||||||||||||||||||||||||||||||||||||||
| 1454 | will probably change in the near future, but its presence allows for | ||||||||||||||||||||||||||||||||||||||||||
| 1455 | arbitrarily complex queries. | ||||||||||||||||||||||||||||||||||||||||||
| 1456 | |||||||||||||||||||||||||||||||||||||||||||
| 1457 | Here's an example, to construct a SQL WHERE clause roughly equivalent to (depending on your SQL dialect): | ||||||||||||||||||||||||||||||||||||||||||
| 1458 | |||||||||||||||||||||||||||||||||||||||||||
| 1459 | parent = 12 AND task_type = 'action' | ||||||||||||||||||||||||||||||||||||||||||
| 1460 | AND (status = 'open' | ||||||||||||||||||||||||||||||||||||||||||
| 1461 | OR (status = 'done' | ||||||||||||||||||||||||||||||||||||||||||
| 1462 | AND completed_on >= '2008-06-26 11:39:22')) | ||||||||||||||||||||||||||||||||||||||||||
| 1463 | |||||||||||||||||||||||||||||||||||||||||||
| 1464 | You can use sub-clauses and C<open_paren> and C<close_paren> as follows: | ||||||||||||||||||||||||||||||||||||||||||
| 1465 | |||||||||||||||||||||||||||||||||||||||||||
| 1466 | $col->limit( column => 'parent', value => 12 ); | ||||||||||||||||||||||||||||||||||||||||||
| 1467 | $col->limit( column => 'task_type', value => 'action' ); | ||||||||||||||||||||||||||||||||||||||||||
| 1468 | |||||||||||||||||||||||||||||||||||||||||||
| 1469 | $col->open_paren("my_clause"); | ||||||||||||||||||||||||||||||||||||||||||
| 1470 | |||||||||||||||||||||||||||||||||||||||||||
| 1471 | $col->limit( subclause => "my_clause", column => 'status', value => 'open' ); | ||||||||||||||||||||||||||||||||||||||||||
| 1472 | |||||||||||||||||||||||||||||||||||||||||||
| 1473 | $col->open_paren("my_clause"); | ||||||||||||||||||||||||||||||||||||||||||
| 1474 | |||||||||||||||||||||||||||||||||||||||||||
| 1475 | $col->limit( subclause => "my_clause", column => 'status', | ||||||||||||||||||||||||||||||||||||||||||
| 1476 | value => 'done', entry_aggregator => 'OR' ); | ||||||||||||||||||||||||||||||||||||||||||
| 1477 | $col->limit( subclause => "my_clause", column => 'completed_on', | ||||||||||||||||||||||||||||||||||||||||||
| 1478 | operator => '>=', value => '2008-06-26 11:39:22' ); | ||||||||||||||||||||||||||||||||||||||||||
| 1479 | |||||||||||||||||||||||||||||||||||||||||||
| 1480 | $col->close_paren("my_clause"); | ||||||||||||||||||||||||||||||||||||||||||
| 1481 | |||||||||||||||||||||||||||||||||||||||||||
| 1482 | $col->close_paren("my_clause"); | ||||||||||||||||||||||||||||||||||||||||||
| 1483 | |||||||||||||||||||||||||||||||||||||||||||
| 1484 | Where the C<"my_clause"> can be any name you choose. | ||||||||||||||||||||||||||||||||||||||||||
| 1485 | |||||||||||||||||||||||||||||||||||||||||||
| 1486 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1487 | |||||||||||||||||||||||||||||||||||||||||||
| 1488 | sub open_paren { | ||||||||||||||||||||||||||||||||||||||||||
| 1489 | 1 | ▶ | my ( $self, $clause ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
open_paren: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1490 | 1 | $self->{_open_parens}{$clause}++; | |||||||||||||||||||||||||||||||||||||||||
| 1491 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1492 | |||||||||||||||||||||||||||||||||||||||||||
| 1493 | =head2 close_paren CLAUSE | ||||||||||||||||||||||||||||||||||||||||||
| 1494 | |||||||||||||||||||||||||||||||||||||||||||
| 1495 | Places a close parenthesis at the current location in the given C<CLAUSE>. | ||||||||||||||||||||||||||||||||||||||||||
| 1496 | Note that this can be used for Deep Magic, and has a high likelihood | ||||||||||||||||||||||||||||||||||||||||||
| 1497 | of allowing you to construct malformed SQL queries. Its interface | ||||||||||||||||||||||||||||||||||||||||||
| 1498 | will probably change in the near future, but its presence allows for | ||||||||||||||||||||||||||||||||||||||||||
| 1499 | arbitrarily complex queries. | ||||||||||||||||||||||||||||||||||||||||||
| 1500 | |||||||||||||||||||||||||||||||||||||||||||
| 1501 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1502 | |||||||||||||||||||||||||||||||||||||||||||
| 1503 | # Immediate Action | ||||||||||||||||||||||||||||||||||||||||||
| 1504 | sub close_paren { | ||||||||||||||||||||||||||||||||||||||||||
| 1505 | 1 | ▶ | my ( $self, $clause ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
close_paren: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1506 | 1 | ▶ | my $restriction = $self->{'restrictions'}{$clause} ||= []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"restrictions"}{$clause} ||= []
MC/DC0%0/1
$self->{"restrictions"}{$clause} ||= []
$self->{"restrictions"}{$clause}
Truth table50%1/2
A
A= $self->{"restrictions"}{$clause}
| |||||||||||||||||||||||||||||||||||||||||||
| 1507 | 1 | push @$restriction, ')'; | |||||||||||||||||||||||||||||||||||||||||
| 1508 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1509 | |||||||||||||||||||||||||||||||||||||||||||
| 1510 | sub _add_subclause { | ||||||||||||||||||||||||||||||||||||||||||
| 1511 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_add_subclause: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 1512 | 0 | my $clauseid = shift; | |||||||||||||||||||||||||||||||||||||||||
| 1513 | 0 | my $subclause = shift; | |||||||||||||||||||||||||||||||||||||||||
| 1514 | |||||||||||||||||||||||||||||||||||||||||||
| 1515 | 0 | $self->{'subclauses'}{"$clauseid"} = $subclause; | |||||||||||||||||||||||||||||||||||||||||
| 1516 | |||||||||||||||||||||||||||||||||||||||||||
| 1517 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1518 | |||||||||||||||||||||||||||||||||||||||||||
| 1519 | sub _where_clause { | ||||||||||||||||||||||||||||||||||||||||||
| 1520 | 324 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_where_clause: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1521 | 324 | my $where_clause = ''; | |||||||||||||||||||||||||||||||||||||||||
| 1522 | |||||||||||||||||||||||||||||||||||||||||||
| 1523 | # Go through all the generic restrictions and build up the | ||||||||||||||||||||||||||||||||||||||||||
| 1524 | # "generic_restrictions" subclause. That's the only one that the | ||||||||||||||||||||||||||||||||||||||||||
| 1525 | # collection builds itself. Arguably, the abstraction should be | ||||||||||||||||||||||||||||||||||||||||||
| 1526 | # better, but I don't really see where to put it. | ||||||||||||||||||||||||||||||||||||||||||
| 1527 | 324 | $self->_compile_generic_restrictions(); | |||||||||||||||||||||||||||||||||||||||||
| 1528 | |||||||||||||||||||||||||||||||||||||||||||
| 1529 | #Go through all restriction types. Build the where clause from the | ||||||||||||||||||||||||||||||||||||||||||
| 1530 | #Various subclauses. | ||||||||||||||||||||||||||||||||||||||||||
| 1531 | |||||||||||||||||||||||||||||||||||||||||||
| 1532 | my @subclauses = grep defined && length, | ||||||||||||||||||||||||||||||||||||||||||
| 1533 | 324 | ▶ | values %{ $self->{'subclauses'} }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
defined $_ && length $_
MC/DC50%1/2
defined $_ && length $_
defined $_
length $_
Truth table66%2/3
A && B
A= defined $_B= length $_
| |||||||||||||||||||||||||||||||||||||||||||
| 1534 | |||||||||||||||||||||||||||||||||||||||||||
| 1535 | 324 | ▶ | $where_clause = " WHERE " . CORE::join( ' AND ', @subclauses ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1536 | if (@subclauses); | ||||||||||||||||||||||||||||||||||||||||||
| 1537 | |||||||||||||||||||||||||||||||||||||||||||
| 1538 | 324 | return ($where_clause); | |||||||||||||||||||||||||||||||||||||||||
| 1539 | |||||||||||||||||||||||||||||||||||||||||||
| 1540 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1541 | |||||||||||||||||||||||||||||||||||||||||||
| 1542 | #Compile the restrictions to a WHERE Clause | ||||||||||||||||||||||||||||||||||||||||||
| 1543 | |||||||||||||||||||||||||||||||||||||||||||
| 1544 | sub _compile_generic_restrictions { | ||||||||||||||||||||||||||||||||||||||||||
| 1545 | 324 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_compile_generic_restrictions: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1546 | |||||||||||||||||||||||||||||||||||||||||||
| 1547 | 324 | delete $self->{'subclauses'}{'generic_restrictions'}; | |||||||||||||||||||||||||||||||||||||||||
| 1548 | |||||||||||||||||||||||||||||||||||||||||||
| 1549 | # Go through all the restrictions of this type. Buld up the generic subclause | ||||||||||||||||||||||||||||||||||||||||||
| 1550 | 324 | my $result = ''; | |||||||||||||||||||||||||||||||||||||||||
| 1551 | 324 | ▶ | foreach my $restriction ( grep $_ && @$_, | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$_ && @$_
MC/DC0%0/2
$_ && @$_
$_
@$_
Truth table33%1/3
A && B
A= $_B= @$_
| |||||||||||||||||||||||||||||||||||||||||||
| 1552 | 324 | values %{ $self->{'restrictions'} } ) | |||||||||||||||||||||||||||||||||||||||||
| 1553 | { | ||||||||||||||||||||||||||||||||||||||||||
| 1554 | 312 | ▶ | $result .= ' AND ' if $result; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1555 | 312 | $result .= '('; | |||||||||||||||||||||||||||||||||||||||||
| 1556 | 312 | foreach my $entry (@$restriction) { | |||||||||||||||||||||||||||||||||||||||||
| 1557 | 328 | ▶ | unless ( ref $entry ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1558 | 10 | $result .= ' ' . $entry . ' '; | |||||||||||||||||||||||||||||||||||||||||
| 1559 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1560 | $result .= join ' ', | ||||||||||||||||||||||||||||||||||||||||||
| 1561 | 1272 | grep {defined} | |||||||||||||||||||||||||||||||||||||||||
| 1562 | 318 | @{$entry}{qw(column operator value escape)}; | |||||||||||||||||||||||||||||||||||||||||
| 1563 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1564 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1565 | 312 | $result .= ')'; | |||||||||||||||||||||||||||||||||||||||||
| 1566 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1567 | 324 | return ( $self->{'subclauses'}{'generic_restrictions'} = $result ); | |||||||||||||||||||||||||||||||||||||||||
| 1568 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1569 | |||||||||||||||||||||||||||||||||||||||||||
| 1570 | # set $self->{$type .'_clause'} to new value | ||||||||||||||||||||||||||||||||||||||||||
| 1571 | # redo_search only if new value is really new | ||||||||||||||||||||||||||||||||||||||||||
| 1572 | sub _set_clause { | ||||||||||||||||||||||||||||||||||||||||||
| 1573 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set_clause: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1574 | 0 | my ( $type, $value ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1575 | 0 | $type .= '_clause'; | |||||||||||||||||||||||||||||||||||||||||
| 1576 | 0 | ▶ | if ( ( $self->{$type} || '' ) ne ( $value || '' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/4
$self->{$type} || ''
$value || ''
MC/DC0%0/1
$self->{$type} || ''
$self->{$type}
MC/DC0%0/1
$value || ''
$value
Truth table0%0/2
A
A= $self->{$type}
Truth table0%0/2
A
A= $value
| |||||||||||||||||||||||||||||||||||||||||||
| 1577 | 0 | $self->redo_search; | |||||||||||||||||||||||||||||||||||||||||
| 1578 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1579 | 0 | $self->{$type} = $value; | |||||||||||||||||||||||||||||||||||||||||
| 1580 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1581 | |||||||||||||||||||||||||||||||||||||||||||
| 1582 | # stub for back-compat | ||||||||||||||||||||||||||||||||||||||||||
| 1583 | sub _quote_value { | ||||||||||||||||||||||||||||||||||||||||||
| 1584 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_quote_value: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 1585 | 0 | return $self->_handle->quote_value(@_); | |||||||||||||||||||||||||||||||||||||||||
| 1586 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1587 | |||||||||||||||||||||||||||||||||||||||||||
| 1588 | =head2 order_by_cols DEPRECATED | ||||||||||||||||||||||||||||||||||||||||||
| 1589 | |||||||||||||||||||||||||||||||||||||||||||
| 1590 | *DEPRECATED*. Use C<order_by> method. | ||||||||||||||||||||||||||||||||||||||||||
| 1591 | |||||||||||||||||||||||||||||||||||||||||||
| 1592 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1593 | |||||||||||||||||||||||||||||||||||||||||||
| 1594 | sub order_by_cols { | ||||||||||||||||||||||||||||||||||||||||||
| 1595 | 0 | ▶ | require Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
order_by_cols: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1596 | 0 | Carp::cluck("order_by_cols is deprecated, use order_by method"); | |||||||||||||||||||||||||||||||||||||||||
| 1597 | 0 | goto &order_by; | |||||||||||||||||||||||||||||||||||||||||
| 1598 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1599 | |||||||||||||||||||||||||||||||||||||||||||
| 1600 | =head2 order_by EMPTY|HASH|ARRAY_OF_HASHES | ||||||||||||||||||||||||||||||||||||||||||
| 1601 | |||||||||||||||||||||||||||||||||||||||||||
| 1602 | Orders the returned results by column(s) and/or function(s) on column(s). | ||||||||||||||||||||||||||||||||||||||||||
| 1603 | |||||||||||||||||||||||||||||||||||||||||||
| 1604 | Takes a paramhash of C<alias>, C<column> and C<order> | ||||||||||||||||||||||||||||||||||||||||||
| 1605 | or C<function> and C<order>. | ||||||||||||||||||||||||||||||||||||||||||
| 1606 | C<alias> defaults to main. | ||||||||||||||||||||||||||||||||||||||||||
| 1607 | C<order> defaults to ASC(ending), DES(cending) is also a valid value. | ||||||||||||||||||||||||||||||||||||||||||
| 1608 | C<column> and C<function> have no default values. | ||||||||||||||||||||||||||||||||||||||||||
| 1609 | |||||||||||||||||||||||||||||||||||||||||||
| 1610 | Use C<function> instead of C<alias> and C<column> to order by | ||||||||||||||||||||||||||||||||||||||||||
| 1611 | the function value. Note that if you want use a column as argument of | ||||||||||||||||||||||||||||||||||||||||||
| 1612 | the function then you have to build correct reference with alias | ||||||||||||||||||||||||||||||||||||||||||
| 1613 | in the C<alias.column> format. | ||||||||||||||||||||||||||||||||||||||||||
| 1614 | |||||||||||||||||||||||||||||||||||||||||||
| 1615 | If you specify C<function> and C<column>, the column (and C<alias>) will be | ||||||||||||||||||||||||||||||||||||||||||
| 1616 | wrapped in the function. This is useful for simple functions like C<min> or | ||||||||||||||||||||||||||||||||||||||||||
| 1617 | C<lower>. | ||||||||||||||||||||||||||||||||||||||||||
| 1618 | |||||||||||||||||||||||||||||||||||||||||||
| 1619 | Use array of hashes to order by many columns/functions. | ||||||||||||||||||||||||||||||||||||||||||
| 1620 | |||||||||||||||||||||||||||||||||||||||||||
| 1621 | Calling this I<sets> the ordering, it doesn't refine it. If you want to keep | ||||||||||||||||||||||||||||||||||||||||||
| 1622 | previous ordering, use C<add_order_by>. | ||||||||||||||||||||||||||||||||||||||||||
| 1623 | |||||||||||||||||||||||||||||||||||||||||||
| 1624 | The results would be unordered if method called without arguments. | ||||||||||||||||||||||||||||||||||||||||||
| 1625 | |||||||||||||||||||||||||||||||||||||||||||
| 1626 | Returns the current list of columns. | ||||||||||||||||||||||||||||||||||||||||||
| 1627 | |||||||||||||||||||||||||||||||||||||||||||
| 1628 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1629 | |||||||||||||||||||||||||||||||||||||||||||
| 1630 | sub order_by { | ||||||||||||||||||||||||||||||||||||||||||
| 1631 | 7 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
order_by: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1632 | 7 | ▶ | return if $self->derived; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1633 | 7 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1634 | 6 | $self->{'order_by'} = []; | |||||||||||||||||||||||||||||||||||||||||
| 1635 | 6 | $self->add_order_by(@_); | |||||||||||||||||||||||||||||||||||||||||
| 1636 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1637 | 7 | ▶ | return ( $self->{'order_by'} || [] ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'order_by'} || []
MC/DC0%0/1
$self->{'order_by'} || []
$self->{'order_by'}
Truth table50%1/2
A
A= $self->{'order_by'}
| |||||||||||||||||||||||||||||||||||||||||||
| 1638 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1639 | |||||||||||||||||||||||||||||||||||||||||||
| 1640 | =head2 add_order_by EMPTY|HASH|ARRAY_OF_HASHES | ||||||||||||||||||||||||||||||||||||||||||
| 1641 | |||||||||||||||||||||||||||||||||||||||||||
| 1642 | Same as order_by, except it will not reset the ordering you have already set. | ||||||||||||||||||||||||||||||||||||||||||
| 1643 | |||||||||||||||||||||||||||||||||||||||||||
| 1644 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1645 | |||||||||||||||||||||||||||||||||||||||||||
| 1646 | sub add_order_by { | ||||||||||||||||||||||||||||||||||||||||||
| 1647 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_order_by: called CC=6 SCAR=18.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1648 | 8 | ▶ | return if $self->derived; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1649 | 8 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1650 | 8 | my @args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1651 | |||||||||||||||||||||||||||||||||||||||||||
| 1652 | 8 | ▶ | unless ( UNIVERSAL::isa( $args[0], 'HASH' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1653 | 7 | @args = {@args}; | |||||||||||||||||||||||||||||||||||||||||
| 1654 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1655 | 8 | ▶ | push @{ $self->{'order_by'} ||= [] }, @args; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"order_by"} ||= []
MC/DC0%0/1
$self->{"order_by"} ||= []
$self->{"order_by"}
Truth table50%1/2
A
A= $self->{"order_by"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1656 | 8 | $self->redo_search(); | |||||||||||||||||||||||||||||||||||||||||
| 1657 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1658 | 8 | ▶ | return ( $self->{'order_by'} || [] ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'order_by'} || []
MC/DC0%0/1
$self->{'order_by'} || []
$self->{'order_by'}
Truth table50%1/2
A
A= $self->{'order_by'}
| |||||||||||||||||||||||||||||||||||||||||||
| 1659 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1660 | |||||||||||||||||||||||||||||||||||||||||||
| 1661 | =head2 clear_order_by | ||||||||||||||||||||||||||||||||||||||||||
| 1662 | |||||||||||||||||||||||||||||||||||||||||||
| 1663 | Clears whatever would normally get set in the ORDER BY clause. | ||||||||||||||||||||||||||||||||||||||||||
| 1664 | |||||||||||||||||||||||||||||||||||||||||||
| 1665 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1666 | |||||||||||||||||||||||||||||||||||||||||||
| 1667 | sub clear_order_by { | ||||||||||||||||||||||||||||||||||||||||||
| 1668 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clear_order_by: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1669 | |||||||||||||||||||||||||||||||||||||||||||
| 1670 | 1 | $self->{'order_by'} = []; | |||||||||||||||||||||||||||||||||||||||||
| 1671 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1672 | |||||||||||||||||||||||||||||||||||||||||||
| 1673 | =head2 _order_clause | ||||||||||||||||||||||||||||||||||||||||||
| 1674 | |||||||||||||||||||||||||||||||||||||||||||
| 1675 | returns the ORDER BY clause for the search. | ||||||||||||||||||||||||||||||||||||||||||
| 1676 | |||||||||||||||||||||||||||||||||||||||||||
| 1677 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1678 | |||||||||||||||||||||||||||||||||||||||||||
| 1679 | sub _order_clause { | ||||||||||||||||||||||||||||||||||||||||||
| 1680 | 256 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_order_clause: called CC=15 SCAR=27.3
| |||||||||||||||||||||||||||||||||||||||||||
| 1681 | |||||||||||||||||||||||||||||||||||||||||||
| 1682 | 256 | ▶ | return '' unless $self->{'order_by'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1683 | |||||||||||||||||||||||||||||||||||||||||||
| 1684 | 13 | my $clause = ''; | |||||||||||||||||||||||||||||||||||||||||
| 1685 | 13 | foreach my $row ( @{ $self->{'order_by'} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 1686 | |||||||||||||||||||||||||||||||||||||||||||
| 1687 | 29 | my %rowhash = ( | |||||||||||||||||||||||||||||||||||||||||
| 1688 | alias => 'main', | ||||||||||||||||||||||||||||||||||||||||||
| 1689 | column => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 1690 | order => 'ASC', | ||||||||||||||||||||||||||||||||||||||||||
| 1691 | %$row | ||||||||||||||||||||||||||||||||||||||||||
| 1692 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1693 | 29 | ▶ | if ( $rowhash{'order'} =~ /^des/i ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1694 | 12 | $rowhash{'order'} = "DESC"; | |||||||||||||||||||||||||||||||||||||||||
| 1695 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1696 | 17 | $rowhash{'order'} = "ASC"; | |||||||||||||||||||||||||||||||||||||||||
| 1697 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1698 | |||||||||||||||||||||||||||||||||||||||||||
| 1699 | 29 | ▶ | if ( $rowhash{'function'} and not defined $rowhash{'column'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$rowhash{"function"} and not defined $rowhash{"column"}
MC/DC100%2/2
$rowhash{"function"} and not defined $rowhash{"column"}
$rowhash{"function"}
not defined $rowhash{"column"}
Truth table100%3/3
A and not(B)
A= $rowhash{"function"}B= not defined $rowhash{"column"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1700 | 6 | ▶ | $clause .= ( $clause ? ", " : " " ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1701 | 6 | $clause .= $rowhash{'function'} . ' '; | |||||||||||||||||||||||||||||||||||||||||
| 1702 | 6 | $clause .= $rowhash{'order'}; | |||||||||||||||||||||||||||||||||||||||||
| 1703 | |||||||||||||||||||||||||||||||||||||||||||
| 1704 | ▶ | } elsif ( ( defined $rowhash{'alias'} ) | |||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
defined $rowhash{"alias"} and $rowhash{"column"}
MC/DC0%0/2
defined $rowhash{"alias"} and $rowhash{"column"}
defined $rowhash{"alias"}
$rowhash{"column"}
Truth table33%1/3
A and B
A= defined $rowhash{"alias"}B= $rowhash{"column"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1705 | and ( $rowhash{'column'} ) ) | ||||||||||||||||||||||||||||||||||||||||||
| 1706 | { | ||||||||||||||||||||||||||||||||||||||||||
| 1707 | 23 | ▶ | if ($rowhash{'column'} =~ /\W/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1708 | 0 | warn "Possible SQL injection in column '$rowhash{column}' in order_by\n"; | |||||||||||||||||||||||||||||||||||||||||
| 1709 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 1710 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1711 | |||||||||||||||||||||||||||||||||||||||||||
| 1712 | 23 | ▶ | $clause .= ( $clause ? ", " : " " ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1713 | 23 | ▶ | $clause .= $rowhash{'function'} . "(" if $rowhash{'function'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1714 | 23 | ▶ | $clause .= $rowhash{'alias'} . "." if $rowhash{'alias'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1715 | 23 | $clause .= $rowhash{'column'}; | |||||||||||||||||||||||||||||||||||||||||
| 1716 | 23 | ▶ | $clause .= ")" if $rowhash{'function'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1717 | 23 | $clause .= " " . $rowhash{'order'}; | |||||||||||||||||||||||||||||||||||||||||
| 1718 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1719 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1720 | 13 | ▶ | $clause = " ORDER BY$clause " if $clause; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1721 | 13 | return $clause; | |||||||||||||||||||||||||||||||||||||||||
| 1722 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1723 | |||||||||||||||||||||||||||||||||||||||||||
| 1724 | =head2 group_by_cols DEPRECATED | ||||||||||||||||||||||||||||||||||||||||||
| 1725 | |||||||||||||||||||||||||||||||||||||||||||
| 1726 | *DEPRECATED*. Use group_by method. | ||||||||||||||||||||||||||||||||||||||||||
| 1727 | |||||||||||||||||||||||||||||||||||||||||||
| 1728 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1729 | |||||||||||||||||||||||||||||||||||||||||||
| 1730 | sub group_by_cols { | ||||||||||||||||||||||||||||||||||||||||||
| 1731 | 0 | ▶ | require Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
group_by_cols: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1732 | 0 | Carp::cluck("group_by_cols is deprecated, use group_by method"); | |||||||||||||||||||||||||||||||||||||||||
| 1733 | 0 | goto &group_by; | |||||||||||||||||||||||||||||||||||||||||
| 1734 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1735 | |||||||||||||||||||||||||||||||||||||||||||
| 1736 | =head2 group_by EMPTY|HASH|ARRAY_OF_HASHES | ||||||||||||||||||||||||||||||||||||||||||
| 1737 | |||||||||||||||||||||||||||||||||||||||||||
| 1738 | Groups the search results by column(s) and/or function(s) on column(s). | ||||||||||||||||||||||||||||||||||||||||||
| 1739 | |||||||||||||||||||||||||||||||||||||||||||
| 1740 | Takes a paramhash of C<alias> and C<column> or C<function>. | ||||||||||||||||||||||||||||||||||||||||||
| 1741 | C<alias> defaults to main. | ||||||||||||||||||||||||||||||||||||||||||
| 1742 | C<column> and C<function> have no default values. | ||||||||||||||||||||||||||||||||||||||||||
| 1743 | |||||||||||||||||||||||||||||||||||||||||||
| 1744 | Use C<function> instead of C<alias> and C<column> to group by | ||||||||||||||||||||||||||||||||||||||||||
| 1745 | the function value. Note that if you want use a column as argument | ||||||||||||||||||||||||||||||||||||||||||
| 1746 | of the function then you have to build correct reference with alias | ||||||||||||||||||||||||||||||||||||||||||
| 1747 | in the C<alias.column> format. | ||||||||||||||||||||||||||||||||||||||||||
| 1748 | |||||||||||||||||||||||||||||||||||||||||||
| 1749 | Use array of hashes to group by many columns/functions. | ||||||||||||||||||||||||||||||||||||||||||
| 1750 | |||||||||||||||||||||||||||||||||||||||||||
| 1751 | The method is EXPERIMENTAL and subject to change. | ||||||||||||||||||||||||||||||||||||||||||
| 1752 | |||||||||||||||||||||||||||||||||||||||||||
| 1753 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1754 | |||||||||||||||||||||||||||||||||||||||||||
| 1755 | sub group_by { | ||||||||||||||||||||||||||||||||||||||||||
| 1756 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
group_by: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1757 | |||||||||||||||||||||||||||||||||||||||||||
| 1758 | 2 | ▶ | return if $self->derived; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1759 | 2 | my @args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1760 | |||||||||||||||||||||||||||||||||||||||||||
| 1761 | 2 | ▶ | unless ( UNIVERSAL::isa( $args[0], 'HASH' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1762 | 2 | @args = {@args}; | |||||||||||||||||||||||||||||||||||||||||
| 1763 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1764 | 2 | $self->{'group_by'} = \@args; | |||||||||||||||||||||||||||||||||||||||||
| 1765 | 2 | $self->redo_search(); | |||||||||||||||||||||||||||||||||||||||||
| 1766 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1767 | |||||||||||||||||||||||||||||||||||||||||||
| 1768 | =head2 _group_clause | ||||||||||||||||||||||||||||||||||||||||||
| 1769 | |||||||||||||||||||||||||||||||||||||||||||
| 1770 | Private function to return the "GROUP BY" clause for this query. | ||||||||||||||||||||||||||||||||||||||||||
| 1771 | |||||||||||||||||||||||||||||||||||||||||||
| 1772 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1773 | |||||||||||||||||||||||||||||||||||||||||||
| 1774 | sub _group_clause { | ||||||||||||||||||||||||||||||||||||||||||
| 1775 | 247 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_group_clause: called CC=10 SCAR=28.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1776 | 247 | ▶ | return '' unless $self->{'group_by'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1777 | |||||||||||||||||||||||||||||||||||||||||||
| 1778 | 3 | my $row; | |||||||||||||||||||||||||||||||||||||||||
| 1779 | my $clause; | ||||||||||||||||||||||||||||||||||||||||||
| 1780 | |||||||||||||||||||||||||||||||||||||||||||
| 1781 | 3 | foreach $row ( @{ $self->{'group_by'} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 1782 | 3 | my %rowhash = ( | |||||||||||||||||||||||||||||||||||||||||
| 1783 | alias => 'main', | ||||||||||||||||||||||||||||||||||||||||||
| 1784 | |||||||||||||||||||||||||||||||||||||||||||
| 1785 | column => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 1786 | %$row | ||||||||||||||||||||||||||||||||||||||||||
| 1787 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1788 | 3 | ▶ | if ( $rowhash{'function'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 1789 | 0 | ▶ | $clause .= ( $clause ? ", " : " " ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1790 | 0 | $clause .= $rowhash{'function'}; | |||||||||||||||||||||||||||||||||||||||||
| 1791 | |||||||||||||||||||||||||||||||||||||||||||
| 1792 | ▶ | } elsif ( ( $rowhash{'alias'} ) | |||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$rowhash{"alias"} and $rowhash{"column"}
MC/DC0%0/2
$rowhash{"alias"} and $rowhash{"column"}
$rowhash{"alias"}
$rowhash{"column"}
Truth table33%1/3
A and B
A= $rowhash{"alias"}B= $rowhash{"column"}
| |||||||||||||||||||||||||||||||||||||||||||
| 1793 | and ( $rowhash{'column'} ) ) | ||||||||||||||||||||||||||||||||||||||||||
| 1794 | { | ||||||||||||||||||||||||||||||||||||||||||
| 1795 | 3 | ▶ | if ($rowhash{'column'} =~ /\W/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1796 | 0 | warn "Possible SQL injection in column '$rowhash{column}' in group_by\n"; | |||||||||||||||||||||||||||||||||||||||||
| 1797 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 1798 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1799 | |||||||||||||||||||||||||||||||||||||||||||
| 1800 | 3 | ▶ | $clause .= ( $clause ? ", " : " " ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1801 | 3 | $clause .= $rowhash{'alias'} . "."; | |||||||||||||||||||||||||||||||||||||||||
| 1802 | 3 | $clause .= $rowhash{'column'}; | |||||||||||||||||||||||||||||||||||||||||
| 1803 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1804 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1805 | 3 | ▶ | if ($clause) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1806 | 3 | return " GROUP BY" . $clause . " "; | |||||||||||||||||||||||||||||||||||||||||
| 1807 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1808 | 0 | return ''; | |||||||||||||||||||||||||||||||||||||||||
| 1809 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1810 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1811 | |||||||||||||||||||||||||||||||||||||||||||
| 1812 | =head2 new_alias table_OR_CLASS | ||||||||||||||||||||||||||||||||||||||||||
| 1813 | |||||||||||||||||||||||||||||||||||||||||||
| 1814 | Takes the name of a table or a Jifty::DBI::Record subclass. | ||||||||||||||||||||||||||||||||||||||||||
| 1815 | Returns the string of a new Alias for that table, which can be used | ||||||||||||||||||||||||||||||||||||||||||
| 1816 | to Join tables or to limit what gets found by | ||||||||||||||||||||||||||||||||||||||||||
| 1817 | a search. | ||||||||||||||||||||||||||||||||||||||||||
| 1818 | |||||||||||||||||||||||||||||||||||||||||||
| 1819 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1820 | |||||||||||||||||||||||||||||||||||||||||||
| 1821 | sub new_alias { | ||||||||||||||||||||||||||||||||||||||||||
| 1822 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new_alias: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1823 | 5 | ▶ | my $refers_to = shift || die "Missing parameter"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || die('Missing parameter')
MC/DC0%0/1
shift() || die('Missing parameter')
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 1824 | 5 | my $table; | |||||||||||||||||||||||||||||||||||||||||
| 1825 | 5 | my $class = undef; | |||||||||||||||||||||||||||||||||||||||||
| 1826 | 5 | ▶ | if ( $refers_to->can('table') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1827 | 2 | $table = $refers_to->table; | |||||||||||||||||||||||||||||||||||||||||
| 1828 | 2 | $class = $refers_to; | |||||||||||||||||||||||||||||||||||||||||
| 1829 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1830 | 3 | $table = $refers_to; | |||||||||||||||||||||||||||||||||||||||||
| 1831 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1832 | |||||||||||||||||||||||||||||||||||||||||||
| 1833 | 5 | my $alias = $self->_get_alias($table); | |||||||||||||||||||||||||||||||||||||||||
| 1834 | |||||||||||||||||||||||||||||||||||||||||||
| 1835 | 5 | ▶ | $self->{'joins'}{$alias} = { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1836 | alias => $alias, | ||||||||||||||||||||||||||||||||||||||||||
| 1837 | table => $table, | ||||||||||||||||||||||||||||||||||||||||||
| 1838 | type => 'CROSS', | ||||||||||||||||||||||||||||||||||||||||||
| 1839 | ( $class ? ( class => $class ) : () ), | ||||||||||||||||||||||||||||||||||||||||||
| 1840 | alias_string => " CROSS JOIN $table $alias ", | ||||||||||||||||||||||||||||||||||||||||||
| 1841 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 1842 | |||||||||||||||||||||||||||||||||||||||||||
| 1843 | 5 | return $alias; | |||||||||||||||||||||||||||||||||||||||||
| 1844 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1845 | |||||||||||||||||||||||||||||||||||||||||||
| 1846 | # _get_alias is a private function which takes an tablename and | ||||||||||||||||||||||||||||||||||||||||||
| 1847 | # returns a new alias for that table without adding something to | ||||||||||||||||||||||||||||||||||||||||||
| 1848 | # self->{'joins'}. This function is used by new_alias and the | ||||||||||||||||||||||||||||||||||||||||||
| 1849 | # as-yet-unnamed left join code | ||||||||||||||||||||||||||||||||||||||||||
| 1850 | |||||||||||||||||||||||||||||||||||||||||||
| 1851 | sub _get_alias { | ||||||||||||||||||||||||||||||||||||||||||
| 1852 | 14 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_alias: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1853 | 14 | my $table = shift; | |||||||||||||||||||||||||||||||||||||||||
| 1854 | |||||||||||||||||||||||||||||||||||||||||||
| 1855 | 14 | return $table . "_" . ++$self->{'alias_count'}; | |||||||||||||||||||||||||||||||||||||||||
| 1856 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1857 | |||||||||||||||||||||||||||||||||||||||||||
| 1858 | =head2 join | ||||||||||||||||||||||||||||||||||||||||||
| 1859 | |||||||||||||||||||||||||||||||||||||||||||
| 1860 | Join instructs Jifty::DBI::Collection to join two tables. | ||||||||||||||||||||||||||||||||||||||||||
| 1861 | |||||||||||||||||||||||||||||||||||||||||||
| 1862 | The standard form takes a paramhash with keys C<alias1>, C<column1>, C<alias2> | ||||||||||||||||||||||||||||||||||||||||||
| 1863 | and C<column2>. C<alias1> and C<alias2> are column aliases obtained from | ||||||||||||||||||||||||||||||||||||||||||
| 1864 | $self->new_alias or a $self->limit. C<column1> and C<column2> are the columns | ||||||||||||||||||||||||||||||||||||||||||
| 1865 | in C<alias1> and C<alias2> that should be linked, respectively. For this | ||||||||||||||||||||||||||||||||||||||||||
| 1866 | type of join, this method has no return value. | ||||||||||||||||||||||||||||||||||||||||||
| 1867 | |||||||||||||||||||||||||||||||||||||||||||
| 1868 | Supplying the parameter C<type> => 'left' causes Join to perform a left | ||||||||||||||||||||||||||||||||||||||||||
| 1869 | join. in this case, it takes C<alias1>, C<column1>, C<table2> and | ||||||||||||||||||||||||||||||||||||||||||
| 1870 | C<column2>. Because of the way that left joins work, this method needs a | ||||||||||||||||||||||||||||||||||||||||||
| 1871 | table for the second column rather than merely an alias. For this type | ||||||||||||||||||||||||||||||||||||||||||
| 1872 | of join, it will return the alias generated by the join. | ||||||||||||||||||||||||||||||||||||||||||
| 1873 | |||||||||||||||||||||||||||||||||||||||||||
| 1874 | The parameter C<operator> defaults C<=>, but you can specify other | ||||||||||||||||||||||||||||||||||||||||||
| 1875 | operators to join with. | ||||||||||||||||||||||||||||||||||||||||||
| 1876 | |||||||||||||||||||||||||||||||||||||||||||
| 1877 | Passing a true value for the C<is_distinct> parameter allows one to | ||||||||||||||||||||||||||||||||||||||||||
| 1878 | specify that, despite the join, the original table's rows are will all | ||||||||||||||||||||||||||||||||||||||||||
| 1879 | still be distinct. | ||||||||||||||||||||||||||||||||||||||||||
| 1880 | |||||||||||||||||||||||||||||||||||||||||||
| 1881 | Instead of C<alias1>/C<column1>, it's possible to specify expression, to join | ||||||||||||||||||||||||||||||||||||||||||
| 1882 | C<alias2>/C<table2> on an arbitrary expression. | ||||||||||||||||||||||||||||||||||||||||||
| 1883 | |||||||||||||||||||||||||||||||||||||||||||
| 1884 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1885 | |||||||||||||||||||||||||||||||||||||||||||
| 1886 | sub join { | ||||||||||||||||||||||||||||||||||||||||||
| 1887 | 12 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
join: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1888 | 12 | my %args = ( | |||||||||||||||||||||||||||||||||||||||||
| 1889 | type => 'normal', | ||||||||||||||||||||||||||||||||||||||||||
| 1890 | column1 => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 1891 | alias1 => 'main', | ||||||||||||||||||||||||||||||||||||||||||
| 1892 | table2 => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 1893 | column2 => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 1894 | alias2 => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 1895 | @_ | ||||||||||||||||||||||||||||||||||||||||||
| 1896 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1897 | |||||||||||||||||||||||||||||||||||||||||||
| 1898 | 12 | ▶ | return if $self->derived; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1899 | 12 | $self->_handle->join( collection => $self, %args ); | |||||||||||||||||||||||||||||||||||||||||
| 1900 | |||||||||||||||||||||||||||||||||||||||||||
| 1901 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1902 | |||||||||||||||||||||||||||||||||||||||||||
| 1903 | =head2 set_page_info [per_page => NUMBER,] [current_page => NUMBER] | ||||||||||||||||||||||||||||||||||||||||||
| 1904 | |||||||||||||||||||||||||||||||||||||||||||
| 1905 | Sets the current page (one-based) and number of items per page on the | ||||||||||||||||||||||||||||||||||||||||||
| 1906 | pager object, and pulls the number of elements from the collection. | ||||||||||||||||||||||||||||||||||||||||||
| 1907 | This both sets up the collection's L<Data::Page> object so that you | ||||||||||||||||||||||||||||||||||||||||||
| 1908 | can use its calculations, and sets the L<Jifty::DBI::Collection> | ||||||||||||||||||||||||||||||||||||||||||
| 1909 | C<first_row> and C<rows_per_page> so that queries return values from | ||||||||||||||||||||||||||||||||||||||||||
| 1910 | the selected page. | ||||||||||||||||||||||||||||||||||||||||||
| 1911 | |||||||||||||||||||||||||||||||||||||||||||
| 1912 | If a C<current_page> of C<all> is passed, then paging is basically disabled | ||||||||||||||||||||||||||||||||||||||||||
| 1913 | (by setting C<per_page> to the number of entries, and C<current_page> to 1) | ||||||||||||||||||||||||||||||||||||||||||
| 1914 | |||||||||||||||||||||||||||||||||||||||||||
| 1915 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1916 | |||||||||||||||||||||||||||||||||||||||||||
| 1917 | sub set_page_info { | ||||||||||||||||||||||||||||||||||||||||||
| 1918 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_page_info: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1919 | 0 | my %args = ( | |||||||||||||||||||||||||||||||||||||||||
| 1920 | per_page => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 1921 | current_page => 1, # 1-based | ||||||||||||||||||||||||||||||||||||||||||
| 1922 | @_ | ||||||||||||||||||||||||||||||||||||||||||
| 1923 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1924 | 0 | ▶ | return if $self->derived; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1925 | |||||||||||||||||||||||||||||||||||||||||||
| 1926 | 0 | my $weakself = $self; | |||||||||||||||||||||||||||||||||||||||||
| 1927 | 0 | weaken($weakself); | |||||||||||||||||||||||||||||||||||||||||
| 1928 | |||||||||||||||||||||||||||||||||||||||||||
| 1929 | 0 | ▶ | my $total_entries = lazy { $weakself->count_all }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 1930 | |||||||||||||||||||||||||||||||||||||||||||
| 1931 | 0 | ▶ | if ($args{'current_page'} eq 'all') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1932 | 0 | $args{'current_page'} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 1933 | 0 | $args{'per_page'} = $total_entries; | |||||||||||||||||||||||||||||||||||||||||
| 1934 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1935 | |||||||||||||||||||||||||||||||||||||||||||
| 1936 | $self->pager->total_entries($total_entries) | ||||||||||||||||||||||||||||||||||||||||||
| 1937 | ->entries_per_page( $args{'per_page'} ) | ||||||||||||||||||||||||||||||||||||||||||
| 1938 | 0 | ->current_page( $args{'current_page'} ); | |||||||||||||||||||||||||||||||||||||||||
| 1939 | |||||||||||||||||||||||||||||||||||||||||||
| 1940 | 0 | $self->rows_per_page( $args{'per_page'} ); | |||||||||||||||||||||||||||||||||||||||||
| 1941 | |||||||||||||||||||||||||||||||||||||||||||
| 1942 | # We're not using $pager->first because it automatically does a count_all | ||||||||||||||||||||||||||||||||||||||||||
| 1943 | # to correctly return '0' for empty collections | ||||||||||||||||||||||||||||||||||||||||||
| 1944 | 0 | $self->first_row( ( $args{'current_page'} - 1 ) * $args{'per_page'} + 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 1945 | |||||||||||||||||||||||||||||||||||||||||||
| 1946 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1947 | |||||||||||||||||||||||||||||||||||||||||||
| 1948 | =head2 rows_per_page | ||||||||||||||||||||||||||||||||||||||||||
| 1949 | |||||||||||||||||||||||||||||||||||||||||||
| 1950 | limits the number of rows returned by the database. Optionally, takes | ||||||||||||||||||||||||||||||||||||||||||
| 1951 | an integer which restricts the # of rows returned in a result Returns | ||||||||||||||||||||||||||||||||||||||||||
| 1952 | the number of rows the database should display. | ||||||||||||||||||||||||||||||||||||||||||
| 1953 | |||||||||||||||||||||||||||||||||||||||||||
| 1954 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1955 | |||||||||||||||||||||||||||||||||||||||||||
| 1956 | =head2 first_row | ||||||||||||||||||||||||||||||||||||||||||
| 1957 | |||||||||||||||||||||||||||||||||||||||||||
| 1958 | Get or set the first row of the result set the database should return. | ||||||||||||||||||||||||||||||||||||||||||
| 1959 | Takes an optional single integer argument. Returns the currently set | ||||||||||||||||||||||||||||||||||||||||||
| 1960 | integer first row that the database should return. | ||||||||||||||||||||||||||||||||||||||||||
| 1961 | |||||||||||||||||||||||||||||||||||||||||||
| 1962 | |||||||||||||||||||||||||||||||||||||||||||
| 1963 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1964 | |||||||||||||||||||||||||||||||||||||||||||
| 1965 | # returns the first row | ||||||||||||||||||||||||||||||||||||||||||
| 1966 | sub first_row { | ||||||||||||||||||||||||||||||||||||||||||
| 1967 | 246 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
first_row: called CC=2 SCAR=9.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1968 | 246 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1969 | 0 | $self->{'first_row'} = shift; | |||||||||||||||||||||||||||||||||||||||||
| 1970 | |||||||||||||||||||||||||||||||||||||||||||
| 1971 | #SQL starts counting at 0 | ||||||||||||||||||||||||||||||||||||||||||
| 1972 | 0 | $self->{'first_row'}--; | |||||||||||||||||||||||||||||||||||||||||
| 1973 | |||||||||||||||||||||||||||||||||||||||||||
| 1974 | #gotta redo the search if changing pages | ||||||||||||||||||||||||||||||||||||||||||
| 1975 | 0 | $self->redo_search(); | |||||||||||||||||||||||||||||||||||||||||
| 1976 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1977 | 246 | return ( $self->{'first_row'} ); | |||||||||||||||||||||||||||||||||||||||||
| 1978 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1979 | |||||||||||||||||||||||||||||||||||||||||||
| 1980 | =head2 _items_counter | ||||||||||||||||||||||||||||||||||||||||||
| 1981 | |||||||||||||||||||||||||||||||||||||||||||
| 1982 | Returns the current position in the record set. | ||||||||||||||||||||||||||||||||||||||||||
| 1983 | |||||||||||||||||||||||||||||||||||||||||||
| 1984 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1985 | |||||||||||||||||||||||||||||||||||||||||||
| 1986 | sub _items_counter { | ||||||||||||||||||||||||||||||||||||||||||
| 1987 | 10 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_items_counter: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1988 | 10 | return $self->{'itemscount'}; | |||||||||||||||||||||||||||||||||||||||||
| 1989 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1990 | |||||||||||||||||||||||||||||||||||||||||||
| 1991 | =head2 count | ||||||||||||||||||||||||||||||||||||||||||
| 1992 | |||||||||||||||||||||||||||||||||||||||||||
| 1993 | Returns the number of records in the set. | ||||||||||||||||||||||||||||||||||||||||||
| 1994 | |||||||||||||||||||||||||||||||||||||||||||
| 1995 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1996 | |||||||||||||||||||||||||||||||||||||||||||
| 1997 | sub count { | ||||||||||||||||||||||||||||||||||||||||||
| 1998 | 119 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
count: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1999 | |||||||||||||||||||||||||||||||||||||||||||
| 2000 | # An unlimited search returns no tickets | ||||||||||||||||||||||||||||||||||||||||||
| 2001 | 119 | ▶ | return 0 unless ( $self->_is_limited ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2002 | |||||||||||||||||||||||||||||||||||||||||||
| 2003 | # If we haven't actually got all objects loaded in memory, we | ||||||||||||||||||||||||||||||||||||||||||
| 2004 | # really just want to do a quick count from the database. | ||||||||||||||||||||||||||||||||||||||||||
| 2005 | 106 | ▶ | if ( $self->{'must_redo_search'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2006 | |||||||||||||||||||||||||||||||||||||||||||
| 2007 | # If we haven't already asked the database for the row count, do that | ||||||||||||||||||||||||||||||||||||||||||
| 2008 | 82 | ▶ | $self->_do_count unless ( $self->{'raw_rows'} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2009 | |||||||||||||||||||||||||||||||||||||||||||
| 2010 | #Report back the raw # of rows in the database | ||||||||||||||||||||||||||||||||||||||||||
| 2011 | 82 | return ( $self->{'raw_rows'} ); | |||||||||||||||||||||||||||||||||||||||||
| 2012 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2013 | |||||||||||||||||||||||||||||||||||||||||||
| 2014 | # If we have loaded everything from the DB we have an | ||||||||||||||||||||||||||||||||||||||||||
| 2015 | # accurate count already. | ||||||||||||||||||||||||||||||||||||||||||
| 2016 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 2017 | 24 | return $self->_record_count; | |||||||||||||||||||||||||||||||||||||||||
| 2018 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2019 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2020 | |||||||||||||||||||||||||||||||||||||||||||
| 2021 | =head2 count_all | ||||||||||||||||||||||||||||||||||||||||||
| 2022 | |||||||||||||||||||||||||||||||||||||||||||
| 2023 | Returns the total number of potential records in the set, ignoring any | ||||||||||||||||||||||||||||||||||||||||||
| 2024 | limit_clause. | ||||||||||||||||||||||||||||||||||||||||||
| 2025 | |||||||||||||||||||||||||||||||||||||||||||
| 2026 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 2027 | |||||||||||||||||||||||||||||||||||||||||||
| 2028 | # 22:24 [Robrt(500@outer.space)] It has to do with Caching. | ||||||||||||||||||||||||||||||||||||||||||
| 2029 | # 22:25 [Robrt(500@outer.space)] The documentation says it ignores the limit. | ||||||||||||||||||||||||||||||||||||||||||
| 2030 | # 22:25 [Robrt(500@outer.space)] But I don't believe thats true. | ||||||||||||||||||||||||||||||||||||||||||
| 2031 | # 22:26 [msg(Robrt)] yeah. I | ||||||||||||||||||||||||||||||||||||||||||
| 2032 | # 22:26 [msg(Robrt)] yeah. I'm not convinced it does anything useful right now | ||||||||||||||||||||||||||||||||||||||||||
| 2033 | # 22:26 [msg(Robrt)] especially since until a week ago, it was setting one variable and returning another | ||||||||||||||||||||||||||||||||||||||||||
| 2034 | # 22:27 [Robrt(500@outer.space)] I remember. | ||||||||||||||||||||||||||||||||||||||||||
| 2035 | # 22:27 [Robrt(500@outer.space)] It had to do with which Cached value was returned. | ||||||||||||||||||||||||||||||||||||||||||
| 2036 | # 22:27 [msg(Robrt)] (given that every time we try to explain it, we get it Wrong) | ||||||||||||||||||||||||||||||||||||||||||
| 2037 | # 22:27 [Robrt(500@outer.space)] Because Count can return a different number than actual NumberOfResults | ||||||||||||||||||||||||||||||||||||||||||
| 2038 | # 22:28 [msg(Robrt)] in what case? | ||||||||||||||||||||||||||||||||||||||||||
| 2039 | # 22:28 [Robrt(500@outer.space)] count_all _always_ used the return value of _do_count(), as opposed to Count which would return the cached number of | ||||||||||||||||||||||||||||||||||||||||||
| 2040 | # results returned. | ||||||||||||||||||||||||||||||||||||||||||
| 2041 | # 22:28 [Robrt(500@outer.space)] IIRC, if you do a search with a limit, then raw_rows will == limit. | ||||||||||||||||||||||||||||||||||||||||||
| 2042 | # 22:31 [msg(Robrt)] ah. | ||||||||||||||||||||||||||||||||||||||||||
| 2043 | # 22:31 [msg(Robrt)] that actually makes sense | ||||||||||||||||||||||||||||||||||||||||||
| 2044 | # 22:31 [Robrt(500@outer.space)] You should paste this conversation into the count_all docs. | ||||||||||||||||||||||||||||||||||||||||||
| 2045 | # 22:31 [msg(Robrt)] perhaps I'll create a new method that _actually_ do that. | ||||||||||||||||||||||||||||||||||||||||||
| 2046 | # 22:32 [msg(Robrt)] since I'm not convinced it's been doing that correctly | ||||||||||||||||||||||||||||||||||||||||||
| 2047 | |||||||||||||||||||||||||||||||||||||||||||
| 2048 | sub count_all { | ||||||||||||||||||||||||||||||||||||||||||
| 2049 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
count_all: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 2050 | |||||||||||||||||||||||||||||||||||||||||||
| 2051 | # An unlimited search returns no tickets | ||||||||||||||||||||||||||||||||||||||||||
| 2052 | 0 | ▶ | return 0 unless ( $self->_is_limited ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2053 | |||||||||||||||||||||||||||||||||||||||||||
| 2054 | # If we haven't actually got all objects loaded in memory, we | ||||||||||||||||||||||||||||||||||||||||||
| 2055 | # really just want to do a quick count from the database. | ||||||||||||||||||||||||||||||||||||||||||
| 2056 | 0 | ▶ | if ( $self->{'must_redo_search'} || !$self->{'count_all'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"must_redo_search"} or not $self->{"count_all"}
MC/DC0%0/2
$self->{"must_redo_search"} or not $self->{"count_all"}
$self->{"must_redo_search"}
not $self->{"count_all"}
Truth table0%0/3
A or not(B)
A= $self->{"must_redo_search"}B= not $self->{"count_all"}
| |||||||||||||||||||||||||||||||||||||||||||
| 2057 | |||||||||||||||||||||||||||||||||||||||||||
| 2058 | # If we haven't already asked the database for the row count, do that | ||||||||||||||||||||||||||||||||||||||||||
| 2059 | 0 | ▶ | $self->_do_count(1) unless ( $self->{'count_all'} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2060 | |||||||||||||||||||||||||||||||||||||||||||
| 2061 | #Report back the raw # of rows in the database | ||||||||||||||||||||||||||||||||||||||||||
| 2062 | 0 | return ( $self->{'count_all'} ); | |||||||||||||||||||||||||||||||||||||||||
| 2063 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2064 | |||||||||||||||||||||||||||||||||||||||||||
| 2065 | # If we have loaded everything from the DB we have an | ||||||||||||||||||||||||||||||||||||||||||
| 2066 | # accurate count already. | ||||||||||||||||||||||||||||||||||||||||||
| 2067 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 2068 | 0 | return $self->_record_count; | |||||||||||||||||||||||||||||||||||||||||
| 2069 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2070 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2071 | |||||||||||||||||||||||||||||||||||||||||||
| 2072 | =head2 is_last | ||||||||||||||||||||||||||||||||||||||||||
| 2073 | |||||||||||||||||||||||||||||||||||||||||||
| 2074 | Returns true if the current row is the last record in the set. | ||||||||||||||||||||||||||||||||||||||||||
| 2075 | |||||||||||||||||||||||||||||||||||||||||||
| 2076 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 2077 | |||||||||||||||||||||||||||||||||||||||||||
| 2078 | sub is_last { | ||||||||||||||||||||||||||||||||||||||||||
| 2079 | 18 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_last: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 2080 | |||||||||||||||||||||||||||||||||||||||||||
| 2081 | 18 | ▶ | return undef unless $self->count; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2082 | |||||||||||||||||||||||||||||||||||||||||||
| 2083 | 10 | ▶ | if ( $self->_items_counter == $self->count ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2084 | 6 | return (1); | |||||||||||||||||||||||||||||||||||||||||
| 2085 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 2086 | 4 | return (0); | |||||||||||||||||||||||||||||||||||||||||
| 2087 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2088 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2089 | |||||||||||||||||||||||||||||||||||||||||||
| 2090 | =head2 DEBUG | ||||||||||||||||||||||||||||||||||||||||||
| 2091 | |||||||||||||||||||||||||||||||||||||||||||
| 2092 | Gets/sets the DEBUG flag. | ||||||||||||||||||||||||||||||||||||||||||
| 2093 | |||||||||||||||||||||||||||||||||||||||||||
| 2094 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 2095 | |||||||||||||||||||||||||||||||||||||||||||
| 2096 | sub DEBUG { | ||||||||||||||||||||||||||||||||||||||||||
| 2097 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DEBUG: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 2098 | 0 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2099 | 0 | $self->{'DEBUG'} = shift; | |||||||||||||||||||||||||||||||||||||||||
| 2100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2101 | 0 | return ( $self->{'DEBUG'} ); | |||||||||||||||||||||||||||||||||||||||||
| 2102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2103 | |||||||||||||||||||||||||||||||||||||||||||
| 2104 | =head2 column | ||||||||||||||||||||||||||||||||||||||||||
| 2105 | |||||||||||||||||||||||||||||||||||||||||||
| 2106 | Normally a collection object contains record objects populated with all columns | ||||||||||||||||||||||||||||||||||||||||||
| 2107 | in the database, but you can restrict the records to only contain some | ||||||||||||||||||||||||||||||||||||||||||
| 2108 | particular columns, by calling the C<column> method once for each column you | ||||||||||||||||||||||||||||||||||||||||||
| 2109 | are interested in. | ||||||||||||||||||||||||||||||||||||||||||
| 2110 | |||||||||||||||||||||||||||||||||||||||||||
| 2111 | Takes a hash of parameters; the C<column>, C<table> and C<alias> keys means | ||||||||||||||||||||||||||||||||||||||||||
| 2112 | the same as in the C<limit> method. A special C<function> key may contain | ||||||||||||||||||||||||||||||||||||||||||
| 2113 | one of several possible kinds of expressions: | ||||||||||||||||||||||||||||||||||||||||||
| 2114 | |||||||||||||||||||||||||||||||||||||||||||
| 2115 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 2116 | |||||||||||||||||||||||||||||||||||||||||||
| 2117 | =item C<DISTINCT COUNT> | ||||||||||||||||||||||||||||||||||||||||||
| 2118 | |||||||||||||||||||||||||||||||||||||||||||
| 2119 | Same as C<COUNT(DISTINCT ?)>. | ||||||||||||||||||||||||||||||||||||||||||
| 2120 | |||||||||||||||||||||||||||||||||||||||||||
| 2121 | =item Expression with C<?> in it | ||||||||||||||||||||||||||||||||||||||||||
| 2122 | |||||||||||||||||||||||||||||||||||||||||||
| 2123 | The C<?> is substituted with the column name, then passed verbatim to the | ||||||||||||||||||||||||||||||||||||||||||
| 2124 | underlying C<SELECT> statement. | ||||||||||||||||||||||||||||||||||||||||||
| 2125 | |||||||||||||||||||||||||||||||||||||||||||
| 2126 | =item Expression with C<(> in it | ||||||||||||||||||||||||||||||||||||||||||
| 2127 | |||||||||||||||||||||||||||||||||||||||||||
| 2128 | The expression is passed verbatim to the underlying C<SELECT>. | ||||||||||||||||||||||||||||||||||||||||||
| 2129 | |||||||||||||||||||||||||||||||||||||||||||
| 2130 | =item Any other expression | ||||||||||||||||||||||||||||||||||||||||||
| 2131 | |||||||||||||||||||||||||||||||||||||||||||
| 2132 | The expression is taken to be a function name. For example, C<SUM> means | ||||||||||||||||||||||||||||||||||||||||||
| 2133 | the same thing as C<SUM(?)>. | ||||||||||||||||||||||||||||||||||||||||||
| 2134 | |||||||||||||||||||||||||||||||||||||||||||
| 2135 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 2136 | |||||||||||||||||||||||||||||||||||||||||||
| 2137 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 2138 | |||||||||||||||||||||||||||||||||||||||||||
| 2139 | sub column { | ||||||||||||||||||||||||||||||||||||||||||
| 2140 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
column: called CC=12 SCAR=34.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 2141 | 3 | my %args = ( | |||||||||||||||||||||||||||||||||||||||||
| 2142 | table => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 2143 | alias => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 2144 | column => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 2145 | function => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 2146 | @_ | ||||||||||||||||||||||||||||||||||||||||||
| 2147 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 2148 | |||||||||||||||||||||||||||||||||||||||||||
| 2149 | 3 | ▶ | my $table = $args{table} || do { | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$args{'table'} || do {
MC/DC0%0/2
$args{'table'} || do {
if (my $alias = $args{'alias'}) {
$alias =~ s/_\d+$//;
$alias;
}
else {
$self->table;
}
}
$args{'table'}
do {
if (my $alias = $args{'alias'}) {
$alias =~ s/_\d+$//;
$alias;
}
else {
$self->table;
}
}
Truth table33%1/3
A || B
A= $args{'table'}B= do {
if (my $alias = $args{'alias'}) {
$alias =~ s/_\d+$//;
$alias;
}
else {
$self->table;
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 2150 | 3 | ▶ | if ( my $alias = $args{alias} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2151 | 0 | $alias =~ s/_\d+$//; | |||||||||||||||||||||||||||||||||||||||||
| 2152 | 0 | $alias; | |||||||||||||||||||||||||||||||||||||||||
| 2153 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 2154 | 3 | $self->table; | |||||||||||||||||||||||||||||||||||||||||
| 2155 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2156 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 2157 | |||||||||||||||||||||||||||||||||||||||||||
| 2158 | 3 | ▶ | my $name = ( $args{alias} || 'main' ) . '.' . $args{column}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{'alias'} || 'main'
MC/DC0%0/1
$args{'alias'} || 'main'
$args{'alias'}
Truth table50%1/2
A
A= $args{'alias'}
| |||||||||||||||||||||||||||||||||||||||||||
| 2159 | 3 | ▶ | if ( my $func = $args{function} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2160 | 0 | ▶ | if ( $func =~ /^DISTINCT\s*COUNT$/i ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 2161 | 0 | $name = "COUNT(DISTINCT $name)"; | |||||||||||||||||||||||||||||||||||||||||
| 2162 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2163 | |||||||||||||||||||||||||||||||||||||||||||
| 2164 | # If we want to substitute | ||||||||||||||||||||||||||||||||||||||||||
| 2165 | elsif ( $func =~ /\?/ ) { | ||||||||||||||||||||||||||||||||||||||||||
| 2166 | 0 | $name =~ s/\?/$name/g; | |||||||||||||||||||||||||||||||||||||||||
| 2167 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2168 | |||||||||||||||||||||||||||||||||||||||||||
| 2169 | # If we want to call a simple function on the column | ||||||||||||||||||||||||||||||||||||||||||
| 2170 | elsif ( $func !~ /\(/ ) { | ||||||||||||||||||||||||||||||||||||||||||
| 2171 | 0 | $name = "\U$func\E($name)"; | |||||||||||||||||||||||||||||||||||||||||
| 2172 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 2173 | 0 | $name = $func; | |||||||||||||||||||||||||||||||||||||||||
| 2174 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2175 | |||||||||||||||||||||||||||||||||||||||||||
| 2176 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2177 | |||||||||||||||||||||||||||||||||||||||||||
| 2178 | 3 | ▶ | my $column = "col" . @{ $self->{columns} ||= [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"columns"} ||= []
MC/DC0%0/1
$self->{"columns"} ||= []
$self->{"columns"}
Truth table50%1/2
A
A= $self->{"columns"}
| |||||||||||||||||||||||||||||||||||||||||||
| 2179 | 3 | ▶ | $column = $args{column} if $table eq $self->table and !$args{alias}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$table eq $self->table and not $args{"alias"}
MC/DC0%0/2
$table eq $self->table and not $args{"alias"}
$table eq $self->table
not $args{"alias"}
Truth table33%1/3
A and not(B)
A= $table eq $self->tableB= not $args{"alias"}
| |||||||||||||||||||||||||||||||||||||||||||
| 2180 | 3 | ▶ | $column = ( $args{'alias'} || 'main' ) . "_" . $column; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{'alias'} || 'main'
MC/DC0%0/1
$args{'alias'} || 'main'
$args{'alias'}
Truth table50%1/2
A
A= $args{'alias'}
| |||||||||||||||||||||||||||||||||||||||||||
| 2181 | 3 | push @{ $self->{columns} }, "$name AS \L$column"; | |||||||||||||||||||||||||||||||||||||||||
| 2182 | 3 | return $column; | |||||||||||||||||||||||||||||||||||||||||
| 2183 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2184 | |||||||||||||||||||||||||||||||||||||||||||
| 2185 | =head2 columns LIST | ||||||||||||||||||||||||||||||||||||||||||
| 2186 | |||||||||||||||||||||||||||||||||||||||||||
| 2187 | Specify that we want to load only the columns in LIST, which should be | ||||||||||||||||||||||||||||||||||||||||||
| 2188 | a list of column names. | ||||||||||||||||||||||||||||||||||||||||||
| 2189 | |||||||||||||||||||||||||||||||||||||||||||
| 2190 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 2191 | |||||||||||||||||||||||||||||||||||||||||||
| 2192 | sub columns { | ||||||||||||||||||||||||||||||||||||||||||
| 2193 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
columns: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 2194 | 1 | $self->column( column => $_ ) for @_; | |||||||||||||||||||||||||||||||||||||||||
| 2195 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2196 | |||||||||||||||||||||||||||||||||||||||||||
| 2197 | =head2 columns_in_db table | ||||||||||||||||||||||||||||||||||||||||||
| 2198 | |||||||||||||||||||||||||||||||||||||||||||
| 2199 | Return a list of columns in table, in lowercase. | ||||||||||||||||||||||||||||||||||||||||||
| 2200 | |||||||||||||||||||||||||||||||||||||||||||
| 2201 | TODO: Why are they in lowercase? | ||||||||||||||||||||||||||||||||||||||||||
| 2202 | |||||||||||||||||||||||||||||||||||||||||||
| 2203 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 2204 | |||||||||||||||||||||||||||||||||||||||||||
| 2205 | sub columns_in_db { | ||||||||||||||||||||||||||||||||||||||||||
| 2206 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
columns_in_db: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 2207 | 0 | my $table = shift; | |||||||||||||||||||||||||||||||||||||||||
| 2208 | |||||||||||||||||||||||||||||||||||||||||||
| 2209 | 0 | my $dbh = $self->_handle->dbh; | |||||||||||||||||||||||||||||||||||||||||
| 2210 | |||||||||||||||||||||||||||||||||||||||||||
| 2211 | # TODO: memoize this | ||||||||||||||||||||||||||||||||||||||||||
| 2212 | |||||||||||||||||||||||||||||||||||||||||||
| 2213 | 0 | return map lc( $_->[0] ), @{ ( | |||||||||||||||||||||||||||||||||||||||||
| 2214 | 0 | ▶ | eval { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
eval { or $dbh->selectall_arrayref("DESCRIBE \u$table;")
eval { or $dbh->selectall_arrayref("DESCRIBE $table;")
MC/DC0%0/3
eval {
do {
$dbh->column_info("", "", $table, "")->fetchall_arrayref([3])
}
} or $dbh->selectall_arrayref("DESCRIBE $table;") or $dbh->selectall_arrayref("DESCRIBE \u$table;")
eval {
do {
$dbh->column_info("", "", $table, "")->fetchall_arrayref([3])
}
}
$dbh->selectall_arrayref("DESCRIBE $table;")
$dbh->selectall_arrayref("DESCRIBE \u$table;")
Truth table0%0/4
A or B or C
A= eval {
do {
$dbh->column_info("", "", $table, "")->fetchall_arrayref([3])
}
}B= $dbh->selectall_arrayref("DESCRIBE $table;")C= $dbh->selectall_arrayref("DESCRIBE \u$table;")
| |||||||||||||||||||||||||||||||||||||||||||
| 2215 | 0 | $dbh->column_info( '', '', $table, '' )->fetchall_arrayref( [3] ); | |||||||||||||||||||||||||||||||||||||||||
| 2216 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2217 | || $dbh->selectall_arrayref("DESCRIBE $table;") | ||||||||||||||||||||||||||||||||||||||||||
| 2218 | || $dbh->selectall_arrayref("DESCRIBE \u$table;") | ||||||||||||||||||||||||||||||||||||||||||
| 2219 | || [] | ||||||||||||||||||||||||||||||||||||||||||
| 2220 | ) }; | ||||||||||||||||||||||||||||||||||||||||||
| 2221 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2222 | |||||||||||||||||||||||||||||||||||||||||||
| 2223 | =head2 has_column { table => undef, column => undef } | ||||||||||||||||||||||||||||||||||||||||||
| 2224 | |||||||||||||||||||||||||||||||||||||||||||
| 2225 | Returns true if table has column column. | ||||||||||||||||||||||||||||||||||||||||||
| 2226 | Return false otherwise | ||||||||||||||||||||||||||||||||||||||||||
| 2227 | |||||||||||||||||||||||||||||||||||||||||||
| 2228 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 2229 | |||||||||||||||||||||||||||||||||||||||||||
| 2230 | sub has_column { | ||||||||||||||||||||||||||||||||||||||||||
| 2231 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
has_column: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 2232 | 0 | my %args = ( | |||||||||||||||||||||||||||||||||||||||||
| 2233 | column => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 2234 | table => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 2235 | @_ | ||||||||||||||||||||||||||||||||||||||||||
| 2236 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 2237 | |||||||||||||||||||||||||||||||||||||||||||
| 2238 | 0 | ▶ | my $table = $args{table} or die; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2239 | 0 | ▶ | my $column = $args{column} or die; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2240 | 0 | return grep { $_ eq $column } $self->columns_in_db($table); | |||||||||||||||||||||||||||||||||||||||||
| 2241 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2242 | |||||||||||||||||||||||||||||||||||||||||||
| 2243 | =head2 table [table] | ||||||||||||||||||||||||||||||||||||||||||
| 2244 | |||||||||||||||||||||||||||||||||||||||||||
| 2245 | If called with an argument, sets this collection's table. | ||||||||||||||||||||||||||||||||||||||||||
| 2246 | |||||||||||||||||||||||||||||||||||||||||||
| 2247 | Always returns this collection's table. | ||||||||||||||||||||||||||||||||||||||||||
| 2248 | |||||||||||||||||||||||||||||||||||||||||||
| 2249 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 2250 | |||||||||||||||||||||||||||||||||||||||||||
| 2251 | sub table { | ||||||||||||||||||||||||||||||||||||||||||
| 2252 | 559 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
table: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 2253 | 559 | ▶ | $self->{table} = shift if (@_); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2254 | 559 | return $self->{table}; | |||||||||||||||||||||||||||||||||||||||||
| 2255 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2256 | |||||||||||||||||||||||||||||||||||||||||||
| 2257 | =head2 clone | ||||||||||||||||||||||||||||||||||||||||||
| 2258 | |||||||||||||||||||||||||||||||||||||||||||
| 2259 | Returns copy of the current object with all search restrictions. | ||||||||||||||||||||||||||||||||||||||||||
| 2260 | |||||||||||||||||||||||||||||||||||||||||||
| 2261 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 2262 | |||||||||||||||||||||||||||||||||||||||||||
| 2263 | sub clone { | ||||||||||||||||||||||||||||||||||||||||||
| 2264 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clone: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 2265 | |||||||||||||||||||||||||||||||||||||||||||
| 2266 | 2 | my $obj = bless {}, ref($self); | |||||||||||||||||||||||||||||||||||||||||
| 2267 | 2 | %$obj = %$self; | |||||||||||||||||||||||||||||||||||||||||
| 2268 | |||||||||||||||||||||||||||||||||||||||||||
| 2269 | 2 | $obj->redo_search(); # clean out the object of data | |||||||||||||||||||||||||||||||||||||||||
| 2270 | |||||||||||||||||||||||||||||||||||||||||||
| 2271 | $obj->{$_} = Clone::clone( $obj->{$_} ) | ||||||||||||||||||||||||||||||||||||||||||
| 2272 | 2 | for grep exists $self->{$_}, $self->_cloned_attributes; | |||||||||||||||||||||||||||||||||||||||||
| 2273 | 2 | return $obj; | |||||||||||||||||||||||||||||||||||||||||
| 2274 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2275 | |||||||||||||||||||||||||||||||||||||||||||
| 2276 | =head2 _cloned_attributes | ||||||||||||||||||||||||||||||||||||||||||
| 2277 | |||||||||||||||||||||||||||||||||||||||||||
| 2278 | Returns list of the object's fields that should be copied. | ||||||||||||||||||||||||||||||||||||||||||
| 2279 | |||||||||||||||||||||||||||||||||||||||||||
| 2280 | If your subclass store references in the object that should be copied while | ||||||||||||||||||||||||||||||||||||||||||
| 2281 | cloning then you probably want override this method and add own values to | ||||||||||||||||||||||||||||||||||||||||||
| 2282 | the list. | ||||||||||||||||||||||||||||||||||||||||||
| 2283 | |||||||||||||||||||||||||||||||||||||||||||
| 2284 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 2285 | |||||||||||||||||||||||||||||||||||||||||||
| 2286 | sub _cloned_attributes { | ||||||||||||||||||||||||||||||||||||||||||
| 2287 | 2 | ▶ | return qw( | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cloned_attributes: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 2288 | joins | ||||||||||||||||||||||||||||||||||||||||||
| 2289 | subclauses | ||||||||||||||||||||||||||||||||||||||||||
| 2290 | restrictions | ||||||||||||||||||||||||||||||||||||||||||
| 2291 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 2292 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2293 | |||||||||||||||||||||||||||||||||||||||||||
| 2294 | =head2 each CALLBACK | ||||||||||||||||||||||||||||||||||||||||||
| 2295 | |||||||||||||||||||||||||||||||||||||||||||
| 2296 | Executes the callback for each item in the collection. The callback receives as | ||||||||||||||||||||||||||||||||||||||||||
| 2297 | arguments each record, its zero-based index, and the collection. The return | ||||||||||||||||||||||||||||||||||||||||||
| 2298 | value of C<each> is the original collection. | ||||||||||||||||||||||||||||||||||||||||||
| 2299 | |||||||||||||||||||||||||||||||||||||||||||
| 2300 | If the callback returns zero, the iteration ends. | ||||||||||||||||||||||||||||||||||||||||||
| 2301 | |||||||||||||||||||||||||||||||||||||||||||
| 2302 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 2303 | |||||||||||||||||||||||||||||||||||||||||||
| 2304 | sub each { | ||||||||||||||||||||||||||||||||||||||||||
| 2305 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
each: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 2306 | 0 | my $cb = shift; | |||||||||||||||||||||||||||||||||||||||||
| 2307 | |||||||||||||||||||||||||||||||||||||||||||
| 2308 | 0 | my $idx = 0; | |||||||||||||||||||||||||||||||||||||||||
| 2309 | 0 | $self->goto_first_item; | |||||||||||||||||||||||||||||||||||||||||
| 2310 | |||||||||||||||||||||||||||||||||||||||||||
| 2311 | 0 | ▶ | while (my $record = $self->next) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 2312 | 0 | my $ret = $cb->($record, $idx++, $self); | |||||||||||||||||||||||||||||||||||||||||
| 2313 | 0 | ▶ | last if defined($ret) && !$ret; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $ret and not $ret
MC/DC0%0/2
defined $ret and not $ret
defined $ret
not $ret
Truth table0%0/3
A and not(B)
A= defined $retB= not $ret
| |||||||||||||||||||||||||||||||||||||||||||
| 2314 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2315 | |||||||||||||||||||||||||||||||||||||||||||
| 2316 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 2317 | } | ||||||||||||||||||||||||||||||||||||||||||
| 2318 | |||||||||||||||||||||||||||||||||||||||||||
| 2319 | 13 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 2320 | __END__ | ||||||||||||||||||||||||||||||||||||||||||
| 2321 | |||||||||||||||||||||||||||||||||||||||||||
| 2322 | |||||||||||||||||||||||||||||||||||||||||||
| 2323 | |||||||||||||||||||||||||||||||||||||||||||
| 2324 | =head1 TESTING | ||||||||||||||||||||||||||||||||||||||||||
| 2325 | |||||||||||||||||||||||||||||||||||||||||||
| 2326 | In order to test most of the features of C<Jifty::DBI::Collection>, | ||||||||||||||||||||||||||||||||||||||||||
| 2327 | you need to provide C<make test> with a test database. For each DBI | ||||||||||||||||||||||||||||||||||||||||||
| 2328 | driver that you would like to test, set the environment variables | ||||||||||||||||||||||||||||||||||||||||||
| 2329 | C<JDBI_TEST_FOO>, C<JDBI_TEST_FOO_USER>, and C<JDBI_TEST_FOO_PASS> to a | ||||||||||||||||||||||||||||||||||||||||||
| 2330 | database name, database username, and database password, where "FOO" | ||||||||||||||||||||||||||||||||||||||||||
| 2331 | is the driver name in all uppercase. You can test as many drivers as | ||||||||||||||||||||||||||||||||||||||||||
| 2332 | you like. (The appropriate C<DBD::> module needs to be installed in | ||||||||||||||||||||||||||||||||||||||||||
| 2333 | order for the test to work.) Note that the C<SQLite> driver will | ||||||||||||||||||||||||||||||||||||||||||
| 2334 | automatically be tested if C<DBD::Sqlite> is installed, using a | ||||||||||||||||||||||||||||||||||||||||||
| 2335 | temporary file as the database. For example: | ||||||||||||||||||||||||||||||||||||||||||
| 2336 | |||||||||||||||||||||||||||||||||||||||||||
| 2337 | JDBI_TEST_MYSQL=test JDBI_TEST_MYSQL_USER=root JDBI_TEST_MYSQL_PASS=foo \ | ||||||||||||||||||||||||||||||||||||||||||
| 2338 | JDBI_TEST_PG=test JDBI_TEST_PG_USER=postgres make test | ||||||||||||||||||||||||||||||||||||||||||
| 2339 | |||||||||||||||||||||||||||||||||||||||||||
| 2340 | |||||||||||||||||||||||||||||||||||||||||||
| 2341 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 2342 | |||||||||||||||||||||||||||||||||||||||||||
| 2343 | Jesse Vincent <jesse@bestpractical.com>, Alex Vandiver | ||||||||||||||||||||||||||||||||||||||||||
| 2344 | <alexmv@bestpractical.com>, Ruslan Zakirov <ruslan.zakirov@gmail.com> | ||||||||||||||||||||||||||||||||||||||||||
| 2345 | |||||||||||||||||||||||||||||||||||||||||||
| 2346 | Based on DBIx::SearchBuilder::Collection, whose credits read: | ||||||||||||||||||||||||||||||||||||||||||
| 2347 | |||||||||||||||||||||||||||||||||||||||||||
| 2348 | Jesse Vincent, <jesse@fsck.com> | ||||||||||||||||||||||||||||||||||||||||||
| 2349 | |||||||||||||||||||||||||||||||||||||||||||
| 2350 | All rights reserved. | ||||||||||||||||||||||||||||||||||||||||||
| 2351 | |||||||||||||||||||||||||||||||||||||||||||
| 2352 | This library is free software; you can redistribute it | ||||||||||||||||||||||||||||||||||||||||||
| 2353 | and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 2354 | |||||||||||||||||||||||||||||||||||||||||||
| 2355 | |||||||||||||||||||||||||||||||||||||||||||
| 2356 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 2357 | |||||||||||||||||||||||||||||||||||||||||||
| 2358 | L<Jifty::DBI>, L<Jifty::DBI::Handle>, L<Jifty::DBI::Record>. | ||||||||||||||||||||||||||||||||||||||||||
| 2359 | |||||||||||||||||||||||||||||||||||||||||||
| 2360 | =cut | ||||||||||||||||||||||||||||||||||||||||||