Skip to content

Commit

Permalink
Improve return type of Index::getPrimary method
Browse files Browse the repository at this point in the history
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Sep 8, 2022
1 parent 9d2350d commit d6b72de
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 28 deletions.
3 changes: 1 addition & 2 deletions libraries/classes/Controllers/Table/RelationController.php
Expand Up @@ -358,9 +358,8 @@ public function getDropdownValueForTable(): void

$this->response->addJSON('columns', $columnList);

// @todo should be: $server->db($db)->table($table)->primary()
$primary = Index::getPrimary($this->dbi, $foreignTable, $_POST['foreignDb']);
if ($primary === false) {
if ($primary === null) {
return;
}

Expand Down
13 changes: 6 additions & 7 deletions libraries/classes/Controllers/Table/StructureController.php
Expand Up @@ -158,18 +158,17 @@ public function __invoke(ServerRequest $request): void
/**
* Displays the table structure ('show table' works correct since 3.23.03)
*
* @param array $columns_with_unique_index Columns with unique index
* @param Index|false $primary_index primary index or false if no one exists
* @param array $fields Fields
* @param array $columns_with_index Columns with index
* @param array $columns_with_unique_index Columns with unique index
* @param array $fields Fields
* @param array $columns_with_index Columns with index
* @psalm-param non-empty-string $route
*
* @return string
*/
protected function displayStructure(
RelationParameters $relationParameters,
array $columns_with_unique_index,
$primary_index,
?Index $primaryIndex,
array $fields,
array $columns_with_index,
bool $isSystemSchema,
Expand Down Expand Up @@ -234,7 +233,7 @@ protected function displayStructure(
$row_comments[$rownum] = $comments_map[$field['Field']];
}

if ($primary_index && $primary_index->hasColumn($field['Field'])) {
if ($primaryIndex !== null && $primaryIndex->hasColumn($field['Field'])) {
$displayed_fields[$rownum]->icon .= Generator::getImage('b_primary', __('Primary'));
}

Expand Down Expand Up @@ -272,7 +271,7 @@ protected function displayStructure(
'tbl_is_view' => $GLOBALS['tbl_is_view'],
'mime_map' => $mime_map,
'tbl_storage_engine' => $GLOBALS['tbl_storage_engine'],
'primary' => $primary_index,
'primary' => $primaryIndex,
'columns_with_unique_index' => $columns_with_unique_index,
'columns_list' => $columns_list,
'table_stats' => $tablestats ?? null,
Expand Down
15 changes: 3 additions & 12 deletions libraries/classes/Index.php
Expand Up @@ -185,20 +185,11 @@ public static function getFromTableByChoice($table, $schema, $choices = 31)
return $indexes;
}

/**
* return primary if set, false otherwise
*
* @return Index|false primary index or false if no one exists
*/
public static function getPrimary(DatabaseInterface $dbi, string $table, string $schema)
public static function getPrimary(DatabaseInterface $dbi, string $table, string $schema): ?Index
{
self::loadIndexes($dbi, $table, $schema);

if (isset(self::$registry[$schema][$table]['PRIMARY'])) {
return self::$registry[$schema][$table]['PRIMARY'];
}

return false;
return self::$registry[$schema][$table]['PRIMARY'] ?? null;
}

/**
Expand Down Expand Up @@ -462,7 +453,7 @@ public static function getIndexTypes()

public function hasPrimary(): bool
{
return (bool) self::getPrimary($GLOBALS['dbi'], $this->table, $this->schema);
return self::getPrimary($GLOBALS['dbi'], $this->table, $this->schema) !== null;
}

/**
Expand Down
12 changes: 6 additions & 6 deletions libraries/classes/Normalization.php
Expand Up @@ -277,7 +277,7 @@ public function getHtmlContentsFor1NFStep2($db, $table)
$hasPrimaryKey = '0';
$legendText = __('Step 1.') . $step . ' ' . $stepTxt;
$extra = '';
if ($primary !== false) {
if ($primary !== null) {
$headText = __('Primary key already exists.');
$subText = __('Taking you to next step…');
$hasPrimaryKey = '1';
Expand Down Expand Up @@ -376,7 +376,7 @@ public function getHtmlContentsFor1NFStep3($db, $table)
. '<input class="btn btn-secondary" type="submit" value="' . __('No repeating group')
. '" onclick="goToStep4();">';
$primary = Index::getPrimary($this->dbi, $table, $db);
$primarycols = $primary === false ? [] : $primary->getColumns();
$primarycols = $primary === null ? [] : $primary->getColumns();
$pk = [];
foreach ($primarycols as $col) {
$pk[] = $col->getName();
Expand All @@ -403,7 +403,7 @@ public function getHtmlFor2NFstep1($db, $table)
{
$legendText = __('Step 2.') . '1 ' . __('Find partial dependencies');
$primary = Index::getPrimary($this->dbi, $table, $db);
$primarycols = $primary === false ? [] : $primary->getColumns();
$primarycols = $primary === null ? [] : $primary->getColumns();
$pk = [];
$subText = '';
$selectPkForm = '';
Expand Down Expand Up @@ -627,7 +627,7 @@ public function getHtmlForNewTables3NF($dependencies, array $tables, $db)
}

$primary = Index::getPrimary($this->dbi, $table, $db);
$primarycols = $primary === false ? [] : $primary->getColumns();
$primarycols = $primary === null ? [] : $primary->getColumns();
$pk = [];
foreach ($primarycols as $col) {
$pk[] = $col->getName();
Expand Down Expand Up @@ -879,7 +879,7 @@ public function getHtmlFor3NFstep1($db, array $tables)
$cnt = 0;
foreach ($tables as $table) {
$primary = Index::getPrimary($this->dbi, $table, $db);
$primarycols = $primary === false ? [] : $primary->getColumns();
$primarycols = $primary === null ? [] : $primary->getColumns();
$selectTdForm = '';
$pk = [];
foreach ($primarycols as $col) {
Expand Down Expand Up @@ -961,7 +961,7 @@ public function findPartialDependencies($table, $db)
);
$totalRows = $totalRowsRes[0];
$primary = Index::getPrimary($this->dbi, $table, $db);
$primarycols = $primary === false ? [] : $primary->getColumns();
$primarycols = $primary === null ? [] : $primary->getColumns();
$pk = [];
foreach ($primarycols as $col) {
$pk[] = Util::backquote($col->getName());
Expand Down
2 changes: 1 addition & 1 deletion libraries/classes/Sql.php
Expand Up @@ -504,7 +504,7 @@ public function getDefaultSqlQueryForBrowse($db, $table): string
$primaryKey = null;
$primary = Index::getPrimary($this->dbi, $table, $db);

if ($primary !== false) {
if ($primary !== null) {
$primarycols = $primary->getColumns();

foreach ($primarycols as $col) {
Expand Down

0 comments on commit d6b72de

Please sign in to comment.