diff --git a/dbdimp.c b/dbdimp.c index 4039d76..084f9dd 100755 --- a/dbdimp.c +++ b/dbdimp.c @@ -80,6 +80,8 @@ count_params(char *statement) if (c == '\\') if (! *ptr) continue; + else + ++ptr; ++ptr; } @@ -92,6 +94,15 @@ count_params(char *statement) ++num_params; break; + case '-': + /* Skip comment */ + if (*ptr == '-') { + while (*ptr && *ptr != '\n') { + ++ptr; + } + } + break; + default: break; } @@ -517,6 +528,16 @@ static char *parse_params( } break; + case '-': + /* Skip comment */ + { + if (*(++statement_ptr) == '-') { + while (*(statement_ptr) && *(statement_ptr) != '\n') + ++statement_ptr; + } + } + break; + case '?': /* Insert parameter */ statement_ptr++; diff --git a/t/comments.t b/t/comments.t new file mode 100755 index 0000000..ac5c5af --- /dev/null +++ b/t/comments.t @@ -0,0 +1,72 @@ +# -*- cperl -*- +# Test special characters inside comments +# http://bugs.debian.org/311040 +# http://bugs.mysql.com/27625 + +use strict; +use DBI (); + +use vars qw($test_dsn $test_user $test_password $state); +require "t/lib.pl"; + +while (Testing()) { + my ($dbh, $sth); + # + # Connect to the database + Test($state or + ($dbh = DBI->connect($test_dsn, $test_user, $test_password, + {RaiseError => 0}))); + + my $q; + + # + # Placeholder inside a comment after a newline + # + $q = " -- Does the question mark at the end confuse DBI::MySQL?\nselect ?"; + + Test($state or ($sth = $dbh->prepare($q))); + Test($state or ($sth->execute(42))); + Test($state or ($sth->{ParamValues})); + Test($state or ($sth->finish)); + + # + # Placeholder inside a comment before a newline + # + $q = "select ?\n-- Does the question mark at the end confuse DBI::MySQL?"; + + Test($state or ($sth = $dbh->prepare($q))); + Test($state or ($sth->execute(42))); + Test($state or ($sth->{ParamValues})); + Test($state or ($sth->finish)); + + # + # Quote inside a string + # + my $table = "test"; + + Test($state or $dbh->do("CREATE TABLE $table (a INTEGER)")); + Test($state or $dbh->do("INSERT INTO $table (a) VALUES (1)")); + + $q = "SELECT 'a\\'b' FROM $table WHERE true or a = ?"; + + Test($state or ($sth = $dbh->prepare($q))); + Test($state or ($sth->execute(42))); + Test($state or ($sth->{ParamValues})); + Test($state or ($sth->finish)); + + Test($state or $dbh->do("DROP TABLE $table")); + + # + # Quote inside a comment + # + $q = " -- 'Tis the quote that confuses DBI::MySQL\nSELECT ?"; + + Test($state or ($sth = $dbh->prepare($q))); + Test($state or ($sth->execute(42))); + Test($state or ($sth->{ParamValues})); + Test($state or ($sth->finish)); + + # + # Close the database connection + Test($state or ($dbh->disconnect() or 1)); +}