ThinkPHP 数据库表结构处理类(简单实用)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/*
  * mysql表结构处理类
  * 创建数据表,增加,编辑,删除表中字段
  *
  */
class  MysqlManage{
     /*
      * 创建数据库,并且主键是aid
      * table 要查询的表名
      */
     function  createTable( $table ){
         $sql = "CREATE TABLE IF NOT EXISTS `$table` (`aid` INT NOT NULL primary key)ENGINE = InnoDB;" ;
         M()->execute( $sql );
         $this ->checkTable( $table );
     }
     /*
      * 检测表是否存在,也可以获取表中所有字段的信息
      * table 要查询的表名
      * return 表里所有字段的信息
      */
     function  checkTable( $table ){
         $sql = "desc `$table`" ;
         $info =M()->execute( $sql );
         return  $info ;
     }
 
     /*
      * 检测字段是否存在,也可以获取字段信息(只能是一个字段)
      * table 表名
      * field 字段名
      */
     function  checkField( $table , $field ){
         $sql = 'desc `$table` $field' ;
         $info =M()->execute( $sql );
         return  $info ;
     }
 
     /*
      * 添加字段
      * table 表名
      * info  字段信息数组 array
      * return 字段信息 array
      */
     function  addField( $table , $info ){
         $sql = "alter table `$table` add " ;
         $sql .= $this ->filterFieldInfo();
         M()->execute( $sql );
         $this ->checkField( $table , $info [ 'name' ]);
     }
 
     /*
      * 修改字段
      * 不能修改字段名称,只能修改
      */
     function  editField( $table , $info ){
         $sql = "alter table `$table` modify " ;
         $sql .= $this ->filterFieldInfo( $info );
         M()->execute( $sql );
         $this ->checkField( $table , $info [ 'name' ]);
     }
 
     /*
      * 字段信息数组处理,供添加更新字段时候使用
      * info[name]   字段名称
      * info[type]   字段类型
      * info[length]  字段长度
      * info[isNull]  是否为空
      * info['default']   字段默认值
      * info['comment']   字段备注
      */
     private  function  filterFieldInfo( $info ){
         if (! is_array ( $info ))
             return
         $newInfo = array ();
         $newInfo [ 'name' ]= $info [ 'name' ];
         $newInfo [ 'type' ]= $info [ 'type' ];
         switch ( $info [ 'type' ]){
             case  'varchar' :
             case  'char' :
                 $newInfo [ 'length' ]= empty ( $info [ 'length' ])?100: $info [ 'length' ];
                 $newInfo [ 'isNull' ]= $info [ 'isNull' ]==1? 'NULL' : 'NOT NULL' ;
                 $newInfo [ 'default' ]= empty ( $info [ 'default' ])? '' : 'DEFAULT ' . $info [ 'default' ];
                 $newInfo [ 'comment' ]= empty ( $info [ 'comment' ])? '' : 'COMMENT ' . $info [ 'comment' ];
             case  'int' :
                 $newInfo [ 'length' ]= empty ( $info [ 'length' ])?7: $info [ 'length' ];
                 $newInfo [ 'isNull' ]= $info [ 'isNull' ]==1? 'NULL' : 'NOT NULL' ;
                 $newInfo [ 'default' ]= empty ( $info [ 'default' ])? '' : 'DEFAULT ' . $info [ 'default' ];
                 $newInfo [ 'comment' ]= empty ( $info [ 'comment' ])? '' : 'COMMENT ' . $info [ 'comment' ];
             case  'text' :
                 $newInfo [ 'length' ]= '' ;
                 $newInfo [ 'isNull' ]= $info [ 'isNull' ]==1? 'NULL' : 'NOT NULL' ;
                 $newInfo [ 'default' ]= '' ;
                 $newInfo [ 'comment' ]= empty ( $info [ 'comment' ])? '' : 'COMMENT ' . $info [ 'comment' ];
         }
         $sql = $newInfo [ 'name' ]. ' ' . $newInfo [ 'type' ];
         $sql .=(! empty ( $newInfo [ 'length' ]))?( $newInfo [ 'length' ]). " " : ' ' ;
         $sql .= $newInfo [ 'isNull' ]. '' ;
         $sql .= $newInfo [ 'default' ];
         $sql .= $newInfo [ 'comment' ];
         return  $sql ;
     }
 
     /*
      * 删除字段
      * 如果返回了字段信息则说明删除失败,返回false,则为删除成功
      */
     function  dropField( $table , $field ){
         $sql = "alter table `$table` drop column $field" ;
         M()->execute( $sql );
         $this ->checkField( $table , $filed );
     }
 
     /*
      * 获取指定表中指定字段的信息(多字段)
      */
     function  getFieldInfo( $table , $field ){
         $info = array ();
         if ( is_string ( $field )){
             $this ->checkField( $table , $field );
         } else {
             foreach ( $field  as  $v ){
                 $info [ $v ]= $this ->checkField( $table , $v );
             }
         }
         return  $info ;
     }
}

    好久没有写博客了,最近忙的要死,搞微信平台,偶尔遇到需要模型管理,前台表单直接修改表结构的,就自己简单写了一下,也不是很难,给大家一个思路,看不懂了可以联系我










本文转自 3147972 51CTO博客,原文链接:http://blog.51cto.com/a3147972/1543179,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值