diff options
author | Kamil Tekiela <tekiela246@gmail.com> | 2022-11-07 01:48:49 +0300 |
---|---|---|
committer | Kamil Tekiela <tekiela246@gmail.com> | 2022-11-08 03:31:47 +0300 |
commit | ba41957e5f51efb63c562e94b986cd8f0912d39b (patch) | |
tree | 25e6207a37b3ab0a259f0115aa51ff858fc7040a /libraries | |
parent | 4405b8afa42ad4057189a48b1ab6ca14d04403fc (diff) |
Redesign PrimaryController
Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
Diffstat (limited to 'libraries')
-rw-r--r-- | libraries/classes/Controllers/Table/Structure/PrimaryController.php | 49 |
1 files changed, 18 insertions, 31 deletions
diff --git a/libraries/classes/Controllers/Table/Structure/PrimaryController.php b/libraries/classes/Controllers/Table/Structure/PrimaryController.php index d218e1d94a..79777e0787 100644 --- a/libraries/classes/Controllers/Table/Structure/PrimaryController.php +++ b/libraries/classes/Controllers/Table/Structure/PrimaryController.php @@ -17,6 +17,7 @@ use PhpMyAdmin\Util; use function __; use function count; +use function is_array; final class PrimaryController extends AbstractController { @@ -43,26 +44,23 @@ final class PrimaryController extends AbstractController $GLOBALS['urlParams'] = $GLOBALS['urlParams'] ?? null; $GLOBALS['errorUrl'] = $GLOBALS['errorUrl'] ?? null; - $selected = $request->getParsedBodyParam('selected', []); - $selected_fld = $request->getParsedBodyParam('selected_fld', []); + /** @var string[]|null $selected */ + $selected = $request->getParsedBodyParam('selected_fld', $request->getParsedBodyParam('selected')); - if (empty($selected) && empty($selected_fld)) { + if (! is_array($selected) || $selected === []) { $this->response->setRequestStatus(false); $this->response->addJSON('message', __('No column selected.')); return; } - $primary = $this->getKeyForTablePrimary(); - if (empty($primary) && ! empty($selected_fld)) { - // no primary key, so we can safely create new - $mult_btn = __('Yes'); - $selected = $selected_fld; - } + $this->dbi->selectDb($GLOBALS['db']); + $hasPrimary = $this->hasPrimaryKey(); - $mult_btn = $_POST['mult_btn'] ?? $mult_btn ?? ''; + /** @var string|null $deletionConfirmed */ + $deletionConfirmed = $request->getParsedBodyParam('mult_btn', null); - if (! empty($selected_fld) && ! empty($primary)) { + if ($hasPrimary && $deletionConfirmed === null) { $this->checkParameters(['db', 'table']); $GLOBALS['urlParams'] = ['db' => $GLOBALS['db'], 'table' => $GLOBALS['table']]; @@ -74,15 +72,15 @@ final class PrimaryController extends AbstractController $this->render('table/structure/primary', [ 'db' => $GLOBALS['db'], 'table' => $GLOBALS['table'], - 'selected' => $selected_fld, + 'selected' => $selected, ]); return; } - if ($mult_btn === __('Yes')) { + if ($deletionConfirmed === __('Yes') || ! $hasPrimary) { $GLOBALS['sql_query'] = 'ALTER TABLE ' . Util::backquote($GLOBALS['table']); - if (! empty($primary)) { + if ($hasPrimary) { $GLOBALS['sql_query'] .= ' DROP PRIMARY KEY,'; } @@ -110,27 +108,16 @@ final class PrimaryController extends AbstractController ($this->structureController)($request); } - /** - * Gets table primary key - * - * @return string - */ - private function getKeyForTablePrimary() + private function hasPrimaryKey(): bool { - $this->dbi->selectDb($GLOBALS['db']); - $result = $this->dbi->query( - 'SHOW KEYS FROM ' . Util::backquote($GLOBALS['table']) . ';' - ); - $primary = ''; + $result = $this->dbi->query('SHOW KEYS FROM ' . Util::backquote($GLOBALS['table'])); + foreach ($result as $row) { - // Backups the list of primary keys - if ($row['Key_name'] !== 'PRIMARY') { - continue; + if ($row['Key_name'] === 'PRIMARY') { + return true; } - - $primary .= $row['Column_name'] . ', '; } - return $primary; + return false; } } |