用户
 找回密码
 入住 CI 中国社区
搜索
查看: 4638|回复: 2
收起左侧

[修补 Patch] CodeIgniter中数据库工具类backup()的问题

[复制链接]
发表于 2008-12-31 23:13:56 | 显示全部楼层 |阅读模式
在数据库备份中,可以利用CodeIgniter自带的数据库工具$this->dbutil->backup()处理,生成备份SQL文件,还是很方便的。但是这个函数不支持VIEW,虽然也能生成SQL语句,但生成的是DROP TABLE,而不是DROP VIEW,VIEW本身是不需要导出里面的数据,因此会导致生成的备份文件无法重新导入数据库中,需要对框架进行修改;

修改文件:\system\database\drivers\mysql\mysql_utility.php中的_backup(...)方法;

增加内容如下(红色部分):

   // Write out the table schema
   $output .= '#'.$newline.'# TABLE STRUCTURE FOR: '.$table.$newline.'#'.$newline.$newline;
    if ($add_drop == TRUE)
    {
    $output .= 'DROP TABLE IF EXISTS '.$table.';'.$newline.$newline;
   }
   
   /* add by adai 20081231 for view */
   $is_view = FALSE;
    if ($add_drop == TRUE)
    {
    $view_query = $this->db->query("SHOW TABLE STATUS FROM ".$this->db->database.' LIKE \''.$table.'\'');
    $view_arr = $view_query->result_array();
    foreach ($view_arr as $val)
    {
     $comment = $val['Comment'];
    }
    if(substr($comment, 0, 4) == 'VIEW')
    {
     $output .= 'DROP VIEW IF EXISTS '.$table.';'.$newline.$newline;
     $is_view = TRUE;
    }

   }
   /* end add 20081231 for view */
   $i = 0;
   $result = $query->result_array();
   foreach ($result[0] as $val)
   {
    if ($i++ % 2)
    {      
     $output .= $val.';'.$newline.$newline;
    }
   }
   
   // If inserts are not needed we're done...
   if ($add_insert == FALSE)
   {
    continue;
   }
   /* add by adai 20081231 for view */
    if ($is_view == TRUE)
    {
    continue;
   }
   /* end add 20081231 for view */

   // Grab all the data from the current table
   $query = $this->db->query("SELECT * FROM $table");
   
   if ($query->num_rows() == 0)
   {
    continue;
   }
发表于 2009-1-9 00:49:11 | 显示全部楼层
这也可以算是一个 BUG 了,呵呵。
发表于 2012-4-20 17:34:32 | 显示全部楼层
标记一下

本版积分规则