Skip to content

Commit

Permalink
respect to platform restrictions when creating an identfier name for …
Browse files Browse the repository at this point in the history
…oracle
  • Loading branch information
karakayasemi committed Jun 29, 2020
1 parent 9daede4 commit e48d65d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lib/Doctrine/DBAL/Platforms/OraclePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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)
Expand Down Expand Up @@ -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 . '"';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Doctrine\Tests\DBAL\Functional\Platform;

use Doctrine\DBAL\Schema\Table;
use Doctrine\Tests\DbalFunctionalTestCase;
use function str_repeat;

/**
* This class holds tests that make sure generated SQL statements respect to platform restrictions
* like maximum element name length
*/
class PlatformRestrictionsTest extends DbalFunctionalTestCase
{
/**
* Tests element names that are at the boundary of the identifier length limit.
* Ensures generated auto-increment identifier name respects to platform restrictions.
*/
public function testMaxIdentifierLengthLimitWithAutoIncrement() : void
{
$platform = $this->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());
}
}

0 comments on commit e48d65d

Please sign in to comment.