Create getForeignKeysData

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
This commit is contained in:
Kamil Tekiela 2024-09-20 18:48:30 +01:00
parent dc889691d7
commit 6a93167d39
3 changed files with 30 additions and 151 deletions

View File

@ -4280,26 +4280,6 @@ parameters:
count: 1
path: src/Controllers/Table/Partition/RepairController.php
-
message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#"
count: 1
path: src/Controllers/Table/RelationController.php
-
message: "#^Cannot access offset 'foreign_db' on mixed\\.$#"
count: 1
path: src/Controllers/Table/RelationController.php
-
message: "#^Cannot access offset 'foreign_field' on mixed\\.$#"
count: 1
path: src/Controllers/Table/RelationController.php
-
message: "#^Cannot access offset 'foreign_table' on mixed\\.$#"
count: 1
path: src/Controllers/Table/RelationController.php
-
message: "#^Cannot access offset 'ref_db_name' on mixed\\.$#"
count: 1
@ -4385,11 +4365,6 @@ parameters:
count: 1
path: src/Controllers/Table/RelationController.php
-
message: "#^Parameter \\#7 \\$existrelForeign of method PhpMyAdmin\\\\Table\\\\Table\\:\\:updateForeignKeys\\(\\) expects array, mixed given\\.$#"
count: 1
path: src/Controllers/Table/RelationController.php
-
message: "#^Argument of an invalid type array\\|string supplied for foreach, only iterables are supported\\.$#"
count: 1
@ -5722,7 +5697,7 @@ parameters:
-
message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#"
count: 3
count: 2
path: src/Database/Designer/Common.php
-
@ -5730,21 +5705,6 @@ parameters:
count: 1
path: src/Database/Designer/Common.php
-
message: "#^Cannot access offset 'foreign_db' on mixed\\.$#"
count: 1
path: src/Database/Designer/Common.php
-
message: "#^Cannot access offset 'foreign_field' on mixed\\.$#"
count: 1
path: src/Database/Designer/Common.php
-
message: "#^Cannot access offset 'foreign_table' on mixed\\.$#"
count: 1
path: src/Database/Designer/Common.php
-
message: "#^Cannot access offset 'index_list' on mixed\\.$#"
count: 1
@ -6337,7 +6297,7 @@ parameters:
-
message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#"
count: 6
count: 5
path: src/Display/Results.php
-
@ -6350,21 +6310,6 @@ parameters:
count: 6
path: src/Display/Results.php
-
message: "#^Cannot access offset 'foreign_db' on mixed\\.$#"
count: 2
path: src/Display/Results.php
-
message: "#^Cannot access offset 'foreign_field' on mixed\\.$#"
count: 1
path: src/Display/Results.php
-
message: "#^Cannot access offset 'foreign_table' on mixed\\.$#"
count: 2
path: src/Display/Results.php
-
message: "#^Cannot access offset 'geoOption' on mixed\\.$#"
count: 8
@ -8925,21 +8870,6 @@ parameters:
count: 4
path: src/Normalization.php
-
message: "#^Cannot access offset 'foreign_db' on mixed\\.$#"
count: 4
path: src/Operations.php
-
message: "#^Cannot access offset 'foreign_field' on mixed\\.$#"
count: 3
path: src/Operations.php
-
message: "#^Cannot access offset 'foreign_table' on mixed\\.$#"
count: 4
path: src/Operations.php
-
message: "#^Cannot access offset 0 on mixed\\.$#"
count: 6
@ -10255,16 +10185,6 @@ parameters:
count: 1
path: src/Plugins/Export/Helpers/Pdf.php
-
message: "#^Cannot access offset 'foreign_field' on mixed\\.$#"
count: 1
path: src/Plugins/Export/Helpers/Pdf.php
-
message: "#^Cannot access offset 'foreign_table' on mixed\\.$#"
count: 1
path: src/Plugins/Export/Helpers/Pdf.php
-
message: "#^Cannot access offset 'olm' on mixed\\.$#"
count: 2
@ -10792,16 +10712,6 @@ parameters:
-
message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#"
count: 2
path: src/Plugins/Schema/Dia/DiaRelationSchema.php
-
message: "#^Cannot access offset 'foreign_field' on mixed\\.$#"
count: 1
path: src/Plugins/Schema/Dia/DiaRelationSchema.php
-
message: "#^Cannot access offset 'foreign_table' on mixed\\.$#"
count: 1
path: src/Plugins/Schema/Dia/DiaRelationSchema.php
@ -10867,16 +10777,6 @@ parameters:
-
message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#"
count: 2
path: src/Plugins/Schema/Eps/EpsRelationSchema.php
-
message: "#^Cannot access offset 'foreign_field' on mixed\\.$#"
count: 1
path: src/Plugins/Schema/Eps/EpsRelationSchema.php
-
message: "#^Cannot access offset 'foreign_table' on mixed\\.$#"
count: 1
path: src/Plugins/Schema/Eps/EpsRelationSchema.php
@ -10992,7 +10892,7 @@ parameters:
-
message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#"
count: 2
count: 1
path: src/Plugins/Schema/Pdf/PdfRelationSchema.php
-
@ -11005,16 +10905,6 @@ parameters:
count: 10
path: src/Plugins/Schema/Pdf/PdfRelationSchema.php
-
message: "#^Cannot access offset 'foreign_field' on mixed\\.$#"
count: 1
path: src/Plugins/Schema/Pdf/PdfRelationSchema.php
-
message: "#^Cannot access offset 'foreign_table' on mixed\\.$#"
count: 1
path: src/Plugins/Schema/Pdf/PdfRelationSchema.php
-
message: "#^Cannot access offset 'index_list' on mixed\\.$#"
count: 1
@ -11187,16 +11077,6 @@ parameters:
-
message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#"
count: 2
path: src/Plugins/Schema/Svg/SvgRelationSchema.php
-
message: "#^Cannot access offset 'foreign_field' on mixed\\.$#"
count: 1
path: src/Plugins/Schema/Svg/SvgRelationSchema.php
-
message: "#^Cannot access offset 'foreign_table' on mixed\\.$#"
count: 1
path: src/Plugins/Schema/Svg/SvgRelationSchema.php

