Created
August 20, 2010 14:56
-
-
Save zigorou/540485 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package SQL::BindedStatement; | |
| use strict; | |
| use warnings; | |
| use Carp; | |
| use Exporter qw(import); | |
| use Data::Util qw(is_number is_string is_array_ref neat); | |
| use Data::Validate qw(is_printable); | |
| use DBI qw(:sql_types); | |
| use SQL::Tokenizer qw(tokenize_sql); | |
| our $VERSION = '0.01'; | |
| our @EXPORT_OK = qw(binded_statement); | |
| sub binded_statement { | |
| shift if $_[0] eq __PACKAGE__; | |
| my ( $dbh, $stmt, $bind ) = @_; | |
| my $bind_index = 0; | |
| my @tokens = tokenize_sql( $stmt ); | |
| for ( my $i = 0, my $l = scalar @tokens; $i < $l; $i++ ) { | |
| my $token = $tokens[$i]; | |
| next unless ( $token eq '?' ); | |
| my $value = $bind->[$bind_index++]; | |
| if ( is_array_ref $value ) { | |
| $tokens[$i] = $dbh->quote( splice(@$value, 0, 2) ); | |
| } | |
| elsif ( is_number $value ) { | |
| $tokens[$i] = $value; | |
| } | |
| elsif ( is_string $value ) { | |
| $tokens[$i] = $dbh->quote( $value, SQL_VARCHAR ); | |
| } | |
| else { | |
| croak sprintf('Unknown bind parameter: %s', neat $value); | |
| } | |
| } | |
| join '' => @tokens; | |
| } | |
| 1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment