Commit 27eb9416 authored by Benjamin Eberlei's avatar Benjamin Eberlei

DBAL-88 - Change index retrieval of MySQL to use information_schema.statistics...

DBAL-88 - Change index retrieval of MySQL to use information_schema.statistics which avoids errors when using reserved words for table names (for example in database driver).
parent 76fa4a80
......@@ -1753,7 +1753,20 @@ abstract class AbstractPlatform
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__);
}
......
......@@ -234,7 +234,7 @@ class DB2Platform extends AbstractPlatform
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 . "')";
}
......
......@@ -340,7 +340,7 @@ class MsSqlPlatform extends AbstractPlatform
/**
* @override
*/
public function getListTableIndexesSQL($table)
public function getListTableIndexesSQL($table, $currentDatabase = null)
{
return "exec sp_helpindex '" . $table . "'";
}
......
......@@ -109,10 +109,26 @@ class MySqlPlatform extends AbstractPlatform
return 'SHOW INDEX FROM ' . $table;
}
public function getListTableIndexesSQL($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)
{
......
......@@ -296,7 +296,7 @@ class OraclePlatform extends AbstractPlatform
* @param string $table
* @return string
*/
public function getListTableIndexesSQL($table)
public function getListTableIndexesSQL($table, $currentDatabase = null)
{
$table = strtoupper($table);
......
......@@ -222,7 +222,7 @@ class PostgreSqlPlatform extends AbstractPlatform
* @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
......
......@@ -316,12 +316,12 @@ class SqlitePlatform extends AbstractPlatform
return "SELECT sql FROM sqlite_master WHERE type='index' AND tbl_name = '$table' AND sql NOT NULL ORDER BY name";
}
public function getListTableColumnsSQL($table, $database = null)
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)";
}
......
......@@ -33,7 +33,6 @@ use Doctrine\DBAL\Platforms\AbstractPlatform;
* @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
......@@ -167,7 +166,7 @@ abstract class AbstractSchemaManager
*/
public function listTableIndexes($table)
{
$sql = $this->_platform->getListTableIndexesSQL($table);
$sql = $this->_platform->getListTableIndexesSQL($table, $this->_conn->getDatabase());
$tableIndexes = $this->_conn->fetchAll($sql);
......
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