Skip to content

Commit

Permalink
DBAL-88 - Change index retrieval of MySQL to use information_schema.s…
Browse files Browse the repository at this point in the history
…tatistics which avoids errors when using reserved words for table names (for example in database driver).
  • Loading branch information
beberlei committed Feb 26, 2011
1 parent 2775ab9 commit ff2a856
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 12 deletions.
15 changes: 14 additions & 1 deletion lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -1660,7 +1660,20 @@ public function getListViewsSQL($database)
throw DBALException::notSupported(__METHOD__);
}

public function getListTableIndexesSQL($table)
/**
* Get the list of indexes for the current database.
*
* The current database parameter is optional but will always be passed
* when using the SchemaManager API and is the database the given table is in.
*
* Attention: Some platforms only support currentDatabase when they
* are connected with that database. Cross-database information schema
* requests may be impossible.
*
* @param string $table
* @param string $currentDatabase
*/
public function getListTableIndexesSQL($table, $currentDatabase = null)
{
throw DBALException::notSupported(__METHOD__);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Platforms/DB2Platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public function getListViewsSQL($database)
return "SELECT NAME, TEXT FROM SYSIBM.SYSVIEWS";
}

public function getListTableIndexesSQL($table)
public function getListTableIndexesSQL($table, $currentDatabase = null)
{
return "SELECT NAME, COLNAMES, UNIQUERULE FROM SYSIBM.SYSINDEXES WHERE TBNAME = UPPER('" . $table . "')";
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Platforms/MsSqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public function getListTableForeignKeysSQL($table, $database = null)
/**
* @override
*/
public function getListTableIndexesSQL($table)
public function getListTableIndexesSQL($table, $currentDatabase = null)
{
return "exec sp_helpindex '" . $table . "'";
}
Expand Down
22 changes: 19 additions & 3 deletions lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,25 @@ public function getListTableConstraintsSQL($table)
return 'SHOW INDEX FROM ' . $table;
}

public function getListTableIndexesSQL($table)
{
return 'SHOW INDEX FROM ' . $table;
/**
* Two approaches to listing the table indexes. The information_schema is
* prefered, because it doesn't cause problems with SQL keywords such as "order" or "table".
*
* @param string $table
* @param string $currentDatabase
* @return string
*/
public function getListTableIndexesSQL($table, $currentDatabase = null)
{
if ($currentDatabase) {
return "SELECT TABLE_NAME AS `Table`, NON_UNIQUE AS Non_Unique, INDEX_NAME AS Key_name, ".
"SEQ_IN_INDEX AS Seq_in_index, COLUMN_NAME AS Column_Name, COLLATION AS Collation, ".
"CARDINALITY AS Cardinality, SUB_PART AS Sub_Part, PACKED AS Packed, " .
"NULLABLE AS `Null`, INDEX_TYPE AS Index_Type, COMMENT AS Comment " .
"FROM information_schema.STATISTICS WHERE TABLE_NAME = '" . $table . "' AND TABLE_SCHEMA = '" . $currentDatabase . "'";
} else {
return 'SHOW INDEX FROM ' . $table;
}
}

public function getListViewsSQL($database)
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Platforms/OraclePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ protected function _getCreateTableSQL($table, array $columns, array $options = a
* @param string $table
* @return string
*/
public function getListTableIndexesSQL($table)
public function getListTableIndexesSQL($table, $currentDatabase = null)
{
$table = strtoupper($table);

Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public function getListTableConstraintsSQL($table)
* @param string $table
* @return string
*/
public function getListTableIndexesSQL($table)
public function getListTableIndexesSQL($table, $currentDatabase = null)
{
return "SELECT relname, pg_index.indisunique, pg_index.indisprimary,
pg_index.indkey, pg_index.indrelid
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/DBAL/Platforms/SqlitePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,12 @@ public function getListTableConstraintsSQL($table)
return "SELECT sql FROM sqlite_master WHERE type='index' AND tbl_name = '$table' AND sql NOT NULL ORDER BY name";
}

public function getListTableColumnsSQL($table)
public function getListTableColumnsSQL($table, $currentDatabase = null)
{
return "PRAGMA table_info($table)";
}

public function getListTableIndexesSQL($table)
public function getListTableIndexesSQL($table, $currentDatabase = null)
{
return "PRAGMA index_list($table)";
}
Expand Down
3 changes: 1 addition & 2 deletions lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
* @author Roman Borschel <roman@code-factory.org>
* @author Jonathan H. Wage <jonwage@gmail.com>
* @author Benjamin Eberlei <kontakt@beberlei.de>
* @version $Revision$
* @since 2.0
*/
abstract class AbstractSchemaManager
Expand Down Expand Up @@ -162,7 +161,7 @@ public function listTableColumns($table)
*/
public function listTableIndexes($table)
{
$sql = $this->_platform->getListTableIndexesSQL($table);
$sql = $this->_platform->getListTableIndexesSQL($table, $this->_conn->getDatabase());

$tableIndexes = $this->_conn->fetchAll($sql);

Expand Down

0 comments on commit ff2a856

Please sign in to comment.