[FEATURE] Add data type in QueryBuilder::set with createNamedParameter 27/61327/4
authorManuel Selbach <manuel_selbach@yahoo.de>
Fri, 19 Jul 2019 12:24:48 +0000 (14:24 +0200)
committerFrank Nägler <frank.naegler@typo3.org>
Wed, 25 Sep 2019 10:16:08 +0000 (12:16 +0200)
With this change it is possible to define the type of the value if the
createNamedParameter option is set.

Releases: master
Resolves: #88805
Change-Id: I964d8fadf5d3e6512d8585c2bbd84e0b14c9d274
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/61327
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Manuel Selbach <manuel_selbach@yahoo.de>
Tested-by: Frank Nägler <frank.naegler@typo3.org>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Manuel Selbach <manuel_selbach@yahoo.de>
Reviewed-by: Frank Nägler <frank.naegler@typo3.org>
typo3/sysext/core/Classes/Database/Query/QueryBuilder.php
typo3/sysext/core/Documentation/Changelog/master/Feature-88805-AddTypeToTYPO3CMSCoreDatabaseQueryQueryBuilderset.rst [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Database/Query/QueryBuilderTest.php

index 143ce15..7a210f0 100644 (file)
@@ -607,14 +607,15 @@ class QueryBuilder
      * @param string $key The column to set.
      * @param string $value The value, expression, placeholder, etc.
      * @param bool $createNamedParameter Automatically create a named parameter for the value
+     * @param int $type
      *
      * @return QueryBuilder This QueryBuilder instance.
      */
-    public function set(string $key, $value, bool $createNamedParameter = true): QueryBuilder
+    public function set(string $key, $value, bool $createNamedParameter = true, int $type = \PDO::PARAM_STR): QueryBuilder
     {
         $this->concreteQueryBuilder->set(
             $this->quoteIdentifier($key),
-            $createNamedParameter ? $this->createNamedParameter($value) : $value
+            $createNamedParameter ? $this->createNamedParameter($value, $type) : $value
         );
 
         return $this;
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-88805-AddTypeToTYPO3CMSCoreDatabaseQueryQueryBuilderset.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-88805-AddTypeToTYPO3CMSCoreDatabaseQueryQueryBuilderset.rst
new file mode 100644 (file)
index 0000000..27f5496
--- /dev/null
@@ -0,0 +1,27 @@
+.. include:: ../../Includes.txt
+
+=============================================================================
+Feature: #88805 - Add type to TYPO3\CMS\Core\Database\Query\QueryBuilder::set
+=============================================================================
+
+See :issue:`88805`
+
+Description
+===========
+
+Using :php:`TYPO3\CMS\Core\Database\Query\QueryBuilder::set()` will accept as fourth parameter a type that the query value should be casted to, if third parameter (:php:`createNamedParameter`) is set to true.
+The default value will be a string type.
+
+
+Impact
+======
+
+Type safe query parameter setting is now also possible via :php:`set()`.
+
+Example:: php
+
+   $queryBuilder->set($fieldName, $fieldValue, true, \PDO::PARAM_INT);
+
+will make sure $fieldValue is handled as a int type in the resulting DB query.
+
+.. index:: Database, ext:core
\ No newline at end of file
index e7577e7..da5da6b 100644 (file)
@@ -1349,4 +1349,43 @@ class QueryBuilderTest extends UnitTestCase
 
         $queryBuilder->resetRestrictions();
     }
+
+    /**
+     * @test
+     * @dataProvider createNamedParameterInput
+     * @param mixed $input
+     * @param int $type
+     */
+    public function setWithNamedParameterPassesGivenTypeToCreateNamedParameter($input, int $type): void
+    {
+        $this->connection->quoteIdentifier('aField')
+            ->willReturnArgument(0);
+        $concreteQueryBuilder = new \Doctrine\DBAL\Query\QueryBuilder($this->connection->reveal());
+
+        $subject = new QueryBuilder($this->connection->reveal(), null, $concreteQueryBuilder);
+        $subject->set('aField', $input, true, $type);
+        self::assertSame($type, $concreteQueryBuilder->getParameterType('dcValue1'));
+    }
+
+    public function createNamedParameterInput(): array
+    {
+        return [
+            'string input and output' => [
+                'aValue',
+                \PDO::PARAM_STR,
+            ],
+            'int input and string output' => [
+                17,
+                \PDO::PARAM_STR,
+            ],
+            'int input and int output' => [
+                17,
+                \PDO::PARAM_INT,
+            ],
+            'string input and array output' => [
+                'aValue',
+                Connection::PARAM_STR_ARRAY
+            ],
+        ];
+    }
 }