Skip to content

Commit 2f11f61

Browse files
committed
Fix quoting reserved words
1 parent 1e32292 commit 2f11f61

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

sources/Formatter/Formatter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ public function formatName(string $name): string
111111
if ($name === '*') {
112112
return '*';
113113
}
114-
$quote = $this->session->getMode()->containsAny(SqlMode::ANSI_QUOTES) ? '"' : '`';
114+
$sqlMode = $this->session->getMode();
115+
$quote = $sqlMode->containsAny(SqlMode::ANSI_QUOTES) ? '"' : '`';
115116
$name = str_replace($quote, $quote . $quote, $name);
116117

117118
$needsQuoting = $this->quoteAllNames
@@ -120,7 +121,7 @@ public function formatName(string $name): string
120121
|| preg_match('~[\pC\pM\pS\pZ\p{Pd}\p{Pe}\p{Pf}\p{Pi}\p{Po}\p{Ps}]~u', ltrim($name, '@')) !== 0 // contains control, mark, symbols, whitespace, punctuation except _
121122
|| $this->session->getPlatform()->isReserved($name);
122123

123-
if ($needsQuoting && !$this->session->getMode()->containsAny(SqlMode::NO_BACKSLASH_ESCAPES)) {
124+
if ($needsQuoting && !$sqlMode->containsAny(SqlMode::NO_BACKSLASH_ESCAPES)) {
124125
$name = str_replace($this->escapeKeys, $this->escapeValues, $name);
125126
}
126127

sources/Platform/Platform.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ public function getFeatures(): array
309309

310310
public function isReserved(string $word): bool
311311
{
312+
$word = strtoupper($word);
313+
312314
return in_array($word, $this->getReserved(), true);
313315
}
314316

@@ -328,6 +330,8 @@ public function getReserved(): array
328330

329331
public function isKeyword(string $word, int $version): bool
330332
{
333+
$word = strtoupper($word);
334+
331335
return in_array($word, $this->getReserved(), true) || in_array($word, $this->getNonReserved(), true);
332336
}
333337

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types = 1);
2+
3+
// phpcs:disable SlevomatCodingStandard.Functions.RequireSingleLineCall
4+
5+
namespace SqlFtw\Parser;
6+
7+
use SqlFtw\Tests\Assert;
8+
9+
require __DIR__ . '/../bootstrap.php';
10+
11+
12+
Assert::parseSerialize("CREATE TABLE occurrence (`empty` TINYINT(1) NOT NULL DEFAULT 0)");
13+
Assert::parseSerialize("CREATE TABLE occurrence (`EMPTY` TINYINT(1) NOT NULL DEFAULT 0)");

0 commit comments

Comments
 (0)