| 1 | package Data::Phrasebook::SQL::Query; | ||||||||||||||||||||||||||||
| 2 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use base qw( Data::Phrasebook::Debug ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use vars qw( $AUTOLOAD ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 6 | $VERSION = '0.35'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | Data::Phrasebook::SQL::Query - Query Extension to the SQL/DBI Phrasebook Model. | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | my $q = $book->query( 'find_author' ); | ||||||||||||||||||||||||||||
| 18 | my $q = $book->query( 'find_author', 'Dictionary' ); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | An extension to the SQL class to specifically handle the DBI interface for | ||||||||||||||||||||||||||||
| 23 | each query requested. | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =head2 new | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | Not to be accessed directly, but via the parent L<Data::Phrasebook>, by | ||||||||||||||||||||||||||||
| 30 | specifying the class as SQL. | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =head1 METHODS | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =head2 sql | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | Get/set the current C<sql> statement, in a form suitable for passing | ||||||||||||||||||||||||||||
| 37 | straight to DBI. | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =head2 sth | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | Get/set the current statement handle. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =head2 args | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | Return list of arguments that will be used as bind parameters to any | ||||||||||||||||||||||||||||
| 46 | placeholders. Any given arguments will replace the whole list. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | Returns list in list context, arrayref in scalar. | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =head2 order | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | As for C<args>, but regarding the corresponding list of argument | ||||||||||||||||||||||||||||
| 53 | B<names>. | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | The assorted C<order_XXX> methods are supported as for C<args_XXX>. | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =head2 dbh | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | Get/set the database handle. | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =cut | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 6 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 64 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 65 | 6 | my %hash = @_; | |||||||||||||||||||||||||||
| 66 | 6 | ▶ | $self->store(3,"$self->new IN") if($self->debug); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 6 | my $atts = \%hash; | |||||||||||||||||||||||||||
| 68 | 6 | bless $atts, $self; | |||||||||||||||||||||||||||
| 69 | 6 | return $atts; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 6 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 73 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 74 | 6 | ▶ | $self->sth->finish if($self->sth); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 6 | return; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 10 | ▶ | sub sql { | ||||||||||||||||||||||||||
|
Subroutine
sql: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 10 | my $self = shift; | |||||||||||||||||||||||||||
| 80 | 10 | ▶ | return @_ ? $self->{sql} = shift : $self->{sql}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 9 | ▶ | sub dbh { | ||||||||||||||||||||||||||
|
Subroutine
dbh: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 9 | my $self = shift; | |||||||||||||||||||||||||||
| 84 | 9 | ▶ | return @_ ? $self->{dbh} = shift : $self->{dbh}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 110 | ▶ | sub sth { | ||||||||||||||||||||||||||
|
Subroutine
sth: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 87 | 110 | my $self = shift; | |||||||||||||||||||||||||||
| 88 | 110 | ▶ | return @_ ? $self->{sth} = shift : $self->{sth}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 18 | ▶ | sub args { | ||||||||||||||||||||||||||
|
Subroutine
args: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | 18 | my $self = shift; | |||||||||||||||||||||||||||
| 92 | 18 | my @args = @_; | |||||||||||||||||||||||||||
| 93 | 18 | ▶ | $self->{args} = \@args if(@_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 18 | return $self->{args}; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 9 | ▶ | sub order { | ||||||||||||||||||||||||||
|
Subroutine
order: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | 9 | my $self = shift; | |||||||||||||||||||||||||||
| 98 | 9 | my @args = @_; | |||||||||||||||||||||||||||
| 99 | 9 | ▶ | $self->{order} = \@args if(@_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 9 | ▶ | return @{$self->{order}} if($self->{order}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | return (); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head1 PREPARATION / EXECUTING METHODS | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head2 execute | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | Executes the query. Returns the result of C<DBI::execute>. | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | Any arguments are given to C<order_args> with the return of that method | ||||||||||||||||||||||||||||
| 111 | being used as arguments to C<DBI::execute>. If no arguments, uses those | ||||||||||||||||||||||||||||
| 112 | already specified. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | Calls C<prepare> if necessary. | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =cut | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 10 | ▶ | sub execute { | ||||||||||||||||||||||||||
|
Subroutine
execute: called CC=9 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||
| 119 | 10 | my $self = shift; | |||||||||||||||||||||||||||
| 120 | 10 | ▶ | $self->store(3,"->execute IN: @_") if($self->debug); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 10 | my $sth = $self->sth; | |||||||||||||||||||||||||||
| 122 | 10 | ▶ | my @args = @_ ? $self->order_args( @_ ) : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 10 | ▶ | @args = () if(@args && !defined $args[0]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@args and not defined $args[0]
MC/DC0%0/2
@args and not defined $args[0]
@args
not defined $args[0]
Truth table66%2/3
A and not(B)
A= @argsB= not defined $args[0]
| |||||||||||||||||||||||||||||
| 124 | 10 | ▶ | $sth = $self->prepare() unless $sth; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 10 | ▶ | unless(@args) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 5 | $self->rebind; | |||||||||||||||||||||||||||
| 128 | 5 | return $sth->execute(); | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | ▶ | $self->store(4,"->execute args[".join(",",map {$_||'undef'} @args)."]") if($self->debug); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$_ || 'undef'
MC/DC0%0/1
$_ || 'undef'
$_
Truth table0%0/2
A
A= $_
| |||||||||||||||||||||||||||||
| 132 | 5 | return $sth->execute( map { $$_ } @args ); | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =head2 order_args | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | Given a hash or hashref of keyword to value mappings, organises | ||||||||||||||||||||||||||||
| 138 | an array of arguments suitable for use as bind parameters | ||||||||||||||||||||||||||||
| 139 | in the order needed by the query itself. | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =cut | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 9 | ▶ | sub order_args { | ||||||||||||||||||||||||||
|
Subroutine
order_args: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 144 | 9 | my $self = shift; | |||||||||||||||||||||||||||
| 145 | 9 | ▶ | my %args = (@_ == 1 ? %{$_[0]} : @_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 9 | my @order = $self->order; | |||||||||||||||||||||||||||
| 147 | 9 | my @args = $self->args; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 9 | for (0..$#order) | |||||||||||||||||||||||||||
| 150 | { | ||||||||||||||||||||||||||||
| 151 | 9 | my $key = $order[$_]; | |||||||||||||||||||||||||||
| 152 | 9 | ▶ | if (exists $args{ $key }) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | { | ||||||||||||||||||||||||||||
| 154 | 9 | my $val = $args{ $key }; | |||||||||||||||||||||||||||
| 155 | 9 | ▶ | $args[$_] = (ref $val) ? $val : \$val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 9 | return @args; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =head2 prepare | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | Prepares the query for execution. This method is called | ||||||||||||||||||||||||||||
| 165 | implicitly in most cases so you generally don't need | ||||||||||||||||||||||||||||
| 166 | to know about it. | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =cut | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 8 | ▶ | sub prepare { | ||||||||||||||||||||||||||
|
Subroutine
prepare: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||
| 171 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 172 | 8 | ▶ | $self->store(3,"$self->prepare IN") if($self->debug); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 8 | my $sql = $self->sql; | |||||||||||||||||||||||||||
| 174 | 8 | ▶ | $self->store(4,"$self->prepare sql=[$sql]") if($self->debug); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 8 | ▶ | croak "Can't prepare without SQL" unless defined $sql; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 8 | my $sth = $self->dbh->prepare_cached( $sql ); | |||||||||||||||||||||||||||
| 177 | 7 | $self->sth( $sth ); | |||||||||||||||||||||||||||
| 178 | 7 | return $sth; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =head2 rebind | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | Rebinds any bound values. Lets one pass a scalar reference in | ||||||||||||||||||||||||||||
| 184 | the arguments to C<order_args> and have the bound value update | ||||||||||||||||||||||||||||
| 185 | if the original scalar changes. | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | This method is not needed externally to this class. | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =cut | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 5 | ▶ | sub rebind { | ||||||||||||||||||||||||||
|
Subroutine
rebind: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 192 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 193 | 5 | my $sth = $self->sth; | |||||||||||||||||||||||||||
| 194 | 5 | my $args = $self->args; | |||||||||||||||||||||||||||
| 195 | 5 | for my $x (0..$#{$args}) | |||||||||||||||||||||||||||
| 196 | { | ||||||||||||||||||||||||||||
| 197 | 0 | ▶ | $self->store(4,'->rebind param['.($x+1).','.(${ $args->[$x] }).']') if($self->debug); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 5 | $sth->bind_param( $x+1, ${ $args->[$x] } ) | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | 5 | return; | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =head1 DELEGATED METHODS | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | Any method not mentioned above is given to the statement | ||||||||||||||||||||||||||||
| 206 | handle. | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | All these delegations will implicitly call C<prepare>. | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | =cut | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | # Currently the following is not true, but will be fixed at some point: | ||||||||||||||||||||||||||||
| 213 | # | ||||||||||||||||||||||||||||
| 214 | #Any C<fetch*> methods will additionally call C<execute> | ||||||||||||||||||||||||||||
| 215 | #unless the statement handle is already active. | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 68 | ▶ | sub _call_other { | ||||||||||||||||||||||||||
|
Subroutine
_call_other: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 218 | 68 | my ($self, $execute, $method) = splice @_, 0, 3; | |||||||||||||||||||||||||||
| 219 | 68 | ▶ | my $sth = $self->sth || $self->prepare(); | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->sth || $self->prepare
MC/DC0%0/2
$self->sth || $self->prepare
$self->sth
$self->prepare
Truth table33%1/3
A || B
A= $self->sthB= $self->prepare
| |||||||||||||||||||||||||||||
| 220 | 68 | ▶ | $self->execute() if $execute and not $sth->{Active}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$execute and not $sth->{"Active"}
MC/DC100%2/2
$execute and not $sth->{"Active"}
$execute
not $sth->{"Active"}
Truth table100%3/3
A and not(B)
A= $executeB= not $sth->{"Active"}
| |||||||||||||||||||||||||||||
| 221 | 68 | return $sth->$method( @_ ); | |||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 6 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 225 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 226 | 6 | my ($method) = $AUTOLOAD =~ /([^:]+)$/; | |||||||||||||||||||||||||||
| 227 | #print STDERR "\n#[$AUTOLOAD][$method]\n"; | ||||||||||||||||||||||||||||
| 228 | 6 | ▶ | my $sth = $self->sth || $self->prepare(); | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->sth || $self->prepare
MC/DC0%0/2
$self->sth || $self->prepare
$self->sth
$self->prepare
Truth table66%2/3
A || B
A= $self->sthB= $self->prepare
| |||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 6 | ▶ | if ($sth->can($method)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | { | ||||||||||||||||||||||||||||
| 232 | 6 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 233 | 6 | ▶ | my $execute = $method =~ /^fetch/ ? 1 : 0 ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 234 | 6 | ▶ | *{$method} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 235 | 68 | my $s = shift; | |||||||||||||||||||||||||||
| 236 | 68 | $s->_call_other( $execute, $method, @_ ) | |||||||||||||||||||||||||||
| 237 | 6 | }; | |||||||||||||||||||||||||||
| 238 | 6 | return $self->$method( @_ ); | |||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | 0 | return; | |||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 6 | 1; | |||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | __END__ | ||||||||||||||||||||||||||||