Skip to content

Commit c4825b1

Browse files
Merge pull request #17263 from kamil-tekiela/dbi-result
Major refactoring of Dbal pt.1
2 parents b1dc033 + 47a039d commit c4825b1

108 files changed

Lines changed: 2460 additions & 3715 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

libraries/classes/Charsets.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,10 @@ private static function loadCharsets(DatabaseInterface $dbi, bool $disableIs): v
9494
$res = $dbi->query($sql);
9595

9696
self::$charsets = [];
97-
while ($row = $dbi->fetchAssoc($res)) {
97+
foreach ($res as $row) {
9898
self::$charsets[$row['Charset']] = Charset::fromServer($row);
9999
}
100100

101-
$dbi->freeResult($res);
102-
103101
ksort(self::$charsets, SORT_STRING);
104102
}
105103

@@ -131,12 +129,10 @@ private static function loadCollations(DatabaseInterface $dbi, bool $disableIs):
131129
$res = $dbi->query($sql);
132130

133131
self::$collations = [];
134-
while ($row = $dbi->fetchAssoc($res)) {
132+
foreach ($res as $row) {
135133
self::$collations[$row['Charset']][$row['Collation']] = Collation::fromServer($row);
136134
}
137135

138-
$dbi->freeResult($res);
139-
140136
foreach (array_keys(self::$collations) as $charset) {
141137
ksort(self::$collations[$charset], SORT_STRING);
142138
}

libraries/classes/CheckUserPrivileges.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,6 @@ private function analyseShowGrant(): void
284284
//break;
285285
}
286286

287-
$this->dbi->freeResult($showGrantsResult);
288-
289287
// must also cacheUnset() them in
290288
// PhpMyAdmin\Plugins\Auth\AuthenticationCookie
291289
SessionCache::set('is_create_db_priv', $GLOBALS['is_create_db_priv']);

libraries/classes/ConfigStorage/Relation.php

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpMyAdmin\ConfigStorage\Features\PdfFeature;
88
use PhpMyAdmin\DatabaseInterface;
9+
use PhpMyAdmin\Dbal\ResultInterface;
910
use PhpMyAdmin\InternalRelations;
1011
use PhpMyAdmin\RecentFavoriteTable;
1112
use PhpMyAdmin\SqlParser\Parser;
@@ -29,7 +30,6 @@
2930
use function implode;
3031
use function in_array;
3132
use function is_array;
32-
use function is_bool;
3333
use function is_scalar;
3434
use function is_string;
3535
use function ksort;
@@ -69,28 +69,24 @@ public function __construct(DatabaseInterface $dbi)
6969
* @param string $sql the query to execute
7070
* @param bool $show_error whether to display SQL error messages or not
7171
* @param int $options query options
72+
* @psalm-param T $show_error
7273
*
73-
* @return mixed|bool the result set, or false if no result set
74+
* @return ResultInterface|false the result set, or false if no result set
75+
* @psalm-return (T is true ? ResultInterface : ResultInterface|false)
76+
*
77+
* @template T as bool
7478
*/
7579
public function queryAsControlUser($sql, $show_error = true, $options = 0)
7680
{
7781
// Avoid caching of the number of rows affected; for example, this function
7882
// is called for tracking purposes but we want to display the correct number
7983
// of rows affected by the original query, not by the query generated for
8084
// tracking.
81-
$cache_affected_rows = false;
82-
8385
if ($show_error) {
84-
$result = $this->dbi->query($sql, DatabaseInterface::CONNECT_CONTROL, $options, $cache_affected_rows);
85-
} else {
86-
$result = @$this->dbi->tryQuery($sql, DatabaseInterface::CONNECT_CONTROL, $options, $cache_affected_rows);
87-
}
88-
89-
if ($result) {
90-
return $result;
86+
return $this->dbi->query($sql, DatabaseInterface::CONNECT_CONTROL, $options, false);
9187
}
9288

93-
return false;
89+
return $this->dbi->tryQuery($sql, DatabaseInterface::CONNECT_CONTROL, $options, false);
9490
}
9591