View File

@ -37,7 +37,6 @@ use function is_string;
use function ksort;
use function mb_check_encoding;
use function mb_strlen;
use function mb_strtolower;
use function mb_strtoupper;
use function mb_substr;
use function natcasesort;
@ -46,6 +45,7 @@ use function sprintf;
use function str_contains;
use function str_replace;
use function strnatcasecmp;
use function strtolower;
use function trim;
use function uksort;
use function usort;
@ -370,8 +370,9 @@ class Relation
* @param string $table the name of the table to check for
* @param string $column the name of the column to check for
* @param string $source the source for foreign key information
* @psalm-param 'both'|'internal'|'foreign' $source
*
* @return mixed[] db,table,column
* @return array<array<mixed>>
*/
public function getForeigners(string $db, string $table, string $column = '', string $source = 'both'): array
{
@ -389,40 +390,27 @@ class Relation
$relQuery .= ' AND `master_field` = ' . $this->dbi->quoteString($column);
}
/** @var array<array<string|null>> */
$foreign = $this->dbi->fetchResult($relQuery, 'master_field', null, ConnectionType::ControlUser);
}
if (($source === 'both' || $source === 'foreign') && $table !== '') {
$tableObj = new Table($table, $db, $this->dbi);
$showCreateTable = $tableObj->showCreate();
if ($showCreateTable !== '') {
$parser = new Parser($showCreateTable);
$stmt = $parser->statements[0];
$foreign['foreign_keys_data'] = [];
if ($stmt instanceof CreateStatement) {
$foreign['foreign_keys_data'] = TableUtils::getForeignKeys($stmt);
}
}
$foreign['foreign_keys_data'] = $this->getForeignKeysData($table, $db);
}
/**
* Emulating relations for some information_schema tables
*/
$isInformationSchema = mb_strtolower($db) === 'information_schema';
$isMysql = mb_strtolower($db) === 'mysql';
if (($isInformationSchema || $isMysql) && ($source === 'internal' || $source === 'both')) {
if ($isInformationSchema) {
$internalRelations = InternalRelations::INFORMATION_SCHEMA;
} else {
$internalRelations = InternalRelations::MYSQL;
}
if (in_array(strtolower($db), ['information_schema', 'mysql'], true) && ($source === 'internal' || $source === 'both')) {
$internalRelations = strtolower($db) === 'information_schema'
? InternalRelations::INFORMATION_SCHEMA
: InternalRelations::MYSQL;
if (isset($internalRelations[$table])) {
foreach ($internalRelations[$table] as $field => $relations) {
if (
($column !== '' && $column != $field)
|| (isset($foreign[$field])
&& $foreign[$field] != '')
($column !== '' && $column !== $field)
|| (isset($foreign[$field]) && $foreign[$field] != '')
) {
continue;
}
@ -435,6 +423,22 @@ class Relation
return $foreign;
}
/** @return list<array<string, array<int|string>|string|null>> */
private function getForeignKeysData(string $table, string $db): array
{
$tableObj = new Table($table, $db, $this->dbi);
$showCreateTable = $tableObj->showCreate();
if ($showCreateTable !== '') {
$parser = new Parser($showCreateTable);
$stmt = $parser->statements[0];
if ($stmt instanceof CreateStatement) {
return TableUtils::getForeignKeys($stmt);
}
}
return [];
}
/**
* Gets the display field of a table
*

View File

@ -130,11 +130,6 @@ class Common
$row = $this->relation->getForeigners(Current::$database, $val, '', 'foreign');
// We do not have access to the foreign keys if the user has partial access to the columns
if (! isset($row['foreign_keys_data'])) {
continue;
}
foreach ($row['foreign_keys_data'] as $oneKey) {
foreach ($oneKey['index_list'] as $index => $oneField) {
$con['C_NAME'][$i] = rawurlencode($oneKey['constraint']);