mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-05-11 04:52:14 +09:00
Add DB::getIndexInfo()
This commit is contained in:
parent
a102f20766
commit
f16da70c64
2 changed files with 67 additions and 1 deletions
|
|
@ -951,7 +951,7 @@ class DB
|
||||||
*/
|
*/
|
||||||
public function getColumnInfo(string $table_name, string $column_name): ?object
|
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)));
|
$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);
|
$column_info = $this->fetch($stmt);
|
||||||
if (!$column_info)
|
if (!$column_info)
|
||||||
|
|
@ -1053,6 +1053,48 @@ class DB
|
||||||
return $result ? new Helpers\DBResultHelper : $this->getError();
|
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.
|
* Add table prefixes to a query string.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -189,6 +189,30 @@ class DBTest extends \Codeception\Test\Unit
|
||||||
$this->assertTrue($info->notnull);
|
$this->assertTrue($info->notnull);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testGetIndexInfo()
|
||||||
|
{
|
||||||
|
$oDB = Rhymix\Framework\DB::getInstance();
|
||||||
|
|
||||||
|
$info = $oDB->getIndexInfo('member', 'idx_nick_name');
|
||||||
|
$this->assertTrue(is_object($info));
|
||||||
|
$this->assertFalse($info->is_unique);
|
||||||
|
$this->assertEquals(1, count($info->columns));
|
||||||
|
$this->assertEquals('nick_name', $info->columns[0]->name);
|
||||||
|
$this->assertNull($info->columns[0]->size);
|
||||||
|
$this->assertNotNull($info->columns[0]->cardinality);
|
||||||
|
|
||||||
|
$info = $oDB->getIndexInfo('module_extra_vars', 'unique_module_vars');
|
||||||
|
$this->assertTrue(is_object($info));
|
||||||
|
$this->assertTrue($info->is_unique);
|
||||||
|
$this->assertEquals(2, count($info->columns));
|
||||||
|
$this->assertEquals('module_srl', $info->columns[0]->name);
|
||||||
|
$this->assertEquals('name', $info->columns[1]->name);
|
||||||
|
$this->assertNull($info->columns[1]->size);
|
||||||
|
|
||||||
|
$info = $oDB->getIndexInfo('documents', 'idx_nonexistent');
|
||||||
|
$this->assertNull($info);
|
||||||
|
}
|
||||||
|
|
||||||
public function testIsValidOldPassword()
|
public function testIsValidOldPassword()
|
||||||
{
|
{
|
||||||
$oDB = Rhymix\Framework\DB::getInstance();
|
$oDB = Rhymix\Framework\DB::getInstance();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue