Skip to content

Commit e96fab6

Browse files
committed
Move DBI's getProceduresOrFunctions into Routines class
Splits the method into Routines::getFunctionNames and Routines::getProcedureNames and adds tests. Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent 7975f5f commit e96fab6

9 files changed

Lines changed: 133 additions & 67 deletions

File tree

‎libraries/classes/Database/Routines.php‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,4 +1692,44 @@ public static function getProcedureDefinition(DatabaseInterface $dbi, string $db
16921692

16931693
return is_string($result) ? $result : null;
16941694
}
1695+
1696+
/**
1697+
* @return array<int, string>
1698+
* @psalm-return list<non-empty-string>
1699+
*/
1700+
public static function getFunctionNames(DatabaseInterface $dbi, string $db): array
1701+
{
1702+
/** @psalm-var list<array{Db: string, Name: string, Type: string}> $functions */
1703+
$functions = $dbi->fetchResult('SHOW FUNCTION STATUS;');
1704+
$names = [];
1705+
foreach ($functions as $function) {
1706+
if ($function['Db'] !== $db || $function['Type'] !== 'FUNCTION' || $function['Name'] === '') {
1707+
continue;
1708+
}
1709+
1710+
$names[] = $function['Name'];
1711+
}
1712+
1713+
return $names;
1714+
}
1715+
1716+
/**
1717+
* @return array<int, string>
1718+
* @psalm-return list<non-empty-string>
1719+
*/
1720+
public static function getProcedureNames(DatabaseInterface $dbi, string $db): array
1721+
{
1722+
/** @psalm-var list<array{Db: string, Name: string, Type: string}> $procedures */
1723+
$procedures = $dbi->fetchResult('SHOW PROCEDURE STATUS;');
1724+
$names = [];
1725+
foreach ($procedures as $procedure) {
1726+
if ($procedure['Db'] !== $db || $procedure['Type'] !== 'PROCEDURE' || $procedure['Name'] === '') {
1727+
continue;
1728+
}
1729+
1730+
$names[] = $procedure['Name'];
1731+
}
1732+
1733+
return $names;
1734+
}
16951735
}

‎libraries/classes/DatabaseInterface.php‎

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,33 +1465,6 @@ public function getWarnings($link = self::CONNECT_USER): array
14651465
return $warnings;
14661466
}
14671467

1468-
/**
1469-
* returns an array of PROCEDURE or FUNCTION names for a db
1470-
*
1471-
* @param string $db db name
1472-
* @param string $which PROCEDURE | FUNCTION
1473-
* @param int $link link type
1474-
*
1475-
* @return string[] the procedure names or function names
1476-
*/
1477-
public function getProceduresOrFunctions(
1478-
string $db,
1479-
string $which,
1480-
$link = self::CONNECT_USER
1481-
): array {
1482-
$shows = $this->fetchResult('SHOW ' . $which . ' STATUS;', null, null, $link);
1483-
$result = [];
1484-
foreach ($shows as $oneShow) {
1485-
if ($oneShow['Db'] != $db || $oneShow['Type'] != $which) {
1486-
continue;
1487-
}
1488-
1489-
$result[] = $oneShow['Name'];
1490-
}
1491-
1492-
return $result;
1493-
}
1494-
14951468
/**
14961469
* gets the current user with host
14971470
*

‎libraries/classes/Dbal/DbalInterface.php‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -429,21 +429,6 @@ public function getCompatibilities(): array;
429429
*/
430430
public function getWarnings($link = DatabaseInterface::CONNECT_USER): array;
431431

