Add "TABLE" to list of keywords to recognize in addPrefixes()

This commit is contained in:
Kijin Sung 2025-09-30 22:27:36 +09:00
parent 5dcc0f92a4
commit 653ca4f697
2 changed files with 19 additions and 3 deletions

View file

@ -1126,17 +1126,17 @@ class DB
} }
else else
{ {
$exceptions = []; $exceptions = ['TABLE'];
} }
// Add prefixes to all other table names in the query string. // Add prefixes to all other table names in the query string.
return preg_replace_callback('/\b((?:DELETE\s+)?FROM|JOIN|INTO|(?<!KEY\s)UPDATE)(?i)\s+((?:`?\w+`?)(?:\s+AS\s+`?\w+`?)?(?:\s*,\s*(?:`?\w+\`?)(?:\s+AS\s+`?\w+`?)?)*)/', function($m) use($exceptions) { return preg_replace_callback('/\b((?:DELETE\s+)?FROM|JOIN|INTO(?: TABLE)?|TABLE|(?<!KEY\s)UPDATE)(?i)\s+((?:`?\w+`?)(?:\s+AS\s+`?\w+`?)?(?:\s*,\s*(?:`?\w+\`?)(?:\s+AS\s+`?\w+`?)?)*)/', function($m) use($exceptions) {
$type = strtoupper($m[1]); $type = strtoupper($m[1]);
$tables = array_map(function($str) use($type, $exceptions) { $tables = array_map(function($str) use($type, $exceptions) {
return preg_replace_callback('/`?(\w+)`?(?:\s+AS\s+`?(\w+)`?)?/i', function($m) use($type, $exceptions) { return preg_replace_callback('/`?(\w+)`?(?:\s+AS\s+`?(\w+)`?)?/i', function($m) use($type, $exceptions) {
if (count($exceptions) && in_array($m[1], $exceptions)) if (count($exceptions) && in_array($m[1], $exceptions))
{ {
return isset($m[2]) ? sprintf('`%s` AS `%s`', $m[1], $m[2]) : sprintf('`%s`', $m[1]); return isset($m[2]) ? sprintf('`%s` AS `%s`', $m[1], $m[2]) : (ctype_upper($m[1]) ? $m[1] : sprintf('`%s`', $m[1]));
} }
elseif ($type === 'FROM' || $type === 'JOIN') elseif ($type === 'FROM' || $type === 'JOIN')
{ {

View file

@ -175,6 +175,22 @@ class DBTest extends \Codeception\Test\Unit
$target = 'INSERT INTO `' . $prefix . 'documents` (a, b, c) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE b = ?, c = ?'; $target = 'INSERT INTO `' . $prefix . 'documents` (a, b, c) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE b = ?, c = ?';
$this->assertEquals($target, $oDB->addPrefixes($source)); $this->assertEquals($target, $oDB->addPrefixes($source));
$source = "LOAD DATA LOCAL INFILE '/tmp/foo.csv' INTO TABLE foo_table FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\\n' (a, b, c)";
$target = "LOAD DATA LOCAL INFILE '/tmp/foo.csv' INTO TABLE `" . $prefix . "foo_table` FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\\n' (a, b, c)";
$this->assertEquals($target, $oDB->addPrefixes($source));
$source = 'ALTER TABLE documents ADD INDEX idx_foo (a, b)';
$target = 'ALTER TABLE `' . $prefix . 'documents` ADD INDEX idx_foo (a, b)';
$this->assertEquals($target, $oDB->addPrefixes($source));
$source = 'TRUNCATE TABLE documents';
$target = 'TRUNCATE TABLE `' . $prefix . 'documents`';
$this->assertEquals($target, $oDB->addPrefixes($source));
$source = 'DROP TABLE documents';
$target = 'DROP TABLE `' . $prefix . 'documents`';
$this->assertEquals($target, $oDB->addPrefixes($source));
$source = 'update documents set a = ?, b = ? where c = ?'; $source = 'update documents set a = ?, b = ? where c = ?';
$this->assertEquals($source, $oDB->addPrefixes($source)); $this->assertEquals($source, $oDB->addPrefixes($source));