From d3ee6b31f149acd5366a46402c8a31303fd0219f Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 8 Feb 2009 23:09:08 +0000 Subject: [PATCH] Added conditional check for compatibility with MySQL 4.x result sets to DboMySqlBase::index(). Tests added. Thanks to 'teknoid' for the help. Closes #4503 git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8020 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/model/datasources/dbo/dbo_mysql.php | 6 +- .../model/datasources/dbo/dbo_mysql.test.php | 94 +++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) diff --git a/cake/libs/model/datasources/dbo/dbo_mysql.php b/cake/libs/model/datasources/dbo/dbo_mysql.php index 16c442bec..2dee3f504 100644 --- a/cake/libs/model/datasources/dbo/dbo_mysql.php +++ b/cake/libs/model/datasources/dbo/dbo_mysql.php @@ -177,7 +177,11 @@ class DboMysqlBase extends DboSource { $table = $this->fullTableName($model); if ($table) { $indexes = $this->query('SHOW INDEX FROM ' . $table); - $keys = Set::extract($indexes, '{n}.STATISTICS'); + if (isset($indexes[0]['STATISTICS'])) { + $keys = Set::extract($indexes, '{n}.STATISTICS'); + } else { + $keys = Set::extract($indexes, '{n}.0'); + } foreach ($keys as $i => $key) { if (!isset($index[$key['Key_name']])) { $col = array(); diff --git a/cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php b/cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php index d328aaa52..54e029077 100644 --- a/cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php +++ b/cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php @@ -24,6 +24,7 @@ */ App::import('Core', array('Model', 'DataSource', 'DboSource', 'DboMysql')); +Mock::generatePartial('DboMysql', 'QueryMockDboMysql', array('query')); /** * Short description for class. * @@ -361,6 +362,99 @@ class DboMysqlTest extends CakeTestCase { $this->assertEqual($expected, $result); $this->db->query('DROP TABLE ' . $name); } +/** + * MySQL 4.x returns index data in a different format, + * Using a mock ensure that MySQL 4.x output is properly parsed. + * + * @return void + **/ + function testIndexOnMySQL4Output() { + $name = $this->db->fullTableName('simple'); + + $mockDbo =& new QueryMockDboMysql($this); + $columnData = array( + array('0' => array( + 'Table' => 'with_compound_keys', + 'Non_unique' => '0', + 'Key_name' => 'PRIMARY', + 'Seq_in_index' => '1', + 'Column_name' => 'id', + 'Collation' => 'A', + 'Cardinality' => '0', + 'Sub_part' => NULL, + 'Packed' => NULL, + 'Null' => '', + 'Index_type' => 'BTREE', + 'Comment' => '' + )), + array('0' => array( + 'Table' => 'with_compound_keys', + 'Non_unique' => '1', + 'Key_name' => 'pointless_bool', + 'Seq_in_index' => '1', + 'Column_name' => 'bool', + 'Collation' => 'A', + 'Cardinality' => NULL, + 'Sub_part' => NULL, + 'Packed' => NULL, + 'Null' => 'YES', + 'Index_type' => 'BTREE', + 'Comment' => '' + )), + array('0' => array( + 'Table' => 'with_compound_keys', + 'Non_unique' => '1', + 'Key_name' => 'pointless_small_int', + 'Seq_in_index' => '1', + 'Column_name' => 'small_int', + 'Collation' => 'A', + 'Cardinality' => NULL, + 'Sub_part' => NULL, + 'Packed' => NULL, + 'Null' => 'YES', + 'Index_type' => 'BTREE', + 'Comment' => '' + )), + array('0' => array( + 'Table' => 'with_compound_keys', + 'Non_unique' => '1', + 'Key_name' => 'one_way', + 'Seq_in_index' => '1', + 'Column_name' => 'bool', + 'Collation' => 'A', + 'Cardinality' => NULL, + 'Sub_part' => NULL, + 'Packed' => NULL, + 'Null' => 'YES', + 'Index_type' => 'BTREE', + 'Comment' => '' + )), + array('0' => array( + 'Table' => 'with_compound_keys', + 'Non_unique' => '1', + 'Key_name' => 'one_way', + 'Seq_in_index' => '2', + 'Column_name' => 'small_int', + 'Collation' => 'A', + 'Cardinality' => NULL, + 'Sub_part' => NULL, + 'Packed' => NULL, + 'Null' => 'YES', + 'Index_type' => 'BTREE', + 'Comment' => '' + )) + ); + $mockDbo->setReturnValue('query', $columnData, array('SHOW INDEX FROM ' . $name)); + + $result = $mockDbo->index($name, false); + $expected = array( + 'PRIMARY' => array('column' => 'id', 'unique' => 1), + 'pointless_bool' => array('column' => 'bool', 'unique' => 0), + 'pointless_small_int' => array('column' => 'small_int', 'unique' => 0), + 'one_way' => array('column' => array('bool', 'small_int'), 'unique' => 0), + ); + $this->assertEqual($result, $expected); + } /** * testColumn method *