瀏覽代碼

Fixed : join bugs修复

zjh 1 月之前
父節點
當前提交
542588f2c5
共有 5 個文件被更改,包括 103 次插入9 次删除
  1. 1 0
      src/Base/Request.php
  2. 4 0
      src/Config/Register.php
  3. 18 4
      src/Driver/Base.php
  4. 71 3
      src/Driver/Entity/Base.php
  5. 9 2
      src/Driver/Traits/SQL.php

+ 1 - 0
src/Base/Request.php

@@ -644,6 +644,7 @@ abstract class Request
         } elseif ($requestUri && is_string($requestUri)) {
             $scriptFileName = $this->getServer('SCRIPT_FILENAME');
 
+            $basename = null;
             do {
                 if ($scriptFileName && is_string($scriptFileName)) {
                     $suffer = (string) \Qii::getInstance()->appConfigure('ext', '.php');

+ 4 - 0
src/Config/Register.php

@@ -292,6 +292,10 @@ class Register
      */
     public static function array_merge_recursive_distinct(array &$array1, array &$array2)
     {
+        if(!is_array($array2) && !is_array($array2)) return [];
+        if(!is_array($array1)) return $array2;
+        if(!is_array($array2)) return $array1;
+
         $merged = $array1;
         foreach ($array2 as $key => &$value)
         {

+ 18 - 4
src/Driver/Base.php

@@ -110,7 +110,10 @@ class Base
             $this->where($where);
         }
         $sql = $this->selectSQL($table);
-        return $this->getAll($sql);
+        $this->startTime($sql);
+        $all = $this->getAll($sql);
+        $this->endTime($sql);
+        return $all;
     }
     /**
      * 返回resource资源
@@ -119,7 +122,10 @@ class Base
     final function rs($table)
     {
         $sql = $this->selectSQL($table);
-        return $this->setQuery($sql);
+        $this->startTime($sql);
+        $rs = $this->setQuery($sql);
+        $this->endTime($sql);
+        return $rs;
     }
     /**
      * 查询一行
@@ -135,7 +141,10 @@ class Base
             $this->where($where);
         }
         $sql = $this->selectSQL($table);
-        return $this->getRow($sql);
+        $this->startTime($sql);
+        $row = $this->getRow($sql);
+        $this->endTime($sql);
+        return $row;
     }
 
     /**
@@ -164,7 +173,10 @@ class Base
             throw new TableException("表名必须是字符串加下划线,目标字符为". gettype($table));
         }
         $sql = $this->selectSQL($table);
-        return $this->getOne($sql);
+        $this->startTime($sql);
+        $one = $this->getOne($sql);
+        $this->endTime($sql);
+        return $one;
     }
 
     /**
@@ -200,8 +212,10 @@ class Base
     final function replaceObject($table, $dataArray)
     {
         $sql = $this->replaceSQL($table, $dataArray);
+        $this->startTime($sql);
         $rs = $this->setQuery($sql);
         $this->setError();
+        $this->endTime($sql);
         return $this->AffectedRows($rs);
     }
     /**

+ 71 - 3
src/Driver/Entity/Base.php

@@ -259,6 +259,14 @@ class Base {
     public function getWhereHooker() {
         $where = $this->properties();
         if(!isset($this->hooker['where']) || !is_array($this->hooker['where'])) {
+            if($this->alias != "" && is_array($where)) {
+                foreach ($where as $key => $value) {
+                    if(!preg_match("/^[a-zA-Z_]\./", $key)) {
+                        $where[$this->alias . '.' . $key] = $value;
+                        unset($where[$key]);
+                    }
+                }
+            }
             return $where;
         }
         // 遍历
@@ -266,10 +274,29 @@ class Base {
             if(is_callable($item['func'])) {
                 $subWhere = call_user_func($item['func'], $where, $item['args']);
                 foreach ($subWhere as $key => $value) {
+                    if(is_array($value)) {
+                        foreach ($value as $k => $v) {
+                            if($this->alias != "") {
+                                if(!preg_match("/^[a-zA-Z_]\./", $k)) {
+                                    $where[$this->alias . '.' . $k] = $v;
+                                    unset($where[$k]);
+                                }else{
+                                    $where[$k] = $v;
+                                }
+                            }else{
+                                $where[$key] = $v;
+                            }
+                        }
+                        unset($subWhere[$key]);
+                        continue;
+                    }
                     if($this->alias != "") {
-                        if(!preg_match("/{$this->alias}\.{$key}/", $key)) {
+                        if(!preg_match("/^[a-zA-Z_]\./", $key)) {
                             $where[$this->alias . '.' . $key] = $value;
                             unset($subWhere[$key]);
+                            unset($where[$key]);
+                        }else{
+                            $where[$key] = $value;
                         }
                     }else{
                         $where[$key] = $value;
@@ -281,7 +308,7 @@ class Base {
     }
 
     /**
-     * join 条件
+     * join 条件 [table, on, join type]
      * @param mixed $join 条件
      * @param mixed $args 参数
      * @return $this
@@ -977,7 +1004,19 @@ class Base {
      */
     public function info() {
         try{
+            $limit = $this->getLimitHooker();
+            $page = null;
+            $pageSize = null;
+            if(count($limit) > 0) {
+                $page = $limit[0];
+                $pageSize = isset($limit[1]) ? $limit[1] : null;
+            }
             $query = $this->createQuery();
+            $query = $query->groupBy($this->getGroupBy());
+            $query = $query->orderBy($this->getOrderBy());
+            if($page !== null) {
+                $query = $query->limit($page, $pageSize);
+            }
             $info = $query->selectRow($this->prepareTable());
             $this->calledSQL = $query->executeSQL;
             if($info) $this->withRow($info);
@@ -1453,10 +1492,35 @@ class Base {
         }
         $join = $this->getJoinHooker();
         foreach ($join as $j) {
-            $query = $query->join($j);
+            if(count($j) != count($j, 1)) {
+                foreach ($j as $v) {
+                    $v = $this->formatJoin($v);
+                    $query = $query->join($v[0], $v[1], $v[2]);
+                }
+            }else{
+                $j = $this->formatJoin($j);
+                $query = $query->join($j[0], $j[1], $j[2]);
+            }
         }
         return $query;
     }
+
+    /**
+     * 格式化 join 参数
+     * @param $v
+     * @return array
+     * @throws InvalidParams
+     */
+    protected function formatJoin($v){
+        if(!is_array($v) || count($v) < 2) {
+            throw new InvalidParams('jon 参数不正确 [table, on, join type]');
+        }
+        if(count($v) < 3) {
+            $v[2] = null;
+        }
+
+        return $v;
+    }
     /**
      * 获取第一条数据
      *
@@ -1692,6 +1756,10 @@ class Base {
         if(count($this->getFields()) > 0 && !in_array($relKey, $this->getFields())) {
             $fields[] = $relKey;
         }
+
+        if(array_search('*', $fields) !== false) {
+            $fields = ['*'];
+        }
         $fields = array_unique($fields);
         $query = $this->createQuery($fields);
         $res = $query->where($args[1])->rs($this->prepareTable());

+ 9 - 2
src/Driver/Traits/SQL.php

@@ -230,6 +230,9 @@ trait SQL
             return $word;
         }
         $this->handleError(in_array(gettype($word), array("object", "resource","resource (closed)")), '期待参数为数组或字符串,获取到的是:'. gettype($word)."(". json_encode($word) .")");
+        if(is_array($word) && count($word) !=  count($word, 1)) {
+            return $word;
+        }
         return is_array($word) ? array_map('addslashes', $word) : addslashes($word);
     }
 
@@ -493,7 +496,7 @@ trait SQL
      */
     final public function join($table, $on, $joinType = JOINTYPE['LEFTJOIN']){
         $arr = [];
-        if(!in_array($joinType, [
+        if(!preg_match("/{$joinType}/i", join(',', [
             JOINTYPE['LEFTJOIN'],
             JOINTYPE['RIGHTJOIN'],
             JOINTYPE['INNERJOIN'],
@@ -501,7 +504,11 @@ trait SQL
             JOINTYPE['FULLOUTERJOIN'],
             JOINTYPE['LEFTOUTERJOIN'],
             JOINTYPE['RIGHTOUTERJOIN']
-        ]))
+        ]))) {
+            $joinType = JOINTYPE['LEFTJOIN'];
+            throw  new InvalidParams('join 仅支持这几种["'. join('", "', JOINTYPE) . '"]');
+        }
+
         if(is_array($on)) {
             foreach($on as $key => $val) {
                 $arr[] = $key .' = '. $val;