完成了,同樣 patch 在 class/database/adodb/drivers/adodb-mysql.inc.php 上面, patch 檔也可以在
http://ccca.nctu.edu.tw/~cwyeh/patch.txt 下載。
基本原理如下:
* _isSupprtUtf8() 判斷 mysql 是否支援 utf8。若 4.0 or under 就 return false; 4.1+ 的則再檢查 compile 進來的 character set 是否包括 utf8,若是沒有的話就 return false, 有就 return true;
* _getDbDefaultEncoding() 若是支援 utf8, 則取得現在資料庫的 encoding, 不論是 utf8/latin1/big5 都可以取得。
* 然後再 apply SET NAMES $dbEncoding。
剛剛測試過應該是可以 work 的,不過如果 db 本身 default encoding 設為 utf8, 結果 table 卻是 big5/latin1, 用這個方法是不行的。
測試前,請確認 apache 沒有用到舊的 mysql connection, 最好的辦法就是
把 mysql restart 一次。另外請
記得刪掉 tmp/ 下面的 cache,不然會看到舊的內容。有問題歡迎提出來。
--- adodb-mysql.inc.php.orig Thu Feb 3 15:30:35 2005
+++ adodb-mysql.inc.php Thu Feb 3 15:28:05 2005
@@ -344,6 +344,14 @@
$this->_connectionID = mysql_connect($argHostname,$argUsername,$argPassword);
if ($this->_connectionID === false) return false;
+
+ if ($this->_supportUtf8() && $argDatabasename) {
+ $dbEncoding = $this->_getDbDefaultEncoding($argDatabasename);
+ if ($dbEncoding) {
+ mysql_query("SET NAMES $dbEncoding", $this->_connectionID);
+ }
+ }
+
if ($argDatabasename) return $this->SelectDB($argDatabasename);
return true;
}
@@ -357,6 +365,13 @@
$this->_connectionID = mysql_pconnect($argHostname,$argUsername,$argPassword);
if ($this->_connectionID === false) return false;
if ($this->autoRollback) $this->RollbackTrans();
+
+ if ($this->_supportUtf8() && $argDatabasename) {
+ $dbEncoding = $this->_getDbDefaultEncoding($argDatabasename);
+ if ($dbEncoding) {
+ mysql_query("SET NAMES $dbEncoding", $this->_connectionID);
+ }
+ }
if ($argDatabasename) return $this->SelectDB($argDatabasename);
return true;
}
@@ -365,6 +380,44 @@
{
$this->forceNewConnect = true;
return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename);
+ }
+
+ function _supportUtf8() {
+
+ // 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;
}
function &MetaColumns($table)