From 258da14eea92ae6cede454e9e650044822953116 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Wed, 26 Dec 2018 12:13:07 +0100 Subject: [PATCH] Fix SQL syntax for statements that have "...limit = ?" Closes #287 --- dbdimp.c | 7 ++++--- t/35limit.t | 26 ++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/dbdimp.c b/dbdimp.c index cd544885..f442e38f 100644 --- a/dbdimp.c +++ b/dbdimp.c @@ -604,9 +604,9 @@ static char *parse_params( /* it would be good to be able to handle any number of cases and orders */ - if ((*statement_ptr == 'l' || *statement_ptr == 'L') && - (!strncmp(statement_ptr+1, "imit ", 5) || - !strncmp(statement_ptr+1, "IMIT ", 5))) + if (((*statement_ptr == ' ') || (*statement_ptr == '\n') || (*statement_ptr == '\t')) && + (!strncmp(statement_ptr+1, "limit ", 5) || + !strncmp(statement_ptr+1, "LIMIT ", 5))) { limit_flag = 1; } @@ -780,6 +780,7 @@ static char *parse_params( /* in case this is a nested LIMIT */ case ')': + case '=': limit_flag = 0; *ptr++ = *statement_ptr++; break; diff --git a/t/35limit.t b/t/35limit.t index 881d61d1..3eda05d2 100644 --- a/t/35limit.t +++ b/t/35limit.t @@ -19,15 +19,15 @@ eval {$dbh= DBI->connect($test_dsn, $test_user, $test_password, if ($@) { plan skip_all => "no database connection"; } -plan tests => 111; +plan tests => 115; ok(defined $dbh, "Connected to database"); ok($dbh->do("DROP TABLE IF EXISTS dbd_mysql_t35"), "making slate clean"); -ok($dbh->do("CREATE TABLE dbd_mysql_t35 (id INT(4), name VARCHAR(64))"), "creating table"); +ok($dbh->do("CREATE TABLE dbd_mysql_t35 (id INT(4), name VARCHAR(64), name_limit VARCHAR(64))"), "creating table"); -ok(($sth = $dbh->prepare("INSERT INTO dbd_mysql_t35 VALUES (?,?)"))); +ok(($sth = $dbh->prepare("INSERT INTO dbd_mysql_t35 VALUES (?,?,?)"))); for my $i (0..99) { my @chars = grep !/[0O1Iil]/, 0..9, 'A'..'Z', 'a'..'z'; @@ -35,7 +35,7 @@ for my $i (0..99) { # save these values for later testing $testInsertVals->{$i} = $random_chars; - ok(($rows = $sth->execute($i, $random_chars))); + ok(($rows = $sth->execute($i, $random_chars, $random_chars))); } ok($sth = $dbh->prepare("SELECT * FROM dbd_mysql_t35 LIMIT ?, ?"), @@ -51,6 +51,24 @@ ok(@$array_ref == 50); ok($sth->finish); +ok($dbh->do("UPDATE dbd_mysql_t35 SET name_limit = ? WHERE id = ?", undef, "updated_string", 1)); + +ok($dbh->do("UPDATE dbd_mysql_t35 SET name = ? WHERE name_limit > ?", undef, "updated_string", 999999)); + +# newline before LIMIT +ok($dbh->do(<<'SQL' +UPDATE dbd_mysql_t35 SET name = ? +LIMIT ? +SQL +, undef, "updated_string", 0)); + +# tab before LIMIT +ok($dbh->do(<<'SQL' + UPDATE dbd_mysql_t35 SET name = ? + LIMIT ? +SQL +, undef, "updated_string", 0)); + ok($dbh->do("DROP TABLE dbd_mysql_t35")); ok($dbh->disconnect);