Skip to content

Commit

Permalink
Improving DboMysql::index()
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Oct 15, 2010
1 parent c54448d commit 5e80cf8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
5 changes: 4 additions & 1 deletion cake/libs/model/datasources/dbo/dbo_mysql.php
Expand Up @@ -249,10 +249,13 @@ function setEncoding($enc) {
function index($model) {
$index = array();
$table = $this->fullTableName($model);
$old = version_compare($this->getVersion(), '4.1', '<=');
if ($table) {
$indices = $this->_execute('SHOW INDEX FROM ' . $table);

while ($idx = $indices->fetch()) {
if ($old) {
$idx = (object) current((array)$idx);
}
if (!isset($index[$idx->Key_name]['column'])) {
$col = array();
$index[$idx->Key_name]['column'] = $idx->Column_name;
Expand Down
18 changes: 13 additions & 5 deletions cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php
Expand Up @@ -313,6 +313,7 @@ function testTinyintCasting() {
/**
* testIndexDetection method
*
* @group indices
* @return void
*/
public function testIndexDetection() {
Expand Down Expand Up @@ -376,7 +377,6 @@ public function testIndexDetection() {
/**
* testBuildColumn method
*
* @access public
* @return void
*/
function testBuildColumn() {
Expand Down Expand Up @@ -413,12 +413,13 @@ function testBuildColumn() {
* MySQL 4.x returns index data in a different format,
* Using a mock ensure that MySQL 4.x output is properly parsed.
*
* @group indices
* @return void
*/
function testIndexOnMySQL4Output() {
$name = $this->Dbo->fullTableName('simple');

$mockDbo = $this->getMock('DboMysql', array('query'));
$mockDbo = $this->getMock('DboMysql', array('connect', '_execute', 'getVersion'));
$columnData = array(
array('0' => array(
'Table' => 'with_compound_keys',
Expand Down Expand Up @@ -491,10 +492,17 @@ function testIndexOnMySQL4Output() {
'Comment' => ''
))
);

$mockDbo->expects($this->once())->method('getVersion')->will($this->returnValue('4.1'));
$resultMock = $this->getMock('PDOStatement', array('fetch'));
$mockDbo->expects($this->once())
->method('query')
->method('_execute')
->with('SHOW INDEX FROM ' . $name)
->will($this->returnValue($columnData));
->will($this->returnValue($resultMock));

foreach ($columnData as $i => $data) {
$resultMock->expects($this->at($i))->method('fetch')->will($this->returnValue((object) $data));
}

$result = $mockDbo->index($name, false);
$expected = array(
Expand Down Expand Up @@ -556,7 +564,7 @@ public function testColumn() {
/**
* testAlterSchemaIndexes method
*
* @access public
* @group indices
* @return void
*/
function testAlterSchemaIndexes() {
Expand Down

0 comments on commit 5e80cf8

Please sign in to comment.