From 4094f0a6962ee3e819fa6d8dc9d323abd182a43c Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 30 Sep 2009 00:39:32 -0400 Subject: [PATCH] Moving DboMysql::listDetailedSources() and DboMysql::getCharsetName() into DboMysqlBase so it works with mysqli as well. Tests added for MySqli. --- cake/libs/model/datasources/dbo/dbo_mysql.php | 102 +++++++++--------- .../model/datasources/dbo/dbo_mysql.test.php | 1 + .../model/datasources/dbo/dbo_mysqli.test.php | 66 ++++++------ 3 files changed, 85 insertions(+), 84 deletions(-) diff --git a/cake/libs/model/datasources/dbo/dbo_mysql.php b/cake/libs/model/datasources/dbo/dbo_mysql.php index 83a7c952e..1e1ebd0ed 100644 --- a/cake/libs/model/datasources/dbo/dbo_mysql.php +++ b/cake/libs/model/datasources/dbo/dbo_mysql.php @@ -369,6 +369,52 @@ class DboMysqlBase extends DboSource { $values = implode(', ', $values); $this->query("INSERT INTO {$table} ({$fields}) VALUES {$values}"); } +/** + * Returns an detailed array of sources (tables) in the database. + * + * @param string $name Table name to get parameters + * @return array Array of tablenames in the database + */ + function listDetailedSources($name = null) { + $condition = ''; + if (is_string($name)) { + $condition = ' WHERE Name = ' . $this->value($name); + } + $result = $this->query('SHOW TABLE STATUS FROM ' . $this->name($this->config['database']) . $condition . ';'); + if (!$result) { + return array(); + } else { + $tables = array(); + foreach ($result as $row) { + $tables[$row['TABLES']['Name']] = $row['TABLES']; + if (!empty($row['TABLES']['Collation'])) { + $charset = $this->getCharsetName($row['TABLES']['Collation']); + if ($charset) { + $tables[$row['TABLES']['Name']]['charset'] = $charset; + } + } + } + if (is_string($name)) { + return $tables[$name]; + } + return $tables; + } + } + +/** + * Query charset by collation + * + * @param string $name Collation name + * @return string Character set name + */ + function getCharsetName($name) { + $cols = $this->query('SELECT CHARACTER_SET_NAME FROM INFORMATION_SCHEMA.COLLATIONS WHERE COLLATION_NAME= ' . $this->value($name) . ';'); + if (isset($cols[0]['COLLATIONS']['CHARACTER_SET_NAME'])) { + return $cols[0]['COLLATIONS']['CHARACTER_SET_NAME']; + } + return false; + } + } /** @@ -481,38 +527,6 @@ class DboMysql extends DboMysqlBase { } } -/** - * Returns an detailed array of sources (tables) in the database. - * - * @param string $name Table name to get parameters - * @return array Array of tablenames in the database - */ - function listDetailedSources($name = null) { - $condition = ''; - if (is_string($name)) { - $condition = ' WHERE Name=' . $this->value($name); - } - $result = $this->query('SHOW TABLE STATUS FROM ' . $this->name($this->config['database']) . $condition . ';'); - if (!$result) { - return array(); - } else { - $tables = array(); - foreach ($result as $row) { - $tables[$row['TABLES']['Name']] = $row['TABLES']; - if (!empty($row['TABLES']['Collation'])) { - $charset = $this->getCharsetName($row['TABLES']['Collation']); - if ($charset) { - $tables[$row['TABLES']['Name']]['charset'] = $charset; - } - } - } - if (is_string($name)) { - return $tables[$name]; - } - return $tables; - } - } - /** * Returns an array of the fields in given table name. * @@ -534,13 +548,13 @@ class DboMysql extends DboMysqlBase { } if (isset($column[0])) { $fields[$column[0]['Field']] = array( - 'type' => $this->column($column[0]['Type']), - 'null' => ($column[0]['Null'] == 'YES' ? true : false), - 'default' => $column[0]['Default'], - 'length' => $this->length($column[0]['Type']), + 'type' => $this->column($column[0]['Type']), + 'null' => ($column[0]['Null'] == 'YES' ? true : false), + 'default' => $column[0]['Default'], + 'length' => $this->length($column[0]['Type']), ); if (!empty($column[0]['Key']) && isset($this->index[$column[0]['Key']])) { - $fields[$column[0]['Field']]['key'] = $this->index[$column[0]['Key']]; + $fields[$column[0]['Field']]['key'] = $this->index[$column[0]['Key']]; } foreach ($this->fieldParameters as $name => $value) { if (!empty($column[0][$value['column']])) { @@ -559,20 +573,6 @@ class DboMysql extends DboMysqlBase { return $fields; } -/** - * Query charset by collation - * - * @param string $name Collation name - * @return string Character set name - */ - function getCharsetName($name) { - $cols = $this->query('SELECT CHARACTER_SET_NAME FROM INFORMATION_SCHEMA.COLLATIONS WHERE COLLATION_NAME= ' . $this->value($name) . ';'); - if (isset($cols[0]['COLLATIONS']['CHARACTER_SET_NAME'])) { - return $cols[0]['COLLATIONS']['CHARACTER_SET_NAME']; - } - return false; - } - /** * Returns a quoted and escaped string of $data for use in an SQL statement. * 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 d2ad2be67..359ff4b6f 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 @@ -649,6 +649,7 @@ class DboMysqlTest extends CakeTestCase { 'collate' => 'utf8_unicode_ci', 'engine' => 'InnoDB'); $this->assertEqual($result, $expected); + $this->db->query('DROP TABLE ' . $this->db->fullTableName('tinyint')); $this->db->query('CREATE TABLE ' . $this->db->fullTableName('tinyint') . ' (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->db->readTableParameters('tinyint'); diff --git a/cake/tests/cases/libs/model/datasources/dbo/dbo_mysqli.test.php b/cake/tests/cases/libs/model/datasources/dbo/dbo_mysqli.test.php index 1ceee963c..eed93c09a 100644 --- a/cake/tests/cases/libs/model/datasources/dbo/dbo_mysqli.test.php +++ b/cake/tests/cases/libs/model/datasources/dbo/dbo_mysqli.test.php @@ -102,34 +102,6 @@ class MysqliTestModel extends Model { */ var $useTable = false; -/** - * find method - * - * @param mixed $conditions - * @param mixed $fields - * @param mixed $order - * @param mixed $recursive - * @access public - * @return void - */ - function find($conditions = null, $fields = null, $order = null, $recursive = null) { - return $conditions; - } - -/** - * findAll method - * - * @param mixed $conditions - * @param mixed $fields - * @param mixed $order - * @param mixed $recursive - * @access public - * @return void - */ - function findAll($conditions = null, $fields = null, $order = null, $recursive = null) { - return $conditions; - } - /** * schema method * @@ -318,20 +290,48 @@ class DboMysqliTest extends CakeTestCase { } /** - * undocumented function + * test transaction commands. * * @return void * @access public */ function testTransactions() { - $this->db->begin($this->model); - $this->assertTrue($this->db->_transactionStarted); + $this->db->testing = false; + $result = $this->db->begin($this->model); + $this->assertTrue($result); $beginSqlCalls = Set::extract('/.[query=START TRANSACTION]', $this->db->_queriesLog); $this->assertEqual(1, count($beginSqlCalls)); - $this->db->commit($this->model); - $this->assertFalse($this->db->_transactionStarted); + $result = $this->db->commit($this->model); + $this->assertTrue($result); + } + +/** + * test that tableParameters like collation, charset and engine are functioning. + * + * @access public + * @return void + */ + function testReadTableParameters() { + $this->db->cacheSources = $this->db->testing = false; + $this->db->query('CREATE TABLE ' . $this->db->fullTableName('tinyint') . ' (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->db->readTableParameters('tinyint'); + $expected = array( + 'charset' => 'utf8', + 'collate' => 'utf8_unicode_ci', + 'engine' => 'InnoDB'); + $this->assertEqual($result, $expected); + + $this->db->query('DROP TABLE ' . $this->db->fullTableName('tinyint')); + $this->db->query('CREATE TABLE ' . $this->db->fullTableName('tinyint') . ' (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->db->readTableParameters('tinyint'); + $expected = array( + 'charset' => 'cp1250', + 'collate' => 'cp1250_general_ci', + 'engine' => 'MyISAM'); + $this->assertEqual($result, $expected); + $this->db->query('DROP TABLE ' . $this->db->fullTableName('tinyint')); } } ?> \ No newline at end of file