9692
public function getRelationParameters(): RelationParameters
@@ -194,11 +190,11 @@ private function fillRelationParamsWithTableNames(array $relationParams): ?array
194190
$tabQuery = 'SHOW TABLES FROM '
195191
. Util::backquote($GLOBALS['cfg']['Server']['pmadb']);
196192
$tableRes = $this->queryAsControlUser($tabQuery, false, DatabaseInterface::QUERY_STORE);
197-
if (is_bool($tableRes)) {
193+
if ($tableRes === false) {
198194
return null;
199195
}
200196

201-
while ($currTable = @$this->dbi->fetchRow($tableRes)) {
197+
while ($currTable = $tableRes->fetchRow()) {
202198
if ($currTable[0] == $GLOBALS['cfg']['Server']['bookmarktable']) {
203199
$relationParams['bookmark'] = (string) $currTable[0];
204200
} elseif ($currTable[0] == $GLOBALS['cfg']['Server']['relation']) {
@@ -240,8 +236,6 @@ private function fillRelationParamsWithTableNames(array $relationParams): ?array
240236
}
241237
}
242238

243-
$this->dbi->freeResult($tableRes);
244-
245239
return $relationParams;
246240
}
247241

@@ -395,8 +389,8 @@ public function tryUpgradeTransformations(): bool
395389
. ' WHERE Field IN (\'' . implode('\', \'', $new_cols) . '\')';
396390
$result = $this->queryAsControlUser($query, false, DatabaseInterface::QUERY_STORE);
397391
if ($result) {
398-
$rows = $this->dbi->numRows($result);
399-
$this->dbi->freeResult($result);
392+
$rows = $result->numRows();
393+
unset($result);
400394
// input transformations are present
401395
// no need to upgrade
402396
if ($rows === 2) {
@@ -620,8 +614,8 @@ public function getDbComment(string $db): string
620614
. ' AND column_name = \'(db_comment)\'';
621615
$com_rs = $this->queryAsControlUser($com_qry, false, DatabaseInterface::QUERY_STORE);
622616

623-
if ($com_rs && $this->dbi->numRows($com_rs) > 0) {
624-
$row = $this->dbi->fetchAssoc($com_rs);
617+
if ($com_rs && $com_rs->numRows() > 0) {
618+
$row = $com_rs->fetchAssoc();
625619

626620
return (string) $row['comment'];
627621
}
@@ -638,7 +632,6 @@ public function getDbComment(string $db): string
638632
public function getDbComments()
639633
{
640634
$columnCommentsFeature = $this->getRelationParameters()->columnCommentsFeature;
641-
$comments = [];
642635

643636
if ($columnCommentsFeature !== null) {
644637
// pmadb internal db comment
@@ -648,16 +641,12 @@ public function getDbComments()
648641
. ' WHERE `column_name` = \'(db_comment)\'';
649642
$com_rs = $this->queryAsControlUser($com_qry, false, DatabaseInterface::QUERY_STORE);
650643

651-
if ($com_rs && $this->dbi->numRows($com_rs) > 0) {
652-
while ($row = $this->dbi->fetchAssoc($com_rs)) {
653-
$comments[$row['db_name']] = $row['comment'];
654-
}
644+
if ($com_rs && $com_rs->numRows() > 0) {
645+
return $com_rs->fetchAllKeyPair();
655646
}
656-
657-
$this->dbi->freeResult($com_rs);
658647
}
659648

660-
return $comments;
649+
return [];
661650
}
662651

663652
/**
@@ -1119,17 +1108,12 @@ public function getForeignData(
11191108
$f_query_main . $f_query_from . $f_query_filter
11201109
. $f_query_order . $f_query_limit
11211110
);
1122-
if ($disp && $this->dbi->numRows($disp) > 0) {
1111+
if ($disp && $disp->numRows() > 0) {
11231112
// If a resultset has been created, pre-cache it in the $disp_row
11241113
// array. This helps us from not needing to use mysql_data_seek by
11251114
// accessing a pre-cached PHP array. Usually those resultsets are
11261115
// not that big, so a performance hit should not be expected.
1127-
$disp_row = [];
1128-
while ($single_disp_row = @$this->dbi->fetchAssoc($disp)) {
1129-
$disp_row[] = $single_disp_row;
1130-
}
1131-
1132-
@$this->dbi->freeResult($disp);
1116+
$disp_row = $disp->fetchAllAssoc();
11331117
} else {
11341118
// Either no data in the foreign table or
11351119
// user does not have select permission to foreign table/field
@@ -1388,10 +1372,8 @@ public function renameTable($source_db, $target_db, $source_table, $target_table
13881372
*
13891373
* @param string|null $newpage name of the new PDF page
13901374
* @param string $db database name
1391-
*
1392-
* @return int|false
13931375
*/
1394-
public function createPage(?string $newpage, PdfFeature $pdfFeature, $db)
1376+
public function createPage(?string $newpage, PdfFeature $pdfFeature, $db): int
13951377
{
13961378
$ins_query = 'INSERT INTO '
13971379
. Util::backquote($pdfFeature->database) . '.'

libraries/classes/ConfigStorage/UserGroups.php

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,15 @@ public static function getHtmlForListingUsersofAGroup(
5151
. "'";
5252
$result = $relation->queryAsControlUser($sql_query, false);
5353
if ($result) {
54-
$numRows = $dbi->numRows($result);
55-
if ($numRows != 0) {
56-
$i = 0;
57-
while ($row = $dbi->fetchRow($result)) {
58-
$i++;
59-
$user = [];
60-
$user['count'] = $i;
61-
$user['user'] = $row[0];
62-
$users[] = $user;
63-
}
54+
$i = 0;
55+
while ($row = $result->fetchRow()) {
56+
$users[] = [
57+
'count' => ++$i,
58+
'user' => $row[0],
59+
];
6460
}
6561
}
6662

67-
$dbi->freeResult($result);
6863
$template = new Template();
6964

7065
return $template->render('server/user_groups/user_listings', [
@@ -88,14 +83,13 @@ public static function getHtmlForUserGroupsTable(ConfigurableMenusFeature $confi
8883
. '.' . Util::backquote($configurableMenusFeature->userGroups);
8984
$sql_query = 'SELECT * FROM ' . $groupTable . ' ORDER BY `usergroup` ASC';
9085
$result = $relation->queryAsControlUser($sql_query, false);
91-
$numRows = $dbi->numRows($result);
9286
$userGroups = [];
9387
$userGroupsValues = [];
9488
$action = Url::getFromRoute('/server/privileges');
9589
$hidden_inputs = null;
96-
if ($result && $numRows) {
90+
if ($result && $result->numRows()) {
9791
$hidden_inputs = Url::getHiddenInputs();
98-
while ($row = $dbi->fetchAssoc($result)) {
92+
foreach ($result as $row) {
9993
$groupName = $row['usergroup'];
10094
if (! isset($userGroups[$groupName])) {
10195
$userGroups[$groupName] = [];
@@ -134,14 +128,12 @@ public static function getHtmlForUserGroupsTable(ConfigurableMenusFeature $confi
134128

135129
$addUserUrl = Url::getFromRoute('/server/user-groups', ['addUserGroup' => 1]);
136130
$addUserIcon = Generator::getIcon('b_usradd');
137-
$dbi->freeResult($result);
138131
$template = new Template();
139132

140133
return $template->render('server/user_groups/user_groups', [
141134
'action' => $action,
142135
'hidden_inputs' => $hidden_inputs ?? '',
143-
'result' => $result,
144-
'has_rows' => $numRows,
136+
'has_rows' => $userGroups !== [],
145137
'user_groups_values' => $userGroupsValues,
146138
'add_user_url' => $addUserUrl,
147139
'add_user_icon' => $addUserIcon,
@@ -237,7 +229,7 @@ public static function getHtmlToEditUserGroup(
237229
. "'";
238230
$result = $relation->queryAsControlUser($sql_query, false);
239231
if ($result) {
240-
while ($row = $dbi->fetchAssoc($result)) {
232+
foreach ($result as $row) {
241233
$key = $row['tab'];
242234
$value = $row['allowed'];
243235
if (substr($key, 0, 7) === 'server_' && $value === 'Y') {
@@ -250,7 +242,7 @@ public static function getHtmlToEditUserGroup(
250242
}
251243
}
252244

253-
$dbi->freeResult($result);
245+
unset($result);
254246
}
255247

256248
$tabList = self::getTabList(

libraries/classes/Controllers/Server/BinlogController.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,7 @@ public function __invoke(): void
7575
$sqlQuery = $this->getSqlQuery($params['log'] ?? '', $position, (int) $cfg['MaxRows']);
7676
$result = $this->dbi->query($sqlQuery);
7777

78-
$numRows = 0;
79-
if (isset($result) && $result) {
80-
$numRows = $this->dbi->numRows($result);
81-
}
78+
$numRows = $result->numRows();
8279

8380
$previousParams = $urlParams;
8481
$fullQueriesParams = $urlParams;
@@ -99,10 +96,7 @@ public function __invoke(): void
9996
$nextParams['pos'] = $position + $cfg['MaxRows'];
10097
}
10198

102-
$values = [];
103-
while ($value = $this->dbi->fetchAssoc($result)) {
104-
$values[] = $value;
105-
}
99+
$values = $result->fetchAllAssoc();
106100

107101
$this->render('server/binlog/index', [
108102
'url_params' => $urlParams,

libraries/classes/Controllers/Server/UserGroupsFormController.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,11 @@ private function getHtmlToChooseUserGroup(
9090
$sqlQuery = 'SELECT DISTINCT `usergroup` FROM ' . $groupTable;
9191
$result = $this->relation->queryAsControlUser($sqlQuery, false);
9292
if ($result) {
93-
while ($row = $this->dbi->fetchRow($result)) {
93+
while ($row = $result->fetchRow()) {
9494
$allUserGroups[$row[0]] = $row[0];
9595
}
9696
}
9797

98-
$this->dbi->freeResult($result);
99-
10098
return $this->template->render('server/privileges/choose_user_group', [
10199
'all_user_groups' => $allUserGroups,
102100
'user_group' => $userGroup,

libraries/classes/Controllers/Server/Variables/SetVariableController.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,8 @@ public function __invoke(ServerRequest $request, array $vars): void
7272
}
7373

7474
$json = [];
75-
if (
76-
! preg_match('/[^a-zA-Z0-9_]+/', $variableName)
77-
&& $this->dbi->query('SET GLOBAL ' . $variableName . ' = ' . $value)
78-
) {
75+
if (! preg_match('/[^a-zA-Z0-9_]+/', $variableName)) {
76+
$this->dbi->query('SET GLOBAL ' . $variableName . ' = ' . $value);
7977
// Some values are rounded down etc.
8078
$varValue = $this->dbi->fetchSingleRow(
8179
'SHOW GLOBAL VARIABLES WHERE Variable_name="'

libraries/classes/Controllers/Server/VariablesController.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,9 @@ public function __invoke(): void
5252
$variables = [];
5353
$serverVarsResult = $this->dbi->tryQuery('SHOW SESSION VARIABLES;');
5454
if ($serverVarsResult !== false) {
55-
$serverVarsSession = [];
56-
while ($arr = $this->dbi->fetchRow($serverVarsResult)) {
57-
$serverVarsSession[$arr[0]] = $arr[1];
58-
}
55+
$serverVarsSession = $serverVarsResult->fetchAllKeyPair();
5956

60-
$this->dbi->freeResult($serverVarsResult);
57+
unset($serverVarsResult);
6158

6259
$serverVars = $this->dbi->fetchResult('SHOW GLOBAL VARIABLES;', 0, 1);
6360

libraries/classes/Controllers/Table/ChartController.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,14 @@ public function __invoke(): void
117117
}
118118
}
119119

120-
$data = [];
121-
122120
$result = $this->dbi->tryQuery($sql_query);
123-
$fields_meta = $this->dbi->getFieldsMeta($result) ?? [];
124-
while ($row = $this->dbi->fetchAssoc($result)) {
125-
$data[] = $row;
121+
$fields_meta = $row = [];
122+
if ($result !== false) {
123+
$fields_meta = $this->dbi->getFieldsMeta($result);
124+
$row = $result->fetchAssoc();
126125
}
127126

128-
$keys = array_keys($data[0]);
127+
$keys = array_keys($row);
129128
$numericColumnFound = false;
130129
foreach (array_keys($keys) as $idx) {
131130
if (
@@ -198,13 +197,13 @@ public function ajax(): void
198197

199198
$sql_with_limit = $statement->build();
200199

201-
$data = [];
202200
$result = $this->dbi->tryQuery($sql_with_limit);
203-
while ($row = $this->dbi->fetchAssoc($result)) {
204-
$data[] = $row;
201+
$data = [];
202+
if ($result !== false) {
203+
$data = $result->fetchAllAssoc();
205204
}
206205

207-
if (empty($data)) {
206+
if ($data === []) {
208207
$this->response->setRequestStatus(false);
209208
$this->response->addJSON('message', __('No data to display'));
210209

libraries/classes/Controllers/Table/FindReplaceController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function __construct(
5656
$this->columnNames = [];
5757
$this->columnTypes = [];
5858
$this->loadTableInfo();
59-
$this->connectionCharSet = $this->dbi->fetchValue('SELECT @@character_set_connection');
59+
$this->connectionCharSet = (string) $this->dbi->fetchValue('SELECT @@character_set_connection');
6060
}
6161

6262
public function __invoke(): void

0 commit comments

Comments
 (0)