[TASK] Doctrine: Migrate exec_SELECTgetSingleRow 25/49525/3
authorChristian Kuhn <lolli@schwarzbu.ch>
Sun, 21 Aug 2016 17:50:50 +0000 (19:50 +0200)
committerBenni Mack <benni@typo3.org>
Sun, 21 Aug 2016 19:25:11 +0000 (21:25 +0200)
Change-Id: I5f1a284c34fe8a238838a7b2c5747746a9aee882
Resolves: #77561
Releases: master
Reviewed-on: https://review.typo3.org/49525
Tested-by: Bamboo TYPO3com <info@typo3.com>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
typo3/sysext/core/Classes/Collection/AbstractRecordCollection.php
typo3/sysext/core/Classes/Collection/RecordCollectionRepository.php
typo3/sysext/core/Classes/Resource/ResourceFactory.php
typo3/sysext/core/Classes/Resource/ResourceStorage.php
typo3/sysext/core/Classes/Utility/RootlineUtility.php
typo3/sysext/core/Tests/Unit/Collection/RecordCollectionRepositoryTest.php
typo3/sysext/core/Tests/Unit/Utility/RootlineUtilityTest.php
typo3/sysext/workspaces/Classes/Domain/Record/AbstractRecord.php

index eefe9c6..3ce90c7 100644 (file)
@@ -13,6 +13,9 @@ namespace TYPO3\CMS\Core\Collection;
  *
  * The TYPO3 project - inspiring people to share!
  */
+use TYPO3\CMS\Core\Database\ConnectionPool;
+use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
 
 /**
  * Abstract implementation of a RecordCollection
@@ -326,11 +329,13 @@ abstract class AbstractRecordCollection implements RecordCollectionInterface, Pe
      */
     public static function load($id, $fillItems = false)
     {
-        $collectionRecord = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
-            '*',
-            static::$storageTableName,
-            'uid=' . (int)$id . \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause(static::$storageTableName)
-        );
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(static::$storageTableName);
+        $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
+        $collectionRecord = $queryBuilder->select('*')
+            ->from(static::$storageTableName)
+            ->where($queryBuilder->expr()->eq('uid', (int)$id))
+            ->execute()
+            ->fetch();
         return self::create($collectionRecord, $fillItems);
     }
 
index c5fc218..1752ba5 100644 (file)
@@ -15,6 +15,9 @@ namespace TYPO3\CMS\Core\Collection;
  */
 
 use TYPO3\CMS\Backend\Utility\BackendUtility;
+use TYPO3\CMS\Core\Database\ConnectionPool;
+use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
 
 /**
  * Implements the repository for record collections.
@@ -52,11 +55,13 @@ class RecordCollectionRepository
     public function findByUid($uid)
     {
         $result = null;
-        $data = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
-            '*',
-            $this->table,
-            'uid=' . (int)$uid . BackendUtility::deleteClause($this->table)
-        );
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
+        $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
+        $data = $queryBuilder->select('*')
+            ->from($this->table)
+            ->where($queryBuilder->expr()->eq('uid', (int)$uid))
+            ->execute()
+            ->fetch();
         if (is_array($data)) {
             $result = $this->createDomainObject($data);
         }
index cd858ba..28d5162 100644 (file)
@@ -15,7 +15,9 @@ namespace TYPO3\CMS\Core\Resource;
  */
 
 use TYPO3\CMS\Backend\Utility\BackendUtility;
+use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Database\DatabaseConnection;
+use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
 use TYPO3\CMS\Core\Resource\Index\FileIndexRepository;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Core\Utility\MathUtility;
