blib/lib/SQL/Tiny.pm

filter: Statementstmt 98.0% 102 / 104 Branchbran 95.0% 19 / 20 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 10 / 10 Podpod 100% 4 / 4 totaltotal 97.8% 138 / 141 CC 15 SCAR 12.8
CC
15
Cov
98%
_build_where_section
6.0
sql_update
4.0
sql_insert
4.0
CRAP 3.6
↑ Summary
1 package SQL::Tiny;
2  
3 5 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 SQL::Tiny - A very simple SQL-building library
10  
11 =head1 VERSION
12  
13 Version 0.02
14  
15 =cut
16  
17 5 our $VERSION = '0.02';
18  
19 5 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 5 our @EXPORT_OK = qw(
22 sql_select
23 sql_insert
24 sql_update
25 sql_delete
26 );
27  
28 5 our %EXPORT_TAGS = (
29 all => [@EXPORT_OK],
30 );
31  
32  
33 =head1 SYNOPSIS
34  
35 my ($sql,$bind) = sql_select( 'users', [ 'name', 'status' ], { status => [ 'Deleted', 'Inactive' ] );
36  
37 my ($sql,$bind) = sql_insert( 'users', { name => 'Dave', status => 'Active' } );
38  
39 my ($sql,$bind) = sql_update( 'users', { status => 'Inactive' }, { password => undef } );
40  
41 my ($sql,$bind) = sql_delete( 'users', { status => 'Inactive' } );
42  
43 =head1 DOCUMENTATION
44  
45 A very simple SQL-building library. It's not for all your SQL needs,
46 only the very simple ones.
47  
48 It doesn't handle JOINs. It doesn't handle GROUP BY. It doesn't handle
49 subselects. It's only for simple SQL.
50  
51 In my test suites, I have a lot of ad hoc SQL queries, and it drives me
52 nuts to have so much SQL code lying around. SQL::Tiny is for generating
53 SQL code for simple cases.
54  
55 I'd far rather have:
56  
57 my ($sql,$binds) = sql_insert(
58 'users',
59 {
60 name => 'Dave',
61 salary => 50000,
62 status => 'Active',
63 dateadded => \'SYSDATE()',
64 }
65 );
66  
67 than hand-coding:
68  
69 my $sql = 'INSERT INTO users (name,salary,status,dateadded) VALUES (:name,:status,:salary,SYSDATE())';
70 my $binds = {
71 ':name' => 'Dave',
72 ':salary' => 50000,
73 ':status' => 'Active',
74 ':dateadded' => \'SYSDATE()',
75 };
76  
77 or even the positional:
78  
79 my $sql = 'INSERT INTO users (name,salary,status,dateadded) VALUES (?,?,?,SYSDATE())';
80 my $binds = [ 'Dave', 50000, 'Active' ];
81  
82 The trade-off for that brevity of code is that SQL::Tiny has to make new
83 SQL and binds from the input every time. You can't cache the SQL that
84 comes back from SQL::Tiny because the placeholders could vary depending
85 on what the input data is. Therefore, you don't want to use SQL::Tiny
86 where speed is essential.
87  
88 The other trade-off is that SQL::Tiny handles only very simple code.
89 It won't handle JOINs of any kind.
90  
91 SQL::Tiny isn't meant for all of your SQL needs, only the simple ones
92 that you do over and over.
93  
94 =head1 EXPORT
95  
96 All subs can be exported, but none are by default. C<:all> exports all subs.
97  
98 =head1 SUBROUTINES/METHODS
99  
100 =head2 sql_select( $table, \@columns, \%where [, \%other ] )
101  
102 Creates simple SELECTs and binds.
103  
104 The C<%other> can contain C<group_by> and C<order_by>.
105  
106 Calling:
107  
108 my ($sql,$binds) = sql_select(
109 'users',
110 [qw( userid name )],
111 { status => 'X' ],
112 { order_by => 'name' },
113 );
114  
115 returns:
116  
117 $sql = 'SELECT userid,name FROM users WHERE status=? ORDER BY name';
118 $binds = [ 'X' ];
119  
120 =cut
121  
122 sub sql_select {
123 6 ▶ my $table = shift;
Subroutine sql_select: called CC=2 SCAR=6.9
Pod documented
124 6 my $columns = shift;
125 6 my $where = shift;
126 6 ▶ my $other = shift // {};
Condition100%2/2
shift() // {}
l!l
42
MC/DC100%1/1
shift() // {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
127  
128 my @parts = (
129 6 'SELECT ' . join( ',', @{$columns} ),
130 "FROM $table",
131 );
132  
133 6 my @binds;
134  
135 6 _build_where_section( \@parts, $where, \@binds );
136 6 _build_by_section( \@parts, 'GROUP BY', $other->{group_by} );
137 6 _build_by_section( \@parts, 'ORDER BY', $other->{order_by} );
138  
139 6 my $sql = join( ' ', @parts );
140  
141 6 return ( $sql, \@binds );
142 }
143  
144 =head2 sql_insert( $table, \%values )
145  
146 Creates simple INSERTs and binds.
147  
148 Calling:
149  
150 my ($sql,$binds) = sql_insert(
151 'users',
152 {
153 serialno => '12345',
154 name => 'Dave',
155 rank => 'Sergeant',
156 height => undef,
157 date_added => \'SYSDATE()',
158 }
159 );
160  
161 returns:
162  
163 $sql = 'INSERT INTO users (date_added,height,name,rank,serialno) VALUES (SYSDATE(),NULL,?,?,?)';
164 $binds = [ 'Dave', 'Sergeant', 12345 ]
165  
166 =cut
167  
168 sub sql_insert {
169 1 ▶ my $table = shift;
Subroutine sql_insert: called CC=4 SCAR=13.9
Pod documented
170 1 my $values = shift;
171  
172 1 my @parts = (
173 "INSERT INTO $table"
174 );
175  
176 1 my @values;
177 my @binds;
178  
179 1 my @columns = sort keys %{$values};
180 1 for my $key ( @columns ) {
181 5 my $value = $values->{$key};
182  
183 5 ▶ if ( !defined($value) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $value) { } 1 4 5
elsif (ref $value eq 'SCALAR') { } 1 3 4
184 1 push @values, 'NULL';
185 }
186 elsif ( ref($value) eq 'SCALAR' ) {
187 1 push @values, ${$value};
188 }
189 else {
190 3 push @values, '?';
191 3 push @binds, $value;
192 }
193 }
194  
195 1 push @parts, '(' . join( ',', @columns ) . ')';
196 1 push @parts, 'VALUES (' . join( ',', @values ) . ')';
197 1 my $sql = join( ' ', @parts );
198  
199 1 return ( $sql, \@binds );
200 }
201  
202  
203 =head2 sql_update( $table, \%values, \%where )
204  
205 Creates simple UPDATE calls and binds.
206  
207 Calling:
208  
209 my ($sql,$binds) = sql_update(
210 'users',
211 {
212 status => 'X',
213 lockdate => undef,
214 },
215 {
216 orderdate => \'SYSDATE()',
217 },
218 );
219  
220 returns:
221  
222 $sql = 'UPDATE users SET lockdate=NULL, status=? WHERE orderdate=SYSDATE()'
223 $binds = [ 'X' ]
224  
225 =cut
226  
227 sub sql_update {
228 3 ▶ my $table = shift;
Subroutine sql_update: called CC=4 SCAR=14.0
Pod documented
229 3 my $values = shift;
230 3 my $where = shift;
231  
232 3 my @parts = (
233 "UPDATE $table"
234 );
235  
236 3 my @columns;
237 my @binds;
238  
239 3 for my $key ( sort keys %{$values} ) {
240 4 my $value = $values->{$key};
241  
242 4 ▶ if ( !defined($value) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $value) { } 1 3 4
elsif (ref $value eq 'SCALAR') { } 0 3 3
243 1 push @columns, "$key=NULL";
244 }
245 elsif ( ref($value) eq 'SCALAR' ) {
246 0 push @columns, "$key=${$value}";
247 }
248 else {
249 3 push @columns, "$key=?";
250 3 push @binds, $value;
251 }
252 }
253 3 push @parts, 'SET ' . join( ', ', @columns );
254  
255 3 _build_where_section( \@parts, $where, \@binds );
256  
257 3 my $sql = join( ' ', @parts );
258  
259 3 return ( $sql, \@binds );
260 }
261  
262  
263 =head2 sql_delete( $table, \%where )
264  
265 Creates simple DELETE calls and binds.
266  
267 Calling:
268  
269 my ($sql,$binds) = sql_delete(
270 'users',
271 {
272 serialno => 12345,
273 height => undef,
274 date_added => \'SYSDATE()',
275 status => [qw( X Y Z )],
276 },
277 );
278  
279 returns:
280  
281 $sql = 'DELETE FROM users WHERE date_added = SYSDATE() AND height IS NULL AND serialno = ? AND status IN (?,?,?)'
282 $binds = [ 12345, 'X', 'Y', 'Z' ]
283  
284 =cut
285  
286 sub sql_delete {
287 3 ▶ my $table = shift;
Subroutine sql_delete: called CC=1 SCAR=0.0
Pod documented
288 3 my $where = shift;
289  
290 3 my @parts = (
291 "DELETE FROM $table"
292 );
293  
294 3 my @binds;
295  
296 3 _build_where_section( \@parts, $where, \@binds );
297  
298 3 my $sql = join( ' ', @parts );
299  
300 3 return ( $sql, \@binds );
301 }
302  
303  
304 sub _build_where_section {
305 12 ▶ my $parts = shift;
Subroutine _build_where_section: called CC=6 SCAR=17.9
306 12 my $where = shift;
307 12 my $binds = shift;
308  
309 12 my @conditions;
310 12 for my $key ( sort keys %{$where} ) {
311 15 my $value = $where->{$key};
312 15 ▶ if ( !defined($value) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $value) { } 2 13 15
elsif (ref $value eq 'ARRAY') { } 7 6 13
elsif (ref $value eq 'SCALAR') { } 3 3 6
313 2 push @conditions, "$key IS NULL";
314 }
315 elsif ( ref($value) eq 'ARRAY' ) {
316 7 push @conditions, "$key IN (" . join( ',', ('?') x @{$value} ) . ')';
317 7 push @{$binds}, @{$value};
318 }
319 elsif ( ref($value) eq 'SCALAR' ) {
320 3 push @conditions, "$key=${$value}";
321 }
322 else {
323 3 push @conditions, "$key=?";
324 3 push @{$binds}, $value;
325 }
326 }
327  
328 12 ▶ if ( @conditions ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@conditions) 8 4 12
329 8 push @{$parts}, 'WHERE ' . join( ' AND ', @conditions );
330 }
331  
332 12 return;
333 }
334  
335  
336 sub _build_by_section {
337 12 ▶ my $parts = shift;
Subroutine _build_by_section: called CC=3 SCAR=11.0
338 12 my $section = shift;
339 12 my $columns = shift;
340  
341 12 ▶ if ( $columns ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($columns) 5 7 12
342 5 ▶ if ( ref($columns) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $columns eq 'ARRAY') { } 3 2 5
343 3 push @{$parts}, $section . ' ' . join( ',', @{$columns} );
344 }
345 else {
346 2 push @{$parts}, "$section $columns";
347 }
348 }
349  
350 12 return;
351 }
352  
353  
354 =head1 AUTHOR
355  
356 Andy Lester, C<< <andy at petdance.com> >>
357  
358 =head1 BUGS
359  
360 Please report any bugs or feature requests to
361 L<https://github.com/petdance/sql-simple/issues>, or email me directly.
362  
363 =head1 SUPPORT
364  
365 You can find documentation for this module with the perldoc command.
366  
367 perldoc SQL::Tiny
368  
369 You can also look for information at:
370  
371 =over 4
372  
373 =item * MetaCPAN
374  
375 L<https://metacpan.org/release/SQL-Tiny>
376  
377 =item * GitHub issue tracker
378  
379 L<https://github.com/petdance/sql-simple/issues>
380  
381 =back
382  
383 =head1 ACKNOWLEDGEMENTS
384  
385 Thanks to the following folks for their contributions:
386 Tim Heaney.
387  
388 =head1 LICENSE AND COPYRIGHT
389  
390 Copyright 2019 Andy Lester.
391  
392 This program is free software; you can redistribute it and/or modify it
393 under the terms of the the Artistic License (2.0). You may obtain a
394 copy of the full license at:
395  
396 L<http://www.perlfoundation.org/artistic_license_2_0>
397  
398 =cut
399  
400 5 1; # End of SQL::Tiny
↑ Summary