重新定义模型
This commit is contained in:
@@ -13,45 +13,59 @@ use think\Db;
|
||||
* 数据库管理类
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
class Datatable{
|
||||
|
||||
protected $table; /*数据库操作的表*/
|
||||
protected $fields = array(); /*数据库操作字段*/
|
||||
protected $charset = 'utf8'; /*数据库操作字符集*/
|
||||
public $prefix = ''; /*数据库操作表前缀*/
|
||||
protected $model_table_prefix = ''; /*模型默认创建的表前缀*/
|
||||
protected $engine_type = 'MyISAM'; /*数据库引擎*/
|
||||
protected $key = 'id'; /*数据库主键*/
|
||||
public $sql = ''; /*最后生成的sql语句*/
|
||||
class Datatable {
|
||||
|
||||
protected $table; /*数据库操作的表*/
|
||||
protected $fields = array(); /*数据库操作字段*/
|
||||
protected $charset = 'utf8'; /*数据库操作字符集*/
|
||||
public $prefix = ''; /*数据库操作表前缀*/
|
||||
protected $model_table_prefix = ''; /*模型默认创建的表前缀*/
|
||||
protected $engine_type = 'MyISAM'; /*数据库引擎*/
|
||||
protected $key = 'id'; /*数据库主键*/
|
||||
public $sql = ''; /*最后生成的sql语句*/
|
||||
|
||||
/**
|
||||
* 初始化数据库信息
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function __construct(){
|
||||
public function __construct() {
|
||||
//创建DB对象
|
||||
$this->prefix = config('database.prefix');
|
||||
$this->prefix = config('database.prefix');
|
||||
$this->model_table_prefix = config('model_table_prefix');
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始创建表
|
||||
* @var $table 表名
|
||||
* @author colin <colin@tensent.cn>
|
||||
* @title 初始化表
|
||||
* @description 初始化创建表
|
||||
* @Author molong
|
||||
* @DateTime 2017-06-11
|
||||
* @param string $table 表名
|
||||
* @return void 空
|
||||
*/
|
||||
public function start_table($table){
|
||||
$this->table = $this->getTablename($table,true);
|
||||
$this->sql .= "CREATE TABLE IF NOT EXISTS `".$this->table."`(";
|
||||
return $this;
|
||||
}
|
||||
public function initTable($table = '', $comment = '', $pk = '', $time = true){
|
||||
$this->table = $this->getTablename($table, true);
|
||||
|
||||
/**
|
||||
* 创建字段
|
||||
* @var $sql 要执行的字段sql语句可以为array()或者strubf
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function create_field($sql){
|
||||
$this->sql .= $sql.',';
|
||||
if ($pk) {
|
||||
$sql[] = $this->generateField('id', 'int', 11, '', '主键', true);
|
||||
}
|
||||
if ($time) {
|
||||
//初始化表内含创建时间和更新时间两个字段
|
||||
$sql[] = $this->generateField('create_time', 'int', 11, 0, '创建时间', false);
|
||||
$sql[] = $this->generateField('update_time', 'int', 11, 0, '创建时间', false);
|
||||
}
|
||||
|
||||
$primary = $pk ? "PRIMARY KEY (`".$pk."`)" : '';
|
||||
if ($primary) {
|
||||
$generatesql = implode(',', $sql) . ',';
|
||||
}else{
|
||||
$generatesql = implode(',', $sql);
|
||||
}
|
||||
|
||||
$create = "CREATE TABLE IF NOT EXISTS `" . $this->table . "`("
|
||||
. $generatesql
|
||||
. $primary
|
||||
. ") ENGINE=" . $this->engine_type . " AUTO_INCREMENT=1 DEFAULT CHARSET=" . $this->charset . " ROW_FORMAT=DYNAMIC COMMENT='" . $comment . "';";
|
||||
$this->sql = $create;
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -61,55 +75,51 @@ class Datatable{
|
||||
* @var comment 字段的描述
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function create_id($key = 'id', $length = 11 , $comment = '主键' , $is_auto_increment = true){
|
||||
$auto_increment = $is_auto_increment ? 'AUTO_INCREMENT' : '';
|
||||
$this->sql .= "`{$key}` int({$length}) unsigned NOT NULL $auto_increment COMMENT '{$comment}',";
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* 快速创建ID字段
|
||||
* @var length 字段的长度
|
||||
* @var comment 字段的描述
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function create_uid(){
|
||||
$this->sql .= "`uid` int(11) NOT NULL DEFAULT '0' COMMENT '用户uid',";
|
||||
return $this;
|
||||
public function generateField($key = '', $type = '', $length = 11, $default = '', $comment = '主键', $is_auto_increment = false){
|
||||
if ($key && $type) {
|
||||
$auto_increment = $is_auto_increment ? 'AUTO_INCREMENT' : '';
|
||||
$field_type = $length ? $type.'('.$length.')' : $type;
|
||||
$signed = in_array($type, array('int', 'float', 'double')) ? 'signed' : '';
|
||||
$comment = $comment ? "COMMENT '" . $comment . "'" : "";
|
||||
$default = $default ? "DEFAULT '" . $default . "'" : "";
|
||||
$sql = "`{$key}` {$field_type} {$signed} NOT NULL {$default} $auto_increment {$comment}";
|
||||
}
|
||||
return $sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* 追加字段
|
||||
* @var $table 追加字段的表名
|
||||
* @var $table 追加字段的表名
|
||||
* @var $attr 属性列表
|
||||
* @var $is_more 是否为多条同时插入
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function colum_field($table,$attr = array()){
|
||||
$field_attr['table'] = $table ? $this->getTablename($table,true) : $this->table;
|
||||
public function columField($table, $attr = array()) {
|
||||
$field_attr['table'] = $table ? $this->getTablename($table, true) : $this->table;
|
||||
$field_attr['field'] = $attr['field'];
|
||||
$field_attr['type'] = $attr['type'] ? $attr['type'] : 'varchar';
|
||||
$field_attr['type'] = $attr['type'] ? $attr['type'] : 'varchar';
|
||||
if (intval($attr['length']) && $attr['length']) {
|
||||
$field_attr['length'] = "(".$attr['length'].")";
|
||||
}else{
|
||||
$field_attr['length'] = "(" . $attr['length'] . ")";
|
||||
} else {
|
||||
$field_attr['length'] = "";
|
||||
}
|
||||
$field_attr['is_null'] = $attr['is_null'] ? 'NOT NULL' : 'null';
|
||||
$field_attr['default'] = $attr['default'] != '' ? 'default "'.$attr['default'].'"' : 'default null';
|
||||
if($field_attr['is_null'] == 'null'){
|
||||
$field_attr['default'] = $attr['default'] != '' ? 'default "' . $attr['default'] . '"' : 'default null';
|
||||
if ($field_attr['is_null'] == 'null') {
|
||||
$field_attr['default'] = $field_attr['default'];
|
||||
}else{
|
||||
} else {
|
||||
$field_attr['default'] = '';
|
||||
}
|
||||
$field_attr['comment'] = (isset($attr['comment']) && $attr['comment']) ? $attr['comment'] : '';
|
||||
$field_attr['oldname'] = (isset($attr['oldname']) && $attr['oldname']) ? $attr['oldname'] : '';
|
||||
$field_attr['newname'] = (isset($attr['newname']) && $attr['newname']) ? $attr['newname'] : $field_attr['field'];
|
||||
$field_attr['after'] = (isset($attr['after']) && $attr['after']) ? ' AFTER `'.$attr['after'].'`' : '';
|
||||
$field_attr['action'] = (isset($attr['action']) && $attr['action']) ? $attr['action'] : 'ADD';
|
||||
$field_attr['after'] = (isset($attr['after']) && $attr['after']) ? ' AFTER `' . $attr['after'] . '`' : '';
|
||||
$field_attr['action'] = (isset($attr['action']) && $attr['action']) ? $attr['action'] : 'ADD';
|
||||
//确认表是否存在
|
||||
|
||||
if($field_attr['action'] == 'ADD'){
|
||||
if ($field_attr['action'] == 'ADD') {
|
||||
$this->sql = "ALTER TABLE `{$field_attr['table']}` ADD `{$field_attr['field']}` {$field_attr['type']}{$field_attr['length']} {$field_attr['is_null']} {$field_attr['default']} COMMENT '{$field_attr['comment']}'";
|
||||
}elseif($field_attr['action'] == 'CHANGE'){
|
||||
} elseif ($field_attr['action'] == 'CHANGE') {
|
||||
$this->sql = "ALTER TABLE `{$field_attr['table']}` CHANGE `{$field_attr['oldname']}` `{$field_attr['newname']}` {$field_attr['type']}{$field_attr['length']} {$field_attr['is_null']} {$field_attr['default']} COMMENT '{$field_attr['comment']}'";
|
||||
}
|
||||
return $this;
|
||||
@@ -117,41 +127,27 @@ class Datatable{
|
||||
|
||||
/**
|
||||
* 删除字段
|
||||
* @var $table 追加字段的表名
|
||||
* @var $table 追加字段的表名
|
||||
* @var $field 字段名
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function del_field($table,$field){
|
||||
$table = $table ? $this->getTablename($table,true) : $this->table;
|
||||
public function delField($table, $field) {
|
||||
$table = $table ? $this->getTablename($table, true) : $this->table;
|
||||
$this->sql = "ALTER TABLE `$table` DROP `$field`";
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除数据表
|
||||
* @var $table 追加字段的表名
|
||||
* @var $table 追加字段的表名
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function del_table($table){
|
||||
$table = $table ? $this->getTablename($table,true) : $this->table;
|
||||
public function delTable($table) {
|
||||
$table = $table ? $this->getTablename($table, true) : $this->table;
|
||||
$this->sql = "DROP TABLE `$table`";
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 主键设置
|
||||
* @var $key 要被设置主键的字段
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function create_key($key = null){
|
||||
if(null != $key){
|
||||
$this->key = $key;
|
||||
}
|
||||
$this->sql .= "PRIMARY KEY (`".$this->key."`)";
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 结束表
|
||||
* @var $engine_type 数据库引擎
|
||||
@@ -159,15 +155,15 @@ class Datatable{
|
||||
* @var $charset 数据库编码
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function end_table($comment,$engine_type = null,$charset = null){
|
||||
if(null != $charset){
|
||||
public function endTable($comment, $engine_type = null, $charset = null) {
|
||||
if (null != $charset) {
|
||||
$this->charset = $charset;
|
||||
}
|
||||
if(null != $engine_type){
|
||||
if (null != $engine_type) {
|
||||
$this->engine_type = $engine_type;
|
||||
}
|
||||
$end = "ENGINE=".$this->engine_type." AUTO_INCREMENT=1 DEFAULT CHARSET=".$this->charset." ROW_FORMAT=DYNAMIC COMMENT='".$comment."';";
|
||||
$this->sql .= ")".$end;
|
||||
$end = "ENGINE=" . $this->engine_type . " AUTO_INCREMENT=1 DEFAULT CHARSET=" . $this->charset . " ROW_FORMAT=DYNAMIC COMMENT='" . $comment . "';";
|
||||
$this->sql .= ")" . $end;
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -176,7 +172,7 @@ class Datatable{
|
||||
* @return int 0
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function create(){
|
||||
public function create() {
|
||||
$res = Db::execute($this->sql);
|
||||
return $res !== false;
|
||||
}
|
||||
@@ -186,7 +182,7 @@ class Datatable{
|
||||
* @return int 0
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function query(){
|
||||
public function query() {
|
||||
return $this->create();
|
||||
}
|
||||
|
||||
@@ -194,7 +190,7 @@ class Datatable{
|
||||
* 获取最后生成的sql语句
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function getLastSql(){
|
||||
public function getLastSql() {
|
||||
return $this->sql;
|
||||
}
|
||||
|
||||
@@ -204,11 +200,11 @@ class Datatable{
|
||||
* @var $prefix 获取表前缀? 默认为不获取 false
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function getTablename($table , $prefix = false){
|
||||
if(false == $prefix){
|
||||
$this->table = $this->model_table_prefix.$table;
|
||||
}else{
|
||||
$this->table = $this->prefix.$this->model_table_prefix.$table;
|
||||
public function getTablename($table, $prefix = false) {
|
||||
if (false == $prefix) {
|
||||
$this->table = $this->model_table_prefix . $table;
|
||||
} else {
|
||||
$this->table = $this->prefix . $this->model_table_prefix . $table;
|
||||
}
|
||||
return $this->table;
|
||||
}
|
||||
@@ -218,23 +214,23 @@ class Datatable{
|
||||
* @var $table 要获取名字的表名 可以为sent_tengsu_photo、tengsu_photo、photo
|
||||
* @author colin <colin@tensent.cn>
|
||||
*/
|
||||
public function getFields($table){
|
||||
if(false == $table){
|
||||
$table = $this->table;//为空调用当前table
|
||||
}else{
|
||||
public function getFields($table) {
|
||||
if (false == $table) {
|
||||
$table = $this->table; //为空调用当前table
|
||||
} else {
|
||||
$table = $table;
|
||||
}
|
||||
$patten = "/\./";
|
||||
if(!preg_match_all($patten,$table)){
|
||||
if (!preg_match_all($patten, $table)) {
|
||||
//匹配_
|
||||
$patten = "/_+/";
|
||||
if(!preg_match_all($patten, $table)){
|
||||
$table = $this->prefix.$this->model_table_prefix.$table;
|
||||
}else{
|
||||
if (!preg_match_all($patten, $table)) {
|
||||
$table = $this->prefix . $this->model_table_prefix . $table;
|
||||
} else {
|
||||
//匹配是否包含表前缀,如果是 那么就是手动输入
|
||||
$patten = "/$this->prefix/";
|
||||
if(!preg_match_all($patten,$table)){
|
||||
$table = $this->prefix.$table;
|
||||
if (!preg_match_all($patten, $table)) {
|
||||
$table = $this->prefix . $table;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -248,10 +244,10 @@ class Datatable{
|
||||
* @author colin <colin@tensent.cn>
|
||||
* @return boolen
|
||||
*/
|
||||
public function CheckTable($table){
|
||||
public function CheckTable($table) {
|
||||
//获取表名
|
||||
$this->table = $this->getTablename($table,true);
|
||||
$result = Db::execute("SHOW TABLES LIKE '%$this->table%'");
|
||||
$this->table = $this->getTablename($table, true);
|
||||
$result = Db::execute("SHOW TABLES LIKE '%$this->table%'");
|
||||
return $result;
|
||||
}
|
||||
|
||||
@@ -262,12 +258,12 @@ class Datatable{
|
||||
* @author colin <colin@tensent.cn>
|
||||
* @return boolen
|
||||
*/
|
||||
public function CheckField($table,$field){
|
||||
public function CheckField($table, $field) {
|
||||
//检查字段是否存在
|
||||
$table = $this->getTablename($table,true);
|
||||
if(!Db::query("Describe $table $field")){
|
||||
$table = $this->getTablename($table, true);
|
||||
if (!Db::query("Describe $table $field")) {
|
||||
return false;
|
||||
}else{
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user