From c90854b529dfc84201fc0f4e6e2b20fe3845b6eb Mon Sep 17 00:00:00 2001 From: mondrake Date: Fri, 23 Jul 2021 09:43:28 +0200 Subject: [PATCH] Fix auto_increment implementation for Oracle Co-authored-by: Sergei Morozov --- src/Platforms/OraclePlatform.php | 2 +- tests/Functional/AutoIncrementColumnTest.php | 45 ++++++++++++++++++++ tests/Platforms/OraclePlatformTest.php | 9 ++-- 3 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 tests/Functional/AutoIncrementColumnTest.php diff --git a/src/Platforms/OraclePlatform.php b/src/Platforms/OraclePlatform.php index 73b3c064999..2ca0f9c0e1c 100644 --- a/src/Platforms/OraclePlatform.php +++ b/src/Platforms/OraclePlatform.php @@ -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 @@ -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;'; diff --git a/tests/Functional/AutoIncrementColumnTest.php b/tests/Functional/AutoIncrementColumnTest.php new file mode 100644 index 00000000000..b57c0ac4ded --- /dev/null +++ b/tests/Functional/AutoIncrementColumnTest.php @@ -0,0 +1,45 @@ +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')); + } +} diff --git a/tests/Platforms/OraclePlatformTest.php b/tests/Platforms/OraclePlatformTest.php index 7c9264b689a..2127d8d9d65 100644 --- a/tests/Platforms/OraclePlatformTest.php +++ b/tests/Platforms/OraclePlatformTest.php @@ -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' @@ -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 ), ]; @@ -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 @@ -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;