[BUGFIX] Fix usage of preparedStatement object in $query->statement 26/28726/9
authorMarc Bastian Heinrichs <typo3@mbh-software.de>
Mon, 24 Mar 2014 21:12:57 +0000 (22:12 +0100)
committerMarkus Klein <klein.t3@mfc-linz.at>
Mon, 24 Mar 2014 21:51:45 +0000 (22:51 +0100)
This is a follow-up fix for the integration of preparedStatements in
Extbase. Due to a wrong comparison, an own preparedStatement object
used in $query->statement() would never be executed.

Resolves: #57254
Releases: 6.2
Change-Id: I3176339be309530b50ff89410affa386d7f6f8e8
Reviewed-on: https://review.typo3.org/28726
Reviewed-by: Felix Oertel
Reviewed-by: Markus Klein
Tested-by: Markus Klein
typo3/sysext/extbase/Classes/Persistence/Generic/Qom/QueryObjectModelFactory.php
typo3/sysext/extbase/Classes/Persistence/Generic/Qom/Statement.php
typo3/sysext/extbase/Classes/Persistence/Generic/Query.php
typo3/sysext/extbase/Classes/Persistence/Generic/Storage/Typo3DbBackend.php

index 8ba3a9f..ccab811 100644 (file)
@@ -56,7 +56,7 @@ class QueryObjectModelFactory implements \TYPO3\CMS\Core\SingletonInterface {
        /**
         * Sets a statement as constraint. This is not part of the JCR 2.0 Specification!
         *
-        * @param string $statement The statement
+        * @param string|\TYPO3\CMS\Core\Database\PreparedStatement $statement The statement
         * @param array $boundVariables An array of variables to bind to the statement
         * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\Statement
         */
index 08c0846..f0bc139 100644 (file)
@@ -33,7 +33,7 @@ namespace TYPO3\CMS\Extbase\Persistence\Generic\Qom;
 class Statement {
 
        /**
-        * @var mixed
+        * @var string|\TYPO3\CMS\Core\Database\PreparedStatement
         */
        protected $statement;
 
@@ -45,7 +45,7 @@ class Statement {
        /**
         * Constructs the Statement instance
         *
-        * @param mixed $statement The statement as sql string or TYPO3\CMS\Core\Database\PreparedStatement
+        * @param string|\TYPO3\CMS\Core\Database\PreparedStatement $statement The statement as sql string or TYPO3\CMS\Core\Database\PreparedStatement
         * @param array $boundVariables An array of variables to bind to the statement, only to be used with preparedStatement
         */
        public function __construct($statement, array $boundVariables = array()) {
@@ -65,7 +65,7 @@ class Statement {
        /**
         * Gets the statement.
         *
-        * @return string the statement; non-null
+        * @return string|\TYPO3\CMS\Core\Database\PreparedStatement the statement; non-null
         */
        public function getStatement() {
                return $this->statement;
index 460421f..c9abe71 100644 (file)
@@ -326,10 +326,10 @@ class Query implements \TYPO3\CMS\Extbase\Persistence\QueryInterface {
        }
 
        /**
-        * Sets the statement of this query programmatically. If you use this, you will lose the abstraction from a concrete storage
+        * Sets the statement of this query. If you use this, you will lose the abstraction from a concrete storage
         * backend (database).
         *
-        * @param string $statement The statement
+        * @param string|\TYPO3\CMS\Core\Database\PreparedStatement $statement The statement
         * @param array $parameters An array of parameters. These will be bound to placeholders '?' in the $statement.
         * @return \TYPO3\CMS\Extbase\Persistence\QueryInterface
         */
index 3fe0a46..8e7eece 100644 (file)
@@ -287,8 +287,9 @@ class Typo3DbBackend implements \TYPO3\CMS\Extbase\Persistence\Generic\Storage\B
         * @return array
         */
        public function getObjectDataByQuery(\TYPO3\CMS\Extbase\Persistence\QueryInterface $query) {
-               if ($query->getStatement() instanceof \TYPO3\CMS\Extbase\Persistence\Generic\Qom\Statement) {
-                       $rows = $this->getObjectDataByRawQuery($query);
+               $statement = $query->getStatement();
+               if ($statement instanceof \TYPO3\CMS\Extbase\Persistence\Generic\Qom\Statement) {
+                       $rows = $this->getObjectDataByRawQuery($statement);
                } else {
                        $rows = $this->getRowsByStatementParts($query);
                }
@@ -386,32 +387,28 @@ class Typo3DbBackend implements \TYPO3\CMS\Extbase\Persistence\Generic\Storage\B
        /**
         * Returns the object data using a custom statement
         *
-        * @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
+        * @param \TYPO3\CMS\Extbase\Persistence\Generic\Qom\Statement $statement
         * @return array
         */
-       protected function getObjectDataByRawQuery(QueryInterface $query) {
-               $statement = $query->getStatement();
+       protected function getObjectDataByRawQuery(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\Statement $statement) {
+               $realStatement = $statement->getStatement();
                $parameters = $statement->getBoundVariables();
 
-               if ($statement instanceof \TYPO3\CMS\Core\Database\PreparedStatement) {
-                       $preparedStatement = $statement->getStatement();
+               if ($realStatement instanceof \TYPO3\CMS\Core\Database\PreparedStatement) {
+                       $realStatement->execute($parameters);
+                       $rows = $realStatement->fetchAll();
 
-                       $preparedStatement->execute($parameters);
-                       $rows = $preparedStatement->fetchAll();
-
-                       $preparedStatement->free();
+                       $realStatement->free();
                } else {
-
-                       $sqlString = $statement->getStatement();
                        /**
                         * @deprecated since 6.2, this block will be removed in two versions
                         * the deprecation log is in Qom\Statement
                         */
                        if (!empty($parameters)) {
-                               $this->replacePlaceholders($sqlString, $parameters);
+                               $this->replacePlaceholders($realStatement, $parameters);
                        }
 
-                       $result = $this->databaseHandle->sql_query($sqlString);
+                       $result = $this->databaseHandle->sql_query($realStatement);
                        $this->checkSqlErrors();
 
                        $rows = array();
@@ -420,6 +417,7 @@ class Typo3DbBackend implements \TYPO3\CMS\Extbase\Persistence\Generic\Storage\B
                                        $rows[] = $row;
                                }
                        }
+                       $this->databaseHandle->sql_free_result($result);
                }
 
                return $rows;