From c9a3c06fe12f13b7428a6271f446b886b9dc2410 Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Tue, 17 Dec 2024 22:12:22 +0000 Subject: [PATCH 1/2] Use named parameter Signed-off-by: Kamil Tekiela --- src/DatabaseInterface.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/DatabaseInterface.php b/src/DatabaseInterface.php index f1c3bade58..787b53e450 100644 --- a/src/DatabaseInterface.php +++ b/src/DatabaseInterface.php @@ -288,7 +288,7 @@ class DatabaseInterface implements DbalInterface // is called for tracking purposes but we want to display the correct number // of rows affected by the original query, not by the query generated for // tracking. - return $this->query($sql, ConnectionType::ControlUser, self::QUERY_BUFFERED, false); + return $this->query($sql, ConnectionType::ControlUser, cacheAffectedRows: false); } /** @@ -305,7 +305,7 @@ class DatabaseInterface implements DbalInterface // is called for tracking purposes but we want to display the correct number // of rows affected by the original query, not by the query generated for // tracking. - return $this->tryQuery($sql, ConnectionType::ControlUser, self::QUERY_BUFFERED, false); + return $this->tryQuery($sql, ConnectionType::ControlUser, cacheAffectedRows: false); } /** @@ -1195,7 +1195,7 @@ class DatabaseInterface implements DbalInterface int|string $field = 0, ConnectionType $connectionType = ConnectionType::User, ): string|false|null { - $result = $this->tryQuery($query, $connectionType, self::QUERY_BUFFERED, false); + $result = $this->tryQuery($query, $connectionType, cacheAffectedRows: false); if ($result === false) { return false; } @@ -1224,7 +1224,7 @@ class DatabaseInterface implements DbalInterface string $type = DbalInterface::FETCH_ASSOC, ConnectionType $connectionType = ConnectionType::User, ): array|null { - $result = $this->tryQuery($query, $connectionType, self::QUERY_BUFFERED, false); + $result = $this->tryQuery($query, $connectionType, cacheAffectedRows: false); if ($result === false) { return null; } @@ -1315,7 +1315,7 @@ class DatabaseInterface implements DbalInterface ): array { $resultRows = []; - $result = $this->tryQuery($query, $connectionType, self::QUERY_BUFFERED, false); + $result = $this->tryQuery($query, $connectionType, cacheAffectedRows: false); // return empty array if result is empty or false if ($result === false) { @@ -1397,7 +1397,7 @@ class DatabaseInterface implements DbalInterface */ public function getWarnings(ConnectionType $connectionType = ConnectionType::User): array { - $result = $this->tryQuery('SHOW WARNINGS', $connectionType, 0, false); + $result = $this->tryQuery('SHOW WARNINGS', $connectionType, cacheAffectedRows: false); if ($result === false) { return []; } From dd05ed6e45ae8340974ac831119b5eb053ede41c Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Tue, 17 Dec 2024 22:38:56 +0000 Subject: [PATCH 2/2] Change $options into $unbuffered Signed-off-by: Kamil Tekiela --- src/DatabaseInterface.php | 31 +++++------------------------ src/Dbal/DbalInterface.php | 18 +++++------------ src/Dbal/DbiExtension.php | 5 +---- src/Dbal/DbiMysqli.php | 13 ++---------- tests/unit/Dbal/DbiMysqliTest.php | 2 +- tests/unit/Stubs/DbiDummy.php | 5 +---- tests/unit/Table/TableTest.php | 10 +++++----- tests/unit/Tracking/TrackerTest.php | 6 +++--- 8 files changed, 23 insertions(+), 67 deletions(-) diff --git a/src/DatabaseInterface.php b/src/DatabaseInterface.php index 787b53e450..e018c8f74b 100644 --- a/src/DatabaseInterface.php +++ b/src/DatabaseInterface.php @@ -81,15 +81,10 @@ class DatabaseInterface implements DbalInterface { public static self|null $instance = null; - /** - * Force STORE_RESULT method, ignored by classic MySQL. - */ - public const QUERY_BUFFERED = 0; - /** * Do not read all rows immediately. */ - public const QUERY_UNBUFFERED = 2; + public const QUERY_UNBUFFERED = true; /** * Get session variable. @@ -165,20 +160,13 @@ class DatabaseInterface implements DbalInterface return self::$instance; } - /** - * runs a query - * - * @param string $query SQL query to execute - * @param int $options optional query options - * @param bool $cacheAffectedRows whether to cache affected rows - */ public function query( string $query, ConnectionType $connectionType = ConnectionType::User, - int $options = self::QUERY_BUFFERED, + bool $unbuffered = false, bool $cacheAffectedRows = true, ): ResultInterface { - $result = $this->tryQuery($query, $connectionType, $options, $cacheAffectedRows); + $result = $this->tryQuery($query, $connectionType, $unbuffered, $cacheAffectedRows); if (! $result) { Generator::mysqlDie($this->getError($connectionType), $query); @@ -192,19 +180,10 @@ class DatabaseInterface implements DbalInterface return $this->cache; } - /** - * runs a query and returns the result - * - * @param string $query query to run - * @param int $options if DatabaseInterface::QUERY_UNBUFFERED - * is provided, it will instruct the extension - * to use unbuffered mode - * @param bool $cacheAffectedRows whether to cache affected row - */ public function tryQuery( string $query, ConnectionType $connectionType = ConnectionType::User, - int $options = self::QUERY_BUFFERED, + bool $unbuffered = false, bool $cacheAffectedRows = true, ): ResultInterface|false { if (! isset($this->connections[$connectionType->value])) { @@ -213,7 +192,7 @@ class DatabaseInterface implements DbalInterface $time = microtime(true); - $result = $this->extension->realQuery($query, $this->connections[$connectionType->value], $options); + $result = $this->extension->realQuery($query, $this->connections[$connectionType->value], $unbuffered); if ($connectionType === ConnectionType::User) { $this->lastQueryExecutionTime = microtime(true) - $time; diff --git a/src/Dbal/DbalInterface.php b/src/Dbal/DbalInterface.php index 7c8a89af84..4e6776a204 100644 --- a/src/Dbal/DbalInterface.php +++ b/src/Dbal/DbalInterface.php @@ -21,32 +21,24 @@ interface DbalInterface public const FETCH_ASSOC = 'ASSOC'; /** - * runs a query - * - * @param string $query SQL query to execute - * @param int $options optional query options - * @param bool $cacheAffectedRows whether to cache affected rows + * Executes a query and returns the result */ public function query( string $query, ConnectionType $connectionType = ConnectionType::User, - int $options = 0, + bool $unbuffered = false, bool $cacheAffectedRows = true, ): ResultInterface; /** - * runs a query and returns the result - * - * @param string $query query to run - * @param int $options query options - * @param bool $cacheAffectedRows whether to cache affected row + * Executes a query and returns the result or false on error */ public function tryQuery( string $query, ConnectionType $connectionType = ConnectionType::User, - int $options = 0, + bool $unbuffered = false, bool $cacheAffectedRows = true, - ): mixed; + ): ResultInterface|false; /** * Send multiple SQL queries to the database server and execute the first one diff --git a/src/Dbal/DbiExtension.php b/src/Dbal/DbiExtension.php index c83082b78b..619f831b54 100644 --- a/src/Dbal/DbiExtension.php +++ b/src/Dbal/DbiExtension.php @@ -32,12 +32,9 @@ interface DbiExtension /** * runs a query and returns the result * - * @param string $query query to execute - * @param int $options query options - * * @return ResultInterface|false result */ - public function realQuery(string $query, Connection $connection, int $options): ResultInterface|false; + public function realQuery(string $query, Connection $connection, bool $unbuffered = false): ResultInterface|false; /** * Run the multi query and output the results diff --git a/src/Dbal/DbiMysqli.php b/src/Dbal/DbiMysqli.php index ab3d6f9763..dd993d4b3b 100644 --- a/src/Dbal/DbiMysqli.php +++ b/src/Dbal/DbiMysqli.php @@ -11,7 +11,6 @@ use mysqli; use mysqli_sql_exception; use PhpMyAdmin\Config; use PhpMyAdmin\Config\Settings\Server; -use PhpMyAdmin\DatabaseInterface; use PhpMyAdmin\Identifiers\DatabaseName; use PhpMyAdmin\Query\Utilities; @@ -154,21 +153,13 @@ class DbiMysqli implements DbiExtension /** * runs a query and returns the result - * - * @param string $query query to execute - * @param int $options query options */ - public function realQuery(string $query, Connection $connection, int $options): MysqliResult|false + public function realQuery(string $query, Connection $connection, bool $unbuffered = false): MysqliResult|false { - $method = MYSQLI_STORE_RESULT; - if ($options === ($options | DatabaseInterface::QUERY_UNBUFFERED)) { - $method = MYSQLI_USE_RESULT; - } - /** @var mysqli $mysqli */ $mysqli = $connection->connection; - $result = $mysqli->query($query, $method); + $result = $mysqli->query($query, $unbuffered ? MYSQLI_USE_RESULT : MYSQLI_STORE_RESULT); if ($result === false) { return false; } diff --git a/tests/unit/Dbal/DbiMysqliTest.php b/tests/unit/Dbal/DbiMysqliTest.php index cb4d9c04c1..f90e26bc52 100644 --- a/tests/unit/Dbal/DbiMysqliTest.php +++ b/tests/unit/Dbal/DbiMysqliTest.php @@ -77,7 +77,7 @@ class DbiMysqliTest extends AbstractTestCase ->with(self::equalTo($query)) ->willReturn($mysqliResult); - self::assertInstanceOf(MysqliResult::class, $this->object->realQuery($query, new Connection($mysqli), 0)); + self::assertInstanceOf(MysqliResult::class, $this->object->realQuery($query, new Connection($mysqli))); } /** diff --git a/tests/unit/Stubs/DbiDummy.php b/tests/unit/Stubs/DbiDummy.php index 25c1d1f97d..ca334e25c0 100644 --- a/tests/unit/Stubs/DbiDummy.php +++ b/tests/unit/Stubs/DbiDummy.php @@ -168,11 +168,8 @@ class DbiDummy implements DbiExtension /** * runs a query and returns the result - * - * @param string $query query to run - * @param int $options query options */ - public function realQuery(string $query, Connection $connection, int $options): DummyResult|false + public function realQuery(string $query, Connection $connection, bool $unbuffered = false): DummyResult|false { $query = trim((string) preg_replace('/ */', ' ', str_replace("\n", ' ', $query))); $found = $this->findFifoQuery($query) ?? $this->findDummyQuery($query); diff --git a/tests/unit/Table/TableTest.php b/tests/unit/Table/TableTest.php index 595cd03d4b..e5d15a4019 100644 --- a/tests/unit/Table/TableTest.php +++ b/tests/unit/Table/TableTest.php @@ -1399,29 +1399,29 @@ class TableTest extends AbstractTestCase [ 'SHOW CREATE TABLE `aa`.`ad`', ConnectionType::User, - DatabaseInterface::QUERY_BUFFERED, + false, true, $resultStub, ], [ 'SHOW TABLE STATUS FROM `aa` WHERE Name = \'ad\'', ConnectionType::User, - DatabaseInterface::QUERY_BUFFERED, + false, true, $resultStub, ], - ['USE `aa`', ConnectionType::User, DatabaseInterface::QUERY_BUFFERED, true, $resultStub], + ['USE `aa`', ConnectionType::User, false, true, $resultStub], [ 'RENAME TABLE `PMA`.`PMA_BookMark` TO `PMA`.`PMA_.BookMark`;', ConnectionType::User, - DatabaseInterface::QUERY_BUFFERED, + false, true, false, ], [ 'RENAME TABLE `aa`.`ad` TO `bb`.`ad`;', ConnectionType::User, - DatabaseInterface::QUERY_BUFFERED, + false, true, false, ], diff --git a/tests/unit/Tracking/TrackerTest.php b/tests/unit/Tracking/TrackerTest.php index a0c029d97c..2b83d5f1c7 100644 --- a/tests/unit/Tracking/TrackerTest.php +++ b/tests/unit/Tracking/TrackerTest.php @@ -184,9 +184,9 @@ class TrackerTest extends AbstractTestCase $useStatement = 'USE `pma_test`'; $showCreateTableQuery = 'SHOW CREATE TABLE `pma_test`.`pma_tbl`'; $dbi->expects(self::exactly(3))->method('tryQuery')->willReturnMap([ - [$showTableStatusQuery, ConnectionType::User, DatabaseInterface::QUERY_BUFFERED, true, $resultStub], - [$useStatement, ConnectionType::User, DatabaseInterface::QUERY_BUFFERED, true, $resultStub], - [$showCreateTableQuery, ConnectionType::User, DatabaseInterface::QUERY_BUFFERED, true, $resultStub], + [$showTableStatusQuery, ConnectionType::User, false, true, $resultStub], + [$useStatement, ConnectionType::User, false, true, $resultStub], + [$showCreateTableQuery, ConnectionType::User, false, true, $resultStub], ]); $dbi->expects(self::any())->method('query')