Skip to content

Commit

Permalink
Fixing DboMysql::index() method
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Oct 15, 2010
1 parent 52ea8fb commit 70ed9a7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 32 deletions.
26 changes: 10 additions & 16 deletions cake/libs/model/datasources/dbo/dbo_mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,23 +250,19 @@ function index($model) {
$index = array();
$table = $this->fullTableName($model);
if ($table) {
$indexes = $this->query('SHOW INDEX FROM ' . $table);
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']])) {
$indices = $this->_execute('SHOW INDEX FROM ' . $table);

while ($idx = $indices->fetch()) {
if (!isset($index[$idx->Key_name]['column'])) {
$col = array();
$index[$key['Key_name']]['column'] = $key['Column_name'];
$index[$key['Key_name']]['unique'] = intval($key['Non_unique'] == 0);
$index[$idx->Key_name]['column'] = $idx->Column_name;
$index[$idx->Key_name]['unique'] = intval($idx->Non_unique == 0);
} else {
if (!is_array($index[$key['Key_name']]['column'])) {
$col[] = $index[$key['Key_name']]['column'];
if (!empty($index[$idx->Key_name]['column']) && !is_array($index[$idx->Key_name]['column'])) {
$col[] = $index[$idx->Key_name]['column'];
}
$col[] = $key['Column_name'];
$index[$key['Key_name']]['column'] = $col;
$col[] = $idx->Column_name;
$index[$idx->Key_name]['column'] = $col;
}
}
}
Expand Down Expand Up @@ -539,8 +535,6 @@ class DboMysql extends DboMysqlBase {
'port' => '3306'
);

protected $_errors = array();

/**
* Reference to the PDO object connection
*
Expand Down
4 changes: 2 additions & 2 deletions cake/libs/model/datasources/dbo_source.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,9 @@ public function expression($expression) {
* @param string $sql SQL statement
* @return boolean
*/
public function rawQuery($sql) {
public function rawQuery($sql, $params = array()) {
$this->took = $this->error = $this->numRows = false;
return $this->execute($sql);
return $this->execute($sql, $params);
}

/**
Expand Down
32 changes: 18 additions & 14 deletions cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public function tearDown() {
/**
* Test Dbo value method
*
* @group quoting
*/
public function testQuoting() {
$result = $this->Dbo->fields($this->model);
Expand Down Expand Up @@ -252,16 +253,17 @@ public function testQuoting() {
/**
* test that localized floats don't cause trouble.
*
* @group quoting
* @return void
*/
function testLocalizedFloats() {
$restore = setlocale(LC_ALL, null);
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');

$result = $this->db->value(3.141593);
$result = $this->Dbo->value(3.141593);
$this->assertEqual((string)$result, '3.141593');

setlocale(LC_ALL, $restore);
Expand All @@ -270,13 +272,14 @@ function testLocalizedFloats() {
/**
* testTinyintCasting method
*
* @access public
*
* @return void
*/
function testTinyintCasting() {
$this->skipIf(true, 'Is this a test over the DBO?');
$this->Dbo->cacheSources = false;
$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(
'name' => 'Tinyint', 'table' => $tableName, 'ds' => 'test'
Expand Down Expand Up @@ -316,47 +319,48 @@ public function testIndexDetection() {
$this->Dbo->cacheSources = false;

$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));
$result = $this->Dbo->index('simple', false);
$this->Dbo->rawQuery('DROP TABLE ' . $name);
$this->assertEqual($expected, $result);
$this->Dbo->query('DROP TABLE ' . $name);


$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(
'PRIMARY' => array('column' => 'id', 'unique' => 1),
'pointless_bool' => array('column' => 'bool', 'unique' => 0),
);
$result = $this->Dbo->index('with_a_key', false);
$this->Dbo->rawQuery('DROP TABLE ' . $name);
$this->assertEqual($expected, $result);
$this->Dbo->query('DROP TABLE ' . $name);

$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(
'PRIMARY' => array('column' => 'id', 'unique' => 1),
'pointless_bool' => array('column' => 'bool', 'unique' => 0),
'pointless_small_int' => array('column' => 'small_int', 'unique' => 0),
);
$result = $this->Dbo->index('with_two_keys', false);
$this->Dbo->rawQuery('DROP TABLE ' . $name);
$this->assertEqual($expected, $result);
$this->Dbo->query('DROP TABLE ' . $name);

$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(
'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),
);
$result = $this->Dbo->index('with_compound_keys', false);
$this->Dbo->rawQuery('DROP TABLE ' . $name);
$this->assertEqual($expected, $result);
$this->Dbo->query('DROP TABLE ' . $name);

$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(
'PRIMARY' => array('column' => 'id', 'unique' => 1),
'pointless_bool' => array('column' => 'bool', 'unique' => 0),
Expand All @@ -365,8 +369,8 @@ public function testIndexDetection() {
'other_way' => array('column' => array('small_int', 'bool'), 'unique' => 0),
);
$result = $this->Dbo->index('with_multiple_compound_keys', false);
$this->Dbo->rawQuery('DROP TABLE ' . $name);
$this->assertEqual($expected, $result);
$this->Dbo->query('DROP TABLE ' . $name);
}

/**
Expand Down

0 comments on commit 70ed9a7

Please sign in to comment.