Skip to content
Browse files

Cache character set names

  • Loading branch information...
1 parent fa6defe commit a1aa73c1a2893469b7f8399d21b86f962a914498 @ADmad ADmad committed Oct 7, 2012
View
31 lib/Cake/Model/Datasource/Database/Mysql.php
@@ -120,6 +120,13 @@ class Mysql extends DboSource {
);
/**
+ * Mapping of collation names to character set names
+ *
+ * @var array
+ */
+ protected $_charsets = array();
+
+/**
* Connects to the database using options in the given configuration array.
*
* @return boolean True if the database could be connected, else false
@@ -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];
}
/**
View
30 lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php
@@ -773,7 +773,7 @@ public function testBuildTableParameters() {
}
/**
- * testBuildTableParameters method
+ * testGetCharsetName method
*
* @return void
*/
@@ -786,6 +786,34 @@ public function testGetCharsetName() {
}
/**
+ * 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.
*
* @return void

0 comments on commit a1aa73c

Please sign in to comment.
Something went wrong with that request. Please try again.