[BUGFIX] DBAL: Properly compile CAST statement after OR condition 70/45070/3
authorMorton Jonuschat <m.jonuschat@mojocode.de>
Mon, 30 Nov 2015 18:39:31 +0000 (19:39 +0100)
committerMorton Jonuschat <m.jonuschat@mojocode.de>
Tue, 1 Dec 2015 09:28:10 +0000 (10:28 +0100)
Properly output the CAST statement after the combination of
using FIND_IN_SET followed by an OR condition.

Resolves: #71979
Releases: master
Change-Id: If9dbdd0743c3bda43e23260ef78752176dd1b85d
Reviewed-on: https://review.typo3.org/45070
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Reviewed-by: Stephan GroƟberndt <stephan@grossberndt.de>
Reviewed-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Tested-by: Morton Jonuschat <m.jonuschat@mojocode.de>
typo3/sysext/dbal/Classes/Database/DatabaseConnection.php
typo3/sysext/dbal/Classes/Database/SqlCompilers/Adodb.php
typo3/sysext/dbal/Tests/Unit/Database/DatabaseConnectionPostgresqlTest.php
typo3/sysext/dbal/Tests/Unit/Database/DatabaseConnectionTest.php

index 0cc2a15..efbcc71 100644 (file)
@@ -1798,6 +1798,8 @@ class DatabaseConnection extends \TYPO3\CMS\Core\Database\DatabaseConnection
                             }
                         }
                         break;
+                    case 'CAST':
+                        // Intentional fallthrough
                     case 'IFNULL':
                         // Intentional fallthrough
                     case 'LOCATE':
index 60e565c..b47de60 100644 (file)
@@ -347,7 +347,12 @@ class Adodb extends AbstractCompiler
                 } elseif (isset($v['func']) && $v['func']['type'] === 'EXISTS') {
                     $output .= ' ' . trim($v['modifier']) . ' EXISTS (' . $this->compileSELECT($v['func']['subquery']) . ')';
                 } else {
-                    if (isset($v['func']) && $v['func']['type'] === 'LOCATE') {
+                    if (isset($v['func']) && $v['func']['type'] === 'CAST') {
+                        $output .= ' ' . trim($v['modifier']);
+                        $output .= ' CAST(';
+                        $output .= ($v['func']['table'] ? $v['func']['table'] . '.' : '') . $v['func']['field'];
+                        $output .= ' AS ' . $v['func']['datatype'][0] . ')';
+                    } elseif (isset($v['func']) && $v['func']['type'] === 'LOCATE') {
                         $output .= ' ' . trim($v['modifier']);
                         switch (true) {
                             case $this->databaseConnection->runningADOdbDriver('mssql') && $functionMapping:
index c3a621a..5b8ec06 100644 (file)
@@ -112,6 +112,17 @@ class DatabaseConnectionPostgresqlTest extends AbstractTestCase
 
     /**
      * @test
+     * @see https://forge.typo3.org/issues/71979
+     */
+    public function canCompileCastOperatorWithOrComparator()
+    {
+        $result = $this->subject->SELECTquery('uid', 'sys_category', 'FIND_IN_SET(\'0\',parent) OR CAST(parent AS CHAR) = \'\'');
+        $expected = 'SELECT "uid" FROM "sys_category" WHERE FIND_IN_SET(\'0\', CAST("parent" AS CHAR)) != 0 OR CAST("parent" AS CHAR) = \'\'';
+        $this->assertEquals($expected, $this->cleanSql($result));
+    }
+
+    /**
+     * @test
      * @see http://forge.typo3.org/issues/21514
      */
     public function likeBinaryOperatorIsRemappedToLike()
index 0deff8e..c9c5079 100644 (file)
@@ -246,6 +246,17 @@ class DatabaseConnectionTest extends AbstractTestCase
 
     /**
      * @test
+     * @see https://forge.typo3.org/issues/71979
+     */
+    public function canCompileCastOperatorWithOrComparator()
+    {
+        $result = $this->subject->SELECTquery('uid', 'sys_category', 'FIND_IN_SET(\'0\',parent) != 0 OR CAST(parent AS CHAR) = \'\'');
+        $expected = 'SELECT uid FROM sys_category WHERE FIND_IN_SET(\'0\',parent) != 0 OR CAST(parent AS CHAR) = \'\'';
+        $this->assertEquals($expected, $this->cleanSql($result));
+    }
+
+    /**
+     * @test
      * @see http://forge.typo3.org/issues/21514
      */
     public function likeBinaryOperatorIsKept()