Greg Sabino Mullane wrote:
there are some modules out there that attempt to deconstruct SQL
commands that might be adapted to the task
As, for example SQL::Statement which would catch a few things your hack
wouldn't - e.g. embedded escaped quotes in values, or INSERT statements
that don't specify the column names (which are valid even if unwise).
#!/usr/bin/perl -w
use strict;
use SQL::Statement;
my $sql = q{ INSERT INTO MyTbl (col1,col2) VALUES (7,'o''brian') };
my ($pholder_sql,@vals) = make_pholders( $sql );
print "$pholder_sql [EMAIL PROTECTED]";
# output : INSERT INTO MYTBL (COL1,COL2) VALUES (?,?) [7 o'brian]
sub make_pholders {
my($sql) = @_;
my $stmt = SQL::Statement->new( $sql );
my($table) = map {$_->name} $stmt->tables;
my($columns) = join ',',map {$_->name} $stmt->columns;
my(@values) = $stmt->row_values;
my $pholder = join ',',('?')[EMAIL PROTECTED];
my $pholder_sql = "INSERT INTO $table ($columns) VALUES ($pholder)";
return $pholder_sql,@values;
}
__END__
--
Jeff