432-
/**
433-
* returns an array of PROCEDURE or FUNCTION names for a db
434-
*
435-
* @param string $db db name
436-
* @param string $which PROCEDURE | FUNCTION
437-
* @param int $link link type
438-
*
439-
* @return string[] the procedure names or function names
440-
*/
441-
public function getProceduresOrFunctions(
442-
string $db,
443-
string $which,
444-
$link = DatabaseInterface::CONNECT_USER
445-
): array;
446-
447432
/**
448433
* gets the current user with host
449434
*

‎libraries/classes/Operations.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function __construct(DatabaseInterface $dbi, Relation $relation)
5757
*/
5858
public function runProcedureAndFunctionDefinitions($db): void
5959
{
60-
$procedure_names = $this->dbi->getProceduresOrFunctions($db, 'PROCEDURE');
60+
$procedure_names = Routines::getProcedureNames($this->dbi, $db);
6161
if ($procedure_names) {
6262
foreach ($procedure_names as $procedure_name) {
6363
$this->dbi->selectDb($db);
@@ -73,7 +73,7 @@ public function runProcedureAndFunctionDefinitions($db): void
7373
}
7474
}
7575

76-
$function_names = $this->dbi->getProceduresOrFunctions($db, 'FUNCTION');
76+
$function_names = Routines::getFunctionNames($this->dbi, $db);
7777
if (! $function_names) {
7878
return;
7979
}

‎libraries/classes/Plugins/Export/ExportSql.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -611,8 +611,8 @@ public function exportRoutines($db, array $aliases = []): bool
611611
$text = '';
612612
$delimiter = '$$';
613613

614-
$procedureNames = $GLOBALS['dbi']->getProceduresOrFunctions($db, 'PROCEDURE');
615-
$functionNames = $GLOBALS['dbi']->getProceduresOrFunctions($db, 'FUNCTION');
614+
$procedureNames = Routines::getProcedureNames($GLOBALS['dbi'], $db);
615+
$functionNames = Routines::getFunctionNames($GLOBALS['dbi'], $db);
616616

617617
if ($procedureNames || $functionNames) {
618618
$text .= $GLOBALS['crlf']

‎libraries/classes/Plugins/Export/ExportXml.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,15 +323,15 @@ public function exportHeader(): bool
323323
$head .= $this->exportDefinitions(
324324
$GLOBALS['db'],
325325
'function',
326-
$GLOBALS['dbi']->getProceduresOrFunctions($GLOBALS['db'], 'FUNCTION')
326+
Routines::getFunctionNames($GLOBALS['dbi'], $GLOBALS['db'])
327327
);
328328
}
329329

330330
if (isset($GLOBALS['xml_export_procedures']) && $GLOBALS['xml_export_procedures']) {
331331
$head .= $this->exportDefinitions(
332332
$GLOBALS['db'],
333333
'procedure',
334-
$GLOBALS['dbi']->getProceduresOrFunctions($GLOBALS['db'], 'PROCEDURE')
334+
Routines::getProcedureNames($GLOBALS['dbi'], $GLOBALS['db'])
335335
);
336336
}
337337

‎psalm-baseline.xml‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5673,11 +5673,8 @@
56735673
<code>uksort($eachTables, 'strnatcasecmp')</code>
56745674
<code>usort($tables, 'strnatcasecmp')</code>
56755675
</MixedArgumentTypeCoercion>
5676-
<MixedArrayAccess occurrences="8">
5676+
<MixedArrayAccess occurrences="5">
56775677
<code>$link</code>
5678-
<code>$oneShow['Db']</code>
5679-
<code>$oneShow['Name']</code>
5680-
<code>$oneShow['Type']</code>
56815678
<code>$tableData[$sortBy]</code>
56825679
<code>$tableData['Data_length']</code>
56835680
<code>$tableData['Engine']</code>
@@ -5695,7 +5692,7 @@
56955692
<code>$this-&gt;links[$link]</code>
56965693
<code>$this-&gt;links[$link]</code>
56975694
</MixedArrayOffset>
5698-
<MixedAssignment occurrences="21">
5695+
<MixedAssignment occurrences="19">
56995696
<code>$aLength</code>
57005697
<code>$bLength</code>
57015698
<code>$database</code>
@@ -5705,11 +5702,9 @@
57055702
<code>$grant</code>
57065703
<code>$keyIndex</code>
57075704
<code>$map['real_column']</code>
5708-
<code>$oneShow</code>
57095705
<code>$resultRows[$row[$key]]</code>
57105706
<code>$resultRows[]</code>
57115707
<code>$resultTarget</code>
5712-
<code>$result[]</code>
57135708
<code>$table</code>
57145709
<code>$tableData</code>
57155710
<code>$tableData</code>
@@ -5734,12 +5729,10 @@
57345729
<code>SessionCache::get('mysql_cur_user')</code>
57355730
<code>reset($columns)</code>
57365731
</MixedReturnStatement>
5737-
<MixedReturnTypeCoercion occurrences="6">
5738-
<code>$result</code>
5732+
<MixedReturnTypeCoercion occurrences="4">
57395733
<code>$this-&gt;fetchResult($sql, null, 'Field', $link)</code>
57405734
<code>$this-&gt;fetchResult($sql, null, null, $link)</code>
57415735
<code>string[]</code>
5742-
<code>string[]</code>
57435736
</MixedReturnTypeCoercion>
57445737
<NullableReturnStatement occurrences="2">
57455738
<code>$user</code>

‎test/classes/Database/RoutinesTest.php‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,4 +1433,84 @@ public function providerGetQueryFromRequest(): array
14331433
],
14341434
];
14351435
}
1436+
1437+
public function testGetFunctionNames(): void
1438+
{
1439+
$dbiDummy = $this->createDbiDummy();
1440+
$dbiDummy->addResult(
1441+
'SHOW FUNCTION STATUS;',
1442+
[
1443+
['db_test', 'test_func', 'FUNCTION'],
1444+
['test_db', 'test_func1', 'FUNCTION'],
1445+
['test_db', '', 'FUNCTION'],
1446+
['test_db', 'test_func2', 'FUNCTION'],
1447+
['test_db', 'test_func', 'PROCEDURE'],
1448+
],
1449+
['Db', 'Name', 'Type']
1450+
);
1451+
1452+
$names = Routines::getFunctionNames($this->createDatabaseInterface($dbiDummy), 'test_db');
1453+
$this->assertSame(['test_func1', 'test_func2'], $names);
1454+
1455+
$dbiDummy->assertAllQueriesConsumed();
1456+
}
1457+
1458+
public function testGetFunctionNamesWithEmptyReturn(): void
1459+
{
1460+
$dbiDummy = $this->createDbiDummy();
1461+
$dbiDummy->addResult(
1462+
'SHOW FUNCTION STATUS;',
1463+
[
1464+
['db_test', 'test_func', 'FUNCTION'],
1465+
['test_db', '', 'FUNCTION'],
1466+
['test_db', 'test_func', 'PROCEDURE'],
1467+
],
1468+
['Db', 'Name', 'Type']
1469+
);
1470+
1471+
$names = Routines::getFunctionNames($this->createDatabaseInterface($dbiDummy), 'test_db');
1472+
$this->assertSame([], $names);
1473+
1474+
$dbiDummy->assertAllQueriesConsumed();
1475+
}
1476+
1477+
public function testGetProcedureNames(): void
1478+
{
1479+
$dbiDummy = $this->createDbiDummy();
1480+
$dbiDummy->addResult(
1481+
'SHOW PROCEDURE STATUS;',
1482+
[
1483+
['db_test', 'test_proc', 'PROCEDURE'],
1484+
['test_db', 'test_proc1', 'PROCEDURE'],
1485+
['test_db', '', 'PROCEDURE'],
1486+
['test_db', 'test_proc2', 'PROCEDURE'],
1487+
['test_db', 'test_proc', 'FUNCTION'],
1488+
],
1489+
['Db', 'Name', 'Type']
1490+
);
1491+
1492+
$names = Routines::getProcedureNames($this->createDatabaseInterface($dbiDummy), 'test_db');
1493+
$this->assertSame(['test_proc1', 'test_proc2'], $names);
1494+
1495+
$dbiDummy->assertAllQueriesConsumed();
1496+
}
1497+
1498+
public function testGetProcedureNamesWithEmptyReturn(): void
1499+
{
1500+
$dbiDummy = $this->createDbiDummy();
1501+
$dbiDummy->addResult(
1502+
'SHOW PROCEDURE STATUS;',
1503+
[
1504+
['db_test', 'test_proc', 'PROCEDURE'],
1505+
['test_db', '', 'PROCEDURE'],
1506+
['test_db', 'test_proc', 'FUNCTION'],
1507+
],
1508+
['Db', 'Name', 'Type']
1509+
);
1510+
1511+
$names = Routines::getProcedureNames($this->createDatabaseInterface($dbiDummy), 'test_db');
1512+
$this->assertSame([], $names);
1513+
1514+
$dbiDummy->assertAllQueriesConsumed();
1515+
}
14361516
}

‎test/classes/Plugins/Export/ExportXmlTest.php‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -215,17 +215,12 @@ public function testExportHeader(): void
215215
'DEFINER' => 'test_user@localhost',
216216
],
217217
];
218+
$functions = [['Db' => 'd<"b', 'Name' => 'fn', 'Type' => 'FUNCTION']];
219+
$procedures = [['Db' => 'd<"b', 'Name' => 'pr', 'Type' => 'PROCEDURE']];
218220

219-
$dbi->expects($this->exactly(4))
221+
$dbi->expects($this->exactly(6))
220222
->method('fetchResult')
221-
->willReturnOnConsecutiveCalls($result, $result, [], $triggers);
222-
223-
$dbi->expects($this->exactly(2))
224-
->method('getProceduresOrFunctions')
225-
->willReturnOnConsecutiveCalls(
226-
['fn'],
227-
['pr']
228-
);
223+
->willReturnOnConsecutiveCalls($result, $result, [], $triggers, $functions, $procedures);
229224

230225
$dbi->expects($this->exactly(2))
231226
->method('fetchValue')

0 commit comments

Comments
 (0)