Skip to content

Commit fdb5528

Browse files
Use Database type instead of string (#17746)
* Refactored tryColumnCreationQuery function inside CreateAddField to use DatabaseName type instead of string * Refactored exportDatabase and lockTables function to use DatabaseName type instead of string for database name Signed-off-by: Umang Patel <umang.patel@healthengine.com.au> Co-authored-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent 10dfc6d commit fdb5528

File tree

5 files changed

+41
-36
lines changed

5 files changed

+41
-36
lines changed

libraries/classes/Controllers/Export/ExportController.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpMyAdmin\Controllers\AbstractController;
88
use PhpMyAdmin\Controllers\Database\ExportController as DatabaseExportController;
99
use PhpMyAdmin\Core;
10+
use PhpMyAdmin\Dbal\DatabaseName;
1011
use PhpMyAdmin\Encoding;
1112
use PhpMyAdmin\Exceptions\ExportException;
1213
use PhpMyAdmin\Export;
@@ -408,10 +409,10 @@ public function __invoke(ServerRequest $request): void
408409
}
409410

410411
if (isset($GLOBALS['lock_tables'])) {
411-
$this->export->lockTables($GLOBALS['db'], $GLOBALS['tables'], 'READ');
412+
$this->export->lockTables(DatabaseName::fromValue($GLOBALS['db']), $GLOBALS['tables'], 'READ');
412413
try {
413414
$this->export->exportDatabase(
414-
$GLOBALS['db'],
415+
DatabaseName::fromValue($GLOBALS['db']),
415416
$GLOBALS['tables'],
416417
$GLOBALS['whatStrucOrData'],
417418
$GLOBALS['table_structure'],
@@ -431,7 +432,7 @@ public function __invoke(ServerRequest $request): void
431432
}
432433
} else {
433434
$this->export->exportDatabase(
434-
$GLOBALS['db'],
435+
DatabaseName::fromValue($GLOBALS['db']),
435436
$GLOBALS['tables'],
436437
$GLOBALS['whatStrucOrData'],
437438
$GLOBALS['table_structure'],
@@ -472,7 +473,7 @@ public function __invoke(ServerRequest $request): void
472473

473474
if (isset($GLOBALS['lock_tables'])) {
474475
try {
475-
$this->export->lockTables($GLOBALS['db'], [$GLOBALS['table']], 'READ');
476+
$this->export->lockTables(DatabaseName::fromValue($GLOBALS['db']), [$GLOBALS['table']], 'READ');
476477
$this->export->exportTable(
477478
$GLOBALS['db'],
478479
$GLOBALS['table'],

libraries/classes/Controllers/Table/AddFieldController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpMyAdmin\Core;
1010
use PhpMyAdmin\CreateAddField;
1111
use PhpMyAdmin\DatabaseInterface;
12+
use PhpMyAdmin\Dbal\DatabaseName;
1213
use PhpMyAdmin\DbTableExists;
1314
use PhpMyAdmin\Html\Generator;
1415
use PhpMyAdmin\Http\ServerRequest;
@@ -123,7 +124,7 @@ public function __invoke(ServerRequest $request): void
123124
}
124125

125126
$GLOBALS['result'] = $createAddField->tryColumnCreationQuery(
126-
$GLOBALS['db'],
127+
DatabaseName::fromValue($GLOBALS['db']),
127128
$GLOBALS['sql_query'],
128129
$GLOBALS['errorUrl']
129130
);

libraries/classes/CreateAddField.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpMyAdmin;
66

7+
use PhpMyAdmin\Dbal\DatabaseName;
78
use PhpMyAdmin\Html\Generator;
89

910
use function count;
@@ -471,12 +472,12 @@ public function getColumnCreationQuery(
471472
/**
472473
* Function to execute the column creation statement
473474
*
474-
* @param string $db current database
475-
* @param string $sqlQuery the query to run
476-
* @param string $errorUrl error page url
475+
* @param DatabaseName $db current database
476+
* @param string $sqlQuery the query to run
477+
* @param string $errorUrl error page url
477478
*/
478479
public function tryColumnCreationQuery(
479-
string $db,
480+
DatabaseName $db,
480481
string $sqlQuery,
481482
string $errorUrl
482483
): bool {
@@ -485,7 +486,7 @@ public function tryColumnCreationQuery(
485486
if (! $this->dbi->selectDb($db)) {
486487
Generator::mysqlDie(
487488
$this->dbi->getError(),
488-
'USE ' . Util::backquote($db),
489+
'USE ' . Util::backquote($db->getName()),
489490
false,
490491
$errorUrl
491492
);

libraries/classes/Export.php

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PhpMyAdmin\Controllers\Database\ExportController as DatabaseExportController;
1111
use PhpMyAdmin\Controllers\Server\ExportController as ServerExportController;
1212
use PhpMyAdmin\Controllers\Table\ExportController as TableExportController;
13+
use PhpMyAdmin\Dbal\DatabaseName;
1314
use PhpMyAdmin\Plugins\ExportPlugin;
1415
use PhpMyAdmin\Plugins\SchemaPlugin;
1516

@@ -599,7 +600,7 @@ public function exportServer(
599600

600601
$tables = $this->dbi->getTables($currentDb);
601602
$this->exportDatabase(
602-
$currentDb,
603+
DatabaseName::fromValue($currentDb),
603604
$tables,
604605
$whatStrucOrData,
605606
$tables,
@@ -625,7 +626,7 @@ public function exportServer(
625626
/**
626627
* Export at the database level
627628
*
628-
* @param string $db the database to export
629+
* @param DatabaseName $db the database to export
629630
* @param array $tables the tables to export
630631
* @param string $whatStrucOrData structure or data or both
631632
* @param array $tableStructure whether to export structure for each table
@@ -641,7 +642,7 @@ public function exportServer(
641642
* @param string $separateFiles whether it is a separate-files export
642643
*/
643644
public function exportDatabase(
644-
string $db,
645+
DatabaseName $db,
645646
array $tables,
646647
string $whatStrucOrData,
647648
array $tableStructure,
@@ -656,14 +657,14 @@ public function exportDatabase(
656657
array $aliases,
657658
string $separateFiles
658659
): void {
659-
$dbAlias = ! empty($aliases[$db]['alias'])
660-
? $aliases[$db]['alias'] : '';
660+
$dbAlias = ! empty($aliases[$db->getName()]['alias'])
661+
? $aliases[$db->getName()]['alias'] : '';
661662

662-
if (! $exportPlugin->exportDBHeader($db, $dbAlias)) {
663+
if (! $exportPlugin->exportDBHeader($db->getName(), $dbAlias)) {
663664
return;
664665
}
665666

666-
if (! $exportPlugin->exportDBCreate($db, $exportType, $dbAlias)) {
667+
if (! $exportPlugin->exportDBCreate($db->getName(), $exportType, $dbAlias)) {
667668
return;
668669
}
669670

@@ -676,7 +677,7 @@ public function exportDatabase(
676677
|| $GLOBALS['sql_structure_or_data'] === 'structure_and_data')
677678
&& isset($GLOBALS['sql_procedure_function'])
678679
) {
679-
$exportPlugin->exportRoutines($db, $aliases);
680+
$exportPlugin->exportRoutines($db->getName(), $aliases);
680681

681682
if ($separateFiles === 'database') {
682683
$this->saveObjectInBuffer('routines');
@@ -686,7 +687,7 @@ public function exportDatabase(
686687
$views = [];
687688

688689
foreach ($tables as $table) {
689-
$tableObject = new Table($table, $db, $this->dbi);
690+
$tableObject = new Table($table, $db->getName(), $this->dbi);
690691
// if this is a view, collect it for later;
691692
// views must be exported after the tables
692693
$isView = $tableObject->isView();
@@ -706,7 +707,7 @@ public function exportDatabase(
706707
$separateFiles == ''
707708
&& isset($GLOBALS['sql_create_view'])
708709
&& ! $exportPlugin->exportStructure(
709-
$db,
710+
$db->getName(),
710711
$table,
711712
$errorUrl,
712713
'stand_in',
@@ -728,7 +729,7 @@ public function exportDatabase(
728729
// This obtains the current table's size
729730
$query = 'SELECT data_length + index_length
730731
from information_schema.TABLES
731-
WHERE table_schema = "' . $this->dbi->escapeString($db) . '"
732+
WHERE table_schema = "' . $this->dbi->escapeString($db->getName()) . '"
732733
AND table_name = "' . $this->dbi->escapeString($table) . '"';
733734

734735
$size = (int) $this->dbi->fetchValue($query);
@@ -741,7 +742,7 @@ public function exportDatabase(
741742

742743
if (
743744
! $exportPlugin->exportStructure(
744-
$db,
745+
$db->getName(),
745746
$table,
746747
$errorUrl,
747748
'create_table',
@@ -764,14 +765,14 @@ public function exportDatabase(
764765
&& in_array($table, $tableData)
765766
&& ! $isView
766767
) {
767-
$tableObj = new Table($table, $db, $this->dbi);
768+
$tableObj = new Table($table, $db->getName(), $this->dbi);
768769
$nonGeneratedCols = $tableObj->getNonGeneratedColumns(true);
769770

770771
$localQuery = 'SELECT ' . implode(', ', $nonGeneratedCols)
771-
. ' FROM ' . Util::backquote($db)
772+
. ' FROM ' . Util::backquote($db->getName())
772773
. '.' . Util::backquote($table);
773774

774-
if (! $exportPlugin->exportData($db, $table, $errorUrl, $localQuery, $aliases)) {
775+
if (! $exportPlugin->exportData($db->getName(), $table, $errorUrl, $localQuery, $aliases)) {
775776
break;
776777
}
777778
}
@@ -793,7 +794,7 @@ public function exportDatabase(
793794

794795
if (
795796
! $exportPlugin->exportStructure(
796-
$db,
797+
$db->getName(),
797798
$table,
798799
$errorUrl,
799800
'triggers',
@@ -824,7 +825,7 @@ public function exportDatabase(
824825

825826
if (
826827
! $exportPlugin->exportStructure(
827-
$db,
828+
$db->getName(),
828829
$view,
829830
$errorUrl,
830831
'create_view',
@@ -847,7 +848,7 @@ public function exportDatabase(
847848
}
848849
}
849850

850-
if (! $exportPlugin->exportDBFooter($db)) {
851+
if (! $exportPlugin->exportDBFooter($db->getName())) {
851852
return;
852853
}
853854

@@ -856,7 +857,7 @@ public function exportDatabase(
856857
// Types of metadata to export.
857858
// In the future these can be allowed to be selected by the user
858859
$metadataTypes = $this->getMetadataTypes();
859-
$exportPlugin->exportMetadata($db, $tables, $metadataTypes);
860+
$exportPlugin->exportMetadata($db->getName(), $tables, $metadataTypes);
860861

861862
if ($separateFiles === 'database') {
862863
$this->saveObjectInBuffer('metadata');
@@ -875,7 +876,7 @@ public function exportDatabase(
875876
return;
876877
}
877878

878-
$exportPlugin->exportEvents($db);
879+
$exportPlugin->exportEvents($db->getName());
879880

880881
if ($separateFiles !== 'database') {
881882
return;
@@ -1167,17 +1168,17 @@ public function mergeAliases(array $aliases1, array $aliases2): array
11671168
/**
11681169
* Locks tables
11691170
*
1170-
* @param string $db database name
1171-
* @param array $tables list of table names
1172-
* @param string $lockType lock type; "[LOW_PRIORITY] WRITE" or "READ [LOCAL]"
1171+
* @param DatabaseName $db database name
1172+
* @param array $tables list of table names
1173+
* @param string $lockType lock type; "[LOW_PRIORITY] WRITE" or "READ [LOCAL]"
11731174
*
11741175
* @return mixed result of the query
11751176
*/
1176-
public function lockTables(string $db, array $tables, string $lockType = 'WRITE')
1177+
public function lockTables(DatabaseName $db, array $tables, string $lockType = 'WRITE')
11771178
{
11781179
$locks = [];
11791180
foreach ($tables as $table) {
1180-
$locks[] = Util::backquote($db) . '.'
1181+
$locks[] = Util::backquote($db->getName()) . '.'
11811182
. Util::backquote($table) . ' ' . $lockType;
11821183
}
11831184

test/classes/ExportTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpMyAdmin\Tests;
66

77
use PhpMyAdmin\ConfigStorage\Relation;
8+
use PhpMyAdmin\Dbal\DatabaseName;
89
use PhpMyAdmin\Export;
910
use PhpMyAdmin\Plugins\Export\ExportPhparray;
1011
use PhpMyAdmin\Plugins\Export\ExportSql;
@@ -157,7 +158,7 @@ public function testExportDatabase(): void
157158
$export = new Export($dbi);
158159

159160
$export->exportDatabase(
160-
'test_db',
161+
DatabaseName::fromValue('test_db'),
161162
['test_table'],
162163
'structure_and_data',
163164
['test_table'],

0 commit comments

Comments
 (0)