Skip to content
Permalink
Browse files

Moving DboMysql::listDetailedSources() and DboMysql::getCharsetName()…

… into DboMysqlBase so it works with mysqli as well. Tests added for MySqli.
  • Loading branch information...
markstory committed Sep 30, 2009
1 parent 0ab6f9f commit 4094f0a6962ee3e819fa6d8dc9d323abd182a43c
@@ -369,6 +369,52 @@ function insertMulti($table, $fields, $values) {
$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 @@ function listSources() {
}
}
/**
* 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 @@ function describe(&$model) {
}
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 @@ function describe(&$model) {
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.
*
@@ -649,6 +649,7 @@ function testReadTableParameters() {
'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');
@@ -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 @@ function testColumn() {
}
/**
* 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'));
}
}
?>

0 comments on commit 4094f0a

Please sign in to comment.
You can’t perform that action at this time.