Commit 05c1d190 authored by Steve Müller's avatar Steve Müller

improve list SQL Server table indexes

parent fe04d1f1
......@@ -553,7 +553,21 @@ class SQLServerPlatform extends AbstractPlatform
*/
public function getListTableIndexesSQL($table, $currentDatabase = null)
{
return "exec sp_helpindex '" . $table . "'";
return "SELECT idx.name AS key_name,
col.name AS column_name,
~idx.is_unique AS non_unique,
idx.is_primary_key AS [primary],
CASE idx.type
WHEN '1' THEN 'clustered'
WHEN '2' THEN 'nonclustered'
ELSE NULL
END AS flags
FROM sys.tables AS tbl
JOIN sys.indexes AS idx ON tbl.object_id = idx.object_id
JOIN sys.index_columns AS idxcol ON idx.object_id = idxcol.object_id AND idx.index_id = idxcol.index_id
JOIN sys.columns AS col ON idxcol.object_id = col.object_id AND idxcol.column_id = col.column_id
WHERE tbl.name = '$table'
ORDER BY idx.index_id ASC, idxcol.index_column_id ASC";
}
/**
......
......@@ -19,8 +19,6 @@
namespace Doctrine\DBAL\Schema;
use Doctrine\DBAL\Events;
use Doctrine\DBAL\Event\SchemaIndexDefinitionEventArgs;
use Doctrine\DBAL\Driver\SQLSrv\SQLSrvException;
use Doctrine\DBAL\Types\Type;
......@@ -142,60 +140,17 @@ class SQLServerSchemaManager extends AbstractSchemaManager
}
/**
* @override
* {@inheritdoc}
*/
protected function _getPortableTableIndexesList($tableIndexRows, $tableName=null)
{
// TODO: Remove code duplication with AbstractSchemaManager;
$result = array();
foreach ($tableIndexRows as $tableIndex) {
$indexName = $keyName = $tableIndex['index_name'];
if (strpos($tableIndex['index_description'], 'primary key') !== false) {
$keyName = 'primary';
}
$keyName = strtolower($keyName);
$flags = array();
if (strpos($tableIndex['index_description'], 'clustered') !== false) {
$flags[] = 'clustered';
} else if (strpos($tableIndex['index_description'], 'nonclustered') !== false) {
$flags[] = 'nonclustered';
}
$result[$keyName] = array(
'name' => $indexName,
'columns' => explode(', ', $tableIndex['index_keys']),
'unique' => strpos($tableIndex['index_description'], 'unique') !== false,
'primary' => strpos($tableIndex['index_description'], 'primary key') !== false,
'flags' => $flags,
);
}
$eventManager = $this->_platform->getEventManager();
$indexes = array();
foreach ($result as $indexKey => $data) {
$index = null;
$defaultPrevented = false;
if (null !== $eventManager && $eventManager->hasListeners(Events::onSchemaIndexDefinition)) {
$eventArgs = new SchemaIndexDefinitionEventArgs($data, $tableName, $this->_conn);
$eventManager->dispatchEvent(Events::onSchemaIndexDefinition, $eventArgs);
$defaultPrevented = $eventArgs->isDefaultPrevented();
$index = $eventArgs->getIndex();
}
if ( ! $defaultPrevented) {
$index = new Index($data['name'], $data['columns'], $data['unique'], $data['primary']);
}
if ($index) {
$indexes[$indexKey] = $index;
}
foreach ($tableIndexRows as &$tableIndex) {
$tableIndex['non_unique'] = (boolean) $tableIndex['non_unique'];
$tableIndex['primary'] = (boolean) $tableIndex['primary'];
$tableIndex['flags'] = $tableIndex['flags'] ? array($tableIndex['flags']) : null;
}
return $indexes;
return parent::_getPortableTableIndexesList($tableIndexRows, $tableName);
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment