Skip to content

Commit 7975f5f

Browse files
committed
Move DBI's getDefinition into the Routines and Events classes
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent 329eb35 commit 7975f5f

11 files changed

Lines changed: 81 additions & 89 deletions

File tree

libraries/classes/Database/Events.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use function htmlspecialchars;
2121
use function in_array;
2222
use function intval;
23+
use function is_string;
2324
use function mb_strtoupper;
2425
use function sprintf;
2526
use function str_contains;
@@ -100,7 +101,7 @@ public function handleEditor(): void
100101
// Execute the created query
101102
if (! empty($_POST['editor_process_edit'])) {
102103
// Backup the old trigger, in case something goes wrong
103-
$create_item = $this->dbi->getDefinition($GLOBALS['db'], 'EVENT', $_POST['item_original_name']);
104+
$create_item = self::getDefinition($this->dbi, $GLOBALS['db'], $_POST['item_original_name']);
104105
$drop_item = 'DROP EVENT IF EXISTS '
105106
. Util::backquote($_POST['item_original_name'])
106107
. ";\n";
@@ -556,7 +557,7 @@ public function export(): void
556557
}
557558

558559
$itemName = $_GET['item_name'];
559-
$exportData = $this->dbi->getDefinition($GLOBALS['db'], 'EVENT', $itemName);
560+
$exportData = self::getDefinition($this->dbi, $GLOBALS['db'], $itemName);
560561

561562
if (! $exportData) {
562563
$exportData = false;
@@ -642,4 +643,14 @@ public function getDetails(string $db, string $name = ''): array
642643

643644
return $result;
644645
}
646+
647+
public static function getDefinition(DatabaseInterface $dbi, string $db, string $name): ?string
648+
{
649+
$result = $dbi->fetchValue(
650+
'SHOW CREATE EVENT ' . Util::backquote($db) . '.' . Util::backquote($name),
651+
'Create Event'
652+
);
653+
654+
return is_string($result) ? $result : null;
655+
}
645656
}

