From eec225c523b507210538cc6f8249d922d3c754dc Mon Sep 17 00:00:00 2001 From: Koala Yeung Date: Sat, 20 Dec 2014 21:30:42 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20Pix=5FTable=20?= =?UTF-8?q?=E7=9A=84=20MySQL=20adapter=20=E4=BB=A5=E6=AD=A3=E7=A2=BA?= =?UTF-8?q?=E7=94=A2=E7=94=9F=E8=B3=87=E6=96=99=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 原裝的 Pix_Table_Db_Adapter_MysqlCommon::createTable 不支援 longtext 資料類型產生資料表,而會將相關欄目宣告為 text。 由於 text 的長度太知,不適合我們使用,較早前改為 longtext 的 資料表宣告並未生較。所以現在將 longtext 加入為該 adapter 的 可支援類型。 2. 原裝 Pix_Table_Db_Adapter_MysqlCommon::createTable 並沒有指明 資料表的編碼類型,許多 MySQL 資料庫預設會用 latin1,這並不能正 確接收中文網站的原始資料。部份中文網站用的冷僻字,需要用到統一 碼的 4 byte 範圍,連正常的 UTF8 編碼也不足以處理。 修改後的 createTable 自動將資料表預設編碼設定為 "utf8mb4",能 正確處理上述情況。 --- .../stdlibs/pixframework/Pix/Table/Db/Adapter/MysqlCommon.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/webdata/stdlibs/pixframework/Pix/Table/Db/Adapter/MysqlCommon.php b/webdata/stdlibs/pixframework/Pix/Table/Db/Adapter/MysqlCommon.php index 1a62db6..0552af2 100644 --- a/webdata/stdlibs/pixframework/Pix/Table/Db/Adapter/MysqlCommon.php +++ b/webdata/stdlibs/pixframework/Pix/Table/Db/Adapter/MysqlCommon.php @@ -12,7 +12,7 @@ abstract class Pix_Table_Db_Adapter_MysqlCommon extends Pix_Table_Db_Adapter_SQL public function createTable($table) { $sql = "CREATE TABLE " . $this->column_quote($table->getTableName()); - $types = array('bigint', 'tinyint', 'int', 'varchar', 'char', 'text', 'float', 'double', 'binary'); + $types = array('bigint', 'tinyint', 'int', 'varchar', 'char', 'text', 'longtext', 'float', 'double', 'binary'); foreach ($table->_columns as $name => $column) { $s = $this->column_quote($name) . ' '; @@ -73,7 +73,7 @@ public function createTable($table) $column_sql[] = $s; } - $sql .= " (\n" . implode(", \n", $column_sql) . ") ENGINE = InnoDB\n"; + $sql .= " (\n" . implode(", \n", $column_sql) . ") ENGINE = InnoDB DEFAULT CHARSET=utf8mb4\n"; return $this->query($sql, $table); } From fff93676c9d3414d7b27d725b0abccb9793184fc Mon Sep 17 00:00:00 2001 From: Koala Yeung Date: Wed, 7 Jan 2015 14:11:20 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=E6=94=B9=E9=80=B2=20news-raw-tables.php=20?= =?UTF-8?q?=E7=94=A2=E7=94=9F=E8=A1=A8=E6=A0=BC=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原有方法直接用 SQL 語句產生表格,表格的結構不能隨 Pix_Table 實現而改變,比較不靈活。 改用 Ronny 在[這裏](https://github.com/ronnywang/newsdiff/issues/18#issuecomment-59484616)提及的方法產生 news_raw 表格。 --- webdata/scripts/news-raw-tables.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/webdata/scripts/news-raw-tables.php b/webdata/scripts/news-raw-tables.php index f07bfa3..489930e 100644 --- a/webdata/scripts/news-raw-tables.php +++ b/webdata/scripts/news-raw-tables.php @@ -5,14 +5,9 @@ * to create 12 tables for NewsRaw */ $theTime = mktime(); -$str = "CREATE TABLE IF NOT EXISTS `news_raw_%s` ( - `news_id` int(11) NOT NULL, - `time` int(11) NOT NULL, - `raw` text NOT NULL, - PRIMARY KEY (`news_id`,`time`) -) ENGINE=InnoDB;"; +$str = 'CREATE TABLE IF NOT EXISTS `news_raw_%s` LIKE news_raw'; $db = NewsRaw::getDb(); for ($i = 0; $i < 12; $i++) { $db->query(sprintf($str, date('Ym', $theTime))); $theTime = strtotime('+1 month', $theTime); -} \ No newline at end of file +} From b73989a4807d92b199d0f4a3eb16ef7958fdfc49 Mon Sep 17 00:00:00 2001 From: Koala Yeung Date: Wed, 7 Jan 2015 14:12:34 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=E6=94=B9=E9=80=B2=20table-build.php=20?= =?UTF-8?q?=E8=99=95=E7=90=86=E9=8C=AF=E8=AA=A4=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新安裝時,table-build.php 的 drop table 指令執行失敗,結果阻礙程式進行。 修改後會偵測錯誤訊息,若果屬於 Unknown Table,則會自動無視。 --- webdata/scripts/table-build.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/webdata/scripts/table-build.php b/webdata/scripts/table-build.php index c756de5..b264817 100644 --- a/webdata/scripts/table-build.php +++ b/webdata/scripts/table-build.php @@ -11,7 +11,18 @@ $p = pathinfo($m); $o = new $p['filename']; if($o instanceof Pix_Table) { - $o->dropTable(); + try { + $o->dropTable(); + } catch (Pix_Table_Exception $e) { + echo 'Unable to drop table "'.$p['filename'].'". '; + if (preg_match('/^Table: \w+SQL Error: \(1051\)Unknown table/', + $e->getMessage())) { + echo 'Table missing. Ignored.'."\n"; + } else { + echo "\n"; + die($e->getMessage()); + } + } $o->createTable(); } -} \ No newline at end of file +}