Changeset 360 for lang/perl/DBIx-Printf

Show
Ignore:
Timestamp:
10/04/07 13:52:31 (6 years ago)
Author:
kazuho
Message:

add %like(fmt) placeholder
update Changes

Location:
lang/perl/DBIx-Printf/trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/DBIx-Printf/trunk/Changes

    r359 r360  
    11Revision history for Perl extension DBIx::Printf. 
     2 
     30.05  Thu Oct 04 13:55:00 2007 
     4        - added %t (passthru) placeholder 
     5        - added %like(fmt) placeholder 
    26 
    370.04  Sat Sep 29 05:40:00 2007 
  • lang/perl/DBIx-Printf/trunk/lib/DBIx/Printf.pm

    r359 r360  
    77 
    88sub DBI::db::printf { 
    9     my ($self, $base, @params) = @_; 
     9    my ($dbh, $fmt, @params) = @_; 
    1010     
    11     $base =~ s/\%(d|f|s|t|\%)/ 
    12         DBIx::Printf::_printf_quote($self, $1, \@params) 
    13                 /eg; 
     11    my $sql = DBIx::Printf::_printf($dbh, $fmt, \@params); 
    1412    die "too many parameters\n" if @params; 
    15     $base; 
     13    $sql; 
    1614} 
    1715 
     
    2018our $VERSION = 0.05; 
    2119 
     20sub _printf { 
     21    my ($dbh, $fmt, $params, $in_like) = @_; 
     22     
     23    $fmt =~ s/\%(?:([dfst\%])|like\((.*?)\))/ 
     24        _printf_quote({ 
     25            dbh      => $dbh, 
     26            params   => $params, 
     27            type     => $1 || 'like', 
     28            like_fmt => $2, 
     29            in_like  => $in_like, 
     30        }) 
     31            /eg; 
     32    $fmt; 
     33} 
     34 
    2235sub _printf_quote { 
    23     my ($dbh, $type, $params) = @_; 
     36    my $in = shift; 
    2437    my $out; 
    2538     
    26     return '%' if $type eq '%'; 
     39    if ($in->{type} eq '%') { 
     40        return '%'; 
     41    } elsif ($in->{type} eq 'like') { 
     42        return "'" 
     43            . _printf($in->{dbh}, $in->{like_fmt}, $in->{params}, 1) 
     44                . "'"; 
     45    } 
    2746     
    28     return _printf_quote_simple($dbh, $type, $params); 
     47    return _printf_quote_simple( 
     48        $in->{dbh}, $in->{type}, $in->{params}, $in->{in_like} 
     49    ); 
    2950} 
    3051 
    3152sub _printf_quote_simple { 
    3253    no warnings; 
    33     my ($dbh, $type, $params) = @_; 
     54    my ($dbh, $type, $params, $in_like) = @_; 
    3455     
    3556    die "too few parameters\n" unless @$params; 
     
    4061    } elsif ($type eq 'f') { 
    4162        $param = $param + 0; 
     63    } elsif ($type eq 'l') { 
     64        $param = s/[\%_]/\\$1/g; 
     65        $param = $dbh->quote($param); # be paranoiac, use DBI::db::quote 
     66        $param =~ s/^'(.*)'$/$1/s 
     67            or die "unexpected quote char used: $param\n"; 
    4268    } elsif ($type eq 's') { 
     69        if ($in_like) { 
     70            $param =~ s/[%_]/\\$&/g; 
     71        } 
    4372        $param = $dbh->quote($param); 
     73        if ($in_like) { 
     74            $param =~ s/^'(.*)'$/$1/s 
     75                or die "unexpected quote char: $param\n"; 
     76        } 
    4477    } elsif ($type eq 't') { 
    4578        # pass thru 
    4679    } else { 
    47         die "unknown type: $type\n"; 
     80        die "unexpected type: $type\n"; 
    4881    } 
    4982     
     
    79112Builds a SQL statement from given statement with placeholders and values.  Following placeholders are supported. 
    80113 
    81   %d - integer 
    82   %f - floating point 
    83   %s - string 
    84   %t - do not quote, pass thru 
     114  %d         - integer 
     115  %f         - floating point 
     116  %s         - string 
     117  %t         - do not quote, pass thru 
     118  %like(fmt) - formats and quotes a string for like expression 
     119 
     120=head3 %like example 
     121 
     122Below is an example of using the %%like placeholder.  Since metacharacters of supplied parameters are escaped, the example would always by a prefix search. 
     123 
     124  $dbh->printf('select * from t where name like %like(%s%%)', $name); 
     125 
    85126 
    86127=head1 AUTHOR 
  • lang/perl/DBIx-Printf/trunk/t/00base.t

    r359 r360  
    22use warnings; 
    33 
    4 use Test::More tests => 17; 
     4use Test::More tests => 20; 
    55 
    66BEGIN { use_ok('DBIx::Printf'); } 
     
    2828is($dbh->printf('select %d,%d,%d', 1, 2, 3), 'select 1,2,3', 'multiple args'); 
    2929 
     30is($dbh->printf('select 1 like %like()'), "select 1 like ''", 'empty %like'); 
     31is($dbh->printf('select 1 like %like(%s%%)', 'a'), "select 1 like 'a%'", '%like'); 
     32is($dbh->printf('select 1 like %like(%s%%)', "%a_b'"), "select 1 like '\\%a\\_b''%'", '%like escape check'); 
     33 
    3034undef $@; 
    3135eval {