Skip to content

Commit

Permalink
Removed unused parameters and methods
Browse files Browse the repository at this point in the history
Removed some unnecessary docbloc comments
Stricter type checking
Signed-off-by: Wiktor Golicz <wiktorgolicz1@gmail.com>
  • Loading branch information
Wiktor102 committed May 26, 2024
1 parent 33fa32d commit 5eebd4e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 159 deletions.
118 changes: 8 additions & 110 deletions src/CheckConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
*/
class CheckConstraint
{
public const COLUMN = 1;
public const TABLE = 2;
public const COLUMN = "Column";
public const TABLE = "Table";

/**
* Class-wide storage container for check constraints (caching, singleton)
Expand All @@ -25,11 +25,12 @@ class CheckConstraint
/** @var string The name of the table */
private string $table = '';

/** @var string The name of the index */
/** @var string The name of the check constraint */
private string $name = '';

/**
* The check constraint level (Column, Table).
* This isn't supported by MySQL and MariaDB <= 10.5.10
*/
private string $level = '';

Expand All @@ -38,12 +39,8 @@ class CheckConstraint
*/
private string $clause = '';

/**
* Parser option for the index
*/
private string $parser = '';

/** @param mixed[] $params parameters */
/** @param string[] $params parameters */
public function __construct(array $params = [])
{
$this->set($params);
Expand Down Expand Up @@ -75,9 +72,9 @@ public static function singleton(
}

/**
* returns an array with all indexes from the given table
* returns an array with all check constraints from the given table
*
* @return Index[]
* @return CheckConstraint[]
*/
public static function getFromTable(DatabaseInterface $dbi, string $table, string $schema): array
{
Expand All @@ -86,45 +83,6 @@ public static function getFromTable(DatabaseInterface $dbi, string $table, strin
return self::$registry[$schema][$table] ?? [];
}

/**
* Returns an array with all indexes from the given table of the requested types
*
* @param string $table table
* @param string $schema schema
* @param int $choices choices
*
* @return CheckConstraint[] array of indexes
*/
public static function getFromTableByChoice(string $table, string $schema, int $choices = 31): array
{
$indexes = [];
// foreach (self::getFromTable(DatabaseInterface::getInstance(), $table, $schema) as $index) {
// if (($choices & self::PRIMARY) && $index->getChoice() === 'PRIMARY') {
// $indexes[] = $index;
// }

// if (($choices & self::UNIQUE) && $index->getChoice() === 'UNIQUE') {
// $indexes[] = $index;
// }

// if (($choices & self::INDEX) && $index->getChoice() === 'INDEX') {
// $indexes[] = $index;
// }

// if (($choices & self::SPATIAL) && $index->getChoice() === 'SPATIAL') {
// $indexes[] = $index;
// }

// if ((($choices & self::FULLTEXT) === 0) || $index->getChoice() !== 'FULLTEXT') {
// continue;
// }

// $indexes[] = $index;
// }

return $indexes;
}

/**
* Load constraint data for table
*/
Expand All @@ -149,7 +107,7 @@ private static function loadCheckConstraints(DatabaseInterface $dbi, string $tab
/**
* Sets check constraint details
*
* @param mixed[] $params check constraint details
* @param string[] $params check constraint details
*/
public function set(array $params): void
{
Expand All @@ -172,86 +130,26 @@ public function set(array $params): void
if (isset($params['CHECK_CLAUSE'])) {
$this->clause = $params['CHECK_CLAUSE'];
}

// if (! isset($params['Parser'])) {
// return;
// }

// $this->parser = $params['Parser'];
}


/**
* Returns check constraint clause
*
* @return string check constraint clause
*/
public function getClause(): string
{
return $this->clause;
}

/**
* Return the parser
*/
public function getParser(): string
{
return $this->parser;
}

/**
* Returns check constraint level (Column, Table)
*
* @return string check constraint level
*/
public function getLevel(): string
{
return $this->level;
}


/**
* Returns the name of the check constraint
*
* @return string the name of the check constraint
*/
public function getName(): string
{
return $this->name;
}

/**
* Sets the name of the check constraint
*/
public function setName(string $name): void
{
$this->name = $name;
}

/**
* Gets the properties in an array for comparison purposes
*
* @return array<string, array<int, array<string, int|string|null>>|string|null>
* @psalm-return array{
* Packed: string|null,
* Index_choice: string,
* columns?: list<array{
* Column_name: string,
* Seq_in_index: int,
* Collation: string|null,
* Sub_part: int|null,
* Null: string
* }>
* }
*/
// public function getCompareData(): array
// {
// $data = ['Packed' => $this->packed, 'Index_choice' => $this->choice];

// foreach ($this->columns as $column) {
// $data['columns'][] = $column->getCompareData();
// }

// return $data;
// }
}
68 changes: 19 additions & 49 deletions src/Table/CheckConstraints.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Identifiers\DatabaseName;
use PhpMyAdmin\Identifiers\TableName;
use PhpMyAdmin\Index;
use PhpMyAdmin\Message;
use PhpMyAdmin\Query\Compatibility;
use PhpMyAdmin\Query\Generator as QueryGenerator;
use PhpMyAdmin\Util;

use function __;
use function implode;
use function in_array;
use function sprintf;

final class CheckConstraints
Expand Down Expand Up @@ -54,73 +51,46 @@ public function getSqlQueryForCreateOrEdit(
if ($oldCheckConstraintName !== null) {
$oldCheckConstraint = CheckConstraint::singleton($this->dbi, $dbName, $tableName, $oldCheckConstraintName);

if ($oldCheckConstraint->getLevel() == "Table") {
if ($oldCheckConstraint->getLevel() === CheckConstraint::TABLE) {
$sqlQuery .= sprintf(' DROP CONSTRAINT %s;', $oldCheckConstraintName);
} else if ($checkConstraint->getLevel() === CheckConstraint::COLUMN) {
// TODO: implement sql for dropping column check constraints
} else {

// This means that we don't know the level of the check constraint.
// This is because that the LEVEL column in information_schema.CHECK_CONSTRAINTS is not available.
// It was introduced around MariaDB version 10.5.10 (check constraints par se are available since 10.2.1) and is still NOT present AT ALL in MySQL
// In both systems information_schema.CHECK_CONSTRAINTS seems to store both table and column check constraints
// Not sure what to do in this scenario
}
}

if ($checkConstraint->getLevel() == "Table") {
if ($checkConstraint->getLevel() === CheckConstraint::TABLE) {
$sqlQuery .= sprintf(' ADD CONSTRAINT %s CHECK(%s);', $oldCheckConstraintName, $checkConstraint->getClause());
} else if ($checkConstraint->getLevel() === CheckConstraint::COLUMN) {
// TODO: implement sql for adding column check constraints
} else {

// This means that we don't know the level of the check constraint.
// This is because that the LEVEL column in information_schema.CHECK_CONSTRAINTS is not available.
// It was introduced around MariaDB version 10.5.10 (check constraints par se are available since 10.2.1) and is still NOT present AT ALL in MySQL
// In both systems information_schema.CHECK_CONSTRAINTS seems to store both table and column check constraints
// Not sure what to do in this scenario
}

return $sqlQuery;
}

public function getSqlQueryForRename(string $oldIndexName, CheckConstraint $index, string $db, string $table): string
public function getSqlQueryForRename(string $oldIndexName, CheckConstraint $checkConstraint, string $db, string $table): string
{
return $this->getSqlQueryForCreateOrEdit($oldIndexName, $checkConstraint, $db, $table);
if (! Compatibility::isCompatibleRenameIndex($this->dbi->getVersion())) {
return $this->getSqlQueryForCreateOrEdit($oldIndexName, $index, $db, $table);
}

if ($oldIndexName === 'PRIMARY') {
if ($index->getName() === '') {
$index->setName('PRIMARY');
} elseif ($index->getName() !== 'PRIMARY') {
$this->error = Message::error(
__('The name of the primary key must be "PRIMARY"!'),
);
}
}

if ($index->getName() === 'PRIMARY') {
$this->error = Message::error(
__('Can\'t rename index to PRIMARY!'),
);
}

return QueryGenerator::getSqlQueryForIndexRename(
$db,
$table,
$oldIndexName,
$index->getName(),
$checkConstraint->getName(),
);
}

public function executeAddIndexSql(string|DatabaseName $db, string $sql): Message
{
$this->dbi->selectDb($db);
$result = $this->dbi->tryQuery($sql);

if (! $result) {
return Message::error($this->dbi->getError());
}

return Message::success();
}

public function hasPrimaryKey(string|TableName $table): bool
{
$result = $this->dbi->query('SHOW KEYS FROM ' . Util::backquote($table));
foreach ($result as $row) {
if ($row['Key_name'] === 'PRIMARY') {
return true;
}
}

return false;
}
}

0 comments on commit 5eebd4e

Please sign in to comment.