Fixing DboMysql::index() method

This commit is contained in:
José Lorenzo Rodríguez 2010-10-15 17:02:37 -04:30
parent 52ea8fb42e
commit 70ed9a7b12
3 changed files with 30 additions and 32 deletions

View file

@ -250,23 +250,19 @@ class DboMysqlBase extends DboSource {
$index = array(); $index = array();
$table = $this->fullTableName($model); $table = $this->fullTableName($model);
if ($table) { if ($table) {
$indexes = $this->query('SHOW INDEX FROM ' . $table); $indices = $this->_execute('SHOW INDEX FROM ' . $table);
if (isset($indexes[0]['STATISTICS'])) {
$keys = Set::extract($indexes, '{n}.STATISTICS'); while ($idx = $indices->fetch()) {
} else { if (!isset($index[$idx->Key_name]['column'])) {
$keys = Set::extract($indexes, '{n}.0');
}
foreach ($keys as $i => $key) {
if (!isset($index[$key['Key_name']])) {
$col = array(); $col = array();
$index[$key['Key_name']]['column'] = $key['Column_name']; $index[$idx->Key_name]['column'] = $idx->Column_name;
$index[$key['Key_name']]['unique'] = intval($key['Non_unique'] == 0); $index[$idx->Key_name]['unique'] = intval($idx->Non_unique == 0);
} else { } else {
if (!is_array($index[$key['Key_name']]['column'])) { if (!empty($index[$idx->Key_name]['column']) && !is_array($index[$idx->Key_name]['column'])) {
$col[] = $index[$key['Key_name']]['column']; $col[] = $index[$idx->Key_name]['column'];
} }
$col[] = $key['Column_name']; $col[] = $idx->Column_name;
$index[$key['Key_name']]['column'] = $col; $index[$idx->Key_name]['column'] = $col;
} }
} }
} }
@ -539,8 +535,6 @@ class DboMysql extends DboMysqlBase {
'port' => '3306' 'port' => '3306'
); );
protected $_errors = array();
/** /**
* Reference to the PDO object connection * Reference to the PDO object connection
* *

View file

@ -218,9 +218,9 @@ class DboSource extends DataSource {
* @param string $sql SQL statement * @param string $sql SQL statement
* @return boolean * @return boolean
*/ */
public function rawQuery($sql) { public function rawQuery($sql, $params = array()) {
$this->took = $this->error = $this->numRows = false; $this->took = $this->error = $this->numRows = false;
return $this->execute($sql); return $this->execute($sql, $params);
} }
/** /**

View file

@ -194,6 +194,7 @@ class DboMysqlTest extends CakeTestCase {
/** /**
* Test Dbo value method * Test Dbo value method
* *
* @group quoting
*/ */
public function testQuoting() { public function testQuoting() {
$result = $this->Dbo->fields($this->model); $result = $this->Dbo->fields($this->model);
@ -252,16 +253,17 @@ class DboMysqlTest extends CakeTestCase {
/** /**
* test that localized floats don't cause trouble. * test that localized floats don't cause trouble.
* *
* @group quoting
* @return void * @return void
*/ */
function testLocalizedFloats() { function testLocalizedFloats() {
$restore = setlocale(LC_ALL, null); $restore = setlocale(LC_ALL, null);
setlocale(LC_ALL, 'de_DE'); setlocale(LC_ALL, 'de_DE');
$result = $this->db->value(3.141593, 'float'); $result = $this->Dbo->value(3.141593, 'float');
$this->assertEqual((string)$result, '3.141593'); $this->assertEqual((string)$result, '3.141593');
$result = $this->db->value(3.141593); $result = $this->Dbo->value(3.141593);
$this->assertEqual((string)$result, '3.141593'); $this->assertEqual((string)$result, '3.141593');
setlocale(LC_ALL, $restore); setlocale(LC_ALL, $restore);
@ -270,13 +272,14 @@ class DboMysqlTest extends CakeTestCase {
/** /**
* testTinyintCasting method * testTinyintCasting method
* *
* @access public *
* @return void * @return void
*/ */
function testTinyintCasting() { function testTinyintCasting() {
$this->skipIf(true, 'Is this a test over the DBO?');
$this->Dbo->cacheSources = false; $this->Dbo->cacheSources = false;
$tableName = 'tinyint_' . uniqid(); $tableName = 'tinyint_' . uniqid();
$this->Dbo->query('CREATE TABLE ' . $this->Dbo->fullTableName($tableName) . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id));'); $this->Dbo->execute('CREATE TABLE ' . $this->Dbo->fullTableName($tableName) . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id));');
$this->model = new CakeTestModel(array( $this->model = new CakeTestModel(array(
'name' => 'Tinyint', 'table' => $tableName, 'ds' => 'test' 'name' => 'Tinyint', 'table' => $tableName, 'ds' => 'test'
@ -316,35 +319,36 @@ class DboMysqlTest extends CakeTestCase {
$this->Dbo->cacheSources = false; $this->Dbo->cacheSources = false;
$name = $this->Dbo->fullTableName('simple'); $name = $this->Dbo->fullTableName('simple');
$this->Dbo->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id));'); $this->Dbo->rawQuery('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id));');
$expected = array('PRIMARY' => array('column' => 'id', 'unique' => 1)); $expected = array('PRIMARY' => array('column' => 'id', 'unique' => 1));
$result = $this->Dbo->index('simple', false); $result = $this->Dbo->index('simple', false);
$this->Dbo->rawQuery('DROP TABLE ' . $name);
$this->assertEqual($expected, $result); $this->assertEqual($expected, $result);
$this->Dbo->query('DROP TABLE ' . $name);
$name = $this->Dbo->fullTableName('with_a_key'); $name = $this->Dbo->fullTableName('with_a_key');
$this->Dbo->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ));'); $this->Dbo->rawQuery('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ));');
$expected = array( $expected = array(
'PRIMARY' => array('column' => 'id', 'unique' => 1), 'PRIMARY' => array('column' => 'id', 'unique' => 1),
'pointless_bool' => array('column' => 'bool', 'unique' => 0), 'pointless_bool' => array('column' => 'bool', 'unique' => 0),
); );
$result = $this->Dbo->index('with_a_key', false); $result = $this->Dbo->index('with_a_key', false);
$this->Dbo->rawQuery('DROP TABLE ' . $name);
$this->assertEqual($expected, $result); $this->assertEqual($expected, $result);
$this->Dbo->query('DROP TABLE ' . $name);
$name = $this->Dbo->fullTableName('with_two_keys'); $name = $this->Dbo->fullTableName('with_two_keys');
$this->Dbo->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ));'); $this->Dbo->rawQuery('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ));');
$expected = array( $expected = array(
'PRIMARY' => array('column' => 'id', 'unique' => 1), 'PRIMARY' => array('column' => 'id', 'unique' => 1),
'pointless_bool' => array('column' => 'bool', 'unique' => 0), 'pointless_bool' => array('column' => 'bool', 'unique' => 0),
'pointless_small_int' => array('column' => 'small_int', 'unique' => 0), 'pointless_small_int' => array('column' => 'small_int', 'unique' => 0),
); );
$result = $this->Dbo->index('with_two_keys', false); $result = $this->Dbo->index('with_two_keys', false);
$this->Dbo->rawQuery('DROP TABLE ' . $name);
$this->assertEqual($expected, $result); $this->assertEqual($expected, $result);
$this->Dbo->query('DROP TABLE ' . $name);
$name = $this->Dbo->fullTableName('with_compound_keys'); $name = $this->Dbo->fullTableName('with_compound_keys');
$this->Dbo->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ), KEY `one_way` ( `bool`, `small_int` ));'); $this->Dbo->rawQuery('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ), KEY `one_way` ( `bool`, `small_int` ));');
$expected = array( $expected = array(
'PRIMARY' => array('column' => 'id', 'unique' => 1), 'PRIMARY' => array('column' => 'id', 'unique' => 1),
'pointless_bool' => array('column' => 'bool', 'unique' => 0), 'pointless_bool' => array('column' => 'bool', 'unique' => 0),
@ -352,11 +356,11 @@ class DboMysqlTest extends CakeTestCase {
'one_way' => array('column' => array('bool', 'small_int'), 'unique' => 0), 'one_way' => array('column' => array('bool', 'small_int'), 'unique' => 0),
); );
$result = $this->Dbo->index('with_compound_keys', false); $result = $this->Dbo->index('with_compound_keys', false);
$this->Dbo->rawQuery('DROP TABLE ' . $name);
$this->assertEqual($expected, $result); $this->assertEqual($expected, $result);
$this->Dbo->query('DROP TABLE ' . $name);
$name = $this->Dbo->fullTableName('with_multiple_compound_keys'); $name = $this->Dbo->fullTableName('with_multiple_compound_keys');
$this->Dbo->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ), KEY `one_way` ( `bool`, `small_int` ), KEY `other_way` ( `small_int`, `bool` ));'); $this->Dbo->rawQuery('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ), KEY `one_way` ( `bool`, `small_int` ), KEY `other_way` ( `small_int`, `bool` ));');
$expected = array( $expected = array(
'PRIMARY' => array('column' => 'id', 'unique' => 1), 'PRIMARY' => array('column' => 'id', 'unique' => 1),
'pointless_bool' => array('column' => 'bool', 'unique' => 0), 'pointless_bool' => array('column' => 'bool', 'unique' => 0),
@ -365,8 +369,8 @@ class DboMysqlTest extends CakeTestCase {
'other_way' => array('column' => array('small_int', 'bool'), 'unique' => 0), 'other_way' => array('column' => array('small_int', 'bool'), 'unique' => 0),
); );
$result = $this->Dbo->index('with_multiple_compound_keys', false); $result = $this->Dbo->index('with_multiple_compound_keys', false);
$this->Dbo->rawQuery('DROP TABLE ' . $name);
$this->assertEqual($expected, $result); $this->assertEqual($expected, $result);
$this->Dbo->query('DROP TABLE ' . $name);
} }
/** /**