Named.pm

filter: Statementstmt 87.6% 64 / 73 Branchbran 60.7% 17 / 28 Conditioncond 44.4% 4 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 90.9% 10 / 11 Podpod 100% 2 / 2 totaltotal 76.7% 99 / 129 CC 19 SCAR 28.2
CC
19
Cov
77%
connect_cached
56.0
connect
7.1
prepare
6.0
CRAP 16.9
↑ Summary ._Named.pm »
1 package DBIx::Placeholder::Named;
2  
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use base qw(DBI);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 our $VERSION = '0.08';
9 7 our $PREFIX = ':';
10 7 our $SUFFIX = '';
11  
12 7 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ATTR => 3,
14 OLD_DRIVER => 4,
15 7 };
16  
17 4 ▶sub connect {
Subroutine connect: called CC=7 SCAR=19.6
Pod documented
18 4 my ( $class, @args ) = @_;
19  
20 4 my ( $prefix, $suffix );
21  
22 4 ▶ if ( $args[ATTR] and ref( $args[ATTR] ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args[3] and ref $args[3] eq "HASH") 4 0 4
Condition33%1/3
$args[3] and ref $args[3] eq "HASH"
!ll&&!rl&&r
004
MC/DC0%0/2
$args[3] and ref $args[3] eq "HASH"
$args[3] ref $args[3] eq "HASH"
Truth table33%1/3
A and B
A= $args[3]B= ref $args[3] eq "HASH"
ABresult
0X0
100
111
23 $prefix = delete $args[ATTR]{PlaceholderPrefix}
24 4 ▶ if ( exists $args[ATTR]{PlaceholderPrefix} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $args[3]{"PlaceholderPrefix"} 3 1 4
25 $suffix = delete $args[ATTR]{PlaceholderSuffix}
26 4 ▶ if ( exists $args[ATTR]{PlaceholderSuffix} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $args[3]{"PlaceholderSuffix"} 2 2 4
27 }
28  
29 4 my $self = $class->SUPER::connect(@args);
30  
31 $self->{private_dbix_placeholder_named_info}{prefix} =
32 4 ▶ defined $prefix ? $prefix : ':';
Branch: 1 branch
BranchTrueFalseTotal
defined $prefix ? : 3 1 4
33 $self->{private_dbix_placeholder_named_info}{suffix} =
34 4 ▶ defined $suffix ? $suffix : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $suffix ? : 2 2 4
35  
36 4 return $self;
37 }
38  
39 0 ▶sub connect_cached {
Subroutine connect_cached: not called CC=7 SCAR=40.3
Pod documented
40 0 my ( $class, @args ) = @_;
41  
42 0 my ( $prefix, $suffix );
43  
44 0 ▶ if ( $args[ATTR] and ref( $args[ATTR] ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args[3] and ref $args[3] eq "HASH") 0 0 0
Condition0%0/3
$args[3] and ref $args[3] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$args[3] and ref $args[3] eq "HASH"
$args[3] ref $args[3] eq "HASH"
Truth table0%0/3
A and B
A= $args[3]B= ref $args[3] eq "HASH"
ABresult
0X0
100
111
45 $prefix = delete $args[ATTR]{PlaceholderPrefix}
46 0 ▶ if ( exists $args[ATTR]{PlaceholderPrefix} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $args[3]{"PlaceholderPrefix"} 0 0 0
47 $suffix = delete $args[ATTR]{PlaceholderSuffix}
48 0 ▶ if ( exists $args[ATTR]{PlaceholderSuffix} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $args[3]{"PlaceholderSuffix"} 0 0 0
49 }
50  
51 0 my $self = $class->SUPER::connect_cached(@args);
52  
53 $self->{private_dbix_placeholder_named_info}{prefix} =
54 0 ▶ defined $prefix ? $prefix : ':';
Branch: 1 branch
BranchTrueFalseTotal
defined $prefix ? : 0 0 0
55 $self->{private_dbix_placeholder_named_info}{suffix} =
56 0 ▶ defined $suffix ? $suffix : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $suffix ? : 0 0 0
57  
58 0 return $self;
59 }
60  
61 package DBIx::Placeholder::Named::db;
62  
63 7 ▶use SQL::Tokenizer;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 7 ▶use base qw(DBI::db);
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 48 ▶sub prepare {
Subroutine prepare: called CC=6 SCAR=17.9
67 48 my ( $dbh, $query ) = @_;
68  
69 # each token is analyzed. if the token starts with ':', it is pushed to
70 # @tao_dbi_placeholders. each element represents the named placeholder,
71 # and its index represents the order we will create the
72 # DBI::st::execute()'s argument (see Tao::DBI::st::execute()).
73 #
74 # TODO: someday we can benchmark this piece of code and check if using
75 # substr is more efficient.
76  
77 48 my @placeholders;
78 48 my @query_tokens = SQL::Tokenizer->tokenize($query);
79  
80 48 my $prefix = $DBIx::Placeholder::Named::PREFIX;
81 48 my $suffix = $DBIx::Placeholder::Named::SUFFIX;
82  
83 48 ▶ if ( exists $dbh->{private_dbix_placeholder_named_info} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $dbh->{"private_dbix_placeholder_named_info"}) 32 16 48
84 32 $prefix = $dbh->{private_dbix_placeholder_named_info}{prefix};
85 32 $suffix = $dbh->{private_dbix_placeholder_named_info}{suffix};
86 }
87  
88 48 my $prefix_length = length($prefix);
89 48 my $suffix_length = length($suffix);
90  
91 48 foreach my $token (@query_tokens) {
92 852 my $token_length = length($token);
93 852 ▶ if ( substr( $token, 0, $prefix_length ) eq $prefix
Branch: 1 branch
BranchTrueFalseTotal
if (substr($token, 0, $prefix_length) eq $prefix and substr($token, $token_length - $suffix_length, $suffix_length) eq $suffix) 48 804 852
Condition100%3/3
substr($token, 0, $prefix_length) eq $prefix and substr($token, $token_length - $suffix_length, $suffix_length) eq $suffix
!ll&&!rl&&r
67013448
MC/DC100%2/2
substr($token, 0, $prefix_length) eq $prefix and substr($token, $token_length - $suffix_length, $suffix_length) eq $suffix
substr($token, 0, $prefix_length) eq $prefix substr($token, $token_length - $suffix_length, $suffix_length) eq $suffix
Truth table100%3/3
A and B
A= substr($token, 0, $prefix_length) eq $prefixB= substr($token, $token_length - $suffix_length, $suffix_length) eq $suffix
ABresult
0X0
100
111
94 and substr( $token, $token_length - $suffix_length, $suffix_length ) eq $suffix )
95 {
96 48 my $token_stripped = substr( $token, $prefix_length );
97 48 $token_stripped =
98 substr( $token_stripped, 0, length($token_stripped) - $suffix_length );
99 48 push @placeholders, $token_stripped;
100 48 $token = '?';
101 }
102 }
103  
104 48 my $new_query = join '', @query_tokens;
105  
106 # it's time to call DBI::st::prepare(). we use the modified tokenized
107 # query (with all named placeholders substituted by '?').
108  
109 48 ▶ my $sth = $dbh->SUPER::prepare($new_query)
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->SUPER::prepare($new_query) or return 42 6 48
110 or return;
111  
112 # we can now store the named placeholders array.
113 $sth->{private_dbix_placeholder_named_info} =
114 42 { placeholders => \@placeholders };
115  
116 42 return $sth;
117 }
118  
119 package DBIx::Placeholder::Named::st;
120  
121 7 ▶use base qw(DBI::st);
Subroutine BEGIN: called CC=1 SCAR=0.0
122  
123 42 ▶sub execute {
Subroutine execute: called CC=2 SCAR=6.9
124 42 my $sth = shift;
125  
126 42 my @params;
127  
128 42 ▶ if ( ref $_[0] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 30 12 42
129  
130 # create the DBI::st::execute()'s parameter. we iterate each named
131 # placeholder stored in Tao::DBI::db::prepare() and retrieve its value
132 # from the user supplied dictionary.
133  
134 @params =
135 30 map { $_[0]->{$_} } @{ $sth->{private_dbix_placeholder_named_info}->{placeholders} };
136  
137 }
138 else {
139  
140 # user haven't supplied a dictionary, so we use the parameters 'as is'
141 12 @params = @_;
142 }
143  
144 # DBI::st::execute() always returns.
145 42 my $rv = $sth->SUPER::execute(@params);
146  
147 42 return $rv;
148 }
149  
150 7 1;
151  
152 =pod
153  
154 =head1 NAME
155  
156 DBIx::Placeholder::Named - DBI with named placeholders
157  
158 =head1 SYNOPSIS
159  
160 use DBIx::Placeholder::Named;
161  
162 my $dbh = DBIx::Placeholder::Named->connect($dsn, $user, $password)
163 or die DBIx::Placeholder::Named->errstr;
164  
165 my $sth = $dbh->prepare(
166 q{ INSERT INTO some_table (this, that) VALUES (:this, :that) }
167 )
168 or die $dbh->errstr;
169  
170 $sth->execute({ this => $this, that => $that, });
171  
172 $dbh =
173 DBIx::Placeholder::Named->connect( $dsn, $user, $password,
174 { PlaceholderPrefix => '__', PlaceholderSuffix => '**' } );
175  
176 my $sth = $dbh->prepare(
177 q{ INSERT INTO some_table (this, that) VALUES (__this**, __that**) }
178 );
179  
180 =head1 DESCRIPTION
181  
182 DBIx::Placeholder::Named is a subclass of DBI, which implements the ability
183 to understand named placeholders.
184  
185 =head1 METHODS
186  
187 =over 4
188  
189 =item DBIx::Placeholder::Named::connect()
190  
191 This method, overloaded from L<DBI|DBI>, is responsible to create a
192 new connection to database. It is overloaded to accept new keywords
193 within the C<$attr> hash.
194  
195 my $dbh =
196 DBIx::Placeholder::Named->connect( $dsn, $user, $password,
197 { RaiseError => 1, PlaceholderSuffix => '', PlaceholderPrefix => ':', } );
198  
199 By default, C<PlaceholderPrefix> is C<:> and C<PlaceholderSuffix> is
200 empty string.
201  
202 =item DBIx::Placeholder::Named::connect_cached()
203  
204 This method, overloaded from L<DBI|DBI>, is responsible to create a
205 cached connection to database. It is overloaded to accept new keywords
206 within the C<$attr> hash.
207  
208 my $dbh =
209 DBIx::Placeholder::Named->connect_cached( $dsn, $user, $password,
210 { RaiseError => 1, PlaceholderSuffix => '', PlaceholderPrefix => ':', } );
211  
212 By default, C<PlaceholderPrefix> is C<:> and C<PlaceholderSuffix> is
213 empty string.
214  
215 =item DBIx::Placeholder::Named::db::prepare()
216  
217 This method, overloaded from L<DBI|DBI::db>, is responsible to create a prepared
218 statement for further execution. It is overloaded to accept a SQL query which
219 has named placeholders, like:
220  
221 SELECT a, b, c FROM t WHERE id = :id
222  
223 It uses L<SQL::Tokenizer|SQL::Tokenizer> to correctly tokenize the SQL query,
224 preventing extract erroneous placeholders (date/time specifications, comments,
225 inside quotes or double quotes, etc).
226  
227 =item DBIx::Placeholder::Named::st::execute()
228  
229 =back
230  
231 =cut
232  
233 =head1 THANKS
234  
235 Gabor Szabo <szabgab@gmail.com> for requesting prefix support.
236  
237 =head1 AUTHOR
238  
239 Copyright (c) 2007, Igor Sutton Lopes "<IZUT@cpan.org>". All rights reserved.
240  
241 This module is free software; you can redistribute it and/or modify it under
242 the same terms as Perl itself.
243  
244 =head1 SEE ALSO
245  
246 L<SQL::Tokenizer>
247  
248 =cut
249  
↑ Summary ._Named.pm »