Skip to content

Commit

Permalink
Cache character set names
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Oct 7, 2012
1 parent fa6defe commit a1aa73c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
31 changes: 24 additions & 7 deletions lib/Cake/Model/Datasource/Database/Mysql.php
Expand Up @@ -119,6 +119,13 @@ class Mysql extends DboSource {
'boolean' => array('name' => 'tinyint', 'limit' => '1')
);

/**
* Mapping of collation names to character set names
*
* @var array
*/
protected $_charsets = array();

/**
* Connects to the database using options in the given configuration array.
*
Expand Down Expand Up @@ -156,6 +163,7 @@ public function connect() {
));
}

$this->_charsets = array();
$this->_useAlias = (bool)version_compare($this->getVersion(), "4.1", ">=");

return $this->connected;
Expand Down Expand Up @@ -262,15 +270,24 @@ public function getEncoding() {
* @return string Character set name
*/
public function getCharsetName($name) {
if ((bool)version_compare($this->getVersion(), "5", ">=")) {
$r = $this->_execute('SELECT CHARACTER_SET_NAME FROM INFORMATION_SCHEMA.COLLATIONS WHERE COLLATION_NAME = ?', array($name));
$cols = $r->fetch(PDO::FETCH_ASSOC);
if ((bool)version_compare($this->getVersion(), "5", "<")) {
return false;
}
if (isset($this->_charsets[$name])) {
return $this->_charsets[$name];
}
$r = $this->_execute(
'SELECT CHARACTER_SET_NAME FROM INFORMATION_SCHEMA.COLLATIONS WHERE COLLATION_NAME = ?',
array($name)
);
$cols = $r->fetch(PDO::FETCH_ASSOC);

if (isset($cols['CHARACTER_SET_NAME'])) {
return $cols['CHARACTER_SET_NAME'];
}
if (isset($cols['CHARACTER_SET_NAME'])) {
$this->_charsets[$name] = $cols['CHARACTER_SET_NAME'];
} else {
$this->_charsets[$name] = false;
}
return false;
return $this->_charsets[$name];
}

/**
Expand Down
30 changes: 29 additions & 1 deletion lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php
Expand Up @@ -773,7 +773,7 @@ public function testBuildTableParameters() {
}

/**
* testBuildTableParameters method
* testGetCharsetName method
*
* @return void
*/
Expand All @@ -785,6 +785,34 @@ public function testGetCharsetName() {
$this->assertEquals('cp1250', $result);
}

/**
* testGetCharsetNameCaching method
*
* @return void
*/
public function testGetCharsetNameCaching() {
$db = $this->getMock('Mysql', array('connect', '_execute', 'getVersion'));
$queryResult = $this->getMock('PDOStatement');

$db->expects($this->exactly(2))->method('getVersion')->will($this->returnValue('5.1'));

$db->expects($this->exactly(1))
->method('_execute')
->with('SELECT CHARACTER_SET_NAME FROM INFORMATION_SCHEMA.COLLATIONS WHERE COLLATION_NAME = ?', array('utf8_unicode_ci'))
->will($this->returnValue($queryResult));

$queryResult->expects($this->once())
->method('fetch')
->with(PDO::FETCH_ASSOC)
->will($this->returnValue(array('CHARACTER_SET_NAME' => 'utf8')));

$result = $db->getCharsetName('utf8_unicode_ci');
$this->assertEquals('utf8', $result);

$result = $db->getCharsetName('utf8_unicode_ci');
$this->assertEquals('utf8', $result);
}

/**
* test that changing the virtualFieldSeparator allows for __ fields.
*
Expand Down

0 comments on commit a1aa73c

Please sign in to comment.