-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
628 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Schema\Name; | ||
|
||
use Doctrine\DBAL\Schema\Name\Parser\Exception; | ||
use Doctrine\DBAL\Schema\Name\Parser\Exception\ExpectedDot; | ||
use Doctrine\DBAL\Schema\Name\Parser\Exception\ExpectedNextIdentifier; | ||
use Doctrine\DBAL\Schema\Name\Parser\Exception\UnableToParseIdentifier; | ||
use Doctrine\DBAL\Schema\Name\Parser\Identifier; | ||
|
||
use function assert; | ||
use function preg_match; | ||
use function str_replace; | ||
use function strlen; | ||
|
||
/** | ||
* Parses a qualified or unqualified SQL-like name. | ||
* | ||
* A name can be either unqualified or qualified: | ||
* - An unqualified name consists of a single identifier. | ||
* - A qualified name is a sequence of two or more identifiers separated by dots. | ||
* | ||
* An identifier can be quoted or unquoted: | ||
* - A quoted identifier is enclosed in double quotes ("), backticks (`), or square brackets ([]). | ||
* The closing quote character can be escaped by doubling it. | ||
* - An unquoted identifier may contain any character except whitespace, dots, or any of the quote characters. | ||
* | ||
* Differences from SQL: | ||
* 1. Identifiers that are reserved keywords or start with a digit do not need to be quoted. | ||
* 2. Whitespace is not allowed between identifiers. | ||
* | ||
* @internal | ||
*/ | ||
final class Parser | ||
{ | ||
private const IDENTIFIER_PATTERN = <<<'PATTERN' | ||
/\G | ||
(?: | ||
"(?<ansi>[^"]*(?:""[^"]*)*)" # ANSI SQL double-quoted | ||
| `(?<mysql>[^`]*(?:``[^`]*)*)` # MySQL-style backtick-quoted | ||
| \[(?<sqlserver>[^]]*(?:]][^]]*)*)] # SQL Server-style square-bracket-quoted | ||
| (?<unquoted>[^\s."`\[\]]+) # Unquoted | ||
) | ||
/x | ||
PATTERN; | ||
|
||
/** | ||
* @return list<Identifier> | ||
* | ||
* @throws Exception | ||
*/ | ||
public function parse(string $input): array | ||
{ | ||
if ($input === '') { | ||
return []; | ||
} | ||
|
||
$offset = 0; | ||
$identifiers = []; | ||
$length = strlen($input); | ||
|
||
while (true) { | ||
if ($offset >= $length) { | ||
throw ExpectedNextIdentifier::new(); | ||
} | ||
|
||
if (preg_match(self::IDENTIFIER_PATTERN, $input, $matches, 0, $offset) === 0) { | ||
throw UnableToParseIdentifier::new($offset); | ||
} | ||
|
||
if (isset($matches['ansi']) && strlen($matches['ansi']) > 0) { | ||
$identifier = Identifier::quoted(str_replace('""', '"', $matches['ansi'])); | ||
} elseif (isset($matches['mysql']) && strlen($matches['mysql']) > 0) { | ||
$identifier = Identifier::quoted(str_replace('``', '`', $matches['mysql'])); | ||
} elseif (isset($matches['sqlserver']) && strlen($matches['sqlserver']) > 0) { | ||
$identifier = Identifier::quoted(str_replace(']]', ']', $matches['sqlserver'])); | ||
} else { | ||
assert(isset($matches['unquoted']) && strlen($matches['unquoted']) > 0); | ||
$identifier = Identifier::unquoted($matches['unquoted']); | ||
} | ||
|
||
$identifiers[] = $identifier; | ||
|
||
$offset += strlen($matches[0]); | ||
|
||
if ($offset >= $length) { | ||
break; | ||
} | ||
|
||
$character = $input[$offset]; | ||
|
||
if ($character !== '.') { | ||
throw ExpectedDot::new($offset, $character); | ||
} | ||
|
||
$offset++; | ||
} | ||
|
||
return $identifiers; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Schema\Name\Parser; | ||
|
||
use Throwable; | ||
|
||
interface Exception extends Throwable | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Schema\Name\Parser\Exception; | ||
|
||
use Doctrine\DBAL\Schema\Name\Parser\Exception; | ||
use LogicException; | ||
|
||
use function sprintf; | ||
|
||
/** @internal */ | ||
class ExpectedDot extends LogicException implements Exception | ||
{ | ||
public static function new(int $position, string $got): self | ||
{ | ||
return new self(sprintf('Expected dot at position %d, got "%s".', $position, $got)); | ||
} | ||
} |
Oops, something went wrong.