[BUGFIX] Doctrine: Provide table name to lastInsertId() 43/49843/4
authorMorton Jonuschat <m.jonuschat@mojocode.de>
Sat, 3 Sep 2016 13:43:29 +0000 (15:43 +0200)
committerAndreas Fernandez <typo3@scripting-base.de>
Sat, 3 Sep 2016 15:21:27 +0000 (17:21 +0200)
Databases that use sequences for auto increment columns need to
have information about the table name to select the correct sequence
to return information from.

Change-Id: I5a4ff882d2237c86030b41a921843afdb95b9789
Resolves: #77833
Releases: master
Reviewed-on: https://review.typo3.org/49843
Tested-by: Bamboo TYPO3com <info@typo3.com>
Reviewed-by: Manuel Selbach <manuel_selbach@yahoo.de>
Tested-by: Manuel Selbach <manuel_selbach@yahoo.de>
Reviewed-by: Andreas Fernandez <typo3@scripting-base.de>
Tested-by: Andreas Fernandez <typo3@scripting-base.de>
14 files changed:
typo3/sysext/core/Classes/Authentication/BackendUserAuthentication.php
typo3/sysext/core/Classes/DataHandling/DataHandler.php
typo3/sysext/core/Classes/Database/Connection.php
typo3/sysext/core/Classes/Resource/Index/FileIndexRepository.php
typo3/sysext/core/Classes/Resource/Index/MetaDataRepository.php
typo3/sysext/core/Classes/Resource/ProcessedFileRepository.php
typo3/sysext/core/Classes/Resource/StorageRepository.php
typo3/sysext/core/Classes/Tests/Testbase.php
typo3/sysext/core/Tests/Functional/Category/Collection/CategoryCollectionTest.php
typo3/sysext/extbase/Classes/Persistence/Generic/Storage/Typo3DbBackend.php
typo3/sysext/indexed_search/Classes/Controller/SearchController.php
typo3/sysext/install/Classes/Controller/Action/Step/DefaultConfiguration.php
typo3/sysext/rsaauth/Classes/Storage/SplitStorage.php
typo3/sysext/scheduler/Classes/Scheduler.php

index c1cf228..d6e5b30 100644 (file)
@@ -2321,7 +2321,7 @@ class BackendUserAuthentication extends \TYPO3\CMS\Core\Authentication\AbstractU
             ]
         );
 
-        return (int)$connection->lastInsertId();
+        return (int)$connection->lastInsertId('sys_log');
     }
 
     /**
index 81b1d39..2b1108f 100644 (file)
@@ -6752,7 +6752,7 @@ class DataHandler
                     // Set mapping for NEW... -> real uid:
                     // the NEW_id now holds the 'NEW....' -id
                     $NEW_id = $id;
-                    $id = $connection->lastInsertId();
+                    $id = $connection->lastInsertId($table);
                     if (!$dontSetNewIdIndex) {
                         $this->substNEWwithIDs[$NEW_id] = $id;
                         $this->substNEWwithIDs_table[$NEW_id] = $table;
index 714554a..aa5e00f 100644 (file)
@@ -19,6 +19,7 @@ use Doctrine\Common\EventManager;
 use Doctrine\DBAL\Configuration;
 use Doctrine\DBAL\Driver;
 use Doctrine\DBAL\Driver\Statement;
+use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
 use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
@@ -387,4 +388,23 @@ class Connection extends \Doctrine\DBAL\Connection
             }
         }
     }
+
+    /**
+     * Returns the ID of the last inserted row or sequence value.
+     * If table and fieldname have been provided it tries to build
+     * the sequence name for PostgreSQL. For MySQL the parameters
+     * are not required / and only the table name is passed through.
+     *
+     * @param string|null $tableName
+     * @param string|null $fieldName
+     * @return string
+     */
+    public function lastInsertId($tableName = null, string $fieldName = 'uid'): string
+    {
+        if ($this->getDatabasePlatform() instanceof PostgreSqlPlatform) {
+            return parent::lastInsertId(trim(implode('_', [$tableName, $fieldName, 'seq']), '_'));
+        }
+
+        return (string)parent::lastInsertId($tableName);
+    }
 }
index 05d1fc2..2537d1d 100644 (file)
@@ -329,7 +329,7 @@ class FileIndexRepository implements SingletonInterface
             $this->table,
             $data
         );
