[BUGFIX] Fix ExpressionBuilder->inSet for PostgreSQL 81/47681/2
authorMorton Jonuschat <m.jonuschat@mojocode.de>
Thu, 14 Apr 2016 22:07:47 +0000 (00:07 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Mon, 18 Apr 2016 16:34:28 +0000 (18:34 +0200)
Disable treating the generated SQL fragment as a field name to avoid
quoting the whole fragment.

Tests for the MySQL and PostgreSQL versions of the statement have been
added.

Resolves: #75635
Releases: master
Change-Id: If15199032e905f7b2a1ca53bfc654d07038cd54a
Reviewed-on: https://review.typo3.org/47681
Reviewed-by: Nicole Cordes <typo3@cordes.co>
Reviewed-by: Jigal van Hemert <jigal.van.hemert@typo3.org>
Tested-by: Jigal van Hemert <jigal.van.hemert@typo3.org>
Reviewed-by: Richard Haeser <richardhaeser@gmail.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/core/Classes/Database/Query/Expression/ExpressionBuilder.php
typo3/sysext/core/Tests/Unit/Database/Query/Expression/ExpressionBuilderTest.php

index f6e8635..a4337c3 100644 (file)
@@ -293,12 +293,13 @@ class ExpressionBuilder
         switch ($this->connection->getDatabasePlatform()->getName()) {
             case 'postgresql':
             case 'pdo_postgresql':
-                return $this->eq(
+                return $this->comparison(
                     sprintf(
                         'any(string_to_array(%s, %s))',
                         $this->connection->quoteIdentifier($fieldName),
                         $this->literal(',')
                     ),
+                    self::EQ,
                     $value
                 );
                 break;
index 6d6bba8..c484fd0 100644 (file)
@@ -20,6 +20,7 @@ use Prophecy\Prophecy\ObjectProphecy;
 use TYPO3\CMS\Core\Database\Connection;
 use TYPO3\CMS\Core\Database\Query\Expression\CompositeExpression;
 use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
+use TYPO3\CMS\Core\Tests\Unit\Database\Mocks\MockPlatform;
 use TYPO3\CMS\Core\Tests\UnitTestCase;
 
 class ExpressionBuilderTest extends UnitTestCase
@@ -234,6 +235,45 @@ class ExpressionBuilderTest extends UnitTestCase
     /**
      * @test
      */
+    public function inSetForMySQL()
+    {
+        $databasePlatform = $this->prophesize(MockPlatform::class);
+        $databasePlatform->getName()->willReturn('mysql');
+
+        $this->connectionProphet->quoteIdentifier(Argument::cetera())->will(function ($args) {
+            return '`' . $args[0] . '`';
+        });
+
+        $this->connectionProphet->getDatabasePlatform()->willReturn($databasePlatform->reveal());
+
+        $result = $this->subject->inSet('aField', "'1'");
+
+        $this->assertSame('FIND_IN_SET(\'1\', `aField`)', $result);
+    }
+
+    /**
+     * @test
+     */
+    public function inSetForPostgreSQL()
+    {
+        $databasePlatform = $this->prophesize(MockPlatform::class);
+        $databasePlatform->getName()->willReturn('postgresql');
+
+        $this->connectionProphet->quote(',', Argument::cetera())->shouldBeCalled()->willReturn("','");
+        $this->connectionProphet->quoteIdentifier(Argument::cetera())->will(function ($args) {
+            return '"' . $args[0] . '"';
+        });
+
+        $this->connectionProphet->getDatabasePlatform()->willReturn($databasePlatform->reveal());
+
+        $result = $this->subject->inSet('aField', "'1'");
+
+        $this->assertSame('any(string_to_array("aField", \',\')) = \'1\'', $result);
+    }
+
+    /**
+     * @test
+     */
     public function literalQuotesValue()
     {
         $this->connectionProphet->quote('aField', 'Doctrine\DBAL\Types\StringType')