[pLog-svn] r936 - plog/trunk/class/database/adodb/drivers

oscar at devel.plogworld.net oscar at devel.plogworld.net
Thu Feb 3 17:38:12 GMT 2005


Author: oscar
Date: 2005-02-03 17:38:12 +0000 (Thu, 03 Feb 2005)
New Revision: 936

Modified:
   plog/trunk/class/database/adodb/drivers/adodb-mysql.inc.php
Log:
fixed issue 201 (http://bugs.plogworld.net/view.php?id=201)

**** NOTE ****

This is a hack/improvement to one of the core components of pLog. We shoulnd't hack core components but sometimes there is no other way and this time, it was the only solution. 

In order to keep track of similar changes in the future, I've set up a page in the wiki that should be used for this kind of purposes:

http://wiki.plogworld.net/index.php/Core_Components_Hacks

Please keep it updated.

Modified: plog/trunk/class/database/adodb/drivers/adodb-mysql.inc.php
===================================================================
--- plog/trunk/class/database/adodb/drivers/adodb-mysql.inc.php	2005-02-03 17:26:14 UTC (rev 935)
+++ plog/trunk/class/database/adodb/drivers/adodb-mysql.inc.php	2005-02-03 17:38:12 UTC (rev 936)
@@ -341,7 +341,17 @@
 			$this->_connectionID = mysql_connect($argHostname,$argUsername,$argPassword,
 												$this->forceNewConnect);
 		else
-			$this->_connectionID = mysql_connect($argHostname,$argUsername,$argPassword);
+			$this->_connectionID = mysql_connect($argHostname,$argUsername,$argPassword);
+			
+        /** hack to adodb **/
+		if ($this->_isSupportUtf8() && $argDatabasename) {
+			$dbEncoding = $this->_getDbDefaultEncoding($argDatabasename);
+			if ($dbEncoding) {
+				mysql_query("SET NAMES $dbEncoding", $this->_connectionID);
+			}
+		}
+		/** end of hack **/
+			
 	
 		if ($this->_connectionID === false) return false;
 		if ($argDatabasename) return $this->SelectDB($argDatabasename);
@@ -356,7 +366,17 @@
 		else
 			$this->_connectionID = mysql_pconnect($argHostname,$argUsername,$argPassword);
 		if ($this->_connectionID === false) return false;
-		if ($this->autoRollback) $this->RollbackTrans();
+		if ($this->autoRollback) $this->RollbackTrans();
+		
+        /** hack to adodb **/
+		if ($this->_isSupportUtf8() && $argDatabasename) {
+			$dbEncoding = $this->_getDbDefaultEncoding($argDatabasename);
+			if ($dbEncoding) {
+				mysql_query("SET NAMES $dbEncoding", $this->_connectionID);
+			}
+		}		
+		/** end of hack **/		
+		
 		if ($argDatabasename) return $this->SelectDB($argDatabasename);
 		return true;	
 	}
@@ -365,7 +385,47 @@
 	{
 		$this->forceNewConnect = true;
 		return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename);
-	}
+	}
+    
+    /** start of adodb hack **/
+	function _isSupportUtf8() {
+	
+		// check mysql version first. Version lower than 4.1 doesn't support utf8
+		$serverVersion = mysql_get_server_info($this->_connectionID);
+		$version = explode('.', $serverVersion);
+		if ($version[0] < 4) return false;
+		if ( ($version[0] == 4) && ($version[1] == 0) ) return false;
+		
+		// check if utf8 support was compiled in
+		$result = mysql_query("SHOW CHARACTER SET like 'utf8'", $this->_connectionID);
+		if (mysql_num_rows($result) > 0) {
+			return true;
+		}
+		return false;
+	}
+	
+	function _getDbDefaultEncoding($argDatabasename)
+	{
+		
+		if (!$argDatabasename) {
+			return false;
+		}
+		
+		// We use a SHOW CREATE DATABASE command to show the original
+		// SQL character set when DB was created.
+		$result = mysql_query("SHOW CREATE DATABASE $argDatabasename", $this->_connectionID);
+		if (mysql_num_rows($result) < 0 ) {
+			// The specified db name is wrong!
+			return false;
+		}
+		$dbInfo = mysql_fetch_row($result);
+		$pattern = '/40100 DEFAULT CHARACTER SET (\w+) /';
+		if ( (preg_match($pattern, $dbInfo[1], $match) > 0) ) {
+			return $match[1];
+		}
+		return false;
+    }
+    /** end of hack **/
 	
  	function &MetaColumns($table) 
 	{




More information about the pLog-svn mailing list