Ver Fonte

Update: database deal

Jinhui Zhu há 4 anos atrás
pai
commit
ba6881a9c7
2 ficheiros alterados com 31 adições e 12 exclusões
  1. 18 1
      src/Driver/Base.php
  2. 13 11
      src/Driver/traitDatabase.php

+ 18 - 1
src/Driver/Base.php

@@ -40,7 +40,7 @@ class Base
     protected $_query = array(
         "INSERT" => "INSERT INTO %s(%s) VALUES('%s')",
         "REPLACE" => "REPLACE %s (%s) VALUES('%s')",
-        "SELECT" => "SELECT %s FROM `%s` %s",
+        "SELECT" => "SELECT %s FROM %s %s",
         "UPDATE" => "UPDATE %s SET ",
         "DELETE" => "DELETE FROM %s %s",
         "WHERE" => " WHERE %s",
@@ -830,12 +830,29 @@ class Base
         //去掉table前后的``符号
         $name = str_replace('`', '', $name);
         $aliases = explode(' ', $name);
+        //检查表名中时候含数据库名,有数据表名的时候需要单独处理
+        $hasDatabaseName = false;
+        if(stristr($name, '.')) {
+            $hasDatabaseName = true;
+        }
         if(count($aliases) == 1) {
+            if($hasDatabaseName) {
+                $names = explode(".", $name);
+                $name = $names[0] . ".`". $names[1] . "`";
+            }else{
+                $name = "`". $name ."`";
+            }
             return array('alias' => '', 'name' => $name);
         }
         $res = array();
         $res['alias'] = array_pop($aliases);
         $res['name'] = join(" ", array_slice($aliases, -1));
+        if($hasDatabaseName) {
+            $names = explode(".", $res['name']);
+            $res['name'] = $names[0] . ".`". $names[1] . "`";
+        }else{
+            $res['name'] = "`". $res['name'] ."`";
+        }
         return $res;
     }
     /**

+ 13 - 11
src/Driver/traitDatabase.php

@@ -73,11 +73,13 @@ trait traitDatabase
     {
         if (!$database) $database = $this->currentDB;
         $sql = "SELECT * from information_schema.COLUMNS where table_name = '" . $table . "' and table_schema = '" . $database . "'";
-        $data = ['fields' => [],
-            'rules' => [
-                'pri' => [], 'required' => []
-            ]
-        ];
+        $data = array(
+            'fields' => array(),
+            'rules' => array(
+                'pri' => array(),
+                'required' => array()
+            )
+        );
 
         $rs = $this->setQuery($sql);
         while ($row = $rs->fetch()) {
@@ -92,24 +94,24 @@ trait traitDatabase
             if ($row['IS_NULLABLE'] == 'NO') {
                 $data['rules']['required'][] = $row['COLUMN_NAME'];
             }
-            if (in_array($row['DATA_TYPE'], ['varchar', 'char'])) {
+            if (in_array($row['DATA_TYPE'], array('varchar', 'char'))) {
                 $data['rules']['maxlength'][$row['COLUMN_NAME']] = $row['CHARACTER_MAXIMUM_LENGTH'];
             }
-            if (in_array($row['DATA_TYPE'], ['mediumtext', 'TINYTEXT', 'text', 'longtext'])) {
+            if (in_array($row['DATA_TYPE'], array('mediumtext', 'TINYTEXT', 'text', 'longtext'))) {
                 $data['rules']['text'][] = $row['COLUMN_NAME'];
             }
-            if (in_array($row['DATA_TYPE'], ['bigint', 'int', 'smallint', 'tinyint', 'integer'])) {
+            if (in_array($row['DATA_TYPE'], array('bigint', 'int', 'smallint', 'tinyint', 'integer'))) {
                 preg_match('/[\d]{1,}/', $row['COLUMN_TYPE'], $matches);
                 $data['rules']['int'][$row['COLUMN_NAME']] = $matches[0];
                 $data['rules']['number'][] = $row['COLUMN_NAME'];
             }
-            if (in_array($row['DATA_TYPE'], ['float', 'double', 'decimal'])) {
+            if (in_array($row['DATA_TYPE'], array('float', 'double', 'decimal'))) {
                 $data['rules']['float'][$row['COLUMN_NAME']] = $this->getValueFromBrackets($row['COLUMN_TYPE']);
             }
-            if (in_array($row['DATA_TYPE'], ['timestamp', 'datatime'])) {
+            if (in_array($row['DATA_TYPE'], array('timestamp', 'datatime'))) {
                 $data['rules']['timestamp'][] = $row['COLUMN_NAME'];
             }
-            if (in_array($row['DATA_TYPE'], ['enum', 'set'])) {
+            if (in_array($row['DATA_TYPE'], array('enum', 'set'))) {
                 $data['rules']['sets'][$row['COLUMN_NAME']] = $this->getValueFromBrackets($row['COLUMN_TYPE']);
             }
             if (isset($row['COLUMN_DEFAULT'])) {