File Coverage

blib/lib/DBIx/Printf/Named.pm
Criterion Covered Total %
statement 45 46 97.8
branch 23 26 88.4
condition 5 7 71.4
subroutine 10 10 100.0
pod n/a
total 83 89 93.2


line stmt bran cond sub pod time code
1 1     1   6 use strict;
  1         2  
  1         44  
2 1     1   6 use warnings;
  1         2  
  1         209  
3              
4 1     1   4181 use DBI;
  1         35980  
  1         359  
5 1     1   3959 use Carp::Clan;
  1         8374  
  1         7  
6              
7             package DBIx::Printf::Named;
8              
9 1     1   1696 use Regexp::Common qw /balanced/;
  1         3842  
  1         5  
10              
11             our $VERSION = '0.01';
12              
13             sub _printf {
14 29     29   47 my ($dbh, $fmt, $params, $in_like, $like_escape) = @_;
15 29         483 my $re = $RE{balanced}{-parens=>'()'}{-keep};
16 29         1703 $fmt =~ s/\%(?:\((.+?)\)([dfst])|like$re((?i)\s+ESCAPE\s+(['"])(.*?)\5(?:\s+|$))?|(\%))/
17 36 100 66     2980 _printf_quote({
18             dbh => $dbh,
19             params => $params,
20             key => $1,
21             type => $2 || ( $7 || 'like'),
22             like_fmt => $3,
23             like_escape => $4,
24             like_escape_char => defined $like_escape ? $like_escape : $6,
25             in_like => $in_like,
26             })
27             /eg;
28 28         1594 $fmt;
29             }
30              
31             sub _printf_quote {
32 36     36   41 my $in = shift;
33              
34 36 100       118 if ($in->{type} eq '%') {
    100          
35 8         25 return '%';
36             } elsif ($in->{type} eq 'like') {
37 7 50       57 $in->{like_fmt} =~ s/(:?^\(|\)$)//g if $in->{like_fmt};
38 7   100     23 return "'"
39             . _printf($in->{dbh}, $in->{like_fmt}, $in->{params}, 1, $in->{like_escape_char})
40             . "'" . ($in->{like_escape} || '');
41             }
42 21   50     38 $in->{params} ||= {};
43 21 100       52 Carp::Clan::croak "$in->{key} is not exists in parameters"
44             if ! exists $in->{params}->{$in->{key}};
45              
46 20         53 return _printf_quote_simple(
47             $in->{dbh}, $in->{type}, $in->{params}->{$in->{key}}, $in->{in_like}, $in->{like_escape_char}
48             );
49             }
50              
51             sub _printf_quote_simple {
52 1     1   2590 no warnings;
  1         2  
  1         320  
53 20     20   36 my ($dbh, $type, $param, $in_like, $like_escape_char) = @_;
54            
55 20 100       53 if ($type eq 'd') {
    100          
    100          
    50          
56 7         14 $param = int($param);
57             } elsif ($type eq 'f') {
58 4         8 $param = $param + 0;
59             } elsif ($type eq 's') {
60 8 100       15 if ($in_like) {
61 6 100       12 my $escape_char = defined $like_escape_char ? $like_escape_char : '\\';
62 6         82 $param =~ s/[${escape_char}%_]/$escape_char$&/g;
63             }
64 8         72 $param = $dbh->quote($param);
65 8 100       145 if ($in_like) {
66 6 50       45 $param =~ s/^'(.*)'$/$1/s
67             or Carp::Clan::croak "unexpected quote char: $param\n";
68             }
69             } elsif ($type eq 't') {
70             # pass thru
71             } else {
72 0         0 Carp::Clan::croak "unexpected type: $type\n";
73             }
74            
75 20         72 $param;
76             }
77              
78             package main;
79              
80             sub DBI::db::nprintf {
81 22     22   51 my ($dbh, $fmt, $params) = @_;
82            
83 22         45 my $sql = DBIx::Printf::Named::_printf($dbh, $fmt, $params);
84 21         117 $sql;
85             }
86              
87             1;
88              
89             1;
90             __END__