Add DB::getIndexInfo()

This commit is contained in:
Kijin Sung 2023-12-31 00:16:43 +09:00
parent a102f20766
commit f16da70c64
2 changed files with 67 additions and 1 deletions

View file

@ -951,7 +951,7 @@ class DB
*/
public function getColumnInfo(string $table_name, string $column_name): ?object
{
// If column information is not found, return false.
// If column information is not found, return null.
$stmt = $this->_handle->query(sprintf("SHOW FIELDS FROM `%s` WHERE Field = '%s'", $this->addQuotes($this->_prefix . $table_name), $this->addQuotes($column_name)));
$column_info = $this->fetch($stmt);
if (!$column_info)
@ -1053,6 +1053,48 @@ class DB
return $result ? new Helpers\DBResultHelper : $this->getError();
}
/**
* Get index information.
*
* @param string $table_name
* @param string $index_name
* @return ?object
*/
public function getIndexInfo(string $table_name, string $index_name): ?object
{
// If the index is not found, return null.
$stmt = $this->_handle->query(sprintf("SHOW INDEX FROM `%s` WHERE Key_name = '%s'", $this->addQuotes($this->_prefix . $table_name), $this->addQuotes($index_name)));
$index_info = $this->fetch($stmt, 0, 'array');
if (!$index_info)
{
return null;
}
// Get the list of columns included in the index.
$is_unique = false;
$columns = [];
foreach ($index_info as $column)
{
if (!$column->Non_unique)
{
$is_unique = true;
}
$columns[] = (object)[
'name' => $column->Column_name,
'size' => $column->Sub_part ? intval($column->Sub_part) : null,
'cardinality' => $column->Cardinality ? intval($column->Cardinality) : null,
];
}
// Return the result as an object.
return (object)array(
'name' => $column->Key_name,
'table' => $column->Table,
'is_unique' => $is_unique,
'columns' => $columns,
);
}
/**
* Add table prefixes to a query string.
*