Cache character set names

This commit is contained in:
ADmad 2012-10-07 20:17:31 +05:30
parent fa6defea37
commit a1aa73c1a2
2 changed files with 54 additions and 9 deletions

View file

@ -119,6 +119,13 @@ class Mysql extends DboSource {
'boolean' => array('name' => 'tinyint', 'limit' => '1') '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. * Connects to the database using options in the given configuration array.
* *
@ -156,6 +163,7 @@ class Mysql extends DboSource {
)); ));
} }
$this->_charsets = array();
$this->_useAlias = (bool)version_compare($this->getVersion(), "4.1", ">="); $this->_useAlias = (bool)version_compare($this->getVersion(), "4.1", ">=");
return $this->connected; return $this->connected;
@ -262,15 +270,24 @@ class Mysql extends DboSource {
* @return string Character set name * @return string Character set name
*/ */
public function getCharsetName($name) { public function getCharsetName($name) {
if ((bool)version_compare($this->getVersion(), "5", ">=")) { if ((bool)version_compare($this->getVersion(), "5", "<")) {
$r = $this->_execute('SELECT CHARACTER_SET_NAME FROM INFORMATION_SCHEMA.COLLATIONS WHERE COLLATION_NAME = ?', array($name)); 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); $cols = $r->fetch(PDO::FETCH_ASSOC);
if (isset($cols['CHARACTER_SET_NAME'])) { if (isset($cols['CHARACTER_SET_NAME'])) {
return $cols['CHARACTER_SET_NAME']; $this->_charsets[$name] = $cols['CHARACTER_SET_NAME'];
} else {
$this->_charsets[$name] = false;
} }
} return $this->_charsets[$name];
return false;
} }
/** /**

View file

@ -773,7 +773,7 @@ class MysqlTest extends CakeTestCase {
} }
/** /**
* testBuildTableParameters method * testGetCharsetName method
* *
* @return void * @return void
*/ */
@ -785,6 +785,34 @@ class MysqlTest extends CakeTestCase {
$this->assertEquals('cp1250', $result); $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. * test that changing the virtualFieldSeparator allows for __ fields.
* *