-        $data['uid'] = $connection->lastInsertId();
+        $data['uid'] = $connection->lastInsertId($this->table);
         $this->updateRefIndex($data['uid']);
         $this->emitRecordCreatedSignal($data);
         return $data['uid'];
index fc8fabc..f4a2c8a 100644 (file)
@@ -152,7 +152,7 @@ class MetaDataRepository implements SingletonInterface
         );
 
         $record = $emptyRecord;
-        $record['uid'] = $connection->lastInsertId();
+        $record['uid'] = $connection->lastInsertId($this->tableName);
         $record['newlyCreated']  = true;
 
         $this->emitRecordCreatedSignal($record);
index 5c22852..d1fcfd8 100644 (file)
@@ -142,7 +142,7 @@ class ProcessedFileRepository extends AbstractRepository
                 $insertFields
             );
 
-            $uid = $connection->lastInsertId();
+            $uid = $connection->lastInsertId($this->table);
             $processedFile->updateProperties(['uid' => $uid]);
         }
     }
index b9fdd75..62fe8ef 100644 (file)
@@ -242,7 +242,7 @@ class StorageRepository extends AbstractRepository
             ->getConnectionForTable($this->table);
         $dbConnection->insert($this->table, $field_values);
 
-        return (int)$dbConnection->lastInsertId();
+        return (int)$dbConnection->lastInsertId($this->table);
     }
 
     /**
index ef84a7b..4a9462a 100644 (file)
@@ -636,7 +636,7 @@ class Testbase
             );
             if (isset($table['id'])) {
                 $elementId = (string)$table['id'];
-                $foreignKeys[$tableName][$elementId] = $connection->lastInsertId();
+                $foreignKeys[$tableName][$elementId] = $connection->lastInsertId($tableName);
             }
         }
     }
index d410b9f..eafa47b 100644 (file)
@@ -359,7 +359,7 @@ class CategoryCollectionTest extends \TYPO3\CMS\Core\Tests\FunctionalTestCase
         ];
 
         $connection->insert('sys_category', $values);
-        $this->categoryUid = $connection->lastInsertId();
+        $this->categoryUid = $connection->lastInsertId('sys_category');
     }
 
     /**
index e34d4e1..d20f11e 100644 (file)
@@ -153,7 +153,7 @@ class Typo3DbBackend implements BackendInterface, SingletonInterface
             throw new SqlErrorException($e->getPrevious()->getMessage(), 1470230766);
         }
 
-        $uid = $connection->lastInsertId();
+        $uid = $connection->lastInsertId($tableName);
 
         if (!$isRelation) {
             $this->clearPageCache($tableName, $uid);
index 8182af6..e899f7d 100644 (file)
@@ -789,7 +789,7 @@ class SearchController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionControlle
         ];
         $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('index_search_stat');
         $connection->insert('index_stat_search', $insertFields);
-        $newId = $connection->lastInsertId();
+        $newId = $connection->lastInsertId('index_stat_search');
         if ($newId) {
             $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('index_stat_word');
             foreach ($searchWords as $val) {
index 7252f1b..0cad1c6 100644 (file)
@@ -72,7 +72,7 @@ class DefaultConfiguration extends AbstractStepAction
                         'perms_everybody' => 1
                     ]
                 );
-                $pageUid = $databaseConnectionForPages->lastInsertId();
+                $pageUid = $databaseConnectionForPages->lastInsertId('pages');
 
                 // add a root sys_template with fluid_styled_content and a default PAGE typoscript snippet
                 $connectionPool->getConnectionForTable('sys_template')->insert(
index 23c4cdb..e26be40 100644 (file)
@@ -118,7 +118,7 @@ class SplitStorage extends AbstractStorage
                     'key_value' => $keyPart2
                 ]
             );
-            $keyId = $connection->lastInsertId();
+            $keyId = $connection->lastInsertId('tx_rsaauth_keys');
             // Store another part in session
             $_SESSION['tx_rsaauth_key'] = [$keyId, $keyPart1];
         }
index 805bd7c..7672d9d 100644 (file)
@@ -73,7 +73,7 @@ class Scheduler implements \TYPO3\CMS\Core\SingletonInterface
             $result = $connection->insert('tx_scheduler_task', $fields);
 
             if ($result) {
-                $task->setTaskUid($connection->lastInsertId());
+                $task->setTaskUid($connection->lastInsertId('tx_scheduler_task'));
                 $task->save();
                 $result = true;
             } else {