@@ -290,9 +292,14 @@ class ResourceFactory implements ResourceFactoryInterface, \TYPO3\CMS\Core\Singl
         if (!$this->collectionInstances[$uid]) {
             // Get mount data if not already supplied as argument to this function
             if (empty($recordData) || $recordData['uid'] !== $uid) {
-                /** @var $GLOBALS['TYPO3_DB'] \TYPO3\CMS\Core\Database\DatabaseConnection */
-                $recordData = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('*', 'sys_file_collection', 'uid=' . (int)$uid . ' AND deleted=0');
-                if (!is_array($recordData)) {
+                $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_file_collection');
+                $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
+                $recordData = $queryBuilder->select('*')
+                    ->from('sys_file_collection')
+                    ->where($queryBuilder->expr()->eq('uid', (int)$uid))
+                    ->execute()
+                    ->fetch();
+                if (empty($recordData)) {
                     throw new \InvalidArgumentException('No collection found for given UID: "' . $uid . '"', 1314085992);
                 }
             }
@@ -643,8 +650,13 @@ class ResourceFactory implements ResourceFactoryInterface, \TYPO3\CMS\Core\Singl
         } elseif (!$raw && is_object($GLOBALS['TSFE'])) {
             $fileReferenceData = $GLOBALS['TSFE']->sys_page->checkRecord('sys_file_reference', $uid);
         } else {
-            /** @var $GLOBALS['TYPO3_DB'] \TYPO3\CMS\Core\Database\DatabaseConnection */
-            $fileReferenceData = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('*', 'sys_file_reference', 'uid=' . (int)$uid . ' AND deleted=0');
+            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_file_reference');
+            $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
+            $fileReferenceData = $queryBuilder->select('*')
+                ->from('sys_file_reference')
+                ->where($queryBuilder->expr()->eq('uid', (int)$uid))
+                ->execute()
+                ->fetch();
         }
         return $fileReferenceData;
     }
index 3589c13..31a4879 100644 (file)
@@ -14,6 +14,7 @@ namespace TYPO3\CMS\Core\Resource;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Database\DatabaseConnection;
 use TYPO3\CMS\Core\Registry;
 use TYPO3\CMS\Core\Resource\Exception\InvalidTargetFolderException;