libraries/classes/Database/Routines.php

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,9 @@ public function handleRequestCreateOrEdit(array $errors, $db)
204204
} else {
205205
// Backup the old routine, in case something goes wrong
206206
if ($_POST['item_original_type'] === 'FUNCTION') {
207-
$create_routine = $this->dbi->getDefinition($db, 'FUNCTION', $_POST['item_original_name']);
207+
$create_routine = self::getFunctionDefinition($this->dbi, $db, $_POST['item_original_name']);
208208
} else {
209-
$create_routine = $this->dbi->getDefinition($db, 'PROCEDURE', $_POST['item_original_name']);
209+
$create_routine = self::getProcedureDefinition($this->dbi, $db, $_POST['item_original_name']);
210210
}
211211

212212
$privilegesBackup = $this->backupPrivileges();
@@ -575,7 +575,11 @@ public function getDataFromName($name, $type, $all = true): ?array
575575
$retval['item_name'] = $routine['SPECIFIC_NAME'];
576576
$retval['item_type'] = $routine['ROUTINE_TYPE'];
577577

578-
$definition = $this->dbi->getDefinition($GLOBALS['db'], $routine['ROUTINE_TYPE'], $routine['SPECIFIC_NAME']);
578+
if ($routine['ROUTINE_TYPE'] === 'FUNCTION') {
579+
$definition = self::getFunctionDefinition($this->dbi, $GLOBALS['db'], $routine['SPECIFIC_NAME']);
580+
} else {
581+
$definition = self::getProcedureDefinition($this->dbi, $GLOBALS['db'], $routine['SPECIFIC_NAME']);
582+
}
579583

580584
if ($definition === null) {
581585
return null;
@@ -1474,7 +1478,12 @@ public function getRow(array $routine, $rowClass = '')
14741478
// we will show a dialog to get values for these parameters,
14751479
// otherwise we can execute it directly.
14761480

1477-
$definition = $this->dbi->getDefinition($GLOBALS['db'], $routine['type'], $routine['name']);
1481+
if ($routine['type'] === 'FUNCTION') {
1482+
$definition = self::getFunctionDefinition($this->dbi, $GLOBALS['db'], $routine['name']);
1483+
} else {
1484+
$definition = self::getProcedureDefinition($this->dbi, $GLOBALS['db'], $routine['name']);
1485+
}
1486+
14781487
$executeAction = '';
14791488

14801489
if ($definition !== null) {
@@ -1540,11 +1549,14 @@ public function export(): void
15401549
return;
15411550
}
15421551

1543-
if ($_GET['item_type'] !== 'FUNCTION' && $_GET['item_type'] !== 'PROCEDURE') {
1552+
if ($_GET['item_type'] === 'FUNCTION') {
1553+
$routineDefinition = self::getFunctionDefinition($this->dbi, $GLOBALS['db'], $_GET['item_name']);
1554+
} elseif ($_GET['item_type'] === 'PROCEDURE') {
1555+
$routineDefinition = self::getProcedureDefinition($this->dbi, $GLOBALS['db'], $_GET['item_name']);
1556+
} else {
15441557
return;
15451558
}
15461559

1547-
$routineDefinition = $this->dbi->getDefinition($GLOBALS['db'], $_GET['item_type'], $_GET['item_name']);
15481560
$exportData = false;
15491561

15501562
if ($routineDefinition !== null) {
@@ -1660,4 +1672,24 @@ public static function getDetails(
16601672

16611673
return $ret;
16621674
}
1675+
1676+
public static function getFunctionDefinition(DatabaseInterface $dbi, string $db, string $name): ?string
1677+
{
1678+
$result = $dbi->fetchValue(
1679+
'SHOW CREATE FUNCTION ' . Util::backquote($db) . '.' . Util::backquote($name),
1680+
'Create Function'
1681+
);
1682+
1683+
return is_string($result) ? $result : null;
1684+
}
1685+
1686+
public static function getProcedureDefinition(DatabaseInterface $dbi, string $db, string $name): ?string
1687+
{
1688+
$result = $dbi->fetchValue(
1689+
'SHOW CREATE PROCEDURE ' . Util::backquote($db) . '.' . Util::backquote($name),
1690+
'Create Procedure'
1691+
);
1692+
1693+
return is_string($result) ? $result : null;
1694+
}
16631695
}

libraries/classes/DatabaseInterface.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,36 +1492,6 @@ public function getProceduresOrFunctions(
14921492
return $result;
14931493
}
14941494

1495-
/**
1496-
* returns the definition of a specific PROCEDURE, FUNCTION, EVENT or VIEW
1497-
*
1498-
* @param string $db db name
1499-
* @param string $which PROCEDURE | FUNCTION | EVENT | VIEW
1500-
* @param string $name the procedure|function|event|view name
1501-
* @param int $link link type
1502-
*
1503-
* @return string|null the definition
1504-
*/
1505-
public function getDefinition(
1506-
string $db,
1507-
string $which,
1508-
string $name,
1509-
$link = self::CONNECT_USER
1510-
): ?string {
1511-
$returnedField = [
1512-
'PROCEDURE' => 'Create Procedure',
1513-
'FUNCTION' => 'Create Function',
1514-
'EVENT' => 'Create Event',
1515-
'VIEW' => 'Create View',
1516-
];
1517-
$query = 'SHOW CREATE ' . $which . ' '
1518-
. Util::backquote($db) . '.'
1519-
. Util::backquote($name);
1520-
$result = $this->fetchValue($query, $returnedField[$which], $link);
1521-
1522-
return is_string($result) ? $result : null;
1523-
}
1524-
15251495
/**
15261496
* gets the current user with host
15271497
*

libraries/classes/Dbal/DbalInterface.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -444,23 +444,6 @@ public function getProceduresOrFunctions(
444444
$link = DatabaseInterface::CONNECT_USER
445445
): array;
446446

447-
/**
448-
* returns the definition of a specific PROCEDURE, FUNCTION, EVENT or VIEW
449-
*
450-
* @param string $db db name
451-
* @param string $which PROCEDURE | FUNCTION | EVENT | VIEW
452-
* @param string $name the procedure|function|event|view name
453-
* @param int $link link type
454-
*
455-
* @return string|null the definition
456-
*/
457-
public function getDefinition(
458-
string $db,
459-
string $which,
460-
string $name,
461-
$link = DatabaseInterface::CONNECT_USER
462-
): ?string;
463-
464447
/**
465448
* gets the current user with host
466449
*

libraries/classes/Operations.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace PhpMyAdmin;
66

77
use PhpMyAdmin\ConfigStorage\Relation;
8+
use PhpMyAdmin\Database\Events;
9+
use PhpMyAdmin\Database\Routines;
810
use PhpMyAdmin\Database\Triggers;
911
use PhpMyAdmin\Engines\Innodb;
1012
use PhpMyAdmin\Partitioning\Partition;
@@ -59,7 +61,7 @@ public function runProcedureAndFunctionDefinitions($db): void
5961
if ($procedure_names) {
6062
foreach ($procedure_names as $procedure_name) {
6163
$this->dbi->selectDb($db);
62-
$tmp_query = $this->dbi->getDefinition($db, 'PROCEDURE', $procedure_name);
64+
$tmp_query = Routines::getProcedureDefinition($this->dbi, $db, $procedure_name);
6365
if ($tmp_query === null) {
6466
continue;
6567
}
@@ -78,7 +80,7 @@ public function runProcedureAndFunctionDefinitions($db): void
7880

7981
foreach ($function_names as $function_name) {
8082
$this->dbi->selectDb($db);
81-
$tmp_query = $this->dbi->getDefinition($db, 'FUNCTION', $function_name);
83+
$tmp_query = Routines::getFunctionDefinition($this->dbi, $db, $function_name);
8284
if ($tmp_query === null) {
8385
continue;
8486
}
@@ -266,7 +268,7 @@ public function runEventDefinitionsForDb($db): void
266268

267269
foreach ($event_names as $event_name) {
268270
$this->dbi->selectDb($db);
269-
$tmp_query = $this->dbi->getDefinition($db, 'EVENT', $event_name);
271+
$tmp_query = Events::getDefinition($this->dbi, $db, $event_name);
270272
// collect for later display
271273
$GLOBALS['sql_query'] .= "\n" . $tmp_query;
272274
$this->dbi->selectDb($_POST['newname']);

libraries/classes/Plugins/Export/ExportSql.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
namespace PhpMyAdmin\Plugins\Export;
99

1010
use PhpMyAdmin\Charsets;
11+
use PhpMyAdmin\Database\Events;
12+
use PhpMyAdmin\Database\Routines;
1113
use PhpMyAdmin\Database\Triggers;
1214
use PhpMyAdmin\DatabaseInterface;
1315
use PhpMyAdmin\FieldMetadata;
@@ -556,9 +558,9 @@ protected function exportRoutineSQL(
556558
}
557559

558560
if ($type === 'FUNCTION') {
559-
$definition = $GLOBALS['dbi']->getDefinition($db, 'FUNCTION', $routine);
561+
$definition = Routines::getFunctionDefinition($GLOBALS['dbi'], $db, $routine);
560562
} else {
561-
$definition = $GLOBALS['dbi']->getDefinition($db, 'PROCEDURE', $routine);
563+
$definition = Routines::getProcedureDefinition($GLOBALS['dbi'], $db, $routine);
562564
}
563565

564566
$createQuery = $this->replaceWithAliases($definition, $aliases, $db, '', $flag);
@@ -1012,7 +1014,7 @@ public function exportEvents($db): bool
10121014
. $delimiter . $GLOBALS['crlf'];
10131015
}
10141016

1015-
$eventDef = $GLOBALS['dbi']->getDefinition($db, 'EVENT', $eventName);
1017+
$eventDef = Events::getDefinition($GLOBALS['dbi'], $db, $eventName);
10161018
if (! empty($eventDef) && $GLOBALS['cfg']['Export']['remove_definer_from_definitions']) {
10171019
// remove definer clause from the event definition
10181020
$parser = new Parser($eventDef);

libraries/classes/Plugins/Export/ExportXml.php

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

55
namespace PhpMyAdmin\Plugins\Export;
66

7+
use PhpMyAdmin\Database\Events;
8+
use PhpMyAdmin\Database\Routines;
79
use PhpMyAdmin\Database\Triggers;
810
use PhpMyAdmin\DatabaseInterface;
911
use PhpMyAdmin\Plugins\ExportPlugin;
@@ -167,11 +169,11 @@ private function exportDefinitions(string $db, string $type, array $names): stri
167169
$head .= ' <pma:' . $type . ' name="' . htmlspecialchars($name) . '">' . $GLOBALS['crlf'];
168170

169171
if ($type === 'function') {
170-
$definition = $GLOBALS['dbi']->getDefinition($db, 'FUNCTION', $name);
172+
$definition = Routines::getFunctionDefinition($GLOBALS['dbi'], $db, $name);
171173
} elseif ($type === 'procedure') {
172-
$definition = $GLOBALS['dbi']->getDefinition($db, 'PROCEDURE', $name);
174+
$definition = Routines::getProcedureDefinition($GLOBALS['dbi'], $db, $name);
173175
} else {
174-
$definition = $GLOBALS['dbi']->getDefinition($db, 'EVENT', $name);
176+
$definition = Events::getDefinition($GLOBALS['dbi'], $db, $name);
175177
}
176178

177179
// Do some formatting

phpstan-baseline.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5420,6 +5420,11 @@ parameters:
54205420
count: 5
54215421
path: libraries/classes/Plugins/Export/ExportSql.php
54225422

5423+
-
5424+
message: "#^Parameter \\#1 \\$sqlQuery of method PhpMyAdmin\\\\Plugins\\\\Export\\\\ExportSql\\:\\:replaceWithAliases\\(\\) expects string, string\\|null given\\.$#"
5425+
count: 1
5426+
path: libraries/classes/Plugins/Export/ExportSql.php
5427+
54235428
-
54245429
message: "#^Parameter \\#1 \\$str of function strtoupper expects string, mixed given\\.$#"
54255430
count: 1

psalm-baseline.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5175,14 +5175,15 @@
51755175
</RedundantPropertyInitializationCheck>
51765176
</file>
51775177
<file src="libraries/classes/Database/Routines.php">
5178-
<MixedArgument occurrences="70">
5178+
<MixedArgument occurrences="71">
51795179
<code>$GLOBALS['errors']</code>
51805180
<code>$_GET['item_name']</code>
51815181
<code>$_GET['item_name']</code>
51825182
<code>$_GET['item_name']</code>
51835183
<code>$_GET['item_name']</code>
51845184
<code>$_GET['item_name']</code>
51855185
<code>$_GET['item_name']</code>
5186+
<code>$_GET['item_name']</code>
51865187
<code>$_GET['item_type']</code>
51875188
<code>$_GET['item_type']</code>
51885189
<code>$_POST['item_comment']</code>
@@ -5217,7 +5218,7 @@
52175218
<code>$itemType</code>
52185219
<code>$newErrors</code>
52195220
<code>$routine</code>
5220-
<code>$routine['ROUTINE_TYPE']</code>
5221+
<code>$routine['SPECIFIC_NAME']</code>
52215222
<code>$routine['SPECIFIC_NAME']</code>
52225223
<code>$routine['item_name']</code>
52235224
<code>$routine['item_name']</code>
@@ -5237,7 +5238,7 @@
52375238
<code>$routine['name']</code>
52385239
<code>$routine['name']</code>
52395240
<code>$routine['name']</code>
5240-
<code>$routine['type']</code>
5241+
<code>$routine['name']</code>
52415242
<code>$routine['type']</code>
52425243
<code>$routine['type']</code>
52435244
<code>$value</code>

test/classes/Plugins/Export/ExportSqlTest.php

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -581,27 +581,11 @@ public function testExportEvents(): void
581581
->will($this->returnValue(['f1', 'f2']));
582582

583583
$dbi->expects($this->exactly(2))
584-
->method('getDefinition')
585-
->will(
586-
$this->returnValueMap(
587-
[
588-
[
589-
'db',
590-
'EVENT',
591-
'f1',
592-
DatabaseInterface::CONNECT_USER,
593-
'f1event',
594-
],
595-
[
596-
'db',
597-
'EVENT',
598-
'f2',
599-
DatabaseInterface::CONNECT_USER,
600-
'f2event',
601-
],
602-
]
603-
)
604-
);
584+
->method('fetchValue')
585+
->will($this->returnValueMap([
586+
['SHOW CREATE EVENT `db`.`f1`', 'Create Event', DatabaseInterface::CONNECT_USER, 'f1event'],
587+
['SHOW CREATE EVENT `db`.`f2`', 'Create Event', DatabaseInterface::CONNECT_USER, 'f2event'],
588+
]));
605589
$dbi->expects($this->any())->method('escapeString')
606590
->will($this->returnArgument(0));
607591

0 commit comments

Comments
 (0)