[TASK] Doctrine: Extend ExpressionBuilder with bitwise and support 60/47760/4
authorMorton Jonuschat <m.jonuschat@mojocode.de>
Mon, 18 Apr 2016 18:24:30 +0000 (20:24 +0200)
committerMorton Jonuschat <m.jonuschat@mojocode.de>
Tue, 19 Apr 2016 04:26:48 +0000 (06:26 +0200)
Extend the ExpressionBuilder with support for creating bitwise and
operations. Oracle needs a special SQL function to perform an &
operation and the core requires bitwise operations.

Releases: master
Resolves: #75563
Change-Id: I2ae7e20a9a4ced5b16330c94a3e9a6f156ba5f61
Reviewed-on: https://review.typo3.org/47760
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Nicole Cordes <typo3@cordes.co>
Tested-by: Nicole Cordes <typo3@cordes.co>
Reviewed-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Tested-by: Morton Jonuschat <m.jonuschat@mojocode.de>
typo3/sysext/core/Classes/Database/Query/Expression/ExpressionBuilder.php
typo3/sysext/core/Tests/Unit/Database/Query/Expression/ExpressionBuilderTest.php

index a4337c3..3246950 100644 (file)
@@ -327,6 +327,32 @@ class ExpressionBuilder
     }
 
     /**
+     * Creates a bitwise AND expression with the given arguments.
+     *
+     * @param string $fieldName The fieldname. Will be quoted according to database platform automatically.
+     * @param int $value Argument to be used in the bitwise AND operation
+     * @return string
+     */
+    public function bitAnd(string $fieldName, int $value): string
+    {
+        switch ($this->connection->getDatabasePlatform()->getName()) {
+            case 'oci8':
+            case 'pdo_oracle':
+                return sprintf(
+                    'BITAND(%s, %s)',
+                    $this->connection->quoteIdentifier($fieldName),
+                    $value
+                );
+            default:
+                return $this->comparison(
+                    $this->connection->quoteIdentifier($fieldName),
+                    '&',
+                    $value
+                );
+        }
+    }
+
+    /**
      * Quotes a given input parameter.
      *
      * @param mixed $input The parameter to be quoted.
index c484fd0..1224f98 100644 (file)
@@ -274,6 +274,39 @@ class ExpressionBuilderTest extends UnitTestCase
     /**
      * @test
      */
+    public function defaultBitwiseAnd()
+    {
+        $databasePlatform = $this->prophesize(MockPlatform::class);
+
+        $this->connectionProphet->quoteIdentifier(Argument::cetera())->will(function ($args) {
+            return '"' . $args[0] . '"';
+        });
+
+        $this->connectionProphet->getDatabasePlatform()->willReturn($databasePlatform->reveal());
+
+        $this->assertSame('"aField" & 1', $this->subject->bitAnd('aField', 1));
+    }
+
+    /**
+     * @test
+     */
+    public function bitwiseAndForOracle()
+    {
+        $databasePlatform = $this->prophesize(MockPlatform::class);
+        $databasePlatform->getName()->willReturn('pdo_oracle');
+
+        $this->connectionProphet->quoteIdentifier(Argument::cetera())->will(function ($args) {
+            return '"' . $args[0] . '"';
+        });
+
+        $this->connectionProphet->getDatabasePlatform()->willReturn($databasePlatform->reveal());
+
+        $this->assertSame('BITAND("aField", 1)', $this->subject->bitAnd('aField', 1));
+    }
+
+    /**
+     * @test
+     */
     public function literalQuotesValue()
     {
         $this->connectionProphet->quote('aField', 'Doctrine\DBAL\Types\StringType')