@@ -399,7 +400,13 @@ class ResourceStorage implements ResourceStorageInterface
     {
         if ($this->getUid() > 0) {
             // @todo: move this to the storage repository
-            $this->getDatabaseConnection()->exec_UPDATEquery('sys_file_storage', 'uid=' . (int)$this->getUid(), array('is_online' => 0));
+            GeneralUtility::makeInstance(ConnectionPool::class)
+                ->getConnectionForTable('sys_file_storage')
+                ->update(
+                    'sys_file_storage',
+                    ['is_online' => 0],
+                    ['uid' => (int)$this->getUid()]
+                );
         }
         $this->storageRecord['is_online'] = 0;
         $this->isOnline = false;
index cb58333..caf41e3 100644 (file)
@@ -14,6 +14,8 @@ namespace TYPO3\CMS\Core\Utility;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Database\ConnectionPool;
+use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
 use TYPO3\CMS\Frontend\Page\PageRepository;
 
 /**
@@ -244,7 +246,16 @@ class RootlineUtility
     {
         $currentCacheIdentifier = $this->getCacheIdentifier($uid);
         if (!isset(self::$pageRecordCache[$currentCacheIdentifier])) {
-            $row = $this->databaseConnection->exec_SELECTgetSingleRow(implode(',', self::$rootlineFields), 'pages', 'uid = ' . (int)$uid . ' AND pages.deleted = 0 AND pages.doktype <> ' . PageRepository::DOKTYPE_RECYCLER);
+            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
+            $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
+            $row = $queryBuilder->select(...self::$rootlineFields)
+                ->from('pages')
+                ->where(
+                    $queryBuilder->expr()->eq('uid', (int)$uid),
+                    $queryBuilder->expr()->neq('doktype', (int)PageRepository::DOKTYPE_RECYCLER)
+                )
+                ->execute()
+                ->fetch();
             if (empty($row)) {
                 throw new \RuntimeException('Could not fetch page data for uid ' . $uid . '.', 1343589451);
             }
index c6f842a..540365a 100644 (file)
@@ -97,51 +97,6 @@ class RecordCollectionRepositoryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
     /**
      * @test
      */
-    public function doesFindByUidReturnNull()
-    {
-        $testUid = rand(1, 1000);
-        $this->databaseMock->expects($this->once())->method('exec_SELECTgetSingleRow')->will($this->returnCallback(array($this, 'getSingleRowCallback')));
-        $this->getSingleRowCallbackReturnValue = null;
-        $object = $this->subject->findByUid($testUid);
-        $this->assertNull($object);
-    }
-
-    /**
-     * @test
-     */
-    public function doesFindByUidReturnObject()
-    {
-        $testUid = rand(1, 1000);
-        $this->databaseMock->expects($this->once())->method('exec_SELECTgetSingleRow')->will($this->returnCallback(array($this, 'getSingleRowCallback')));
-        $this->getSingleRowCallbackReturnValue = array(
-            'uid' => $testUid,
-            'type' => \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE_Static,
-            'table_name' => $this->testTableName
-        );
-        $object = $this->subject->findByUid($testUid);
-        $this->assertInstanceOf(\TYPO3\CMS\Core\Collection\StaticRecordCollection::class, $object);
-    }
-
-    /**
-     * @test
-     */
-    public function doesFindByUidThrowException()
-    {
-        $this->expectException(\RuntimeException::class);
-        $this->expectExceptionCode(1328646798);
-
-        $testUid = rand(1, 1000);
-        $this->databaseMock->expects($this->once())->method('exec_SELECTgetSingleRow')->will($this->returnCallback(array($this, 'getSingleRowCallback')));
-        $this->getSingleRowCallbackReturnValue = array(
-            'uid' => $testUid,
-            'type' => $this->getUniqueId('unknown')
-        );
-        $object = $this->subject->findByUid($testUid);
-    }
-
-    /**
-     * @test
-     */
     public function doesFindByTypeReturnNull()
     {
         $type = \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE_Static;
index c015b17..6293b38 100644 (file)
@@ -290,47 +290,6 @@ class RootlineUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
     /**
      * @test
      */
-    public function getRecordArrayFetchesTranslationWhenLanguageIdIsSet()
-    {
-        $pageData = array(
-            'uid' => 1,
-            'title' => 'Original',
-        );
-        $pageDataTranslated = array(
-            'uid' => 1,
-            'title' => 'Translated',
-            '_PAGES_OVERLAY_UID' => '2',
-        );
-
-        $this->subject
-            ->expects($this->any())
-            ->method('enrichWithRelationFields')
-            ->with(2, $pageDataTranslated)
-            ->will($this->returnArgument(1));
-
-        $databaseConnectionMock = $this->createMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class);
-        $databaseConnectionMock
-            ->expects($this->once())
-            ->method('exec_SELECTgetSingleRow')
-            ->will(
-                $this->returnValue($pageData)
-            );
-        $this->subject->_set('databaseConnection',
-            $databaseConnectionMock
-        );
-
-        $this->pageContextMock
-                ->expects($this->any())
-                ->method('getPageOverlay')
-                ->will($this->returnValue($pageDataTranslated));
-
-        $this->subject->_set('languageUid', 1);
-        $this->assertSame($pageDataTranslated, $this->subject->_call('getRecordArray', 1));
-    }
-
-    /**
-     * @test
-     */
     public function enrichWithRelationFieldsCreatesWhereClauseForDisabledField()
     {
         $mockDatabaseConnection = $this->getMockBuilder(\TYPO3\CMS\Core\Database\DatabaseConnection::class)
index 0659d37..afef579 100644 (file)
@@ -13,6 +13,9 @@ namespace TYPO3\CMS\Workspaces\Domain\Record;
  *
  * The TYPO3 project - inspiring people to share!
  */
+
+use TYPO3\CMS\Core\Database\ConnectionPool;
+use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Workspaces\Service\StagesService;
 
@@ -28,7 +31,13 @@ abstract class AbstractRecord
 
     protected static function fetch($tableName, $uid)
     {
-        $record = static::getDatabaseConnection()->exec_SELECTgetSingleRow('*', $tableName, 'deleted=0 AND uid=' . (int)$uid);
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($tableName);
+        $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
+        $record = $queryBuilder->select('*')
+            ->from($tableName)
+            ->where($queryBuilder->expr()->eq('uid', (int)$uid))
+            ->execute()
+            ->fetch();
         if (empty($record)) {
             throw new \RuntimeException('Record "' . $tableName . ': ' . $uid . '" not found');
         }