DBI/Collection.pm

filter: Statementstmt 82.8% 560 / 676 Branchbran 68.0% 234 / 344 Conditioncond 54.9% 83 / 151 MC/DCmcdc 28.5% 26 / 91 Subroutinesub 82.5% 71 / 86 Podpod 100% 51 / 51 totaltotal 73.2% 1025 / 1399 CC 248 SCAR 30.3
CC
248
Cov
75%
limit
75.0
column
31.2
count_all
30.0
CRAP 20.6
« DBI/Handle.pm ↑ Summary DBI/Handle/Pg.pm »
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
l!l&&r!l&&!r
22280
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
96 230 my $self = {};
97 230 bless( $self, $class );
98 230 ▶ $self->record_class( $proto->record_class ) if ref $proto;
Branch: 1 branch
BranchTrueFalseTotal
if ref $proto 2 228 230
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
BranchTrueFalseTotal
if $args{"handle"} 230 0 230
119 230 ▶ $self->derived( $args{'derived'} ) if ( $args{'derived'} );
Branch: 1 branch
BranchTrueFalseTotal
if $args{"derived"} 4 226 230
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
BranchTrueFalseTotal
unless $records 0 48 48
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
BranchTrueFalseTotal
$self->prefetch_related or {} 45 3 48
206  
207 48 ▶ unless ( @tables ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@tables) 45 3 48
208 45 ▶ while ( my $row = $records->fetchrow_hashref() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $records->fetchrow_hashref) 130 45 175
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
BranchTrueFalseTotal
if ($records->err) 0 45 45
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
BranchTrueFalseTotal
if (my $base_row = $records->fetchrow_hashref) 18 3 21
225 18 my $main_pkey = $base_row->{ $names[0] };
226 18 ▶ $main_pkey = 'unique-'.$i++ if $self->{group_by};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"group_by"} 0 18 18
227 18 ▶ push @order, $main_pkey
Branch: 1 branch
BranchTrueFalseTotal
unless $order[0] and $order[-1] eq $main_pkey 10 8 18
Condition100%3/3
$order[0] and $order[-1] eq $main_pkey
!ll&&!rl&&r
378
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if keys %tmp 36 0 36
Condition33%1/3
$$base_row{$table . '_id'} || $main_pkey
l!l&&r!l&&!r
3600
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
ABresult
1X1
011
000
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
BranchTrueFalseTotal
unless $class 0 10 10
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
BranchTrueFalseTotal
if ($class->isa('Jifty::DBI::Collection')) { } 4 6 10
elsif ($class->isa('Jifty::DBI::Record')) { } 6 0 6
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
BranchTrueFalseTotal
if @rows > 1 0 6 6
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
BranchTrueFalseTotal
if @rows 6 0 6
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
BranchTrueFalseTotal
if ($records->err) 0 3 3
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
BranchTrueFalseTotal
unless defined $self->{"items"} 20 220 240
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
l!l
078
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
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
BranchTrueFalseTotal
unless $records 0 78 78
345  
346 78 my @row = $records->fetchrow_array();
347 78 ▶ return 0 if $records->err;
Branch: 1 branch
BranchTrueFalseTotal
if $records->err 0 78 78
348  
349 78 ▶ $self->{ $all ? 'count_all' : 'raw_rows' } = $row[0];
Branch: 1 branch
BranchTrueFalseTotal
$all ? : 0 78 78
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
BranchTrueFalseTotal
if ($self->{'joins'} and keys %{$$self{'joins'};}) { } 19 314 333
Condition100%3/3
$self->{"joins"} and keys %{$$self{"joins"};}
!ll&&!rl&&r
830619
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"};}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($self->{"joins"}) 18 0 18
429 18 for ( values %{ $self->{'joins'} } ) {
430 18 ▶ return 0 unless $_->{is_distinct};
Branch: 1 branch
BranchTrueFalseTotal
unless $_->{"is_distinct"} 14 4 18
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
BranchTrueFalseTotal
if $self->derived 0 246 246
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
BranchTrueFalseTotal
if ($self->_is_limited) 246 0 246
465 246 $query_string .= $self->_where_clause . " ";
466 }
467 246 ▶ if ( $self->distinct_required ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->distinct_required) { } 5 241 246
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
BranchTrueFalseTotal
if ($self->{'columns'} and @{$self->{'columns'};}) { } 3 243 246
Condition66%2/3
$self->{"columns"} and @{$self->{"columns"};}
!ll&&!rl&&r
24303
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"};}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
$self->prefetch_related or {} 243 3 246
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
BranchTrueFalseTotal
if ($class->isa('Jifty::DBI::Collection')) { } 2 1 3
elsif ($class->isa('Jifty::DBI::Record')) { } 1 0 1
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
BranchTrueFalseTotal
$self->prefetch_related or {} 0 10 10
530 10 my $related = $prefetch{$alias};
531 10 ▶ return unless $related;
Branch: 1 branch
BranchTrueFalseTotal
unless $related 0 10 10
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
l!l&&r!l&&!r
180998
MC/DC50%1/2
$_->virtual || $_->computed
$_->virtual $_->computed
Truth table66%2/3
A || B
A= $_->virtualB= $_->computed
ABresult
1X1
011
000
542 246 ▶ if ($only_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if ($only_cols) 0 246 246
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
BranchTrueFalseTotal
if (@_ and $self->{"joins"}{$_[0]}) 1 2 3
Condition66%2/3
@_ and $self->{"joins"}{$_[0]}
!ll&&!rl&&r
021
MC/DC50%1/2
@_ and $self->{"joins"}{$_[0]}
@_ $self->{"joins"}{$_[0]}
Truth table66%2/3
A and B
A= @_B= $self->{"joins"}{$_[0]}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless $args{"name"} 0 3 3
619  
620 # Reference defaults to name
621 3 ▶ $args{reference} ||= $args{name};
Condition33%1/3
$args{"reference"} ||= $args{"name"}
l!l&&r!l&&!r
030
MC/DC0%0/2
$args{"reference"} ||= $args{"name"}
$args{"reference"} $args{"name"}
Truth table33%1/3
A ||= B
A= $args{"reference"}B= $args{"name"}
ABresult
1X1
011
000
622  
623 # If we don't have an alias, do the join
624 3 ▶ if ( not $args{alias} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"alias"}) 2 1 3
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
BranchTrueFalseTotal
unless ($args{"class"}) 1 2 3
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
BranchTrueFalseTotal
if $column 1 0 1
636  
637 1 ▶ die "Don't know class" unless $args{class};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"class"} 0 1 1
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
BranchTrueFalseTotal
unless (UNIVERSAL::isa($args{"class"}, "Jifty::DBI::Record") or UNIVERSAL::isa($args{"class"}, "Jifty::DBI::Collection")) 0 3 3
Condition66%2/3
UNIVERSAL::isa($args{"class"}, "Jifty::DBI::Record") or UNIVERSAL::isa($args{"class"}, "Jifty::DBI::Collection")
l!l&&r!l&&!r
120
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")
ABresult
1X1
011
000
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
BranchTrueFalseTotal
unless $self->prefetch_related 3 0 3
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
BranchTrueFalseTotal
if UNIVERSAL::isa($class, "Jifty::DBI::Collection") 0 0 0
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
BranchTrueFalseTotal
unless $column 0 0 0
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
BranchTrueFalseTotal
if (my $name = shift @names) 2 2 4
697 2 my $column = $itemclass->column($name);
698 2 ▶ die "$itemclass has no column '$name'" unless $column;
Branch: 1 branch
BranchTrueFalseTotal
unless $column 0 2 2
699  
700 2 push @res, $column;
701  
702 2 my $classname = $column->refers_to;
703 2 ▶ unless ($classname) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($classname) 0 2 2
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
BranchTrueFalseTotal
if (UNIVERSAL::isa($classname, 'Jifty::DBI::Collection')) { } 1 1 2
elsif (UNIVERSAL::isa($classname, 'Jifty::DBI::Record')) { } 1 0 1
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
BranchTrueFalseTotal
unless ($classname) 0 2 2
741 0 die "column '$name' of is not a reference";
742 }
743  
744 2 ▶ if ( UNIVERSAL::isa( $classname, 'Jifty::DBI::Collection' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($classname, 'Jifty::DBI::Collection')) { } 1 1 2
elsif (UNIVERSAL::isa($classname, 'Jifty::DBI::Record')) { } 1 0 1
745 1 my $right_alias = $self->new_alias($classname->record_class);
746 1 ▶ $self->join(
Condition50%1/2
$column->by || 'id'
l!l
10
MC/DC0%0/1
$column->by || 'id'
$column->by
Truth table50%1/2
A
A= $column->by
Aresult
11
00
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'
l!l
10
MC/DC0%0/1
$column->by || 'id'
$column->by
Truth table50%1/2
A
A= $column->by
Aresult
11
00
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
BranchTrueFalseTotal
$self->_is_joined ? : 18 306 324
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
BranchTrueFalseTotal
if $self->derived 0 78 78
805  
806 78 my $query_string = $self->_build_joins . " ";
807  
808 78 ▶ if ( $self->_is_limited ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_limited) 78 0 78
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
BranchTrueFalseTotal
if ($self->distinct_required or $self->prefetch_related) { } 11 67 78
Condition100%3/3
$self->distinct_required or $self->prefetch_related
l!l&&r!l&&!r
9267
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
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if $self->derived 0 0 0
836 0 ▶ $self->_do_search() if $self->{'must_redo_search'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"must_redo_search"} 0 0 0
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
BranchTrueFalseTotal
if ($self->{'itemscount'} < $self->_record_count) { } 65 18 83
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
BranchTrueFalseTotal
unless $self->_is_limited 8 83 91
881  
882 83 ▶ $self->_do_search() if $self->{'must_redo_search'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"must_redo_search"} 46 37 83
883  
884 83 ▶ if ( $self->{'itemscount'} < $self->_record_count )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'itemscount'} < $self->_record_count) { } 67 16 83
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
BranchTrueFalseTotal
@_ % 2 ? : 4 0 4
962 column => undef,
963 sort => undef,
964 max => undef,
965 @_%2 ? (column => @_) : (@_)
966 );
967  
968 4 ▶ return () if $self->derived;
Branch: 1 branch
BranchTrueFalseTotal
if $self->derived 0 4 4
969  
970 4 my $query_string = $self->_build_joins;
971 4 ▶ if ( $self->_is_limited ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_limited) 0 4 4
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
BranchTrueFalseTotal
if ($args{"sort"}) 3 1 4
979 $query_string .= ' ORDER BY '. $column
980 3 ▶ .' '. ($args{'sort'} =~ /^des/i ? 'DESC' : 'ASC');
Branch: 1 branch
BranchTrueFalseTotal
$args{'sort'} =~ /^des/i ? : 2 1 3
981 }
982  
983 4 ▶ my $sth = $self->_handle->simple_query( $query_string ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $self->_handle->simple_query($query_string) or return 0 4 4
984 4 my $value;
985 4 ▶ $sth->bind_col(1, \$value) or return;
Branch: 1 branch
BranchTrueFalseTotal
$sth->bind_col(1, \$value) or return 0 4 4
986 4 my @col;
987 4 ▶ if ($args{max}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'max'}) { } 1 3 4
988 1 ▶ push @col, $value while 0 < $args{max}-- && $sth->fetch;
Branch: 1 branch
BranchTrueFalseTotal
0 < $args{"max"}-- and $sth->fetch and do { push @col, $value } 2 1 3
Condition66%2/3
0 < $args{"max"}-- and $sth->fetch
!ll&&!rl&&r
102
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
ABresult
0X0
100
111
989 } else {
990 3 ▶ push @col, $value while $sth->fetch;
Branch: 1 branch
BranchTrueFalseTotal
$sth->fetch and do { push @col, $value } 9 3 12
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
BranchTrueFalseTotal
unless $self->_is_limited 2 6 8
1014  
1015 # Do a search if we need to.
1016 6 ▶ $self->_do_search() if $self->{'must_redo_search'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"must_redo_search"} 2 4 6
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'} || []
l!l
60
MC/DC0%0/1
$self->{'items'} || []
$self->{'items'}
Truth table50%1/2
A
A= $self->{'items'}
Aresult
11
00
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
BranchTrueFalseTotal
unless $class 0 185 185
1041 unless $class;
1042  
1043 185 ▶ unless ( exists $cache{$class} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $cache{$class}) 9 176 185
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
BranchTrueFalseTotal
if (@_) { } 2 946 948
elsif (not ref $self && $self->{'record_class'}) { } 231 715 946
1070 2 ▶ $self->{record_class} = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 0 2
1071 $self->{record_class} = ref $self->{record_class}
1072 2 ▶ if ref $self->{record_class};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"record_class"} 0 2 2
1073 ▶ } elsif ( not ref $self or not $self->{record_class} ) {
Condition100%3/3
ref $self && $self->{'record_class'}
!ll&&!rl&&r
3228715
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'}
ABresult
0X0
100
111
1074 231 ▶ my $class = ref($self) || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
22830
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
1075 231 ▶ $class =~ s/(?<!:)(Collection|s)$//
Branch: 1 branch
BranchTrueFalseTotal
$class =~ s/(?<!:)(Collection|s)$// or die "Can't guess record class from $class" 0 231 231
1076 || die "Can't guess record class from $class";
1077 231 ▶ return $class unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 3 228 231
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
BranchTrueFalseTotal
if $self->derived 0 279 279
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
BranchTrueFalseTotal
if (defined $args{"leftjoin"} and not defined $args{"alias"}) 0 279 279
Condition33%1/3
defined $args{"leftjoin"} and not defined $args{"alias"}
!ll&&!rl&&r
27900
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"}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless (defined $args{"alias"}) 271 8 279
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
BranchTrueFalseTotal
if (not defined $args{'table'} or $args{'table'} eq $self->table) { } 271 0 271
Condition33%1/3
not defined $args{"table"} or $args{"table"} eq $self->table
l!l&&r!l&&!r
27100
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
ABresult
1X1
011
000
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
BranchTrueFalseTotal
$self->{'joins'}{$args{'alias'}} && $self->{'joins'}{$args{'alias'}}{'class'} ? : 0 279 279
Condition66%2/3
$self->{"joins"}{$args{"alias"}} and $self->{"joins"}{$args{"alias"}}{"class"}
!ll&&!rl&&r
27180
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"}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if $column_obj and $column_obj->encode_on_select and not $args{"operator"} =~ /IS/ 12 267 279
Condition83%5/6
$column_obj and $column_obj->encode_on_select and not $args{"operator"} =~ /IS/
!ll&&!rl&&r
267012
$column_obj and $column_obj->encode_on_select
!ll&&!rl&&r
726012
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/
ABCresult
0XX0
10X0
1100
1111
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
BranchTrueFalseTotal
if ($args{"column"} =~ /\W/) 0 279 279
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
BranchTrueFalseTotal
unless ($args{"operator"} =~ /^(=|<|>|!=|<>|<=|>= |(NOT\s*)?LIKE |(NOT\s*)?(STARTS|ENDS)_?WITH |(NOT\s*)?MATCHES |IS(\s*NOT)? |IN)$/xi) 0 279 279
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
BranchTrueFalseTotal
$args{'alias'} ? : 279 0 279
1314 279 ▶ my $clause_id = $args{'subclause'} || $qualified_column;
Condition66%2/3
$args{'subclause'} || $qualified_column
l!l&&r!l&&!r
22770
MC/DC0%0/2
$args{'subclause'} || $qualified_column
$args{'subclause'} $qualified_column
Truth table66%2/3
A || B
A= $args{'subclause'}B= $qualified_column
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if ($value_ref) 31 248 279
1320 31 ▶ if ( ( $value_ref ne 'ARRAY' )
Branch: 2 branches
BranchTrueFalseTotal
if ($value_ref ne 'ARRAY' and $args{'value'}->isa('Jifty::DBI::Record')) { } 2 29 31
elsif ($value_ref eq 'ARRAY') { } 29 0 29
Condition66%2/3
$value_ref ne "ARRAY" and $args{"value"}->isa("Jifty::DBI::Record")
!ll&&!rl&&r
2902
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")
ABresult
0X0
100
111
1321 && $args{value}->isa('Jifty::DBI::Record') )
1322 {
1323 2 ▶ my $by = (defined $column_obj and defined $column_obj->by)
Branch: 1 branch
BranchTrueFalseTotal
defined $column_obj && defined $column_obj->by ? : 2 0 2
Condition33%1/3
defined $column_obj and defined $column_obj->by
!ll&&!rl&&r
002
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
defined $column_obj && defined $column_obj->by ? : 2 60 62
Condition66%2/3
defined $column_obj and defined $column_obj->by
!ll&&!rl&&r
0602
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
ABresult
0X0
100
111
1333 ? $column_obj->by
1334 : 'id';
1335 62 ▶ $_ = (
Branch: 1 branch
BranchTrueFalseTotal
ref $_ && $_->isa('Jifty::DBI::Record') ? : 1 61 62
Condition66%2/3
ref $_ and $_->isa("Jifty::DBI::Record")
!ll&&!rl&&r
6101
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")
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($args{'operator'} =~ /MATCHES/i) { } 25 254 279
elsif ($args{'operator'} =~ /STARTS_?WITH/i) { } 17 237 254
elsif ($args{'operator'} =~ /ENDS_?WITH/i) { } 17 220 237
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
BranchTrueFalseTotal
if ($args{"operator"} =~ /^IS(\s*NOT)?$/i) 54 225 279
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
BranchTrueFalseTotal
if ($args{"quote_value"}) 223 56 279
1365 223 ▶ if ( $value_ref eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value_ref eq 'ARRAY') { } 29 194 223
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
BranchTrueFalseTotal
if ($args{"escape"}) 4 275 279
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
BranchTrueFalseTotal
if ($args{'leftjoin'}) { } 0 279 279
1381 $restriction
1382 0 ▶ = $self->{'joins'}{ $args{'leftjoin'} }{'criteria'}{$clause_id}
Condition0%0/2
$self->{"joins"}{$args{"leftjoin"}}{"criteria"}{$clause_id} ||= []
l!l
00
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}
Aresult
11
00
1383 ||= [];
1384 } else {
1385 279 ▶ $restriction = $self->{'restrictions'}{$clause_id} ||= [];
Condition100%2/2
$self->{"restrictions"}{$clause_id} ||= []
l!l
5274
MC/DC100%1/1
$self->{"restrictions"}{$clause_id} ||= []
$self->{"restrictions"}{$clause_id}
Truth table100%2/2
A
A= $self->{"restrictions"}{$clause_id}
Aresult
11
00
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
BranchTrueFalseTotal
if (defined $args{"value"} and $args{"quote_value"}) 223 56 279
Condition66%2/3
defined $args{"value"} and $args{"quote_value"}
!ll&&!rl&&r
056223
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"}
ABresult
0X0
100
111
1391 223 my $case_sensitive = 0;
1392 223 ▶ if ( defined $args{'case_sensitive'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args{'case_sensitive'}) { } 114 109 223
elsif ($column_obj) { } 103 6 109
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
BranchTrueFalseTotal
if ($column_obj and not $case_sensitive and not $column_obj->is_string) 68 155 223
Condition100%6/6
$column_obj and not $case_sensitive and not $column_obj->is_string
!ll&&!rl&&r
946168
$column_obj and not $case_sensitive
!ll&&!rl&&r
688129
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
ABCresult
0XX0
10X0
1100
1111
1401 68 $case_sensitive = 1;
1402 }
1403  
1404 223 ▶ if ( !$case_sensitive && $self->_handle->case_sensitive ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $case_sensitive and $self->_handle->case_sensitive) 67 156 223
Condition66%2/3
not $case_sensitive and $self->_handle->case_sensitive
!ll&&!rl&&r
156067
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($value_ref eq "ARRAY") 29 250 279
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
BranchTrueFalseTotal
unless $args{"operator"} =~ /^(IN|=)$/i 0 29 29
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
BranchTrueFalseTotal
if ($self->{"_open_parens"}{$clause_id}) 1 278 279
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
BranchTrueFalseTotal
if (lc($args{'entry_aggregator'} || '') eq 'none' or not @$restriction) { } 274 5 279
Condition60%3/5
lc($args{"entry_aggregator"} || "") eq "none" or not @$restriction
l!l&&r!l&&!r
02745
$args{'entry_aggregator'} || ''
l!l
2790
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
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if (defined $args{'alias'}) { } 279 0 279
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} ||= []
l!l
10
MC/DC0%0/1
$self->{"restrictions"}{$clause} ||= []
$self->{"restrictions"}{$clause}
Truth table50%1/2
A
A= $self->{"restrictions"}{$clause}
Aresult
11
00
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 $_
!ll&&!rl&&r
016308
MC/DC50%1/2
defined $_ && length $_
defined $_ length $_
Truth table66%2/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
1534  
1535 324 ▶ $where_clause = " WHERE " . CORE::join( ' AND ', @subclauses )
Branch: 1 branch
BranchTrueFalseTotal
if @subclauses 308 16 324
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
$_ && @$_
!ll&&!rl&&r
00312
MC/DC0%0/2
$_ && @$_
$_ @$_
Truth table33%1/3
A && B
A= $_B= @$_
ABresult
0X0
100
111
1552 324 values %{ $self->{'restrictions'} } )
1553 {
1554 312 ▶ $result .= ' AND ' if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 4 308 312
1555 312 $result .= '(';
1556 312 foreach my $entry (@$restriction) {
1557 328 ▶ unless ( ref $entry ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $entry) { } 318 10 328
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
BranchTrueFalseTotal
if (($self->{$type} || "") ne ($value || "")) 0 0 0
Condition0%0/4
$self->{$type} || ''
l!l
00
$value || ''
l!l
00
MC/DC0%0/1
$self->{$type} || ''
$self->{$type}
MC/DC0%0/1
$value || ''
$value
Truth table0%0/2
A
A= $self->{$type}
Aresult
11
00
Truth table0%0/2
A
A= $value
Aresult
11
00
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
BranchTrueFalseTotal
if $self->derived 0 7 7
1633 7 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 1 7
1634 6 $self->{'order_by'} = [];
1635 6 $self->add_order_by(@_);
1636 }
1637 7 ▶ return ( $self->{'order_by'} || [] );
Condition50%1/2
$self->{'order_by'} || []
l!l
70
MC/DC0%0/1
$self->{'order_by'} || []
$self->{'order_by'}
Truth table50%1/2
A
A= $self->{'order_by'}
Aresult
11
00
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
BranchTrueFalseTotal
if $self->derived 0 8 8
1649 8 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 8 0 8
1650 8 my @args = @_;
1651  
1652 8 ▶ unless ( UNIVERSAL::isa( $args[0], 'HASH' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($args[0], "HASH")) 7 1 8
1653 7 @args = {@args};
1654 }
1655 8 ▶ push @{ $self->{'order_by'} ||= [] }, @args;
Condition50%1/2
$self->{"order_by"} ||= []
l!l
80
MC/DC0%0/1
$self->{"order_by"} ||= []
$self->{"order_by"}
Truth table50%1/2
A
A= $self->{"order_by"}
Aresult
11
00
1656 8 $self->redo_search();
1657 }
1658 8 ▶ return ( $self->{'order_by'} || [] );
Condition50%1/2
$self->{'order_by'} || []
l!l
80
MC/DC0%0/1
$self->{'order_by'} || []
$self->{'order_by'}
Truth table50%1/2
A
A= $self->{'order_by'}
Aresult
11
00
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
BranchTrueFalseTotal
unless $self->{"order_by"} 243 13 256
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
BranchTrueFalseTotal
if ($rowhash{'order'} =~ /^des/i) { } 12 17 29
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
BranchTrueFalseTotal
if ($rowhash{'function'} and not defined $rowhash{'column'}) { } 6 23 29
elsif (defined $rowhash{'alias'} and $rowhash{'column'}) { } 23 0 23
Condition100%3/3
$rowhash{"function"} and not defined $rowhash{"column"}
!ll&&!rl&&r
2216
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"}
ABresult
0X0
100
111
1700 6 ▶ $clause .= ( $clause ? ", " : " " );
Branch: 1 branch
BranchTrueFalseTotal
$clause ? : 5 1 6
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"}
!ll&&!rl&&r
0023
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"}
ABresult
0X0
100
111
1705 and ( $rowhash{'column'} ) )
1706 {
1707 23 ▶ if ($rowhash{'column'} =~ /\W/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rowhash{"column"} =~ /\W/) 0 23 23
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
BranchTrueFalseTotal
$clause ? : 12 11 23
1713 23 ▶ $clause .= $rowhash{'function'} . "(" if $rowhash{'function'};
Branch: 1 branch
BranchTrueFalseTotal
if $rowhash{"function"} 1 22 23
1714 23 ▶ $clause .= $rowhash{'alias'} . "." if $rowhash{'alias'};
Branch: 1 branch
BranchTrueFalseTotal
if $rowhash{"alias"} 18 5 23
1715 23 $clause .= $rowhash{'column'};
1716 23 ▶ $clause .= ")" if $rowhash{'function'};
Branch: 1 branch
BranchTrueFalseTotal
if $rowhash{"function"} 1 22 23
1717 23 $clause .= " " . $rowhash{'order'};
1718 }
1719 }
1720 13 ▶ $clause = " ORDER BY$clause " if $clause;
Branch: 1 branch
BranchTrueFalseTotal
if $clause 12 1 13
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
BranchTrueFalseTotal
if $self->derived 0 2 2
1759 2 my @args = @_;
1760  
1761 2 ▶ unless ( UNIVERSAL::isa( $args[0], 'HASH' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($args[0], "HASH")) 2 0 2
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
BranchTrueFalseTotal
unless $self->{"group_by"} 244 3 247
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
BranchTrueFalseTotal
if ($rowhash{'function'}) { } 0 3 3
elsif ($rowhash{'alias'} and $rowhash{'column'}) { } 3 0 3
1789 0 ▶ $clause .= ( $clause ? ", " : " " );
Branch: 1 branch
BranchTrueFalseTotal
$clause ? : 0 0 0
1790 0 $clause .= $rowhash{'function'};
1791  
1792 ▶ } elsif ( ( $rowhash{'alias'} )
Condition33%1/3
$rowhash{"alias"} and $rowhash{"column"}
!ll&&!rl&&r
003
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"}
ABresult
0X0
100
111
1793 and ( $rowhash{'column'} ) )
1794 {
1795 3 ▶ if ($rowhash{'column'} =~ /\W/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rowhash{"column"} =~ /\W/) 0 3 3
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
BranchTrueFalseTotal
$clause ? : 0 3 3
1801 3 $clause .= $rowhash{'alias'} . ".";
1802 3 $clause .= $rowhash{'column'};
1803 }
1804 }
1805 3 ▶ if ($clause) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clause) { } 3 0 3
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')
l!l
50
MC/DC0%0/1
shift() || die('Missing parameter')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1824 5 my $table;
1825 5 my $class = undef;
1826 5 ▶ if ( $refers_to->can('table') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($refers_to->can('table')) { } 2 3 5
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
BranchTrueFalseTotal
$class ? : 2 3 5
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
BranchTrueFalseTotal
if $self->derived 0 12 12
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
BranchTrueFalseTotal
if $self->derived 0 0 0
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
BranchTrueFalseTotal
if ($args{"current_page"} eq "all") 0 0 0
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
BranchTrueFalseTotal
if (@_) 0 246 246
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
BranchTrueFalseTotal
unless $self->_is_limited 13 106 119
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
BranchTrueFalseTotal
if ($self->{'must_redo_search'}) { } 82 24 106
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
BranchTrueFalseTotal
unless $self->{"raw_rows"} 78 4 82
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
BranchTrueFalseTotal
unless $self->_is_limited 0 0 0
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
BranchTrueFalseTotal
if ($self->{'must_redo_search'} or not $self->{'count_all'}) { } 0 0 0
Condition0%0/3
$self->{"must_redo_search"} or not $self->{"count_all"}
l!l&&r!l&&!r
000
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"}
ABresult
1X1
011
000
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
BranchTrueFalseTotal
unless $self->{"count_all"} 0 0 0
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
BranchTrueFalseTotal
unless $self->count 8 10 18
2082  
2083 10 ▶ if ( $self->_items_counter == $self->count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_items_counter == $self->count) { } 6 4 10
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
BranchTrueFalseTotal
if (@_) 0 0 0
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 {
l!l&&r!l&&!r
030
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; } }
ABresult
1X1
011
000
2150 3 ▶ if ( my $alias = $args{alias} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $alias = $args{'alias'}) { } 0 3 3
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'
l!l
03
MC/DC0%0/1
$args{'alias'} || 'main'
$args{'alias'}
Truth table50%1/2
A
A= $args{'alias'}
Aresult
11
00
2159 3 ▶ if ( my $func = $args{function} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $func = $args{"function"}) 0 3 3
2160 0 ▶ if ( $func =~ /^DISTINCT\s*COUNT$/i ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($func =~ /^DISTINCT\s*COUNT$/i) { } 0 0 0
elsif ($func =~ /\?/) { } 0 0 0
elsif (not $func =~ /\(/) { } 0 0 0
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"} ||= []
l!l
03
MC/DC0%0/1
$self->{"columns"} ||= []
$self->{"columns"}
Truth table50%1/2
A
A= $self->{"columns"}
Aresult
11
00
2179 3 ▶ $column = $args{column} if $table eq $self->table and !$args{alias};
Branch: 1 branch
BranchTrueFalseTotal
if $table eq $self->table and not $args{"alias"} 3 0 3
Condition33%1/3
$table eq $self->table and not $args{"alias"}
!ll&&!rl&&r
003
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"}
ABresult
0X0
100
111
2180 3 ▶ $column = ( $args{'alias'} || 'main' ) . "_" . $column;
Condition50%1/2
$args{'alias'} || 'main'
l!l
03
MC/DC0%0/1
$args{'alias'} || 'main'
$args{'alias'}
Truth table50%1/2
A
A= $args{'alias'}
Aresult
11
00
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
BranchTrueFalseTotal
eval { do { $dbh->column_info("", "", $table, "")->fetchall_arrayref([3]) } } or $dbh->selectall_arrayref("DESCRIBE $table;") or $dbh->selectall_arrayref("DESCRIBE \u$table;") or [] 0 0 0
Condition0%0/6
eval { or $dbh->selectall_arrayref("DESCRIBE \u$table;")
l!l&&r!l&&!r
000
eval { or $dbh->selectall_arrayref("DESCRIBE $table;")
l!l&&r!l&&!r
000
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;")
ABCresult
1XX1
01X1
0011
0000
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
BranchTrueFalseTotal
my $table = $args{"table"} or die 0 0 0
2239 0 ▶ my $column = $args{column} or die;
Branch: 1 branch
BranchTrueFalseTotal
my $column = $args{"column"} or die 0 0 0
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
BranchTrueFalseTotal
if @_ 240 319 559
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
BranchTrueFalseTotal
if (my $record = $self->next) 0 0 0
2312 0 my $ret = $cb->($record, $idx++, $self);
2313 0 ▶ last if defined($ret) && !$ret;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ret and not $ret 0 0 0
Condition0%0/3
defined $ret and not $ret
!ll&&!rl&&r
000
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
ABresult
0X0
100
111
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
« DBI/Handle.pm ↑ Summary DBI/Handle/Pg.pm »