From e48d65df95cd9ef4865e01bcf690601cd6b84365 Mon Sep 17 00:00:00 2001 From: Semih Serhat Karakaya Date: Thu, 25 Jun 2020 06:04:23 -0700 Subject: [PATCH] respect to platform restrictions when creating an identfier name for oracle --- .../DBAL/Platforms/OraclePlatform.php | 20 +++++++++-- .../Platform/PlatformRestrictionsTest.php | 33 +++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 tests/Doctrine/Tests/DBAL/Functional/Platform/PlatformRestrictionsTest.php diff --git a/lib/Doctrine/DBAL/Platforms/OraclePlatform.php b/lib/Doctrine/DBAL/Platforms/OraclePlatform.php index 1c21e219010..2f7d789584a 100644 --- a/lib/Doctrine/DBAL/Platforms/OraclePlatform.php +++ b/lib/Doctrine/DBAL/Platforms/OraclePlatform.php @@ -586,6 +586,22 @@ private function normalizeIdentifier($name) return $identifier->isQuoted() ? $identifier : new Identifier(strtoupper($name)); } + /** + * Adds suffix to identifier, + * + * if the new string exceeds max identifier length, + * keeps $suffix, cuts from $identifier as much as the part exceeding. + */ + private function addSuffix(string $identifier, string $suffix) : string + { + $maxPossibleLengthWithoutSuffix = $this->getMaxIdentifierLength() - strlen($suffix); + if (strlen($identifier) > $maxPossibleLengthWithoutSuffix) { + $identifier = substr($identifier, 0, $maxPossibleLengthWithoutSuffix); + } + + return $identifier . $suffix; + } + /** * Returns the autoincrement primary key identifier name for the given table identifier. * @@ -598,7 +614,7 @@ private function normalizeIdentifier($name) */ private function getAutoincrementIdentifierName(Identifier $table) { - $identifierName = $table->getName() . '_AI_PK'; + $identifierName = $this->addSuffix($table->getName(), '_AI_PK'); return $table->isQuoted() ? $this->quoteSingleIdentifier($identifierName) @@ -966,7 +982,7 @@ public function getIdentitySequenceName($tableName, $columnName) $table = new Identifier($tableName); // No usage of column name to preserve BC compatibility with <2.5 - $identitySequenceName = $table->getName() . '_SEQ'; + $identitySequenceName = $this->addSuffix($table->getName(), '_SEQ'); if ($table->isQuoted()) { $identitySequenceName = '"' . $identitySequenceName . '"'; diff --git a/tests/Doctrine/Tests/DBAL/Functional/Platform/PlatformRestrictionsTest.php b/tests/Doctrine/Tests/DBAL/Functional/Platform/PlatformRestrictionsTest.php new file mode 100644 index 00000000000..5f6deefe3ba --- /dev/null +++ b/tests/Doctrine/Tests/DBAL/Functional/Platform/PlatformRestrictionsTest.php @@ -0,0 +1,33 @@ +connection->getDatabasePlatform(); + $tableName = str_repeat('x', $platform->getMaxIdentifierLength()); + $columnName = str_repeat('y', $platform->getMaxIdentifierLength()); + $table = new Table($tableName); + $table->addColumn($columnName, 'integer', ['autoincrement' => true]); + $table->setPrimaryKey([$columnName]); + $this->connection->getSchemaManager()->dropAndCreateTable($table); + $createdTable = $this->connection->getSchemaManager()->listTableDetails($tableName); + + $this->assertTrue($createdTable->hasColumn($columnName)); + $this->assertTrue($createdTable->hasPrimaryKey()); + } +}