| 1 | # The contents of this file are Copyright (c) 2010 Daniel Norman | ||||||||||||||||||||||||||||||||||
| 2 | # This program is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||||||||
| 3 | # modify it under the terms of the GNU General Public License as | ||||||||||||||||||||||||||||||||||
| 4 | # published by the Free Software Foundation. | ||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||
| 6 | ########################################### | ||||||||||||||||||||||||||||||||||
| 7 | package DBR::Query::Select; | ||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||
| 9 | 18 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | 18 | ▶ | use base 'DBR::Query'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 11 | 18 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 12 | 18 | ▶ | use DBR::Record::Maker; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||
| 14 | 590 | ▶ | sub _params { qw (fields tables where builder limit lock quiet_error) } | ||||||||||||||||||||||||||||||||
|
Subroutine
_params: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 15 | 574 | ▶ | sub _reqparams { qw (fields tables) } | ||||||||||||||||||||||||||||||||
|
Subroutine
_reqparams: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 16 | 574 | ▶ | sub _validate_self{ 1 } # If I exist, I'm valid | ||||||||||||||||||||||||||||||||
|
Subroutine
_validate_self: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||
| 18 | 603 | ▶ | sub fields{ | ||||||||||||||||||||||||||||||||
|
Subroutine
fields: called CC=8 SCAR=21.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 19 | 603 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 20 | 603 | ▶ | exists( $_[0] ) or return wantarray?( @{$self->{fields}||[]} ) : $self->{fields} || undef; | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%2/4
$self->{'fields'} || []
$self->{"fields"} or undef
MC/DC0%0/1
$self->{'fields'} || []
$self->{'fields'}
MC/DC0%0/1
$self->{"fields"} or undef
$self->{"fields"}
Truth table50%1/2
A
A= $self->{'fields'}
Truth table50%1/2
A
A= $self->{"fields"}
| |||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||
| 22 | 574 | my @fields = $self->_arrayify(@_); | |||||||||||||||||||||||||||||||||
| 23 | 574 | ▶ | scalar(@fields) || croak('must provide at least one field'); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||
| 25 | 574 | my $lastidx = -1; | |||||||||||||||||||||||||||||||||
| 26 | 574 | for (@fields){ | |||||||||||||||||||||||||||||||||
| 27 | 2455 | ▶ | ref($_) =~ /^DBR::Config::Field/ || croak('must specify field as a DBR::Config::Field object'); # Could also be ::Anon | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 28 | 2455 | $_->index( ++$lastidx ); | |||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||
| 30 | 574 | $self->{last_idx} = $lastidx; | |||||||||||||||||||||||||||||||||
| 31 | 574 | $self->{fields} = \@fields; | |||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||
| 33 | 574 | return 1; | |||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||
| 37 | 564 | ▶ | sub sql{ | ||||||||||||||||||||||||||||||||
|
Subroutine
sql: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 38 | 564 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 39 | 564 | ▶ | my $conn = $self->instance->connect('conn') or return $self->_error('failed to connect'); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 40 | 564 | my $sql; | |||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||
| 42 | 564 | my $tables = join(',', map { $_->sql( $conn ) } @{$self->{tables}} ); | |||||||||||||||||||||||||||||||||
| 43 | 564 | my $fields = join(',', map { $_->sql( $conn ) } @{$self->{fields}} ); | |||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||
| 45 | 564 | $sql = "SELECT $fields FROM $tables"; | |||||||||||||||||||||||||||||||||
| 46 | 564 | ▶ | $sql .= ' WHERE ' . $self->{where}->sql($conn) if $self->{where}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 47 | 564 | ▶ | $sql .= ' FOR UPDATE' if $self->{lock}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 48 | 564 | ▶ | $sql .= ' LIMIT ' . $self->{limit} if $self->{limit}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||
| 50 | 564 | $self->_logDebug2( $sql ); | |||||||||||||||||||||||||||||||||
| 51 | 564 | return $sql; | |||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||
| 54 | 27 | ▶ | sub lastidx { $_[0]{last_idx} } | ||||||||||||||||||||||||||||||||
|
Subroutine
lastidx: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 55 | 1 | ▶ | sub can_be_subquery { scalar( @{ $_[0]->fields || [] } ) == 1 }; # Must have exactly one field | ||||||||||||||||||||||||||||||||
|
Subroutine
can_be_subquery: called CC=2 SCAR=7.2
Pod
undocumented
Condition50%1/2
$_[0]->fields || []
MC/DC0%0/1
$_[0]->fields || []
$_[0]->fields
Truth table50%1/2
A
A= $_[0]->fields
| |||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||
| 57 | 567 | ▶ | sub run { | ||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=3 SCAR=11.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 58 | 567 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 59 | 567 | ▶ | return $self->{sth} ||= $self->instance->getconn->prepare( $self->sql ) || confess "Failed to prepare"; # only run once | ||||||||||||||||||||||||||||||||
|
Condition60%3/5
$self->{"sth"} ||= $self->instance->getconn->prepare($self->sql) || confess("Failed to prepare")
$self->instance->getconn->prepare($self->sql) || confess('Failed to prepare')
MC/DC0%0/2
$self->{"sth"} ||= $self->instance->getconn->prepare($self->sql) || confess("Failed to prepare")
$self->{"sth"}
$self->instance->getconn->prepare($self->sql)
Truth table66%2/3
A ||= B
A= $self->{"sth"}B= $self->instance->getconn->prepare($self->sql)
| |||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | sub reset { | ||||||||||||||||||||||||||||||||
|
Subroutine
reset: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 62 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | return $self->{sth} && $self->{sth}->finish; | ||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->{'sth'} && $self->{'sth'}->finish
MC/DC0%0/2
$self->{'sth'} && $self->{'sth'}->finish
$self->{'sth'}
$self->{'sth'}->finish
Truth table0%0/3
A && B
A= $self->{'sth'}B= $self->{'sth'}->finish
| |||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||
| 66 | # HERE - it's a little funky that we are handling split queries here, | ||||||||||||||||||||||||||||||||||
| 67 | # but non-split queries in ResultSet. Not horrible... just funky. | ||||||||||||||||||||||||||||||||||
| 68 | 10 | ▶ | sub fetch_segment{ | ||||||||||||||||||||||||||||||||
|
Subroutine
fetch_segment: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 69 | 10 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 70 | 10 | my $value = shift; | |||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||
| 72 | 10 | ▶ | return ( $self->{spvals} ||= $self->_do_split )->{ $value } || []; | ||||||||||||||||||||||||||||||||
|
Condition60%3/5
+($self->{'spvals'} ||= $self->_do_split)->{$value} || []
$self->{"spvals"} ||= $self->_do_split
MC/DC0%0/1
+($self->{'spvals'} ||= $self->_do_split)->{$value} || []
+($self->{'spvals'} ||= $self->_do_split)->{$value}
MC/DC0%0/2
$self->{"spvals"} ||= $self->_do_split
$self->{"spvals"}
$self->_do_split
Truth table50%1/2
A
A= +($self->{'spvals'} ||= $self->_do_split)->{$value}
Truth table66%2/3
A ||= B
A= $self->{"spvals"}B= $self->_do_split
| |||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||
| 75 | 4 | ▶ | sub _do_split{ | ||||||||||||||||||||||||||||||||
|
Subroutine
_do_split: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||
| 76 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||
| 78 | # Should have a splitfield if we're getting here. Don't check for it. speeed. | ||||||||||||||||||||||||||||||||||
| 79 | 4 | ▶ | defined( my $idx = $self->{splitfield}->index ) or croak 'field object must provide an index'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||
| 81 | 4 | my $sth = $self->run; | |||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||
| 83 | 4 | ▶ | defined( $sth->execute ) or croak 'failed to execute statement (' . $sth->errstr. ')'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||
| 85 | 4 | my $row; | |||||||||||||||||||||||||||||||||
| 86 | 4 | my $code = 'while($row = $sth->fetch){ push @{$groupby{ $row->[' . $idx . '] }}, [@$row] }'; | |||||||||||||||||||||||||||||||||
| 87 | 4 | $self->_logDebug3($code); | |||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||
| 89 | 4 | my %groupby; | |||||||||||||||||||||||||||||||||
| 90 | 4 | eval $code; | |||||||||||||||||||||||||||||||||
| 91 | 4 | ▶ | $@ && confess $@; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||
| 93 | 4 | $sth->finish; | |||||||||||||||||||||||||||||||||
| 94 | 4 | return \%groupby; | |||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||
| 98 | 39 | ▶ | sub get_record_obj{ | ||||||||||||||||||||||||||||||||
|
Subroutine
get_record_obj: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 99 | 39 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||
| 101 | # Only make the record-maker object once per query. Even split queries should be able to share the same one. | ||||||||||||||||||||||||||||||||||
| 102 | return $self->{recordobj} ||= DBR::Record::Maker->new( | ||||||||||||||||||||||||||||||||||
| 103 | session => $self->{session}, | ||||||||||||||||||||||||||||||||||
| 104 | 39 | ▶ | query => $self, # This value is not preserved by the record maker, thus no memory leak | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"recordobj"} ||= "DBR::Record::Maker"->new("session", $self->{"session"}, "query", $self)
MC/DC0%0/2
$self->{"recordobj"} ||= "DBR::Record::Maker"->new("session", $self->{"session"}, "query", $self)
$self->{"recordobj"}
"DBR::Record::Maker"->new("session", $self->{"session"}, "query", $self)
Truth table66%2/3
A ||= B
A= $self->{"recordobj"}B= "DBR::Record::Maker"->new("session", $self->{"session"}, "query", $self)
| |||||||||||||||||||||||||||||||||||
| 105 | ) or confess ('failed to create record class'); | ||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||
| 108 | 580 | ▶ | sub DESTROY{ | ||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 109 | 580 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||
| 111 | # Can't finish the sth when going out of scope, it might live longer than this object. | ||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||
| 113 | 580 | return 1; | |||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||
| 115 | 18 | 1; | |||||||||||||||||||||||||||||||||