From 7cf42e26c97ec78f910a36c402b69d009f06fd11 Mon Sep 17 00:00:00 2001 From: Amir Sarabadani Date: Mon, 3 Jul 2023 11:12:03 +0200 Subject: [PATCH] rdbms: Add support for ALL_ROWS in two query builders Delete and Update query builders set the ->where( Database::ALL_ROWS ) as [ Database::ALL_ROWS ] internally and this makes sure they are basically treated as condition simply being Database::ALL_ROWS Bug: T332329 Change-Id: I79c308a8951f99869ffa09bd47c8e9bdf23f312e --- includes/libs/rdbms/platform/SQLPlatform.php | 4 ++-- .../libs/rdbms/platform/SQLPlatformTest.php | 20 ++++++++++++++++++- .../querybuilder/DeleteQueryBuilderTest.php | 8 ++++++++ .../querybuilder/UpdateQueryBuilderTest.php | 9 +++++++++ 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/includes/libs/rdbms/platform/SQLPlatform.php b/includes/libs/rdbms/platform/SQLPlatform.php index 00cb4ee03fa..d4cc18fb9f2 100644 --- a/includes/libs/rdbms/platform/SQLPlatform.php +++ b/includes/libs/rdbms/platform/SQLPlatform.php @@ -1508,7 +1508,7 @@ class SQLPlatform implements ISQLPlatform { $condsSql = ''; $cleanCondsSql = ''; - if ( $conds !== self::ALL_ROWS ) { + if ( $conds !== self::ALL_ROWS && $conds !== [ self::ALL_ROWS ] ) { $cleanCondsSql = ' WHERE ' . $this->scrubConditions( $conds ); if ( is_array( $conds ) ) { $conds = $this->makeList( $conds, self::LIST_AND ); @@ -1541,7 +1541,7 @@ class SQLPlatform implements ISQLPlatform { $opts = $this->makeUpdateOptions( $options ); $sql = "UPDATE $opts $table SET " . $this->makeList( $set, self::LIST_SET ); - if ( $conds && $conds !== self::ALL_ROWS ) { + if ( $conds && $conds !== self::ALL_ROWS && $conds !== [ self::ALL_ROWS ] ) { if ( is_array( $conds ) ) { $conds = $this->makeList( $conds, self::LIST_AND ); } diff --git a/tests/phpunit/unit/includes/libs/rdbms/platform/SQLPlatformTest.php b/tests/phpunit/unit/includes/libs/rdbms/platform/SQLPlatformTest.php index 0dacea429b4..6cd9f487f63 100644 --- a/tests/phpunit/unit/includes/libs/rdbms/platform/SQLPlatformTest.php +++ b/tests/phpunit/unit/includes/libs/rdbms/platform/SQLPlatformTest.php @@ -508,6 +508,16 @@ class SQLPlatformTest extends PHPUnit\Framework\TestCase { "SET field = other" . ",field2 = 'text2'" ], + [ + [ + 'table' => 'table', + 'values' => [ 'field = other', 'field2' => 'text2' ], + 'conds' => [ '*' ], + ], + "UPDATE table " . + "SET field = other" . + ",field2 = 'text2'" + ], [ [ 'table' => 'table', @@ -537,7 +547,7 @@ class SQLPlatformTest extends PHPUnit\Framework\TestCase { "UPDATE table " . "SET field = other" . ",field2 = 'text2'", - ] + ], ]; } @@ -619,6 +629,14 @@ class SQLPlatformTest extends PHPUnit\Framework\TestCase { "DELETE FROM table", "DELETE FROM table", ], + [ + [ + 'table' => 'table', + 'conds' => [ '*' ], + ], + "DELETE FROM table", + "DELETE FROM table", + ], ]; } diff --git a/tests/phpunit/unit/includes/libs/rdbms/querybuilder/DeleteQueryBuilderTest.php b/tests/phpunit/unit/includes/libs/rdbms/querybuilder/DeleteQueryBuilderTest.php index d524c4b4110..7ee820d5227 100644 --- a/tests/phpunit/unit/includes/libs/rdbms/querybuilder/DeleteQueryBuilderTest.php +++ b/tests/phpunit/unit/includes/libs/rdbms/querybuilder/DeleteQueryBuilderTest.php @@ -1,6 +1,7 @@ assertSQL( 'DELETE FROM 1 WHERE k = \'v1\' AND (k = \'v2\')', __METHOD__ ); } + public function testCondsAllRows() { + $this->dqb + ->table( 'a' ) + ->where( ISQLPlatform::ALL_ROWS ); + $this->assertSQL( 'DELETE FROM a', __METHOD__ ); + } + public function testExecute() { $this->dqb->delete( 't' )->where( 'c' )->caller( __METHOD__ ); $this->dqb->execute(); diff --git a/tests/phpunit/unit/includes/libs/rdbms/querybuilder/UpdateQueryBuilderTest.php b/tests/phpunit/unit/includes/libs/rdbms/querybuilder/UpdateQueryBuilderTest.php index 484b2397f54..aa601a58858 100644 --- a/tests/phpunit/unit/includes/libs/rdbms/querybuilder/UpdateQueryBuilderTest.php +++ b/tests/phpunit/unit/includes/libs/rdbms/querybuilder/UpdateQueryBuilderTest.php @@ -1,5 +1,6 @@ assertSQL( 'UPDATE 1 SET a WHERE k = \'v1\' AND (k = \'v2\')', __METHOD__ ); } + public function testCondsAllRows() { + $this->uqb + ->update( '1' ) + ->set( 'a' ) + ->where( ISQLPlatform::ALL_ROWS ); + $this->assertSQL( 'UPDATE 1 SET a', __METHOD__ ); + } + public function testIgnore() { $this->uqb ->update( 'f' )