/**
* CodeIgniter
*
* An open source application development framework for PHP 5.1.6 or newer
*
* @packageCodeIgniter
* @authorExpressionEngine Dev Team
* @copyrightCopyright (c) 2008 - 2011, EllisLab, Inc.
* @licensehttp://codeigniter.com/user_guide/license.html
* @linkhttp://codeigniter.com
* @sinceVersion 1.0
* @filesource
*/
// ------------------------------------------------------------------------
/**
* MySQL Forge Class
* mysql 实用工具类
*
* @categoryDatabase
* @authorExpressionEngine Dev Team
* @linkhttp://codeigniter.com/user_guide/database/
*/
class CI_DB_mysql_forge extends CI_DB_forge {
/**
* Create database
* 创建数据库 create database databasename
* @accessprivate
* @paramstringthe database name
* @returnbool
*/
function _create_database($name)
{
return "CREATE DATABASE ".$name;
}
// --------------------------------------------------------------------
/**
* Drop database
* 清空数据库 drop database databasename
* @accessprivate
* @paramstringthe database name
* @returnbool
*/
function _drop_database($name)
{
return "DROP DATABASE ".$name;
}
// --------------------------------------------------------------------
/**
* Process Fields
*
* @accessprivate
* @parammixedthe fields
* @returnstring
*/
function _process_fields($fields)
{
$current_field_count = 0; //当前字段的总数
$sql = '';
//开始循环字段
foreach ($fields as $field=>$attributes)
{
// Numeric field names aren't allowed in databases, so if the key is
// numeric, we know it was assigned by PHP and the developer manually
// entered the field information, so we'll simply add it to the list
// 数字字段名不允许在数据库中,因此,如果关键是数字,我们知道它是PHP和开发商手动分配
// 输入的字段信息,所以我们只需将其添加到列表
//如果是数字,直接放在$sql.
if (is_numeric($field))
{
$sql .= "\n\t$attributes";
}
else
{
//array_change_key_case() 返回字符串键名全为小写或大写的数组
$attributes = array_change_key_case($attributes, CASE_UPPER);
$sql .= "\n\t".$this->db->_protect_identifiers($field);
//判断在$attributes 数组中是否存在NAME这个键 值名称
if (array_key_exists('NAME', $attributes))
{
//取得NAME这个键 值
$sql .= ' '.$this->db->_protect_identifiers($attributes['NAME']).' ';
}
//判断是否存在TYPE这个键 值名称
if (array_key_exists('TYPE', $attributes))
{
//
$sql .= ' '.$attributes['TYPE'];
//取CONSTRAINT
//constraint
//consttrint
//CONSTTRINT子句是指限制和索引相似,虽然限制也能被用于建立的另一个表的关联,用ALTER TABLE
// 和CREATE TABLE语句中的CONSTRAINT 子句来建立或删除条件,constraint子句可以分为两种类型,第一种
// 是在单一字段上创建条件,第二种是在一个以上的字段上创建条件
if (array_key_exists('CONSTRAINT', $attributes))
{
switch ($attributes['TYPE'])
{
case 'decimal': //双精度
case 'float': //浮点型
case 'numeric': //整型
$sql .= '('.implode(',', $attributes['CONSTRAINT']).')';
break;
case 'enum': //enum
case 'set': //set
$sql .= '("'.implode('","', $attributes['CONSTRAINT']).'")';
break;
default:
$sql .= '('.$attributes['CONSTRAINT'].')';
}
}
}
if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
{
$sql .= ' UNSIGNED';
}
//default 默认值
if (array_key_exists('DEFAULT', $attributes))
{
$sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
}
//是否为null
if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
{
$sql .= ' NULL';
}
else
{
$sql .= ' NOT NULL';
}
//是否为自增长
//auto_increment
if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
{
$sql .= ' AUTO_INCREMENT';
}
}
// don't add a comma on the end of the last field 不上的最后一个字段的末尾添加一个逗号
if (++$current_field_count < count($fields))
{
$sql .= ',';
}
}
return $sql;
}
// --------------------------------------------------------------------
/**
* Create Table
* 创建数据表
*
* @accessprivate
* @paramstringthe table name 表名
* @parammixedthe fields 字段
* @parammixedprimary key(s) //
* @parammixedkey(s)
* @parambooleanshould 'IF NOT EXISTS' be added to the SQL 判断表是否存在
* @returnbool
*/
function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
{
$sql = 'CREATE TABLE ';
if ($if_not_exists === TRUE)
{
$sql .= 'IF NOT EXISTS ';
}
$sql .= $this->db->_escape_identifiers($table)." (";
$sql .= $this->_process_fields($fields); //字段进行处理
//如果大于0
if (count($primary_keys) > 0)
{
//
$key_name = $this->db->_protect_identifiers(implode('_', $primary_keys));
$primary_keys = $this->db->_protect_identifiers($primary_keys);
$sql .= ",\n\tPRIMARY KEY ".$key_name." (" . implode(', ', $primary_keys) . ")";
}
//索引值吗?
if (is_array($keys) && count($keys) > 0)
{
foreach ($keys as $key)
{
if (is_array($key))
{
$key_name = $this->db->_protect_identifiers(implode('_', $key));
$key = $this->db->_protect_identifiers($key);
}
else
{
$key_name = $this->db->_protect_identifiers($key);
$key = array($key_name);
}
$sql .= ",\n\tKEY {$key_name} (" . implode(', ', $key) . ")";
}
}
//设置数据库的编码
//default character set char_set collate dbcollat
$sql .= "\n) DEFAULT CHARACTER SET {$this->db->char_set} COLLATE {$this->db->dbcollat};";
return $sql;
}
// --------------------------------------------------------------------
/**
* Drop Table
* 清空表
*
* @accessprivate
* @returnstring drop table if exists
*/
function _drop_table($table)
{
return "DROP TABLE IF EXISTS ".$this->db->_escape_identifiers($table);
}
// --------------------------------------------------------------------
/**
* Alter table query
* ALTER TABLE查询
*
* Generates a platform-specific query so that a table can be altered
* Called by add_column(), drop_column(), and column_alter(),
* 生成一个特定于平台的查询,以便可以改变一个表
* 由add_column(),drop_column(),column_alter()调用,
*
* @accessprivate
* @paramstringthe ALTER type (ADD, DROP, CHANGE)
* @paramstringthe column name
* @paramarrayfields
* @paramstringthe field after which we should add the new field
* @returnobject
*/
function _alter_table($alter_type, $table, $fields, $after_field = '')
{
//ALTER TABLE 语句用于在已有的表中添加,修改或删除列
$sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table)." $alter_type ";
// DROP has everything it needs now.
// DROP它现在需要的一切。
if ($alter_type == 'DROP')
{
return $sql.$this->db->_protect_identifiers($fields);
}
$sql .= $this->_process_fields($fields);
if ($after_field != '')
{
$sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field);
}
return $sql;
}
// --------------------------------------------------------------------
/**
* Rename a table
* 重命名表名
*
* Generates a platform-specific query so that a table can be renamed
* 生成一个特定于平台的查询,以便可以重命名的表
* @accessprivate
* @paramstringthe old table name
* @paramstringthe new table name
* @returnstring rename to
*/
function _rename_table($table_name, $new_table_name)
{
$sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
return $sql;
}
}
/* End of file mysql_forge.php */
/* Location: ./system/database/drivers/mysql/mysql_forge.php */