Skip to content
Permalink
Browse files

Cache character set names

  • Loading branch information...
ADmad committed Oct 7, 2012
1 parent fa6defe commit a1aa73c1a2893469b7f8399d21b86f962a914498
@@ -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.
*
@@ -156,6 +163,7 @@ public function connect() {
));
}
$this->_charsets = array();
$this->_useAlias = (bool)version_compare($this->getVersion(), "4.1", ">=");
return $this->connected;
@@ -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];
}
/**
@@ -773,7 +773,7 @@ public function testBuildTableParameters() {
}
/**
* testBuildTableParameters method
* testGetCharsetName method
*
* @return void
*/
@@ -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.
*

0 comments on commit a1aa73c

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