diff --git a/typo3/sysext/core/Classes/Database/Query/Expression/ExpressionBuilder.php b/typo3/sysext/core/Classes/Database/Query/Expression/ExpressionBuilder.php index a4337c35184dc9d6aeaee8f07e60d13e745fdabe..3246950503d7050b818e8890ab429c70adf6a398 100644 --- a/typo3/sysext/core/Classes/Database/Query/Expression/ExpressionBuilder.php +++ b/typo3/sysext/core/Classes/Database/Query/Expression/ExpressionBuilder.php @@ -326,6 +326,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. * diff --git a/typo3/sysext/core/Tests/Unit/Database/Query/Expression/ExpressionBuilderTest.php b/typo3/sysext/core/Tests/Unit/Database/Query/Expression/ExpressionBuilderTest.php index c484fd003023f5ec3134c7e358af5db19881d53d..1224f98f38873bb8837d271db8aa8625280ceb6f 100644 --- a/typo3/sysext/core/Tests/Unit/Database/Query/Expression/ExpressionBuilderTest.php +++ b/typo3/sysext/core/Tests/Unit/Database/Query/Expression/ExpressionBuilderTest.php @@ -271,6 +271,39 @@ class ExpressionBuilderTest extends UnitTestCase $this->assertSame('any(string_to_array("aField", \',\')) = \'1\'', $result); } + /** + * @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 */