Browse Source

新增设置唯一键方法 、 修改table信息返回内容

朱金辉 3 weeks ago
parent
commit
7f69e6cf7a
3 changed files with 35 additions and 2 deletions
  1. 7 2
      src/Driver/Entity/Base.php
  2. 2 0
      src/Driver/Entity/Entity.php
  3. 26 0
      src/Driver/TraitDatabase.php

+ 7 - 2
src/Driver/Entity/Base.php

@@ -403,7 +403,7 @@ class Base {
      * @return void
      */
     public function setUniqKeys($uniqKeys) {
-        $this->uniqKeys = $uniqKeys;
+        self::$uniqKeys = $uniqKeys;
     }
     /**
      * unique (unique 如果是 array 则表示 联合唯一,如果是string则是任意唯一,多个单独唯一使用字符串以逗号隔开, 主键除外)
@@ -784,7 +784,12 @@ class Base {
      */
     public function remove() {
         if(!$this->properties()) {
-            return false;
+            return Response::Fail(static::class .'::'. __FUNCTION__,
+            [
+                '_result' => ['code' => Response::FAIL_FOR_VALIDATE, 'msg' => '未指定删除目标', 'body' => []],
+                'message' => '未指定删除目标'
+            ]
+            );
         }
         $properties = [];
         foreach ($this->properties() as $key => $val) {

+ 2 - 0
src/Driver/Entity/Entity.php

@@ -281,6 +281,7 @@ DOC;
      * @throws \Exception
      */
     public function uniqueKey(){
+        if(self::\$uniqKeys !== null) return self::\$uniqKeys;
         throw new \Exception('请设置唯一值');
     }
 
@@ -308,6 +309,7 @@ DOC;
      * @return array
      */
     public function exclude(){
+        if(self::\$exclude !== null) return self::\$exclude;
         return array();
     }
     /**

+ 26 - 0
src/Driver/TraitDatabase.php

@@ -80,6 +80,7 @@ trait TraitDatabase
         $data = array(
             'table' => $table,
             'fields' => array(),
+            'fields_type' => array(),
             'rules' => array(
                 'pri' => array(),
                 'required' => array()
@@ -98,20 +99,37 @@ trait TraitDatabase
         while ($row = $rs->fetch()) {
             $data['fields'][] = $row['COLUMN_NAME'];
             $data['type'][$row['DATA_TYPE']][] = $row['COLUMN_NAME'];
+            $fieldsType = array();
+            $fieldsType['type'] = strtolower($row['DATA_TYPE']);
+            $fieldsType['is_pri'] = false;
+            $fieldsType['is_uni'] = false;
+            $fieldsType['is_null'] = true;
+            $fieldsType['is_number'] = false;
+            $fieldsType['is_float'] = false;
+            $fieldsType['is_decimal'] = false;
+            $fieldsType['is_timestamp'] = false;
+            $fieldsType['is_set'] = false;
+            $fieldsType['default'] = false;
+            $fieldsType['comment'] = '';
             if ($row['EXTRA']) {
                 $data['rules']['extra'][$row['EXTRA']][] = $row['COLUMN_NAME'];
+                $fieldsType['extra'] = $row['EXTRA'];
             }
             if ($row['COLUMN_KEY'] == 'PRI') {
                 $data['rules']['pri'][] = $row['COLUMN_NAME'];
                 $data['rules']['required'][] = $row['COLUMN_NAME'];
+                $fieldsType['is_pri'] = true;
             } else if($row['COLUMN_KEY'] == 'UNI'){
                 //$data['rules']['uni'][] = $row['COLUMN_NAME'];
                 $data['rules']['required'][] = $row['COLUMN_NAME'];
+                $fieldsType['is_uni'] = true;
             }else if ($row['IS_NULLABLE'] == 'NO') {
                 $data['rules']['required'][] = $row['COLUMN_NAME'];
+                $fieldsType['is_null'] = false;
             }
             if (in_array($row['DATA_TYPE'], array('varchar', 'char'))) {
                 $data['rules']['maxlength'][$row['COLUMN_NAME']] = $row['CHARACTER_MAXIMUM_LENGTH'];
+                $fieldsType['maxlength'] = $row['CHARACTER_MAXIMUM_LENGTH'];
             }
             if (in_array($row['DATA_TYPE'], array('mediumtext', 'tinytext', 'text', 'longtext'))) {
                 $data['rules']['text'][] = $row['COLUMN_NAME'];
@@ -121,26 +139,34 @@ trait TraitDatabase
                 if(!empty($matches)) {
                     $data['rules']['int'][$row['COLUMN_NAME']] = isset($matches[0]) && $matches[0] ? $matches[0] : $maxRange[$row['DATA_TYPE']];
                     $data['rules']['number'][] = $row['COLUMN_NAME'];
+                    $fieldsType['is_number'] = true;
                 }
             }
             if (in_array($row['DATA_TYPE'], array('float', 'double'))) {
                 $data['rules']['float'][$row['COLUMN_NAME']] = $this->getValueFromBrackets($row['COLUMN_TYPE']);
+                $fieldsType['is_float'] = true;
             }
             if (in_array($row['DATA_TYPE'], array('decimal'))) {
                 $data['rules']['decimal'][$row['COLUMN_NAME']] = $this->getValueFromBrackets($row['COLUMN_TYPE']);
+                $fieldsType['is_decimal'] = true;
             }
             if (in_array($row['DATA_TYPE'], array('timestamp', 'datetime'))) {
                 $data['rules']['timestamp'][] = $row['COLUMN_NAME'];
+                $fieldsType['is_timestamp'] = true;
             }
             if (in_array($row['DATA_TYPE'], array('enum', 'set'))) {
                 $data['rules']['sets'][$row['COLUMN_NAME']] = $this->getValueFromBrackets($row['COLUMN_TYPE']);
+                $fieldsType['is_set'] = true;
             }
             if (isset($row['COLUMN_DEFAULT'])) {
                 $data['rules']['default'][$row['COLUMN_NAME']] = $row['COLUMN_DEFAULT'];
+                $fieldsType['default'] = $row['COLUMN_DEFAULT'];
             }
             if(isset($row['COLUMN_COMMENT'])) {
                 $data['comment'][$row['COLUMN_NAME']] = $row['COLUMN_COMMENT'];
+                $fieldsType['comment'] = $row['COLUMN_COMMENT'];
             }
+            $data['fields_type'][$row['COLUMN_NAME']] = $fieldsType;
         }
 
         $sql = "SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE table_name = '" . $table . "'";