Fixing DboMysql::listDetailedSources()

This commit is contained in:
José Lorenzo Rodríguez 2010-10-16 13:28:18 -04:30
parent e03cbcb167
commit 84283ed6f3
2 changed files with 24 additions and 19 deletions

View file

@ -707,6 +707,7 @@ class DboMysql extends DboSource {
$values = implode(', ', $values); $values = implode(', ', $values);
$this->query("INSERT INTO {$table} ({$fields}) VALUES {$values}"); $this->query("INSERT INTO {$table} ({$fields}) VALUES {$values}");
} }
/** /**
* Returns an detailed array of sources (tables) in the database. * Returns an detailed array of sources (tables) in the database.
* *
@ -715,20 +716,24 @@ class DboMysql extends DboSource {
*/ */
function listDetailedSources($name = null) { function listDetailedSources($name = null) {
$condition = ''; $condition = '';
$params = array();
if (is_string($name)) { if (is_string($name)) {
$condition = ' LIKE ' . $this->value($name); $condition = ' WHERE name = ?' ;
$params = array($name);
} }
$result = $this->query('SHOW TABLE STATUS FROM ' . $this->name($this->config['database']) . $condition . ';'); $result = $this->_execute('SHOW TABLE STATUS ' . $condition, $params);
if (!$result) { if (!$result) {
return array(); return array();
} else { } else {
$tables = array(); $tables = array();
foreach ($result as $row) { while ($row = $result->fetch()) {
$tables[$row['TABLES']['Name']] = $row['TABLES']; $tables[$row->Name] = (array) $row;
if (!empty($row['TABLES']['Collation'])) { unset($tables[$row->Name]['queryString']);
$charset = $this->getCharsetName($row['TABLES']['Collation']); if (!empty($row->Collation)) {
$charset = $this->getCharsetName($row->Collation);
if ($charset) { if ($charset) {
$tables[$row['TABLES']['Name']]['charset'] = $charset; $tables[$row->Name]['charset'] = $charset;
} }
} }
} }

View file

@ -705,7 +705,7 @@ class DboMysqlTest extends CakeTestCase {
) )
) )
)); ));
$this->Dbo->query($this->Dbo->createSchema($schema1)); $this->Dbo->rawQuery($this->Dbo->createSchema($schema1));
$schema2 = new CakeSchema(array( $schema2 = new CakeSchema(array(
'name' => 'AlterTest1', 'name' => 'AlterTest1',
'connection' => 'test', 'connection' => 'test',
@ -720,17 +720,17 @@ class DboMysqlTest extends CakeTestCase {
) )
)); ));
$result = $this->Dbo->alterSchema($schema2->compare($schema1)); $result = $this->Dbo->alterSchema($schema2->compare($schema1));
$this->assertPattern('/DEFAULT CHARSET=utf8/', $result); $this->assertContains('DEFAULT CHARSET=utf8', $result);
$this->assertPattern('/ENGINE=InnoDB/', $result); $this->assertContains('ENGINE=InnoDB', $result);
$this->assertPattern('/COLLATE=utf8_general_ci/', $result); $this->assertContains('COLLATE=utf8_general_ci', $result);
$this->Dbo->query($result); $this->Dbo->rawQuery($result);
$result = $this->Dbo->listDetailedSources('altertest'); $result = $this->Dbo->listDetailedSources('altertest');
$this->assertEqual($result['Collation'], 'utf8_general_ci'); $this->assertEqual($result['Collation'], 'utf8_general_ci');
$this->assertEqual($result['Engine'], 'InnoDB'); $this->assertEqual($result['Engine'], 'InnoDB');
$this->assertEqual($result['charset'], 'utf8'); $this->assertEqual($result['charset'], 'utf8');
$this->Dbo->query($this->Dbo->dropSchema($schema1)); $this->Dbo->rawQuery($this->Dbo->dropSchema($schema1));
} }
/** /**
@ -739,7 +739,7 @@ class DboMysqlTest extends CakeTestCase {
* @return void * @return void
*/ */
function testAlteringTwoTables() { function testAlteringTwoTables() {
$schema1 =& new CakeSchema(array( $schema1 = new CakeSchema(array(
'name' => 'AlterTest1', 'name' => 'AlterTest1',
'connection' => 'test', 'connection' => 'test',
'altertest' => array( 'altertest' => array(
@ -751,7 +751,7 @@ class DboMysqlTest extends CakeTestCase {
'name' => array('type' => 'string', 'null' => false, 'length' => 50), 'name' => array('type' => 'string', 'null' => false, 'length' => 50),
) )
)); ));
$schema2 =& new CakeSchema(array( $schema2 = new CakeSchema(array(
'name' => 'AlterTest1', 'name' => 'AlterTest1',
'connection' => 'test', 'connection' => 'test',
'altertest' => array( 'altertest' => array(
@ -776,23 +776,23 @@ class DboMysqlTest extends CakeTestCase {
function testReadTableParameters() { function testReadTableParameters() {
$this->Dbo->cacheSources = $this->Dbo->testing = false; $this->Dbo->cacheSources = $this->Dbo->testing = 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)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;'); $this->Dbo->rawQuery('CREATE TABLE ' . $this->Dbo->fullTableName($tableName) . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;');
$result = $this->Dbo->readTableParameters($tableName); $result = $this->Dbo->readTableParameters($tableName);
$this->Dbo->rawQuery('DROP TABLE ' . $this->Dbo->fullTableName($tableName));
$expected = array( $expected = array(
'charset' => 'utf8', 'charset' => 'utf8',
'collate' => 'utf8_unicode_ci', 'collate' => 'utf8_unicode_ci',
'engine' => 'InnoDB'); 'engine' => 'InnoDB');
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$this->Dbo->query('DROP TABLE ' . $this->Dbo->fullTableName($tableName)); $this->Dbo->rawQuery('CREATE TABLE ' . $this->Dbo->fullTableName($tableName) . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id)) ENGINE=MyISAM DEFAULT CHARSET=cp1250 COLLATE=cp1250_general_ci;');
$this->Dbo->query('CREATE TABLE ' . $this->Dbo->fullTableName($tableName) . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id)) ENGINE=MyISAM DEFAULT CHARSET=cp1250 COLLATE=cp1250_general_ci;');
$result = $this->Dbo->readTableParameters($tableName); $result = $this->Dbo->readTableParameters($tableName);
$this->Dbo->rawQuery('DROP TABLE ' . $this->Dbo->fullTableName($tableName));
$expected = array( $expected = array(
'charset' => 'cp1250', 'charset' => 'cp1250',
'collate' => 'cp1250_general_ci', 'collate' => 'cp1250_general_ci',
'engine' => 'MyISAM'); 'engine' => 'MyISAM');
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$this->Dbo->query('DROP TABLE ' . $this->Dbo->fullTableName($tableName));
} }
/** /**