Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix auto_increment implementation for Oracle #4733

Merged
merged 1 commit into from
Jul 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Platforms/OraclePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,6 @@ public function getCreateAutoincrementSql($name, $table, $start = 1)
last_Sequence NUMBER;
last_InsertID NUMBER;
BEGIN
SELECT ' . $sequenceName . '.NEXTVAL INTO :NEW.' . $quotedName . ' FROM DUAL;
IF (:NEW.' . $quotedName . ' IS NULL OR :NEW.' . $quotedName . ' = 0) THEN
SELECT ' . $sequenceName . '.NEXTVAL INTO :NEW.' . $quotedName . ' FROM DUAL;
ELSE
Expand All @@ -540,6 +539,7 @@ public function getCreateAutoincrementSql($name, $table, $start = 1)
WHILE (last_InsertID > last_Sequence) LOOP
SELECT ' . $sequenceName . '.NEXTVAL INTO last_Sequence FROM DUAL;
END LOOP;
SELECT ' . $sequenceName . '.NEXTVAL INTO last_Sequence FROM DUAL;
END IF;
END;';

Expand Down
45 changes: 45 additions & 0 deletions tests/Functional/AutoIncrementColumnTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Doctrine\DBAL\Tests\Functional;

use Doctrine\DBAL\Platforms\SQLServer2012Platform;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;

class AutoIncrementColumnTest extends FunctionalTestCase
{
/** @var bool */
private $shouldDisableIdentityInsert = false;

protected function setUp(): void
{
parent::setUp();

$table = new Table('auto_increment_table');
$table->addColumn('id', 'integer', ['autoincrement' => true]);
$table->setPrimaryKey(['id']);

$this->connection->createSchemaManager()
->dropAndCreateTable($table);
}

protected function tearDown(): void
{
if ($this->shouldDisableIdentityInsert) {
$this->connection->executeStatement('SET IDENTITY_INSERT auto_increment_table OFF');
}

parent::tearDown();
}

public function testInsertIdentityValue(): void
{
if ($this->connection->getDatabasePlatform() instanceof SQLServer2012Platform) {
$this->connection->executeStatement('SET IDENTITY_INSERT auto_increment_table ON');
$this->shouldDisableIdentityInsert = true;
}

$this->connection->insert('auto_increment_table', ['id' => 2]);
self::assertEquals(2, $this->connection->fetchOne('SELECT id FROM auto_increment_table'));
}
}
9 changes: 4 additions & 5 deletions tests/Platforms/OraclePlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,7 @@ public function testGenerateTableWithAutoincrement(): void
sprintf(
'CREATE TRIGGER %s_AI_PK BEFORE INSERT ON %s FOR EACH ROW DECLARE last_Sequence NUMBER;'
. ' last_InsertID NUMBER;'
. ' BEGIN SELECT %s_SEQ.NEXTVAL'
. ' INTO :NEW.%s FROM DUAL;'
. ' BEGIN'
. ' IF (:NEW.%s IS NULL OR :NEW.%s = 0)'
. ' THEN SELECT %s_SEQ.NEXTVAL INTO :NEW.%s FROM DUAL;'
. ' ELSE SELECT NVL(Last_Number, 0) INTO last_Sequence'
Expand All @@ -368,18 +367,18 @@ public function testGenerateTableWithAutoincrement(): void
. ' WHILE (last_InsertID > last_Sequence) LOOP'
. ' SELECT %s_SEQ.NEXTVAL INTO last_Sequence FROM DUAL;'
. ' END LOOP;'
. ' SELECT %s_SEQ.NEXTVAL INTO last_Sequence FROM DUAL;'
. ' END IF;'
. ' END;',
$tableName,
$tableName,
$tableName,
$columnName,
$columnName,
$columnName,
$tableName,
$columnName,
$tableName,
$columnName,
$tableName,
$tableName
),
];
Expand Down Expand Up @@ -811,7 +810,6 @@ public function testQuotedTableNames(): void
last_Sequence NUMBER;
last_InsertID NUMBER;
BEGIN
SELECT "test_SEQ".NEXTVAL INTO :NEW."id" FROM DUAL;
IF (:NEW."id" IS NULL OR :NEW."id" = 0) THEN
SELECT "test_SEQ".NEXTVAL INTO :NEW."id" FROM DUAL;
ELSE
Expand All @@ -822,6 +820,7 @@ public function testQuotedTableNames(): void
WHILE (last_InsertID > last_Sequence) LOOP
SELECT "test_SEQ".NEXTVAL INTO last_Sequence FROM DUAL;
END LOOP;
SELECT "test_SEQ".NEXTVAL INTO last_Sequence FROM DUAL;
END IF;
END;
EOD;
Expand Down