Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Improve list SQL Server table indexes #300

Merged
merged 1 commit into from

3 participants

@deeky666
Collaborator

This PR improves listing table indexes in SQL Server. Currently table indexes are retrieved in the platform via a stored procedure sp_helpindex which causes problems with some SQL Server drivers like ODBC that cannot retrieve information from stored procedure calls. Table indexes are retrieved via native system tables now, which seems cleaner and is more flexible concerning which information to retrieve.
Additionally code duplication in SQLServerSchemaManager::_getPortableTableIndexesList is removed and simplified by this PR.

@doctrinebot
Collaborator

Hello,

thank you for positing this Pull Request. I have automatically opened an issue on our Jira Bug Tracker for you with the details of this Pull-Request. See the Link:

http://doctrine-project.org/jira/browse/DBAL-486

@beberlei
Owner

This works for every SQL Server version?

@beberlei beberlei merged commit 5a8297a into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 6, 2013
  1. @deeky666
This page is out of date. Refresh to see the latest.
View
16 lib/Doctrine/DBAL/Platforms/SQLServerPlatform.php
@@ -553,7 +553,21 @@ public function getListTableForeignKeysSQL($table, $database = null)
*/
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";
}
/**
View
57 lib/Doctrine/DBAL/Schema/SQLServerSchemaManager.php
@@ -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 @@ protected function _getPortableTableForeignKeysList($tableForeignKeys)
}
/**
- * @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);
}
/**
Something went wrong with that request. Please try again.