File Coverage

blib/lib/DBIx/Printf.pm
Criterion Covered Total %
statement 42 46 91.3
branch 25 30 83.3
condition 4 4 100.0
subroutine 9 9 100.0
pod n/a
total 80 89 89.8


line stmt bran cond sub pod time code
1 1     1   24251 use strict;
  1         2  
  1         40  
2 1     1   6 use warnings;
  1         2  
  1         30  
3              
4 1     1   22345 use DBI;
  1         38139  
  1         77  
5 1     1   1005 use Carp::Clan;
  1         4242  
  1         5  
6              
7             package DBIx::Printf;
8              
9             our $VERSION = '0.08';
10              
11             sub _printf {
12 30     30   52 my ($dbh, $fmt, $params, $in_like, $like_escape) = @_;
13            
14 30         167 $fmt =~ s/\%(?:([dfst\%])|like\((.*?)\)((?i)\s+ESCAPE\s+(['"])(.*?)\4(?:\s+|$))?)/
15 36 100 100     383 _printf_quote({
16             dbh => $dbh,
17             params => $params,
18             type => $1 || 'like',
19             like_fmt => $2,
20             like_escape => $3,
21             like_escape_char => defined $like_escape ? $like_escape : $5,
22             in_like => $in_like,
23             })
24             /eg;
25 29         192 $fmt;
26             }
27              
28             sub _printf_quote {
29 36     36   86 my $in = shift;
30 36         39 my $out;
31            
32 36 100       124 if ($in->{type} eq '%') {
    100          
33 8         109 return '%';
34             } elsif ($in->{type} eq 'like') {
35 7   100     26 return "'"
36             . _printf(
37             $in->{dbh},
38             $in->{like_fmt},
39             $in->{params},
40             1,
41             $in->{like_escape_char},
42             ) . "'" . ($in->{like_escape} || '');
43             }
44            
45 21         132 return _printf_quote_simple(
46             $in->{dbh},
47             $in->{type},
48             $in->{params},
49             $in->{in_like},
50             $in->{like_escape_char}
51             );
52             }
53              
54             sub _printf_quote_simple {
55 1     1   409 no warnings;
  1         2  
  1         362  
56 21     21   121 my ($dbh, $type, $params, $in_like, $like_escape_char) = @_;
57            
58 21 100       48 Carp::Clan::croak "too few parameters\n" unless @$params;
59 20         110 my $param = shift @$params;
60            
61 20 100       69 if ($type eq 'd') {
    100          
    50          
    100          
    50          
62 8         15 $param = int($param);
63             } elsif ($type eq 'f') {
64 4         11 $param = $param + 0;
65             } elsif ($type eq 'l') {
66 0         0 $param = s/[\%_]/\\$1/g;
67 0         0 $param = $dbh->quote($param); # be paranoiac, use DBI::db::quote
68 0 0       0 $param =~ s/^'(.*)'$/$1/s
69             or Carp::Clan::croak "unexpected quote char used: $param\n";
70             } elsif ($type eq 's') {
71 7 100       18 if ($in_like) {
72 6 100       15 my $escape_char = defined $like_escape_char ? $like_escape_char : '\\';
73 6         88 $param =~ s/[${escape_char}%_]/$escape_char$&/g;
74             }
75 7         140 $param = $dbh->quote($param);
76 7 100       83 if ($in_like) {
77 6 50       43 $param =~ s/^'(.*)'$/$1/s
78             or Carp::Clan::croak "unexpected quote char: $param\n";
79             }
80             } elsif ($type eq 't') {
81             # pass thru
82             } else {
83 0         0 Carp::Clan::croak "unexpected type: $type\n";
84             }
85            
86 20         96 $param;
87             }
88              
89             package main;
90              
91             sub DBI::db::printf {
92 23     23   498 my ($dbh, $fmt, @params) = @_;
93            
94 23         135 my $sql = DBIx::Printf::_printf($dbh, $fmt, \@params);
95 22 100       58 Carp::Clan::croak "too many parameters\n" if @params;
96 21         106 $sql;
97             }
98              
99             1;
100              
101             __END__