Skip to content
This repository
Browse code

Cache character set names

  • Loading branch information...
commit a1aa73c1a2893469b7f8399d21b86f962a914498 1 parent fa6defe
ADmad authored October 07, 2012
31  lib/Cake/Model/Datasource/Database/Mysql.php
@@ -120,6 +120,13 @@ class Mysql extends DboSource {
120 120
 	);
121 121
 
122 122
 /**
  123
+ * Mapping of collation names to character set names
  124
+ *
  125
+ * @var array
  126
+ */
  127
+	protected $_charsets = array();
  128
+
  129
+/**
123 130
  * Connects to the database using options in the given configuration array.
124 131
  *
125 132
  * @return boolean True if the database could be connected, else false
@@ -156,6 +163,7 @@ public function connect() {
156 163
 			));
157 164
 		}
158 165
 
  166
+		$this->_charsets = array();
159 167
 		$this->_useAlias = (bool)version_compare($this->getVersion(), "4.1", ">=");
160 168
 
161 169
 		return $this->connected;
@@ -262,15 +270,24 @@ public function getEncoding() {
262 270
  * @return string Character set name
263 271
  */
264 272
 	public function getCharsetName($name) {
265  
-		if ((bool)version_compare($this->getVersion(), "5", ">=")) {
266  
-			$r = $this->_execute('SELECT CHARACTER_SET_NAME FROM INFORMATION_SCHEMA.COLLATIONS WHERE COLLATION_NAME = ?', array($name));
267  
-			$cols = $r->fetch(PDO::FETCH_ASSOC);
  273
+		if ((bool)version_compare($this->getVersion(), "5", "<")) {
  274
+			return false;
  275
+		}
  276
+		if (isset($this->_charsets[$name])) {
  277
+			return $this->_charsets[$name];
  278
+		}
  279
+		$r = $this->_execute(
  280
+			'SELECT CHARACTER_SET_NAME FROM INFORMATION_SCHEMA.COLLATIONS WHERE COLLATION_NAME = ?',
  281
+			array($name)
  282
+		);
  283
+		$cols = $r->fetch(PDO::FETCH_ASSOC);
268 284
 
269  
-			if (isset($cols['CHARACTER_SET_NAME'])) {
270  
-				return $cols['CHARACTER_SET_NAME'];
271  
-			}
  285
+		if (isset($cols['CHARACTER_SET_NAME'])) {
  286
+			$this->_charsets[$name] = $cols['CHARACTER_SET_NAME'];
  287
+		} else {
  288
+			$this->_charsets[$name] = false;
272 289
 		}
273  
-		return false;
  290
+		return $this->_charsets[$name];
274 291
 	}
275 292
 
276 293
 /**
30  lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php
@@ -773,7 +773,7 @@ public function testBuildTableParameters() {
773 773
 	}
774 774
 
775 775
 /**
776  
- * testBuildTableParameters method
  776
+ * testGetCharsetName method
777 777
  *
778 778
  * @return void
779 779
  */
@@ -786,6 +786,34 @@ public function testGetCharsetName() {
786 786
 	}
787 787
 
788 788
 /**
  789
+ * testGetCharsetNameCaching method
  790
+ *
  791
+ * @return void
  792
+ */
  793
+	public function testGetCharsetNameCaching() {
  794
+		$db = $this->getMock('Mysql', array('connect', '_execute', 'getVersion'));
  795
+		$queryResult = $this->getMock('PDOStatement');
  796
+
  797
+		$db->expects($this->exactly(2))->method('getVersion')->will($this->returnValue('5.1'));
  798
+
  799
+		$db->expects($this->exactly(1))
  800
+			->method('_execute')
  801
+			->with('SELECT CHARACTER_SET_NAME FROM INFORMATION_SCHEMA.COLLATIONS WHERE COLLATION_NAME = ?', array('utf8_unicode_ci'))
  802
+			->will($this->returnValue($queryResult));
  803
+
  804
+		$queryResult->expects($this->once())
  805
+			->method('fetch')
  806
+			->with(PDO::FETCH_ASSOC)
  807
+			->will($this->returnValue(array('CHARACTER_SET_NAME' => 'utf8')));
  808
+
  809
+		$result = $db->getCharsetName('utf8_unicode_ci');
  810
+		$this->assertEquals('utf8', $result);
  811
+
  812
+		$result = $db->getCharsetName('utf8_unicode_ci');
  813
+		$this->assertEquals('utf8', $result);
  814
+	}
  815
+
  816
+/**
789 817
  * test that changing the virtualFieldSeparator allows for __ fields.
790 818
  *
791 819
  * @return void

0 notes on commit a1aa73c

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