Skip to content

Commit 37018db

Browse files
Merge pull request #19487 from kamil-tekiela/Improvements-to-DatabaseInterface
Improvements to database interface
2 parents 47652b1 + 04d3912 commit 37018db

File tree

14 files changed

+64
-153
lines changed

14 files changed

+64
-153
lines changed

phpstan-baseline.neon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6684,7 +6684,7 @@ parameters:
66846684
-
66856685
message: '#^Only booleans are allowed in a negated boolean, PhpMyAdmin\\Dbal\\ResultInterface\|false given\.$#'
66866686
identifier: booleanNot.exprNotBoolean
6687-
count: 3
6687+
count: 2
66886688
path: src/Dbal/DatabaseInterface.php
66896689

66906690
-
@@ -10170,7 +10170,7 @@ parameters:
1017010170
Use dependency injection instead\.$#
1017110171
'''
1017210172
identifier: staticMethod.deprecated
10173-
count: 7
10173+
count: 8
1017410174
path: src/Navigation/Nodes/Node.php
1017510175

1017610176
-

psalm-baseline.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5993,6 +5993,7 @@
59935993
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
59945994
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
59955995
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
5996+
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
59965997
</DeprecatedMethod>
59975998
<MixedArgument>
59985999
<code><![CDATA[$db]]></code>

src/Dbal/DatabaseInterface.php

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,25 +1711,6 @@ public function getError(ConnectionType $connectionType = ConnectionType::User):
17111711
return $this->extension->getError($this->connections[$connectionType->value]);
17121712
}
17131713

1714-
/**
1715-
* returns the number of rows returned by last query
1716-
* used with tryQuery as it accepts false
1717-
*
1718-
* @param string $query query to run
1719-
*
1720-
* @psalm-return int|numeric-string
1721-
*/
1722-
public function queryAndGetNumRows(string $query): string|int
1723-
{
1724-
$result = $this->tryQuery($query);
1725-
1726-
if (! $result) {
1727-
return 0;
1728-
}
1729-
1730-
return $result->numRows();
1731-
}
1732-
17331714
/**
17341715
* returns last inserted auto_increment id for given $link
17351716
*/
@@ -1970,14 +1951,13 @@ public function setVersion(array $version): void
19701951
$this->isPercona = stripos($this->versionComment, 'percona') !== false;
19711952
}
19721953

1973-
/**
1974-
* Prepare an SQL statement for execution.
1975-
*
1976-
* @param string $query The query, as a string.
1977-
*/
1978-
public function prepare(string $query, ConnectionType $connectionType = ConnectionType::User): Statement|null
1979-
{
1980-
return $this->extension->prepare($this->connections[$connectionType->value], $query);
1954+
/** @param list<string> $params */
1955+
public function executeQuery(
1956+
string $query,
1957+
array $params,
1958+
ConnectionType $connectionType = ConnectionType::User,
1959+
): ResultInterface|null {
1960+
return $this->extension->executeQuery($this->connections[$connectionType->value], $query, $params);
19811961
}
19821962

19831963
public function getDatabaseList(): ListDatabase

src/Dbal/DbiExtension.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ public function affectedRows(Connection $connection): int|string;
9191
public function escapeString(Connection $connection, string $string): string;
9292

9393
/**
94-
* Prepare an SQL statement for execution.
94+
* Execute a prepared statement and return the result.
9595
*
96-
* @param string $query The query, as a string.
96+
* @param list<string> $params
9797
*/
98-
public function prepare(Connection $connection, string $query): Statement|null;
98+
public function executeQuery(Connection $connection, string $query, array $params): ResultInterface|null;
9999

100100
/**
101101
* Returns the number of warnings from the last query.

src/Dbal/DbiMysqli.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -284,20 +284,21 @@ public function escapeString(Connection $connection, string $string): string
284284
}
285285

286286
/**
287-
* Prepare an SQL statement for execution.
287+
* Execute a prepared statement and return the result.
288288
*
289-
* @param string $query The query, as a string.
289+
* @param list<string> $params
290290
*/
291-
public function prepare(Connection $connection, string $query): Statement|null
291+
public function executeQuery(Connection $connection, string $query, array $params): MysqliResult|null
292292
{
293293
/** @var mysqli $mysqli */
294294
$mysqli = $connection->connection;
295-
$statement = $mysqli->prepare($query);
296-
if ($statement === false) {
295+
$result = $mysqli->execute_query($query, $params);
296+
297+
if ($result === false) {
297298
return null;
298299
}
299300

300-
return new MysqliStatement($statement);
301+
return new MysqliResult($result);
301302
}
302303

303304
/**

src/Dbal/MysqliStatement.php

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/Dbal/Statement.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/Navigation/Nodes/Node.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,13 +379,13 @@ public function getPresence(UserPrivileges $userPrivileges, string $type = '', s
379379
$query = 'SHOW DATABASES ';
380380
$query .= $this->getWhereClause('Database', $searchClause);
381381

382-
return (int) $dbi->queryAndGetNumRows($query);
382+
return $this->queryAndGetNumRows($query);
383383
}
384384

385385
$retval = 0;
386386
foreach ($this->getDatabasesToSearch($userPrivileges, $searchClause) as $db) {
387387
$query = 'SHOW DATABASES LIKE ' . $dbi->quoteString($db);
388-
$retval += (int) $dbi->queryAndGetNumRows($query);
388+
$retval += $this->queryAndGetNumRows($query);
389389
}
390390

391391
return $retval;
@@ -831,4 +831,20 @@ private function getDataFromShowDatabasesLike(UserPrivileges $userPrivileges, in
831831

832832
return $retval;
833833
}
834+
835+
/**
836+
* returns the number of rows returned by last query
837+
* used with tryQuery as it accepts false
838+
*/
839+
protected function queryAndGetNumRows(string $query): int
840+
{
841+
$dbi = DatabaseInterface::getInstance();
842+
$result = $dbi->tryQuery($query);
843+
844+
if ($result === false) {
845+
return 0;
846+
}
847+
848+
return (int) $result->numRows();
849+
}
834850
}

src/Navigation/Nodes/NodeTable.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,15 @@ public function getPresence(UserPrivileges $userPrivileges, string $type = '', s
9494
$db = Util::backquote($db);
9595
$table = Util::backquote($table);
9696
$query = 'SHOW COLUMNS FROM ' . $table . ' FROM ' . $db;
97-
$retval = (int) $dbi->queryAndGetNumRows($query);
97+
$retval = $this->queryAndGetNumRows($query);
9898
}
9999

100100
break;
101101
case 'indexes':
102102
$db = Util::backquote($db);
103103
$table = Util::backquote($table);
104104
$query = 'SHOW INDEXES FROM ' . $table . ' FROM ' . $db;
105-
$retval = (int) $dbi->queryAndGetNumRows($query);
105+
$retval = $this->queryAndGetNumRows($query);
106106
break;
107107
case 'triggers':
108108
if (! $this->config->selectedServer['DisableIS']) {
@@ -116,7 +116,7 @@ public function getPresence(UserPrivileges $userPrivileges, string $type = '', s
116116
} else {
117117
$db = Util::backquote($db);
118118
$query = 'SHOW TRIGGERS FROM ' . $db . ' WHERE `Table` = ' . $dbi->quoteString($table);
119-
$retval = (int) $dbi->queryAndGetNumRows($query);
119+
$retval = $this->queryAndGetNumRows($query);
120120
}
121121

122122
break;

src/Server/Privileges.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,12 +1196,12 @@ private function getTablePrivileges(DatabaseName $db, TableName $table): array
11961196
NOT (`Table_priv` = \'\' AND Column_priv = \'\')
11971197
ORDER BY `User` ASC, `Host` ASC, `Db` ASC, `Table_priv` ASC;
11981198
';
1199-
$statement = $this->dbi->prepare($query);
1200-
if ($statement === null || ! $statement->execute([$db->getName(), $table->getName()])) {
1199+
$result = $this->dbi->executeQuery($query, [$db->getName(), $table->getName()]);
1200+
if ($result === null) {
12011201
return [];
12021202
}
12031203

1204-
return $statement->getResult()->fetchAllAssoc();
1204+
return $result->fetchAllAssoc();
12051205
}
12061206

12071207
/** @return array<int, array<string|null>> */
@@ -3171,12 +3171,11 @@ public function getFormForChangePassword(
31713171
private function getUserPrivileges(string $user, string $host, bool $hasAccountLocking): array|null
31723172
{
31733173
$query = 'SELECT * FROM `mysql`.`user` WHERE `User` = ? AND `Host` = ?;';
3174-
$statement = $this->dbi->prepare($query);
3175-
if ($statement === null || ! $statement->execute([$user, $host])) {
3174+
$result = $this->dbi->executeQuery($query, [$user, $host]);
3175+
if ($result === null) {
31763176
return null;
31773177
}
31783178

3179-
$result = $statement->getResult();
31803179
/** @var array<string, string|null>|null $userPrivileges */
31813180
$userPrivileges = $result->fetchAssoc();
31823181
if ($userPrivileges === []) {
@@ -3190,12 +3189,11 @@ private function getUserPrivileges(string $user, string $host, bool $hasAccountL
31903189
$userPrivileges['account_locked'] = 'N';
31913190

31923191
$query = 'SELECT * FROM `mysql`.`global_priv` WHERE `User` = ? AND `Host` = ?;';
3193-
$statement = $this->dbi->prepare($query);
3194-
if ($statement === null || ! $statement->execute([$user, $host])) {
3192+
$result = $this->dbi->executeQuery($query, [$user, $host]);
3193+
if ($result === null) {
31953194
return $userPrivileges;
31963195
}
31973196

3198-
$result = $statement->getResult();
31993197
/** @var array<string, string|null>|null $globalPrivileges */
32003198
$globalPrivileges = $result->fetchAssoc();
32013199
if ($globalPrivileges === []) {

0 commit comments

Comments
 (0)