blib/lib/SQL/String.pm

filter: Statementstmt 89.0% 57 / 64 Branchbran 75.0% 18 / 24 Conditioncond 63.1% 12 / 19 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 93.3% 14 / 15 Podpod 100% 7 / 7 totaltotal 79.7% 110 / 138 CC 18 SCAR 16.0
CC
18
Cov
81%
_concat
9.7
concat
6.4
new
6.1
CRAP 5.0
↑ Summary
1 package SQL::String;
2  
3 =pod
4  
5 =head1 NAME
6  
7 SQL::String - An object representation of a chunk of SQL
8  
9 =head1 SYNOPSIS
10  
11 <iAlias> I hate SQL::
12 <iAlias> Guess what SQL::Snippit is
13 <brevity> a not very useful library. I've looked at that.
14 <iAlias> "A snippit of SQL"? NO! A giant complex library and storage and frameworks and junk
15 <iAlias> That's all I want
16 <iAlias> Every decent name I could take for a simple little tied-param chunk of SQL is taken by a "giant framework"
17 <brevity> Alias: the reason is because SQL does not lend itself to small helpful modules
18 <iAlias> It's like CGI all over again :)
19 <rhizo> hehe
20 <brevity> alias: dunno if you want to make your own big framework
21 <iAlias> I have my own big framework :)
22 <xantus_> heh
23 <iAlias> I just want some nice little toys to clean it up a bit
24 <iAlias> aha! Nobody has taken SQL::String yet
25 <iAlias> It's mine I tellses you! My own... my precious...
26  
27 =head1 DESCRIPTION
28  
29 SQL::String is a simple object class that lets you create "chunks" of SQL
30 that intrinsicly have their parameters attached to them.
31  
32 Quite a few standard SQL queries won't need this, you create your main
33 select statement once, and then provide the parameters different for each
34 call.
35  
36 However, several types of queries can benefit from this. In particular,
37 the creation of large and complex search queries can be tricky to build
38 what might be 1000 character of SQL and keep track of all the required
39 parameters (short of doing them in a named form, with all the problems
40 of namespace management that entails).
41  
42 SQL::String solves this problem by embedding the parameters into the SQL.
43  
44 A SQL::String object exists as a reference to an array containing the SQL,
45 and a number of parameters intended to be used with it.
46  
47 More usefully, SQL::String overloads concatonation so that you can still
48 use a SQL::String object naturally is if it was just SQL.
49  
50 Once you have created your large complex query, you simple split out the
51 SQL and parameters parts and hand them off to DBI normally.
52  
53 Although SQL::String WILL check to make sure that the SQL is a simple string
54 of at least one character, it makes no judgements whatsoever about the
55 parameters. C<undef>, references, objects, everything is legal.
56  
57 This enables custom database backends that do translation of non-DBI
58 parameters normally.
59  
60 =head2 Overloads
61  
62 SQL::String objects ALWAYS evaluate as true, stringify to just the SQL,
63 and act properly in concatination, merging in other parameters in the
64 correct order as expected.
65  
66 The concatination is completely interpolation-safe. That is you can do
67 something like the following.
68  
69 my $sql = SQL::String( 'foo = ?', 10 );
70 $sql = "select * from table where $sql";
71  
72 =head2 Sub-classing
73  
74 Due to the nature of it's internal design, for the time being you are
75 forbidden to sub-class SQL::String.
76  
77 There are some future issues relating to internal structure and XS
78 acceleration that have not been resolved.
79  
80 =head1 METHODS
81  
82 =cut
83  
84 2 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
87 2 ▶use overload 'bool' => sub () { 1 },
Subroutine BEGIN: called CC=1 SCAR=0.2
88 0 ▶ 'eq' => sub { $_[0]->[0] eq $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
89 2 '""' => 'sql',
90 '.' => '_concat',
91 '.=' => 'concat',
92 2 '=' => 'clone';
93  
94 2 ▶use vars qw{$VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
95 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
96 2 $VERSION = '0.02';
97 }
98  
99  
100  
101  
102  
103 #####################################################################
104 # Constructor
105  
106 =pod
107  
108 =head2 new $sql [, $param, $param, $param ]
109  
110 The C<new> constructor takes a fragment of SQL and zero or more parameters
111 and creates a new SQL::String object.
112  
113 Returns a new SQL::String object, or C<undef> if the SQL argument is not a
114 simple (defined, non-reference, and with non-zero length) string.
115  
116 =cut
117  
118 19 ▶sub new {
Subroutine new: called CC=6 SCAR=18.0
Pod documented
119 19 ▶ my $class = ref $_[0] ? ref shift : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 19 19
120 19 ▶ my $self = (ref $_[0] eq 'ARRAY') ? shift : [ @_ ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 1 18 19
121 19 ▶ defined $self->[0] and ! ref $self->[0] and CORE::length $self->[0] or return undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->[0] and not ref $self->[0] and length $self->[0] or return undef 14 5 19
Condition83%5/6
defined $self->[0] and not ref $self->[0] and length $self->[0]
!ll&&!rl&&r
5014
defined $self->[0] and not ref $self->[0]
!ll&&!rl&&r
3214
MC/DC66%2/3
defined $self->[0] and not ref $self->[0] and length $self->[0]
defined $self->[0] not ref $self->[0] length $self->[0]
Truth table75%3/4
A and not(B) and C
A= defined $self->[0]B= not ref $self->[0]C= length $self->[0]
ABCresult
0XX0
10X0
1100
1111
122 14 bless $self, $class;
123 }
124  
125 =pod
126  
127 =head2 sql
128  
129 The C<sql> accessor provides direct access to the SQL within the object.
130  
131 =cut
132  
133 22 ▶sub sql { $_[0]->[0] }
Subroutine sql: called CC=1 SCAR=0.0
Pod documented
134  
135 =pod
136  
137 =head2 params
138  
139 The C<params> method returns a list of the zero or more SQL parameters.
140  
141 When called in scalar context, it returns the number of parameters.
142  
143 =cut
144  
145 6 ▶sub params {
Subroutine params: called CC=2 SCAR=6.9
Pod documented
146 6 ▶ return $#{$_[0]} unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 3 3 6
147 3 my @params = @{$_[0]};
148 3 shift @params;
149 3 @params;
150 }
151  
152 =pod
153  
154 =head2 params_ref
155  
156 The C<params_ref> method also returns the SQL parameters, but as a
157 reference to an ARRAY.
158  
159 =cut
160  
161 16 ▶sub params_ref {
Subroutine params_ref: called CC=1 SCAR=0.0
Pod documented
162 16 my @params = @{$_[0]};
163 16 shift @params;
164 16 \@params;
165 }
166  
167 =pod
168  
169 =head2 stable
170  
171 The C<stable> method can be used to double-check that the SQL::String object
172 contains a matching number of placeholders and parameters. At this time,
173 only '?' placeholders are recommended in SQL::String objects.
174  
175 Returns true if the number of placeholders match the number of parameters,
176 or false otherwise (in the same way as the == operator).
177  
178 =cut
179  
180 3 ▶sub stable {
Subroutine stable: called CC=1 SCAR=0.0
Pod documented
181 3 my $self = shift;
182 3 my $placeholders =()= $self->[0] =~ /(\?)/g;
183 3 my $params = $#$self;
184 3 $placeholders == $params;
185 }
186  
187  
188  
189  
190  
191 #####################################################################
192 # Overloaded Concatination
193  
194 =pod
195  
196 =head2 clone
197  
198 Make a copy of the SQL::String object. The C<clone> function does NOT
199 deep copy the parameters, so you will end up with references to the
200 same refs if you are using refs or objects in the params list.
201  
202 Returns a new and identical SQL::String object with shared param refs.
203  
204 =cut
205  
206 9 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
207 9 my $self = shift;
208 9 bless [ @$self ], ref $self;
209 }
210  
211 # This is likely to be by FAR the most common operation
212  
213 =pod
214  
215 =head2 concat $string | \@array | $SQLString
216  
217 The C<concat> method contatonates another string or SQL::String object
218 to the end of the current object.
219  
220 It takes only a single parameter and behaves in the following way
221  
222 - If passed C<undef>, throws the same warning as for a normal undef
223 concatonation.
224  
225 - If passed a zero-length or simple string, concatonates it normally.
226  
227 - If passed another SQL::String object, joins both the SQL and parameter
228 lists in the way you would expect, retaining the correct order of
229 placeholders and parameters. To make the process faster, the SQL::String
230 argument will be probably be destroyed in the process.
231  
232 - If passed an ARRAY reference it will be treated as a SQL::String object,
233 with the first element as a SQL string and the rest as parameters, as with
234 the SQL::String parameter above.
235  
236 - If passed any other type of reference of object, will die with an
237 appropriate error message.
238  
239 In all cases, it returns the same object as a convenience.
240  
241 =cut
242  
243 12 ▶sub concat {
Subroutine concat: called CC=6 SCAR=18.6
Pod documented
244 12 my $self = shift;
245 12 my $right = shift;
246  
247 # The argument is undef
248 12 ▶ defined $right or Carp::carp('Use of uninitialized value in concatenation (.) or string') and return $self;
Branch: 1 branch
BranchTrueFalseTotal
defined $right or Carp::carp("Use of uninitialized value in concatenation (.) or string") and return $self 9 3 12
Condition60%3/5
defined $right || Carp::carp('Use of uninitialized value in concatenation (.) or string') && (return $self)
l!l&&r!l&&!r
930
Carp::carp("Use of uninitialized value in concatenation (.) or string") and return $self
!ll
03
MC/DC0%0/2
defined $right || Carp::carp('Use of uninitialized value in concatenation (.) or string') && (return $self)
defined $right Carp::carp("Use of uninitialized value in concatenation (.) or string")
Truth table66%2/3
A || B
A= defined $rightB= Carp::carp("Use of uninitialized value in concatenation (.) or string")
ABresult
1X1
011
000
249  
250 # Add a plain string
251 9 my $reftype = ref $right;
252 9 ▶ unless ( $reftype ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reftype) 5 4 9
253 5 $self->[0] .= $right;
254 5 return $self;
255 }
256  
257 # A plain ARRAY or another SQL::String
258 4 ▶ if ( $reftype eq 'ARRAY' or $reftype eq 'SQL::String' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reftype eq "ARRAY" or $reftype eq "SQL::String") 4 0 4
Condition33%1/3
$reftype eq "ARRAY" or $reftype eq "SQL::String"
l!l&&r!l&&!r
040
MC/DC0%0/2
$reftype eq "ARRAY" or $reftype eq "SQL::String"
$reftype eq "ARRAY" $reftype eq "SQL::String"
Truth table33%1/3
A or B
A= $reftype eq "ARRAY"B= $reftype eq "SQL::String"
ABresult
1X1
011
000
259 4 $self->[0] .= shift @$right;
260 4 push @$self, @$right;
261  
262 4 return $self;
263 }
264  
265 # Something unknown, because we don't allow subclasses.
266 0 Carp::croak("Tried to SQL::String::concat an illegal object ($reftype)");
267 }
268  
269 6 ▶sub _concat {
Subroutine _concat: called CC=7 SCAR=22.7
270 6 ▶ return shift->concat(shift) unless defined $_[2];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[2] 0 6 6
271 6 ▶ return shift->clone->concat(shift) unless $_[2];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[2] 3 3 6
272  
273 # Handle the reversed case ourselves
274 3 my $self = shift->clone;
275 3 my $left = shift;
276  
277 # The argument is undef
278 3 ▶ defined $left or Carp::carp('Use of uninitialized value in concatenation (.) or string') and return $self;
Branch: 1 branch
BranchTrueFalseTotal
defined $left or Carp::carp("Use of uninitialized value in concatenation (.) or string") and return $self 2 1 3
Condition60%3/5
defined $left || Carp::carp('Use of uninitialized value in concatenation (.) or string') && (return $self)
l!l&&r!l&&!r
210
Carp::carp("Use of uninitialized value in concatenation (.) or string") and return $self
!ll
01
MC/DC0%0/2
defined $left || Carp::carp('Use of uninitialized value in concatenation (.) or string') && (return $self)
defined $left Carp::carp("Use of uninitialized value in concatenation (.) or string")
Truth table66%2/3
A || B
A= defined $leftB= Carp::carp("Use of uninitialized value in concatenation (.) or string")
ABresult
1X1
011
000
279  
280 # Add a plain string
281 2 my $reftype = ref $left;
282 2 ▶ unless ( $reftype ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reftype) 2 0 2
283 2 $self->[0] = $left . $self->[0];
284 2 return $self;
285 }
286  
287 # A plain ARRAY (it can't be another SQL::String this time)
288 0 ▶ if ( $reftype eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reftype eq "ARRAY") 0 0 0
289 0 $self->[0] = shift(@$left) . $self->[0];
290 0 unshift @$self, @$left;
291 0 return $self;
292 }
293  
294 # Something unknown, because we don't allow subclasses.
295 0 Carp::croak("Tried to SQL::String::concat an illegal object ($reftype)");
296 }
297  
298 2 1;
299  
300 ### Keeping this for future uses... it was delicate and tricky to create
301 ### $self->[0] .= ' ' unless substr($sql, 0, 1) eq ' ' or substr($self->[0], -1, 1) eq ' ';
302  
303 =pod
304  
305 =head1 TO DO
306  
307 - Write a faster XS version?
308  
309 - Change param handling to Params::Util
310  
311 - Make use of bytes and potentially unicode
312  
313 - Test to see if if would be better to include the params in their own
314 ARRAY reference.
315  
316 =head1 SUPPORT
317  
318 Bugs should be submitted via the CPAN bug tracker, located at
319  
320 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SQL-String>
321  
322 For other issues, contact the author
323  
324 =head1 AUTHOR
325  
326 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
327  
328 Thank you to Phase N Australia (L<http://phase-n.com/>) for permitting the
329 open sourcing and release of this distribution.
330  
331 =head1 COPYRIGHT
332  
333 Copyright 2004 - 2008 Adam Kennedy.
334  
335 This program is free software; you can redistribute
336 it and/or modify it under the same terms as Perl itself.
337  
338 The full text of the license can be found in the
339 LICENSE file included with this module.
340  
341 =cut
↑ Summary