Skip to content

Commit

Permalink
Merge pull request #4733 from mondrake/patch-1
Browse files Browse the repository at this point in the history
Fix auto_increment implementation for Oracle
  • Loading branch information
morozov authored Jul 31, 2021
2 parents 891ec18 + c90854b commit 7ac46c8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
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

0 comments on commit 7ac46c8

Please sign in to comment.