Skip to content

Commit 40bb6ff

Browse files
committed
Use new name parser in AbstractAsset::_setName()
1 parent 78d4cfc commit 40bb6ff

19 files changed

+464
-537
lines changed

src/Schema/AbstractAsset.php

Lines changed: 21 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,15 @@
55
namespace Doctrine\DBAL\Schema;
66

77
use Doctrine\DBAL\Platforms\AbstractPlatform;
8+
use Doctrine\DBAL\Schema\Exception\InvalidObjectName;
89
use Doctrine\DBAL\Schema\Name\Parser;
910
use Doctrine\DBAL\Schema\Name\Parser\Identifier;
10-
use Doctrine\Deprecations\Deprecation;
1111

1212
use function array_map;
1313
use function count;
1414
use function crc32;
1515
use function dechex;
16-
use function explode;
1716
use function implode;
18-
use function sprintf;
19-
use function str_contains;
2017
use function str_replace;
2118
use function strtolower;
2219
use function strtoupper;
@@ -43,51 +40,28 @@ abstract class AbstractAsset
4340
/** @var list<Identifier> */
4441
private array $identifiers = [];
4542

46-
private bool $validateFuture = false;
47-
4843
/**
4944
* Sets the name of this asset.
5045
*/
5146
protected function _setName(string $name): void
5247
{
53-
$input = $name;
54-
55-
if ($this->isIdentifierQuoted($name)) {
56-
$this->_quoted = true;
57-
$name = $this->trimQuotes($name);
58-
}
59-
60-
if (str_contains($name, '.')) {
61-
$parts = explode('.', $name);
62-
$this->_namespace = $parts[0];
63-
$name = $parts[1];
64-
}
65-
66-
$this->_name = $name;
67-
68-
$this->validateFuture = false;
69-
70-
if ($input !== '') {
48+
if ($name !== '') {
7149
$parser = new Parser();
7250

7351
try {
74-
$identifiers = $parser->parse($input);
52+
$identifiers = $parser->parse($name);
7553
} catch (Parser\Exception $e) {
76-
Deprecation::trigger(
77-
'doctrine/dbal',
78-
'https://github.com/doctrine/dbal/pull/6592',
79-
'Unable to parse object name: %s.',
80-
$e->getMessage(),
81-
);
82-
83-
return;
54+
throw InvalidObjectName::fromParserException($name, $e);
8455
}
8556
} else {
8657
$identifiers = [];
8758
}
8859

8960
switch (count($identifiers)) {
9061
case 0:
62+
$this->_name = '';
63+
$this->_quoted = false;
64+
$this->_namespace = null;
9165
$this->identifiers = [];
9266

9367
return;
@@ -102,43 +76,13 @@ protected function _setName(string $name): void
10276
break;
10377

10478
default:
105-
Deprecation::trigger(
106-
'doctrine/dbal',
107-
'https://github.com/doctrine/dbal/pull/6592',
108-
'An object name may consist of at most 2 identifiers (<namespace>.<name>), %d given.',
109-
count($identifiers),
110-
);
111-
112-
return;
113-
}
114-
115-
$this->identifiers = $identifiers;
116-
$this->validateFuture = true;
117-
118-
$futureName = $name->getValue();
119-
$futureNamespace = $namespace?->getValue();
120-
121-
if ($this->_name !== $futureName) {
122-
Deprecation::trigger(
123-
'doctrine/dbal',
124-
'https://github.com/doctrine/dbal/pull/6592',
125-
'Instead of "%s", this name will be interpreted as "%s" in 5.0',
126-
$this->_name,
127-
$futureName,
128-
);
79+
throw InvalidObjectName::tooManyQualifiers($name, count($identifiers) - 1);
12980
}
13081

131-
if ($this->_namespace === $futureNamespace) {
132-
return;
133-
}
134-
135-
Deprecation::trigger(
136-
'doctrine/dbal',
137-
'https://github.com/doctrine/dbal/pull/6592',
138-
'Instead of %s, the namespace in this name will be interpreted as %s in 5.0.',
139-
$this->_namespace !== null ? sprintf('"%s"', $this->_namespace) : 'null',
140-
$futureNamespace !== null ? sprintf('"%s"', $futureNamespace) : 'null',
141-
);
82+
$this->_name = $name->getValue();
83+
$this->_quoted = $name->isQuoted();
84+
$this->_namespace = $namespace?->getValue();
85+
$this->identifiers = $identifiers;
14286
}
14387

14488
/**
@@ -210,53 +154,22 @@ public function getName(): string
210154
}
211155

212156
/**
213-
* Gets the quoted representation of this asset but only if it was defined with one. Otherwise
214-
* return the plain unquoted value as inserted.
157+
* Returns the quoted representation of this asset's name. If the name is unquoted, it is normalized according to
158+
* the platform's unquoted name normalization rules.
215159
*/
216160
public function getQuotedName(AbstractPlatform $platform): string
217161
{
218-
$keywords = $platform->getReservedKeywordsList();
219-
$parts = $normalizedParts = [];
220-
221-
foreach (explode('.', $this->getName()) as $identifier) {
222-
$isQuoted = $this->_quoted || $keywords->isKeyword($identifier);
162+
$parts = array_map(static function (Identifier $identifier) use ($platform): string {
163+
$value = $identifier->getValue();
223164

224-
if (! $isQuoted) {
225-
$parts[] = $identifier;
226-
$normalizedParts[] = $platform->normalizeUnquotedIdentifier($identifier);
227-
} else {
228-
$parts[] = $platform->quoteSingleIdentifier($identifier);
229-
$normalizedParts[] = $identifier;
165+
if (! $identifier->isQuoted()) {
166+
$value = $platform->normalizeUnquotedIdentifier($value);
230167
}
231-
}
232168

233-
$name = implode('.', $parts);
234-
235-
if ($this->validateFuture) {
236-
$futureParts = array_map(static function (Identifier $identifier) use ($platform): string {
237-
$value = $identifier->getValue();
238-
239-
if (! $identifier->isQuoted()) {
240-
$value = $platform->normalizeUnquotedIdentifier($value);
241-
}
242-
243-
return $value;
244-
}, $this->identifiers);
245-
246-
if ($normalizedParts !== $futureParts) {
247-
Deprecation::trigger(
248-
'doctrine/dbal',
249-
'https://github.com/doctrine/dbal/pull/6592',
250-
'Relying on implicitly quoted identifiers preserving their original case is deprecated. '
251-
. 'The current name %s will become %s in 5.0. '
252-
. 'Please quote the name if the case needs to be preserved.',
253-
$name,
254-
implode('.', array_map([$platform, 'quoteSingleIdentifier'], $futureParts)),
255-
);
256-
}
257-
}
169+
return $platform->quoteSingleIdentifier($value);
170+
}, $this->identifiers);
258171

259-
return $name;
172+
return implode('.', $parts);
260173
}
261174

262175
/**

src/Schema/AbstractSchemaManager.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,7 @@ protected function _getPortableTableColumnList(string $table, string $database,
671671
foreach ($tableColumns as $tableColumn) {
672672
$column = $this->_getPortableTableColumnDefinition($tableColumn);
673673

674-
$name = strtolower($column->getQuotedName($this->platform));
675-
$list[$name] = $column;
674+
$list[strtolower($column->getName())] = $column;
676675
}
677676

678677
return $list;

src/Schema/Exception/IndexNameInvalid.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@
44

55
namespace Doctrine\DBAL\Schema\Exception;
66

7-
use Doctrine\DBAL\Schema\SchemaException;
8-
use InvalidArgumentException;
9-
107
use function sprintf;
118

129
/** @psalm-immutable */
13-
final class IndexNameInvalid extends InvalidArgumentException implements SchemaException
10+
final class IndexNameInvalid extends InvalidObjectName
1411
{
1512
public static function new(string $indexName): self
1613
{
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Schema\Exception;
6+
7+
use Doctrine\DBAL\Schema\Name\Parser;
8+
use Doctrine\DBAL\Schema\SchemaException;
9+
use InvalidArgumentException;
10+
11+
use function sprintf;
12+
13+
/** @psalm-immutable */
14+
class InvalidObjectName extends InvalidArgumentException implements SchemaException
15+
{
16+
public static function fromParserException(string $name, Parser\Exception $parserException): self
17+
{
18+
return new self(sprintf('Unable to parse object name "%s".', $name), 0, $parserException);
19+
}
20+
21+
public static function tooManyQualifiers(string $tableName, int $number): self
22+
{
23+
return new self(sprintf('Object name %s contains %d qualifiers. At most one is allowed.', $tableName, $number));
24+
}
25+
}

src/Schema/Exception/InvalidTableName.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@
44

55
namespace Doctrine\DBAL\Schema\Exception;
66

7-
use Doctrine\DBAL\Schema\SchemaException;
8-
use InvalidArgumentException;
9-
107
use function sprintf;
118

129
/** @psalm-immutable */
13-
final class InvalidTableName extends InvalidArgumentException implements SchemaException
10+
final class InvalidTableName extends InvalidObjectName
1411
{
1512
public static function new(string $tableName): self
1613
{

tests/Functional/Schema/SQLiteSchemaManagerTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,13 @@ public function testShorthandInForeignKeyReferenceWithMultipleColumns(): void
384384

385385
self::assertSame(
386386
[
387-
'CREATE TABLE track (trackid INTEGER DEFAULT NULL, trackname CLOB DEFAULT NULL COLLATE "BINARY",'
388-
. ' trackartist INTEGER DEFAULT NULL, FOREIGN KEY (trackartist) REFERENCES artist (artistid, isrc) ON'
389-
. ' UPDATE NO ACTION ON DELETE NO ACTION NOT DEFERRABLE INITIALLY IMMEDIATE)',
390-
'CREATE INDEX IDX_D6E3F8A6FB96D8BC ON track (trackartist)',
387+
'CREATE TABLE "track" ('
388+
. '"trackid" INTEGER DEFAULT NULL,'
389+
. ' "trackname" CLOB DEFAULT NULL COLLATE "BINARY",'
390+
. ' "trackartist" INTEGER DEFAULT NULL,'
391+
. ' FOREIGN KEY ("trackartist") REFERENCES "artist" ("artistid", "isrc")'
392+
. ' ON UPDATE NO ACTION ON DELETE NO ACTION NOT DEFERRABLE INITIALLY IMMEDIATE)',
393+
'CREATE INDEX "IDX_D6E3F8A6FB96D8BC" ON "track" ("trackartist")',
391394
],
392395
$createTableTrackSql,
393396
);

tests/Functional/Schema/SchemaManagerFunctionalTestCase.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,7 +1293,7 @@ public function testIntrospectReservedKeywordTableViaListTableDetails(): void
12931293
{
12941294
$this->createReservedKeywordTables();
12951295

1296-
$user = $this->schemaManager->introspectTable('"user"');
1296+
$user = $this->schemaManager->introspectTable('user');
12971297
self::assertCount(2, $user->getColumns());
12981298
self::assertCount(2, $user->getIndexes());
12991299
self::assertCount(1, $user->getForeignKeys());
@@ -1316,8 +1316,12 @@ private function createReservedKeywordTables(): void
13161316
{
13171317
$platform = $this->connection->getDatabasePlatform();
13181318

1319-
$this->dropTableIfExists($platform->quoteSingleIdentifier('user'));
1320-
$this->dropTableIfExists($platform->quoteSingleIdentifier('group'));
1319+
$this->dropTableIfExists($platform->quoteSingleIdentifier(
1320+
$platform->normalizeUnquotedIdentifier('user'),
1321+
));
1322+
$this->dropTableIfExists($platform->quoteSingleIdentifier(
1323+
$platform->normalizeUnquotedIdentifier('group'),
1324+
));
13211325

13221326
$schema = new Schema();
13231327

0 commit comments

Comments
 (0)