From a1aa73c1a2893469b7f8399d21b86f962a914498 Mon Sep 17 00:00:00 2001 From: ADmad Date: Sun, 7 Oct 2012 20:17:31 +0530 Subject: [PATCH] Cache character set names --- lib/Cake/Model/Datasource/Database/Mysql.php | 33 ++++++++++++++----- .../Model/Datasource/Database/MysqlTest.php | 30 ++++++++++++++++- 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/lib/Cake/Model/Datasource/Database/Mysql.php b/lib/Cake/Model/Datasource/Database/Mysql.php index dff20613b..aa00f9d2c 100644 --- a/lib/Cake/Model/Datasource/Database/Mysql.php +++ b/lib/Cake/Model/Datasource/Database/Mysql.php @@ -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 @@ class Mysql extends DboSource { )); } + $this->_charsets = array(); $this->_useAlias = (bool)version_compare($this->getVersion(), "4.1", ">="); return $this->connected; @@ -262,15 +270,24 @@ class Mysql extends DboSource { * @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 (isset($cols['CHARACTER_SET_NAME'])) { - return $cols['CHARACTER_SET_NAME']; - } + if ((bool)version_compare($this->getVersion(), "5", "<")) { + return false; } - 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'])) { + $this->_charsets[$name] = $cols['CHARACTER_SET_NAME']; + } else { + $this->_charsets[$name] = false; + } + return $this->_charsets[$name]; } /** diff --git a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php index caddc5b7a..a738a9528 100644 --- a/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php @@ -773,7 +773,7 @@ class MysqlTest extends CakeTestCase { } /** - * testBuildTableParameters method + * testGetCharsetName method * * @return void */ @@ -785,6 +785,34 @@ class MysqlTest extends CakeTestCase { $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. *