<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Code Igniter
*
* An open source application development framework for PHP 5.1.6 or newer
*
* @package CodeIgniter
* @author ExpressionEngine Dev Team
* @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
* @license http://codeigniter.com/user_guide/license.html
* @link http://codeigniter.com
* @since Version 1.0
* @filesource
*/
// ------------------------------------------------------------------------
/**
* Database Utility Class
* 数据库实用工具类
* @category Database
* @author ExpressionEngine Dev Team
* @link http://codeigniter.com/user_guide/database/
*/
class CI_DB_forge {
var $fields = array(); //字段值
var $keys = array(); //键值数组
var $primary_keys = array(); //唯一的键值数组
var $db_char_set = ''; //数据库的编码设置
/**
* Constructor
*
* Grabs the CI super object instance so we can access it.
* 抓住CI超级对象实例,这样我们就可以访问它。
*/
function __construct()
{
// Assign the main database object to $this->db 主数据库对象分配到$> DB
$CI =& get_instance();
$this->db =& $CI->db;
log_message('debug', "Database Forge Class Initialized 初始化数据库建立阶级");
}
// --------------------------------------------------------------------
/**
* Create database
* 创建数据库
* @access public
* @param string the database name
* @return bool
*/
function create_database($db_name)
{
//生成创建数据库sql,这个应该由各个数据库驱动来实现
$sql = $this->_create_database($db_name);
if (is_bool($sql))
{
return $sql;
}
//执行
return $this->db->query($sql);
}
// --------------------------------------------------------------------
/**
* Drop database
* 删除数据库
* @access public
* @param string the database name
* @return bool
*/
function drop_database($db_name)
{
$sql = $this->_drop_database($db_name);
if (is_bool($sql))
{
return $sql;
}
return $this->db->query($sql);
}
// --------------------------------------------------------------------
/**
* Add Key
* 添加key值
* @access public
* @param string key
* @param string type
* @return void
*/
function add_key($key = '', $primary = FALSE)
{
if (is_array($key))
{
foreach ($key as $one)
{
$this->add_key($one, $primary); //递归进行添加key
}
return;
}
if ($key == '')
{
show_error('Key information is required for that operation.该操作需要关键信息');
}
//如果为主键值放入到primary_keys中
if ($primary === TRUE)
{
$this->primary_keys[] = $key;
}
else
{
//其它的放入到keys中
$this->keys[] = $key;
}
}
// --------------------------------------------------------------------
/**
* Add Field
* 添加字段
* @access public
* @param string collation
* @return void
*/
function add_field($field = '')
{
if ($field == '')
{
show_error('Field information is required. 字段信息是必需的。');
}
//如果为字符串
if (is_string($field))
{
//如果是id
if ($field == 'id')
{
$this->add_field(array(
'id' => array(
'type' => 'INT',
'constraint' => 9,
'auto_increment' => TRUE
)
));
$this->add_key('id', TRUE);
}
else
{
//如果没有空格的话
if (strpos($field, ' ') === FALSE)
{
show_error('Field information is required for that operation.该操作需要现场信息');
}
//将$field放入到$this->fields[]数组中
$this->fields[] = $field;
}
}
//如果为数组,直接与array_merge() 将$this->fields与$field数组进行合并
if (is_array($field))
{
$this->fields = array_merge($this->fields, $field);
}
}
// --------------------------------------------------------------------
/**
* Create Table
* 创建表
* @access public
* @param string the table name
* @return bool
*/
function create_table($table = '', $if_not_exists = FALSE)
{
if ($table == '')
{
show_error('A table name is required for that operation.该操作需要一个表名');
}
if (count($this->fields) == 0)
{
show_error('Field information is required.字段信息是必需的。');
}
$sql = $this->_create_table($this->db->dbprefix.$table, $this->fields, $this->primary_keys, $this->keys, $if_not_exists);
$this->_reset();
return $this->db->query($sql);
}
// --------------------------------------------------------------------
/**
* Drop Table
* 删除表
* @access public
* @param string the table name
* @return bool
*/
function drop_table($table_name)
{
$sql = $this->_drop_table($this->db->dbprefix.$table_name);
if (is_bool($sql))
{
return $sql;
}
return $this->db->query($sql);
}
// --------------------------------------------------------------------
/**
* Rename Table
* 重命名表
* @access public
* @param string the old table name
* @param string the new table name
* @return bool
*/
function rename_table($table_name, $new_table_name)
{
if ($table_name == '' OR $new_table_name == '')
{
show_error('A table name is required for that operation.该操作需要一个表名');
}
$sql = $this->_rename_table($this->db->dbprefix.$table_name, $this->db->dbprefix.$new_table_name);
return $this->db->query($sql);
}
// --------------------------------------------------------------------
/**
* Column Add
* 添加列
* @access public
* @param string the table name //表名称
* @param string the column name //列名称
* @param string the column definition//列的默认值
* @return bool
*/
function add_column($table = '', $field = array(), $after_field = '')
{
if ($table == '')
{
show_error('A table name is required for that operation.该操作需要一个表名。');
}
// add field info into field array, but we can only do one at a time
// so we cycle through
// 添加字段的信息阵到现场,但我们可以在一个时间只能做一
// 所以我们循环
foreach ($field as $k => $v)
{
//将字段数组一个一个的进行添加
$this->add_field(array($k => $field[$k]));
//
if (count($this->fields) == 0)
{
show_error('Field information is required.字段信息是必需的。');
}
//现在才是将字段添加到数据表中的的,第一个是操作动作
//第二参数是表名
//第三是字段数组
//第四是字段的默认值
$sql = $this->_alter_table('ADD', $this->db->dbprefix.$table, $this->fields, $after_field);
$this->_reset();
if ($this->db->query($sql) === FALSE)
{
return FALSE;
}
}
return TRUE;
}
// --------------------------------------------------------------------
/**
* Column Drop
* 删除数据表列
* @access public
* @param string the table name 数据表名
* @param string the column name 数据表例名
* @return bool
*/
function drop_column($table = '', $column_name = '')
{
if ($table == '')
{
show_error('A table name is required for that operation.该操作需要一个表名');
}
if ($column_name == '')
{
show_error('A column name is required for that operation.该操作需要一个列名');
}
$sql = $this->_alter_table('DROP', $this->db->dbprefix.$table, $column_name);
return $this->db->query($sql);
}
// --------------------------------------------------------------------
/**
* Column Modify
* 列修改
* @access public
* @param string the table name 表名
* @param string the column name 列名
* @param string the column definition
* @return bool
*/
function modify_column($table = '', $field = array())
{
if ($table == '')
{
show_error('A table name is required for that operation.需要一个列名');
}
// add field info into field array, but we can only do one at a time
// so we cycle through
// 添加字段的信息阵到现场,但我们可以在一个时间只能做一
// 所以我们循环
foreach ($field as $k => $v)
{
// If no name provided, use the current name 如果没有名字,使用目前的名字
if ( ! isset($field[$k]['name']))
{
$field[$k]['name'] = $k;
}
$this->add_field(array($k => $field[$k]));
if (count($this->fields) == 0)
{
show_error('Field information is required.');
}
$sql = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->fields);
$this->_reset();
if ($this->db->query($sql) === FALSE)
{
return FALSE;
}
}
return TRUE;
}
// --------------------------------------------------------------------
/**
* Reset
* 重置
* Resets table creation vars
* 重置创建表的参数
* @access private
* @return void
*/
function _reset()
{
$this->fields = array();
$this->keys = array();
$this->primary_keys = array();
}
}
/* End of file DB_forge.php */
/* Location: ./system/database/DB_forge.php */