Skip to content

Commit 9d2350d

Browse files
committed
Extract dbi global variable from Index::loadIndexes
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent bc4fa31 commit 9d2350d

16 files changed

Lines changed: 41 additions & 56 deletions

File tree

libraries/classes/Controllers/Database/DataDictionaryController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function __invoke(ServerRequest $request): void
110110
'has_relation' => $hasRelation,
111111
'has_mime' => $relationParameters->browserTransformationFeature !== null,
112112
'columns' => $rows,
113-
'indexes' => Index::getFromTable($tableName, $GLOBALS['db']),
113+
'indexes' => Index::getFromTable($this->dbi, $tableName, $GLOBALS['db']),
114114
];
115115
}
116116

libraries/classes/Controllers/Table/OperationsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ public function __invoke(ServerRequest $request): void
407407
// a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
408408
// InnoDB always orders table rows according to such an index if one is present.
409409
if ($GLOBALS['tbl_storage_engine'] === 'INNODB') {
410-
$GLOBALS['indexes'] = Index::getFromTable($GLOBALS['table'], $GLOBALS['db']);
410+
$GLOBALS['indexes'] = Index::getFromTable($this->dbi, $GLOBALS['table'], $GLOBALS['db']);
411411
foreach ($GLOBALS['indexes'] as $name => $idx) {
412412
if ($name === 'PRIMARY') {
413413
$GLOBALS['hideOrderTable'] = true;

libraries/classes/Controllers/Table/RelationController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ public function getDropdownValueForTable(): void
359359
$this->response->addJSON('columns', $columnList);
360360

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

libraries/classes/Controllers/Table/StructureController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public function __invoke(ServerRequest $request): void
134134

135135
DbTableExists::check($GLOBALS['db'], $GLOBALS['table']);
136136

137-
$primary = Index::getPrimary($GLOBALS['table'], $GLOBALS['db']);
137+
$primary = Index::getPrimary($this->dbi, $GLOBALS['table'], $GLOBALS['db']);
138138
$columns_with_index = $this->dbi
139139
->getTable($GLOBALS['db'], $GLOBALS['table'])
140140
->getColumnsWithIndex(Index::UNIQUE | Index::INDEX | Index::SPATIAL | Index::FULLTEXT);
@@ -262,7 +262,7 @@ protected function displayStructure(
262262
return $this->template->render('table/structure/display_structure', [
263263
'collations' => $collations,
264264
'is_foreign_key_supported' => ForeignKey::isSupported($engine),
265-
'indexes' => Index::getFromTable($GLOBALS['table'], $GLOBALS['db']),
265+
'indexes' => Index::getFromTable($this->dbi, $GLOBALS['table'], $GLOBALS['db']),
266266
'indexes_duplicates' => Index::findDuplicates($GLOBALS['table'], $GLOBALS['db']),
267267
'relation_parameters' => $relationParameters,
268268
'hide_structure_actions' => $GLOBALS['cfg']['HideStructureActions'] === true,

libraries/classes/Database/Designer/Common.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ public function getAllKeys(array $designerTables, bool $unique_only = false): ar
221221
foreach ($designerTables as $designerTable) {
222222
$schema = $designerTable->getDatabaseName();
223223
// for now, take into account only the first index segment
224-
foreach (Index::getFromTable($designerTable->getTableName(), $schema) as $index) {
224+
foreach (Index::getFromTable($this->dbi, $designerTable->getTableName(), $schema) as $index) {
225225
if ($unique_only && ! $index->isUnique()) {
226226
continue;
227227
}

libraries/classes/DatabaseInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ private function attachIndexInfoToColumns(
964964
}
965965

966966
// Check if column is a part of multiple-column index and set its 'Key'.
967-
$indexes = Index::getFromTable($table, $database);
967+
$indexes = Index::getFromTable($this, $table, $database);
968968
foreach ($fields as $field => $fieldData) {
969969
if (! empty($fieldData['Key'])) {
970970
continue;

libraries/classes/Display/Results.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ private function getSortByKeyDropDown(
997997
string $unsortedSqlQuery
998998
): array {
999999
// grab indexes data:
1000-
$indexes = Index::getFromTable($this->properties['table'], $this->properties['db']);
1000+
$indexes = Index::getFromTable($this->dbi, $this->properties['table'], $this->properties['db']);
10011001

10021002
// do we have any index?
10031003
if ($indexes === []) {

libraries/classes/Index.php

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,17 @@ public function __construct(array $params = [])
106106
}
107107

108108
/**
109-
* Creates(if not already created) and returns the corresponding Index object
110-
*
111-
* @param string $schema database name
112-
* @param string $table table name
113-
* @param string $index_name index name
109+
* Creates (if not already created) and returns the corresponding Index object
114110
*
115111
* @return Index corresponding Index object
116112
*/
117-
public static function singleton($schema, $table, $index_name = '')
118-
{
119-
self::loadIndexes($table, $schema);
113+
public static function singleton(
114+
DatabaseInterface $dbi,
115+
string $schema,
116+
string $table,
117+
string $index_name = ''
118+
): Index {
119+
self::loadIndexes($dbi, $table, $schema);
120120
if (! isset(self::$registry[$schema][$table][$index_name])) {
121121
$index = new Index();
122122
if (strlen($index_name) > 0) {
@@ -133,14 +133,11 @@ public static function singleton($schema, $table, $index_name = '')
133133
/**
134134
* returns an array with all indexes from the given table
135135
*
136-
* @param string $table table
137-
* @param string $schema schema
138-
*
139-
* @return Index[] array of indexes
136+
* @return Index[]
140137
*/
141-
public static function getFromTable($table, $schema)
138+
public static function getFromTable(DatabaseInterface $dbi, string $table, string $schema): array
142139
{
143-
self::loadIndexes($table, $schema);
140+
self::loadIndexes($dbi, $table, $schema);
144141

145142
if (isset(self::$registry[$schema][$table])) {
146143
return self::$registry[$schema][$table];
@@ -161,7 +158,7 @@ public static function getFromTable($table, $schema)
161158
public static function getFromTableByChoice($table, $schema, $choices = 31)
162159
{
163160
$indexes = [];
164-
foreach (self::getFromTable($table, $schema) as $index) {
161+
foreach (self::getFromTable($GLOBALS['dbi'], $table, $schema) as $index) {
165162
if (($choices & self::PRIMARY) && $index->getChoice() === 'PRIMARY') {
166163
$indexes[] = $index;
167164
}
@@ -191,14 +188,11 @@ public static function getFromTableByChoice($table, $schema, $choices = 31)
191188
/**
192189
* return primary if set, false otherwise
193190
*
194-
* @param string $table table
195-
* @param string $schema schema
196-
*
197191
* @return Index|false primary index or false if no one exists
198192
*/
199-
public static function getPrimary($table, $schema)
193+
public static function getPrimary(DatabaseInterface $dbi, string $table, string $schema)
200194
{
201-
self::loadIndexes($table, $schema);
195+
self::loadIndexes($dbi, $table, $schema);
202196

203197
if (isset(self::$registry[$schema][$table]['PRIMARY'])) {
204198
return self::$registry[$schema][$table]['PRIMARY'];
@@ -209,17 +203,14 @@ public static function getPrimary($table, $schema)
209203

210204
/**
211205
* Load index data for table
212-
*
213-
* @param string $table table
214-
* @param string $schema schema
215206
*/
216-
private static function loadIndexes($table, $schema): bool
207+
private static function loadIndexes(DatabaseInterface $dbi, string $table, string $schema): bool
217208
{
218209
if (isset(self::$registry[$schema][$table])) {
219210
return true;
220211
}
221212

222-
$_raw_indexes = $GLOBALS['dbi']->getTableIndexes($schema, $table);
213+
$_raw_indexes = $dbi->getTableIndexes($schema, $table);
223214
foreach ($_raw_indexes as $_each_index) {
224215
$_each_index['Schema'] = $schema;
225216
$keyName = $_each_index['Key_name'];
@@ -471,7 +462,7 @@ public static function getIndexTypes()
471462

472463
public function hasPrimary(): bool
473464
{
474-
return (bool) self::getPrimary($this->table, $this->schema);
465+
return (bool) self::getPrimary($GLOBALS['dbi'], $this->table, $this->schema);
475466
}
476467

477468
/**
@@ -592,7 +583,7 @@ public function getCompareData()
592583
*/
593584
public static function findDuplicates($table, $schema)
594585
{
595-
$indexes = self::getFromTable($table, $schema);
586+
$indexes = self::getFromTable($GLOBALS['dbi'], $table, $schema);
596587

597588
$output = '';
598589

libraries/classes/Normalization.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public function getHtmlContentsFor1NFStep2($db, $table)
273273
{
274274
$step = 2;
275275
$stepTxt = __('Have a primary key');
276-
$primary = Index::getPrimary($table, $db);
276+
$primary = Index::getPrimary($this->dbi, $table, $db);
277277
$hasPrimaryKey = '0';
278278
$legendText = __('Step 1.') . $step . ' ' . $stepTxt;
279279
$extra = '';
@@ -375,7 +375,7 @@ public function getHtmlContentsFor1NFStep3($db, $table)
375375
. __('Done') . '">'
376376
. '<input class="btn btn-secondary" type="submit" value="' . __('No repeating group')
377377
. '" onclick="goToStep4();">';
378-
$primary = Index::getPrimary($table, $db);
378+
$primary = Index::getPrimary($this->dbi, $table, $db);
379379
$primarycols = $primary === false ? [] : $primary->getColumns();
380380
$pk = [];
381381
foreach ($primarycols as $col) {
@@ -402,7 +402,7 @@ public function getHtmlContentsFor1NFStep3($db, $table)
402402
public function getHtmlFor2NFstep1($db, $table)
403403
{
404404
$legendText = __('Step 2.') . '1 ' . __('Find partial dependencies');
405-
$primary = Index::getPrimary($table, $db);
405+
$primary = Index::getPrimary($this->dbi, $table, $db);
406406
$primarycols = $primary === false ? [] : $primary->getColumns();
407407
$pk = [];
408408
$subText = '';
@@ -626,7 +626,7 @@ public function getHtmlForNewTables3NF($dependencies, array $tables, $db)
626626
continue;
627627
}
628628

629-
$primary = Index::getPrimary($table, $db);
629+
$primary = Index::getPrimary($this->dbi, $table, $db);
630630
$primarycols = $primary === false ? [] : $primary->getColumns();
631631
$pk = [];
632632
foreach ($primarycols as $col) {
@@ -878,7 +878,7 @@ public function getHtmlFor3NFstep1($db, array $tables)
878878
);
879879
$cnt = 0;
880880
foreach ($tables as $table) {
881-
$primary = Index::getPrimary($table, $db);
881+
$primary = Index::getPrimary($this->dbi, $table, $db);
882882
$primarycols = $primary === false ? [] : $primary->getColumns();
883883
$selectTdForm = '';
884884
$pk = [];
@@ -960,7 +960,7 @@ public function findPartialDependencies($table, $db)
960960
. Util::backquote($table) . ' LIMIT 500) as dt;'
961961
);
962962
$totalRows = $totalRowsRes[0];
963-
$primary = Index::getPrimary($table, $db);
963+
$primary = Index::getPrimary($this->dbi, $table, $db);
964964
$primarycols = $primary === false ? [] : $primary->getColumns();
965965
$pk = [];
966966
foreach ($primarycols as $col) {

libraries/classes/Plugins/Schema/TableStats.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ protected function validateTableAndLoadFields(): void
133133
}
134134

135135
if ($this->showKeys) {
136-
$indexes = Index::getFromTable($this->tableName, $this->db);
136+
$indexes = Index::getFromTable($GLOBALS['dbi'], $this->tableName, $this->db);
137137
$all_columns = [];
138138
foreach ($indexes as $index) {
139139
$all_columns = array_merge(

0 commit comments

Comments
 (0)