Changeset 360 for lang/perl/DBIx-Printf
- Timestamp:
- 10/04/07 13:52:31 (6 years ago)
- Location:
- lang/perl/DBIx-Printf/trunk
- Files:
-
- 3 modified
-
Changes (modified) (1 diff)
-
lib/DBIx/Printf.pm (modified) (4 diffs)
-
t/00base.t (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/DBIx-Printf/trunk/Changes
r359 r360 1 1 Revision history for Perl extension DBIx::Printf. 2 3 0.05 Thu Oct 04 13:55:00 2007 4 - added %t (passthru) placeholder 5 - added %like(fmt) placeholder 2 6 3 7 0.04 Sat Sep 29 05:40:00 2007 -
lang/perl/DBIx-Printf/trunk/lib/DBIx/Printf.pm
r359 r360 7 7 8 8 sub DBI::db::printf { 9 my ($ self, $base, @params) = @_;9 my ($dbh, $fmt, @params) = @_; 10 10 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); 14 12 die "too many parameters\n" if @params; 15 $ base;13 $sql; 16 14 } 17 15 … … 20 18 our $VERSION = 0.05; 21 19 20 sub _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 22 35 sub _printf_quote { 23 my ($dbh, $type, $params) = @_;36 my $in = shift; 24 37 my $out; 25 38 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 } 27 46 28 return _printf_quote_simple($dbh, $type, $params); 47 return _printf_quote_simple( 48 $in->{dbh}, $in->{type}, $in->{params}, $in->{in_like} 49 ); 29 50 } 30 51 31 52 sub _printf_quote_simple { 32 53 no warnings; 33 my ($dbh, $type, $params ) = @_;54 my ($dbh, $type, $params, $in_like) = @_; 34 55 35 56 die "too few parameters\n" unless @$params; … … 40 61 } elsif ($type eq 'f') { 41 62 $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"; 42 68 } elsif ($type eq 's') { 69 if ($in_like) { 70 $param =~ s/[%_]/\\$&/g; 71 } 43 72 $param = $dbh->quote($param); 73 if ($in_like) { 74 $param =~ s/^'(.*)'$/$1/s 75 or die "unexpected quote char: $param\n"; 76 } 44 77 } elsif ($type eq 't') { 45 78 # pass thru 46 79 } else { 47 die "un knowntype: $type\n";80 die "unexpected type: $type\n"; 48 81 } 49 82 … … 79 112 Builds a SQL statement from given statement with placeholders and values. Following placeholders are supported. 80 113 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 122 Below 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 85 126 86 127 =head1 AUTHOR -
lang/perl/DBIx-Printf/trunk/t/00base.t
r359 r360 2 2 use warnings; 3 3 4 use Test::More tests => 17;4 use Test::More tests => 20; 5 5 6 6 BEGIN { use_ok('DBIx::Printf'); } … … 28 28 is($dbh->printf('select %d,%d,%d', 1, 2, 3), 'select 1,2,3', 'multiple args'); 29 29 30 is($dbh->printf('select 1 like %like()'), "select 1 like ''", 'empty %like'); 31 is($dbh->printf('select 1 like %like(%s%%)', 'a'), "select 1 like 'a%'", '%like'); 32 is($dbh->printf('select 1 like %like(%s%%)', "%a_b'"), "select 1 like '\\%a\\_b''%'", '%like escape check'); 33 30 34 undef $@; 31 35 eval {
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)