Skip to content

Commit

Permalink
Merge pull request phpmyadmin#19487 from kamil-tekiela/Improvements-t…
Browse files Browse the repository at this point in the history
…o-DatabaseInterface

Improvements to database interface
  • Loading branch information
MauricioFauth authored Jan 2, 2025
2 parents 47652b1 + 04d3912 commit 37018db
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 153 deletions.
4 changes: 2 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6684,7 +6684,7 @@ parameters:
-
message: '#^Only booleans are allowed in a negated boolean, PhpMyAdmin\\Dbal\\ResultInterface\|false given\.$#'
identifier: booleanNot.exprNotBoolean
count: 3
count: 2
path: src/Dbal/DatabaseInterface.php

-
Expand Down Expand Up @@ -10170,7 +10170,7 @@ parameters:
Use dependency injection instead\.$#
'''
identifier: staticMethod.deprecated
count: 7
count: 8
path: src/Navigation/Nodes/Node.php

-
Expand Down
1 change: 1 addition & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5993,6 +5993,7 @@
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
</DeprecatedMethod>
<MixedArgument>
<code><![CDATA[$db]]></code>
Expand Down
34 changes: 7 additions & 27 deletions src/Dbal/DatabaseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -1711,25 +1711,6 @@ public function getError(ConnectionType $connectionType = ConnectionType::User):
return $this->extension->getError($this->connections[$connectionType->value]);
}

/**
* returns the number of rows returned by last query
* used with tryQuery as it accepts false
*
* @param string $query query to run
*
* @psalm-return int|numeric-string
*/
public function queryAndGetNumRows(string $query): string|int
{
$result = $this->tryQuery($query);

if (! $result) {
return 0;
}

return $result->numRows();
}

/**
* returns last inserted auto_increment id for given $link
*/
Expand Down Expand Up @@ -1970,14 +1951,13 @@ public function setVersion(array $version): void
$this->isPercona = stripos($this->versionComment, 'percona') !== false;
}

/**
* Prepare an SQL statement for execution.
*
* @param string $query The query, as a string.
*/
public function prepare(string $query, ConnectionType $connectionType = ConnectionType::User): Statement|null
{
return $this->extension->prepare($this->connections[$connectionType->value], $query);
/** @param list<string> $params */
public function executeQuery(
string $query,
array $params,
ConnectionType $connectionType = ConnectionType::User,
): ResultInterface|null {
return $this->extension->executeQuery($this->connections[$connectionType->value], $query, $params);
}

public function getDatabaseList(): ListDatabase
Expand Down
6 changes: 3 additions & 3 deletions src/Dbal/DbiExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ public function affectedRows(Connection $connection): int|string;
public function escapeString(Connection $connection, string $string): string;

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

/**
* Returns the number of warnings from the last query.
Expand Down
13 changes: 7 additions & 6 deletions src/Dbal/DbiMysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,20 +284,21 @@ public function escapeString(Connection $connection, string $string): string
}

/**
* Prepare an SQL statement for execution.
* Execute a prepared statement and return the result.
*
* @param string $query The query, as a string.
* @param list<string> $params
*/
public function prepare(Connection $connection, string $query): Statement|null
public function executeQuery(Connection $connection, string $query, array $params): MysqliResult|null
{
/** @var mysqli $mysqli */
$mysqli = $connection->connection;
$statement = $mysqli->prepare($query);
if ($statement === false) {
$result = $mysqli->execute_query($query, $params);

if ($result === false) {
return null;
}

return new MysqliStatement($statement);
return new MysqliResult($result);
}

/**
Expand Down
39 changes: 0 additions & 39 deletions src/Dbal/MysqliStatement.php

This file was deleted.

20 changes: 0 additions & 20 deletions src/Dbal/Statement.php

This file was deleted.

20 changes: 18 additions & 2 deletions src/Navigation/Nodes/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,13 +379,13 @@ public function getPresence(UserPrivileges $userPrivileges, string $type = '', s
$query = 'SHOW DATABASES ';
$query .= $this->getWhereClause('Database', $searchClause);

return (int) $dbi->queryAndGetNumRows($query);
return $this->queryAndGetNumRows($query);
}

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

return $retval;
Expand Down Expand Up @@ -831,4 +831,20 @@ private function getDataFromShowDatabasesLike(UserPrivileges $userPrivileges, in

return $retval;
}

/**
* returns the number of rows returned by last query
* used with tryQuery as it accepts false
*/
protected function queryAndGetNumRows(string $query): int
{
$dbi = DatabaseInterface::getInstance();
$result = $dbi->tryQuery($query);

if ($result === false) {
return 0;
}

return (int) $result->numRows();
}
}
6 changes: 3 additions & 3 deletions src/Navigation/Nodes/NodeTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,15 @@ public function getPresence(UserPrivileges $userPrivileges, string $type = '', s
$db = Util::backquote($db);
$table = Util::backquote($table);
$query = 'SHOW COLUMNS FROM ' . $table . ' FROM ' . $db;
$retval = (int) $dbi->queryAndGetNumRows($query);
$retval = $this->queryAndGetNumRows($query);
}

break;
case 'indexes':
$db = Util::backquote($db);
$table = Util::backquote($table);
$query = 'SHOW INDEXES FROM ' . $table . ' FROM ' . $db;
$retval = (int) $dbi->queryAndGetNumRows($query);
$retval = $this->queryAndGetNumRows($query);
break;
case 'triggers':
if (! $this->config->selectedServer['DisableIS']) {
Expand All @@ -116,7 +116,7 @@ public function getPresence(UserPrivileges $userPrivileges, string $type = '', s
} else {
$db = Util::backquote($db);
$query = 'SHOW TRIGGERS FROM ' . $db . ' WHERE `Table` = ' . $dbi->quoteString($table);
$retval = (int) $dbi->queryAndGetNumRows($query);
$retval = $this->queryAndGetNumRows($query);
}

break;
Expand Down
16 changes: 7 additions & 9 deletions src/Server/Privileges.php
Original file line number Diff line number Diff line change
Expand Up @@ -1196,12 +1196,12 @@ private function getTablePrivileges(DatabaseName $db, TableName $table): array
NOT (`Table_priv` = \'\' AND Column_priv = \'\')
ORDER BY `User` ASC, `Host` ASC, `Db` ASC, `Table_priv` ASC;
';
$statement = $this->dbi->prepare($query);
if ($statement === null || ! $statement->execute([$db->getName(), $table->getName()])) {
$result = $this->dbi->executeQuery($query, [$db->getName(), $table->getName()]);
if ($result === null) {
return [];
}

return $statement->getResult()->fetchAllAssoc();
return $result->fetchAllAssoc();
}

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

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

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

$result = $statement->getResult();
/** @var array<string, string|null>|null $globalPrivileges */
$globalPrivileges = $result->fetchAssoc();
if ($globalPrivileges === []) {
Expand Down
15 changes: 7 additions & 8 deletions tests/unit/DatabaseInterfaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use PhpMyAdmin\Dbal\DatabaseInterface;
use PhpMyAdmin\Dbal\DbiExtension;
use PhpMyAdmin\Dbal\ResultInterface;
use PhpMyAdmin\Dbal\Statement;
use PhpMyAdmin\I18n\LanguageManager;
use PhpMyAdmin\Index;
use PhpMyAdmin\Query\Utilities;
Expand Down Expand Up @@ -758,17 +757,17 @@ public function testGetDatabasesFullDisabledISAndSortIntColumn(): void
$dummyDbi->assertAllQueriesConsumed();
}

public function testPrepare(): void
public function testExecuteQuery(): void
{
$query = 'SELECT * FROM `mysql`.`user` WHERE `User` = ? AND `Host` = ?;';
$stmtStub = self::createStub(Statement::class);
$resultStub = self::createStub(ResultInterface::class);
$dummyDbi = $this->createMock(DbiExtension::class);
$dummyDbi->expects(self::once())->method('prepare')
->with(self::isType('object'), self::equalTo($query))
->willReturn($stmtStub);
$dummyDbi->expects(self::once())->method('executeQuery')
->with(self::isType('object'), self::equalTo($query), self::equalTo(['root', 'localhost']))
->willReturn($resultStub);
$dbi = $this->createDatabaseInterface($dummyDbi);
$stmt = $dbi->prepare($query, ConnectionType::ControlUser);
self::assertSame($stmtStub, $stmt);
$stmt = $dbi->executeQuery($query, ['root', 'localhost'], ConnectionType::ControlUser);
self::assertSame($resultStub, $stmt);
}

/**
Expand Down
25 changes: 0 additions & 25 deletions tests/unit/Dbal/MysqliStatementTest.php

This file was deleted.

10 changes: 3 additions & 7 deletions tests/unit/Server/PrivilegesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use PhpMyAdmin\Dbal\ConnectionType;
use PhpMyAdmin\Dbal\DatabaseInterface;
use PhpMyAdmin\Dbal\ResultInterface;
use PhpMyAdmin\Dbal\Statement;
use PhpMyAdmin\Html\Generator;
use PhpMyAdmin\Http\Factory\ServerRequestFactory;
use PhpMyAdmin\Message;
Expand Down Expand Up @@ -1896,18 +1895,15 @@ public function testGetFormForChangePassword(): void
public function testGetUserPrivileges(): void
{
$mysqliResultStub = $this->createMock(ResultInterface::class);
$mysqliStmtStub = $this->createMock(Statement::class);
$mysqliStmtStub->expects(self::exactly(2))->method('execute')->willReturn(true);
$mysqliStmtStub->expects(self::exactly(2))->method('getResult')->willReturn($mysqliResultStub);

$dbi = $this->createMock(DatabaseInterface::class);
$dbi->expects(self::once())->method('isMariaDB')->willReturn(true);

$userQuery = 'SELECT * FROM `mysql`.`user` WHERE `User` = ? AND `Host` = ?;';
$globalPrivQuery = 'SELECT * FROM `mysql`.`global_priv` WHERE `User` = ? AND `Host` = ?;';
$dbi->expects(self::exactly(2))->method('prepare')->willReturnMap([
[$userQuery, ConnectionType::User, $mysqliStmtStub],
[$globalPrivQuery, ConnectionType::User, $mysqliStmtStub],
$dbi->expects(self::exactly(2))->method('executeQuery')->willReturnMap([
[$userQuery, ['test.user', 'test.host'], ConnectionType::User, $mysqliResultStub],
[$globalPrivQuery,['test.user', 'test.host'], ConnectionType::User, $mysqliResultStub],
]);

$mysqliResultStub->expects(self::exactly(2))
Expand Down
8 changes: 6 additions & 2 deletions tests/unit/Stubs/DbiDummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use PhpMyAdmin\Dbal\Connection;
use PhpMyAdmin\Dbal\DbiExtension;
use PhpMyAdmin\Dbal\ResultInterface;
use PhpMyAdmin\Dbal\Statement;
use PhpMyAdmin\FieldMetadata;
use PhpMyAdmin\Identifiers\DatabaseName;
use PhpMyAdmin\Tests\FieldHelper;
Expand Down Expand Up @@ -314,7 +313,12 @@ public function removeDefaultResults(): void
$this->dummyQueries = [];
}

public function prepare(Connection $connection, string $query): Statement|null
/**
* Execute a prepared statement and return the result.
*
* @param list<string> $params
*/
public function executeQuery(Connection $connection, string $query, array $params): ResultInterface|null
{
return null;
}
Expand Down

0 comments on commit 37018db

Please sign in to comment.