Skip to content

Commit 51868a0

Browse files
Merge pull request #19457 from kamil-tekiela/Dbi-named-params-and-unbuffered
Dbi named params and unbuffered param
2 parents c69fa7c + dd05ed6 commit 51868a0

8 files changed

Lines changed: 29 additions & 73 deletions

File tree

src/DatabaseInterface.php

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,10 @@ class DatabaseInterface implements DbalInterface
8181
{
8282
public static self|null $instance = null;
8383

84-
/**
85-
* Force STORE_RESULT method, ignored by classic MySQL.
86-
*/
87-
public const QUERY_BUFFERED = 0;
88-
8984
/**
9085
* Do not read all rows immediately.
9186
*/
92-
public const QUERY_UNBUFFERED = 2;
87+
public const QUERY_UNBUFFERED = true;
9388

9489
/**
9590
* Get session variable.
@@ -165,20 +160,13 @@ public static function getInstance(): self
165160
return self::$instance;
166161
}
167162

168-
/**
169-
* runs a query
170-
*
171-
* @param string $query SQL query to execute
172-
* @param int $options optional query options
173-
* @param bool $cacheAffectedRows whether to cache affected rows
174-
*/
175163
public function query(
176164
string $query,
177165
ConnectionType $connectionType = ConnectionType::User,
178-
int $options = self::QUERY_BUFFERED,
166+
bool $unbuffered = false,
179167
bool $cacheAffectedRows = true,
180168
): ResultInterface {
181-
$result = $this->tryQuery($query, $connectionType, $options, $cacheAffectedRows);
169+
$result = $this->tryQuery($query, $connectionType, $unbuffered, $cacheAffectedRows);
182170

183171
if (! $result) {
184172
Generator::mysqlDie($this->getError($connectionType), $query);
@@ -192,19 +180,10 @@ public function getCache(): Cache
192180
return $this->cache;
193181
}
194182

195-
/**
196-
* runs a query and returns the result
197-
*
198-
* @param string $query query to run
199-
* @param int $options if DatabaseInterface::QUERY_UNBUFFERED
200-
* is provided, it will instruct the extension
201-
* to use unbuffered mode
202-
* @param bool $cacheAffectedRows whether to cache affected row
203-
*/
204183
public function tryQuery(
205184
string $query,
206185
ConnectionType $connectionType = ConnectionType::User,
207-
int $options = self::QUERY_BUFFERED,
186+
bool $unbuffered = false,
208187
bool $cacheAffectedRows = true,
209188
): ResultInterface|false {
210189
if (! isset($this->connections[$connectionType->value])) {
@@ -213,7 +192,7 @@ public function tryQuery(
213192

214193
$time = microtime(true);
215194

216-
$result = $this->extension->realQuery($query, $this->connections[$connectionType->value], $options);
195+
$result = $this->extension->realQuery($query, $this->connections[$connectionType->value], $unbuffered);
217196

218197
if ($connectionType === ConnectionType::User) {
219198
$this->lastQueryExecutionTime = microtime(true) - $time;
@@ -288,7 +267,7 @@ public function queryAsControlUser(string $sql): ResultInterface
288267
// is called for tracking purposes but we want to display the correct number
289268
// of rows affected by the original query, not by the query generated for
290269
// tracking.
291-
return $this->query($sql, ConnectionType::ControlUser, self::QUERY_BUFFERED, false);
270+
return $this->query($sql, ConnectionType::ControlUser, cacheAffectedRows: false);
292271
}
293272

294273
/**
@@ -305,7 +284,7 @@ public function tryQueryAsControlUser(string $sql): ResultInterface|false
305284
// is called for tracking purposes but we want to display the correct number
306285
// of rows affected by the original query, not by the query generated for
307286
// tracking.
308-
return $this->tryQuery($sql, ConnectionType::ControlUser, self::QUERY_BUFFERED, false);
287+
return $this->tryQuery($sql, ConnectionType::ControlUser, cacheAffectedRows: false);
309288
}
310289

311290
/**
@@ -1195,7 +1174,7 @@ public function fetchValue(
11951174
int|string $field = 0,
11961175
ConnectionType $connectionType = ConnectionType::User,
11971176
): string|false|null {
1198-
$result = $this->tryQuery($query, $connectionType, self::QUERY_BUFFERED, false);
1177+
$result = $this->tryQuery($query, $connectionType, cacheAffectedRows: false);
11991178
if ($result === false) {
12001179
return false;
12011180
}
@@ -1224,7 +1203,7 @@ public function fetchSingleRow(
12241203
string $type = DbalInterface::FETCH_ASSOC,
12251204
ConnectionType $connectionType = ConnectionType::User,
12261205
): array|null {
1227-
$result = $this->tryQuery($query, $connectionType, self::QUERY_BUFFERED, false);
1206+
$result = $this->tryQuery($query, $connectionType, cacheAffectedRows: false);
12281207
if ($result === false) {
12291208
return null;
12301209
}
@@ -1315,7 +1294,7 @@ public function fetchResult(
13151294
): array {
13161295
$resultRows = [];
13171296

1318-
$result = $this->tryQuery($query, $connectionType, self::QUERY_BUFFERED, false);
1297+
$result = $this->tryQuery($query, $connectionType, cacheAffectedRows: false);
13191298

13201299
// return empty array if result is empty or false
13211300
if ($result === false) {
@@ -1397,7 +1376,7 @@ public function getCompatibilities(): array
13971376
*/
13981377
public function getWarnings(ConnectionType $connectionType = ConnectionType::User): array
13991378
{
1400-
$result = $this->tryQuery('SHOW WARNINGS', $connectionType, 0, false);
1379+
$result = $this->tryQuery('SHOW WARNINGS', $connectionType, cacheAffectedRows: false);
14011380
if ($result === false) {
14021381
return [];
14031382
}

src/Dbal/DbalInterface.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,24 @@ interface DbalInterface
2121
public const FETCH_ASSOC = 'ASSOC';
2222

2323
/**
24-
* runs a query
25-
*
26-
* @param string $query SQL query to execute
27-
* @param int $options optional query options
28-
* @param bool $cacheAffectedRows whether to cache affected rows
24+
* Executes a query and returns the result
2925
*/
3026
public function query(
3127
string $query,
3228
ConnectionType $connectionType = ConnectionType::User,
33-
int $options = 0,
29+
bool $unbuffered = false,
3430
bool $cacheAffectedRows = true,
3531
): ResultInterface;
3632

3733
/**
38-
* runs a query and returns the result
39-
*
40-
* @param string $query query to run
41-
* @param int $options query options
42-
* @param bool $cacheAffectedRows whether to cache affected row
34+
* Executes a query and returns the result or false on error
4335
*/
4436
public function tryQuery(
4537
string $query,
4638
ConnectionType $connectionType = ConnectionType::User,
47-
int $options = 0,
39+
bool $unbuffered = false,
4840
bool $cacheAffectedRows = true,
49-
): mixed;
41+
): ResultInterface|false;
5042

5143
/**
5244
* Send multiple SQL queries to the database server and execute the first one

src/Dbal/DbiExtension.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,9 @@ public function selectDb(string|DatabaseName $databaseName, Connection $connecti
3232
/**
3333
* runs a query and returns the result
3434
*
35-
* @param string $query query to execute
36-
* @param int $options query options
37-
*
3835
* @return ResultInterface|false result
3936
*/
40-
public function realQuery(string $query, Connection $connection, int $options): ResultInterface|false;
37+
public function realQuery(string $query, Connection $connection, bool $unbuffered = false): ResultInterface|false;
4138

4239
/**
4340
* Run the multi query and output the results

src/Dbal/DbiMysqli.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use mysqli_sql_exception;
1212
use PhpMyAdmin\Config;
1313
use PhpMyAdmin\Config\Settings\Server;
14-
use PhpMyAdmin\DatabaseInterface;
1514
use PhpMyAdmin\Identifiers\DatabaseName;
1615
use PhpMyAdmin\Query\Utilities;
1716

@@ -154,21 +153,13 @@ public function selectDb(string|DatabaseName $databaseName, Connection $connecti
154153

155154
/**
156155
* runs a query and returns the result
157-
*
158-
* @param string $query query to execute
159-
* @param int $options query options
160156
*/
161-
public function realQuery(string $query, Connection $connection, int $options): MysqliResult|false
157+
public function realQuery(string $query, Connection $connection, bool $unbuffered = false): MysqliResult|false
162158
{
163-
$method = MYSQLI_STORE_RESULT;
164-
if ($options === ($options | DatabaseInterface::QUERY_UNBUFFERED)) {
165-
$method = MYSQLI_USE_RESULT;
166-
}
167-
168159
/** @var mysqli $mysqli */
169160
$mysqli = $connection->connection;
170161

171-
$result = $mysqli->query($query, $method);
162+
$result = $mysqli->query($query, $unbuffered ? MYSQLI_USE_RESULT : MYSQLI_STORE_RESULT);
172163
if ($result === false) {
173164
return false;
174165
}

tests/unit/Dbal/DbiMysqliTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function testrealQuery(): void
7777
->with(self::equalTo($query))
7878
->willReturn($mysqliResult);
7979

80-
self::assertInstanceOf(MysqliResult::class, $this->object->realQuery($query, new Connection($mysqli), 0));
80+
self::assertInstanceOf(MysqliResult::class, $this->object->realQuery($query, new Connection($mysqli)));
8181
}
8282

8383
/**

tests/unit/Stubs/DbiDummy.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,8 @@ private function findDummyQuery(string $query): array|null
168168

169169
/**
170170
* runs a query and returns the result
171-
*
172-
* @param string $query query to run
173-
* @param int $options query options
174171
*/
175-
public function realQuery(string $query, Connection $connection, int $options): DummyResult|false
172+
public function realQuery(string $query, Connection $connection, bool $unbuffered = false): DummyResult|false
176173
{
177174
$query = trim((string) preg_replace('/ */', ' ', str_replace("\n", ' ', $query)));
178175
$found = $this->findFifoQuery($query) ?? $this->findDummyQuery($query);

tests/unit/Table/TableTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,29 +1399,29 @@ public function testMoveCopy(): void
13991399
[
14001400
'SHOW CREATE TABLE `aa`.`ad`',
14011401
ConnectionType::User,
1402-
DatabaseInterface::QUERY_BUFFERED,
1402+
false,
14031403
true,
14041404
$resultStub,
14051405
],
14061406
[
14071407
'SHOW TABLE STATUS FROM `aa` WHERE Name = \'ad\'',
14081408
ConnectionType::User,
1409-
DatabaseInterface::QUERY_BUFFERED,
1409+
false,
14101410
true,
14111411
$resultStub,
14121412
],
1413-
['USE `aa`', ConnectionType::User, DatabaseInterface::QUERY_BUFFERED, true, $resultStub],
1413+
['USE `aa`', ConnectionType::User, false, true, $resultStub],
14141414
[
14151415
'RENAME TABLE `PMA`.`PMA_BookMark` TO `PMA`.`PMA_.BookMark`;',
14161416
ConnectionType::User,
1417-
DatabaseInterface::QUERY_BUFFERED,
1417+
false,
14181418
true,
14191419
false,
14201420
],
14211421
[
14221422
'RENAME TABLE `aa`.`ad` TO `bb`.`ad`;',
14231423
ConnectionType::User,
1424-
DatabaseInterface::QUERY_BUFFERED,
1424+
false,
14251425
true,
14261426
false,
14271427
],

tests/unit/Tracking/TrackerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ public function testCreateVersion(): void
184184
$useStatement = 'USE `pma_test`';
185185
$showCreateTableQuery = 'SHOW CREATE TABLE `pma_test`.`pma_tbl`';
186186
$dbi->expects(self::exactly(3))->method('tryQuery')->willReturnMap([
187-
[$showTableStatusQuery, ConnectionType::User, DatabaseInterface::QUERY_BUFFERED, true, $resultStub],
188-
[$useStatement, ConnectionType::User, DatabaseInterface::QUERY_BUFFERED, true, $resultStub],
189-
[$showCreateTableQuery, ConnectionType::User, DatabaseInterface::QUERY_BUFFERED, true, $resultStub],
187+
[$showTableStatusQuery, ConnectionType::User, false, true, $resultStub],
188+
[$useStatement, ConnectionType::User, false, true, $resultStub],
189+
[$showCreateTableQuery, ConnectionType::User, false, true, $resultStub],
190190
]);
191191

192192
$dbi->expects(self::any())->method('query')

0 commit comments

Comments
 (0)