Skip to content

Commit

Permalink
Fix quoting reserved words
Browse files Browse the repository at this point in the history
  • Loading branch information
paranoiq committed Oct 19, 2023
1 parent 1e32292 commit 2f11f61
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
5 changes: 3 additions & 2 deletions sources/Formatter/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ public function formatName(string $name): string
if ($name === '*') {
return '*';
}
$quote = $this->session->getMode()->containsAny(SqlMode::ANSI_QUOTES) ? '"' : '`';
$sqlMode = $this->session->getMode();
$quote = $sqlMode->containsAny(SqlMode::ANSI_QUOTES) ? '"' : '`';
$name = str_replace($quote, $quote . $quote, $name);

$needsQuoting = $this->quoteAllNames
Expand All @@ -120,7 +121,7 @@ public function formatName(string $name): string
|| 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 _
|| $this->session->getPlatform()->isReserved($name);

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

Expand Down
4 changes: 4 additions & 0 deletions sources/Platform/Platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ public function getFeatures(): array

public function isReserved(string $word): bool
{
$word = strtoupper($word);

return in_array($word, $this->getReserved(), true);
}

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

public function isKeyword(string $word, int $version): bool
{
$word = strtoupper($word);

return in_array($word, $this->getReserved(), true) || in_array($word, $this->getNonReserved(), true);
}

Expand Down
13 changes: 13 additions & 0 deletions tests/Parser/ReservedWordsQuoting.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types = 1);

// phpcs:disable SlevomatCodingStandard.Functions.RequireSingleLineCall

namespace SqlFtw\Parser;

use SqlFtw\Tests\Assert;

require __DIR__ . '/../bootstrap.php';


Assert::parseSerialize("CREATE TABLE occurrence (`empty` TINYINT(1) NOT NULL DEFAULT 0)");
Assert::parseSerialize("CREATE TABLE occurrence (`EMPTY` TINYINT(1) NOT NULL DEFAULT 0)");

0 comments on commit 2f11f61

Please sign in to comment.