浏览代码

Update: 数据库连接字符集默认改为utf8mb4,新增between and查询

zjh 4 月之前
父节点
当前提交
5125409f66

+ 14 - 2
src/Driver/Base.php

@@ -82,7 +82,8 @@ class Base
         'less' => "%s`%s` < '%s'",
         'less' => "%s`%s` < '%s'",
         'lessEqual' => "%s`%s` <= '%s'",
         'lessEqual' => "%s`%s` <= '%s'",
         'lte' => "%s`%s` <= '%s'",
         'lte' => "%s`%s` <= '%s'",
-        'like' => "%s`%s` like '%%%s%%'"
+        'like' => "%s`%s` like '%%%s%%'",
+        'between' => "%s`%s` between %s",
     );
     );
 
 
     public $operateTable = array('leftJoin', 'rightJoin', 'innerJoin');//链接表的操作
     public $operateTable = array('leftJoin', 'rightJoin', 'innerJoin');//链接表的操作
@@ -1100,6 +1101,12 @@ class Base
                 $tmpWhere[$index] = $val;
                 $tmpWhere[$index] = $val;
                 continue;
                 continue;
             }
             }
+            if(stristr($index, ':between') !== false) {
+                if(is_array($val)) {
+                    $val = $this->setQuote($val);
+                    $val = "'". join("' and '", $val) ."'";
+                }
+            }
             if(is_array($val)) {
             if(is_array($val)) {
                 // group array 的情况,每组条件之间加 and
                 // group array 的情况,每组条件之间加 and
                 $slices[] = $this->groupContents($val)[0];
                 $slices[] = $this->groupContents($val)[0];
@@ -1250,7 +1257,12 @@ class Base
                 if(!isset($operator[$opt])) {
                 if(!isset($operator[$opt])) {
                     throw new \Exception("Unknow operator " . $opt, __LINE__);
                     throw new \Exception("Unknow operator " . $opt, __LINE__);
                 }
                 }
-                $where[] = sprintf($operator[$opt], $alias, $name, $this->setQuote($val));
+                // between and 不做转换
+                $val = $val;
+                if($opt != 'between') {
+                    $val = $this->setQuote($val);
+                }
+                $where[] = sprintf($operator[$opt], $alias, $name, $val);
             }
             }
             $lastIsValue = $isValue;
             $lastIsValue = $isValue;
             $lastIsOperator = $isOperator;
             $lastIsOperator = $isOperator;

+ 1 - 1
src/Driver/ConnBase.php

@@ -81,7 +81,7 @@ class ConnBase
         if(get_called_class() == 'Qii\Driver\Pdo\Connection'){
         if(get_called_class() == 'Qii\Driver\Pdo\Connection'){
             $connection->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
             $connection->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
             $connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
             $connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
-            $connection->query('set names utf8');
+            $connection->query('set names utf8mb4');
         }
         }
         return $connection;
         return $connection;
 	}
 	}

+ 61 - 15
src/Driver/Entity/Base.php

@@ -1,8 +1,5 @@
 <?php
 <?php
 namespace Qii\Driver\Entity;
 namespace Qii\Driver\Entity;
-
-
-
 use Qii\Driver\Model;
 use Qii\Driver\Model;
 use Qii\Driver\Response;
 use Qii\Driver\Response;
 use Qii\Exceptions\InvalidParams;
 use Qii\Exceptions\InvalidParams;
@@ -321,6 +318,39 @@ class Base {
         }
         }
         return $join;
         return $join;
     }
     }
+
+    /**
+     * 设置group
+     *
+     * @param array $group group对应的数组
+     * @param mixed $args
+     * @return void
+     */
+    final public function group($group, $args = null) {
+        if(!isset($this->hooker['group'])) {
+            $this->hooker['group'] = [];
+        }
+        $this->hooker['group'][] = ['func' => function ()use ($group) {
+            return $group;
+        }, 'args' => $args];
+
+        return $this;
+    }
+
+    /**
+     * get group hooker
+     * @return array
+     */
+    final public function getGroupBy() {
+        if(!isset($this->hooker['group']) || !is_array($this->hooker['group'])) {
+            return [];
+        }
+        $group = [];
+        foreach ($this->hooker['group'] as $value) {
+            $group = array_merge(call_user_func($value['func'], [], $value['args']));
+        }
+        return $group;
+    }
     /**
     /**
      * order by
      * order by
      * @param array $order
      * @param array $order
@@ -711,6 +741,19 @@ class Base {
         throw new \Exception('请设置更新数据验证的字段,格式如:["Id", "Title"],Id和Title为entity的属性');
         throw new \Exception('请设置更新数据验证的字段,格式如:["Id", "Title"],Id和Title为entity的属性');
     }
     }
 
 
+
+    /**
+     * 更新数据验证字段
+     *
+     * $fields = [];
+     * return $this->valid($fields);
+     *
+     * @return mixed
+     * @throws \Exception
+     */
+    public function validFieldsForReplace() {
+        throw new \Exception('请设置更新数据验证的字段,格式如:["Id", "Title"],Id和Title为entity的属性');
+    }
     /**
     /**
      * 设置需要更新为空的字段列表
      * 设置需要更新为空的字段列表
      *
      *
@@ -785,6 +828,7 @@ class Base {
     public function count() {
     public function count() {
         $query = $this->createQuery();
         $query = $this->createQuery();
         $query->fields(' COUNT(1) as count');
         $query->fields(' COUNT(1) as count');
+        $query->groupBy($this->getGroupBy());
         return $query->selectOne($this->prepareTable());
         return $query->selectOne($this->prepareTable());
     }
     }
     /**
     /**
@@ -1026,7 +1070,7 @@ class Base {
      * @throws InvalidParams
      * @throws InvalidParams
      */
      */
     final public function replace(){
     final public function replace(){
-        $valid = $this->validFieldsForAdd();
+        $valid = $this->validFieldsForReplace();
         if($valid->isError()) {
         if($valid->isError()) {
             return $valid;
             return $valid;
         }
         }
@@ -1072,8 +1116,8 @@ class Base {
             if($exist) {
             if($exist) {
                 return Response::Exist(static::class .'::'. __FUNCTION__,
                 return Response::Exist(static::class .'::'. __FUNCTION__,
                     [
                     [
-                        '_result' => ['code' => Response::DOES_EXIST, 'msg' => '数据已经存在', 'body' => $exist],
-                        'message' => '数据已经存在'
+                        '_result' => ['code' => Response::DOES_EXIST, 'msg' => '数据已经存在(1)', 'body' => $exist],
+                        'message' => '数据已经存在(1)'
                     ]
                     ]
                 );
                 );
             }
             }
@@ -1084,8 +1128,8 @@ class Base {
             if($exist) {
             if($exist) {
                 return Response::Exist(static::class .'::'. __FUNCTION__,
                 return Response::Exist(static::class .'::'. __FUNCTION__,
                     [
                     [
-                        '_result' => ['code' => Response::DOES_EXIST, 'msg' => '数据已经存在', 'body' => $exist],
-                        'message' => '数据已存在'
+                        '_result' => ['code' => Response::DOES_EXIST, 'msg' => '数据已经存在(2)', 'body' => $exist],
+                        'message' => '数据已存在(2)'
                     ]
                     ]
                 );
                 );
             }
             }
@@ -1097,8 +1141,8 @@ class Base {
         if($this->db()->isError()) {
         if($this->db()->isError()) {
             return Response::FailSave(static::class .'::'. __FUNCTION__,
             return Response::FailSave(static::class .'::'. __FUNCTION__,
                 [
                 [
-                    '_result' => ['code' => Response::FAIL_FOR_SAVE, 'msg' => $this->db()->getMessage(), 'body' => []],
-                    'message' => $this->db()->getMessage()
+                    '_result' => ['code' => Response::FAIL_FOR_SAVE, 'msg' => $this->db()->getMessage() . "(3)", 'body' => []],
+                    'message' => $this->db()->getMessage() . "(3)"
                 ]);
                 ]);
         }
         }
         return Response::Success(static::class .'::'. __FUNCTION__,
         return Response::Success(static::class .'::'. __FUNCTION__,
@@ -1313,8 +1357,10 @@ class Base {
         unset($where, $or, $exclude);
         unset($where, $or, $exclude);
         $property = $this->properties();
         $property = $this->properties();
         $incr = [];
         $incr = [];
+        $sets = [];
         foreach ($property as $key => $value) {
         foreach ($property as $key => $value) {
             if(!is_numeric($value)) {
             if(!is_numeric($value)) {
+                $sets[$key] = $value;
                 continue;
                 continue;
             }
             }
             $incr[$key] = $value;
             $incr[$key] = $value;
@@ -1328,7 +1374,6 @@ class Base {
                 ]
                 ]
             );
             );
         }
         }
-        $sets = [];
         foreach ($diff as $key => $val) {
         foreach ($diff as $key => $val) {
             if($val == 0) {
             if($val == 0) {
                 $sets[$key] = $val;
                 $sets[$key] = $val;
@@ -1399,7 +1444,7 @@ class Base {
             $orderBy[$key] = 'ASC';
             $orderBy[$key] = 'ASC';
         }
         }
         $query = $this->createQuery();
         $query = $this->createQuery();
-        $row = $query->limit(1)->orderBy($orderBy)->selectRow($this->prepareTable());
+        $row = $query->limit(1)->groupBy($this->getGroupBy())->orderBy($orderBy)->selectRow($this->prepareTable());
         if($this->db()->isError()) {
         if($this->db()->isError()) {
             return Response::Fail(static::class .'::'. __FUNCTION__,
             return Response::Fail(static::class .'::'. __FUNCTION__,
                 [
                 [
@@ -1429,7 +1474,7 @@ class Base {
             $orderBy[$key] = 'DESC';
             $orderBy[$key] = 'DESC';
         }
         }
         $query = $this->createQuery();
         $query = $this->createQuery();
-        $row = $query->orderBy($orderBy)->limit(1)->selectRow($this->prepareTable());
+        $row = $query->groupBy($this->getGroupBy())->orderBy($orderBy)->limit(1)->selectRow($this->prepareTable());
         $this->calledSQL = $query->modelSQL;
         $this->calledSQL = $query->modelSQL;
         if($this->db()->isError()) {
         if($this->db()->isError()) {
             return Response::Fail(static::class .'::'. __FUNCTION__,
             return Response::Fail(static::class .'::'. __FUNCTION__,
@@ -1499,7 +1544,7 @@ class Base {
      * @throws \Qii\Exceptions\InvalidParams
      * @throws \Qii\Exceptions\InvalidParams
      */
      */
     final public function rs($page = null, $pageSize = null) {
     final public function rs($page = null, $pageSize = null) {
-        $query = $this->createQuery()->orderBy($this->getOrderBy());
+        $query = $this->createQuery()->groupBy($this->getGroupBy())->orderBy($this->getOrderBy());
         if($page && $pageSize) {
         if($page && $pageSize) {
             $start = ((max($page, 1)) - 1) * $pageSize;
             $start = ((max($page, 1)) - 1) * $pageSize;
             $query->limit($start, $pageSize);
             $query->limit($start, $pageSize);
@@ -1521,6 +1566,7 @@ class Base {
             $pageSize = isset($limit[1]) ? $limit[1] : null;
             $pageSize = isset($limit[1]) ? $limit[1] : null;
         }
         }
         $query = $this->createQuery();
         $query = $this->createQuery();
+        $query = $query->groupBy($this->getGroupBy());
         $query = $query->orderBy($this->getOrderBy());
         $query = $query->orderBy($this->getOrderBy());
         if($page !== null) {
         if($page !== null) {
             $query = $query->limit($page, $pageSize);
             $query = $query->limit($page, $pageSize);
@@ -1672,7 +1718,7 @@ class Base {
      */
      */
     final public function listAll() {
     final public function listAll() {
         $limit = $this->getLimitHooker();
         $limit = $this->getLimitHooker();
-        $query = $this->createQuery()->orderBy($this->getOrderBy());
+        $query = $this->createQuery()->groupBy($this->getGroupBy())->orderBy($this->getOrderBy());
         if(empty($limit) || !is_array($limit)) {
         if(empty($limit) || !is_array($limit)) {
             $list =  $query
             $list =  $query
                 ->selectRows($this->prepareTable());
                 ->selectRows($this->prepareTable());

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

@@ -383,6 +383,13 @@ DOC;
         return \$this->valid(\$fields);
         return \$this->valid(\$fields);
     }
     }
     
     
+    /**
+     * 添加时验证的字段,自行添加
+     */
+    public function validFieldsForReplace(){
+        \$fields = [];
+        return \$this->valid(\$fields);
+    }
     /**
     /**
      * 验证更新时的字段,自行添加
      * 验证更新时的字段,自行添加
      */
      */

+ 2 - 2
src/Driver/Mysql/Driver.php

@@ -62,7 +62,7 @@ class Driver extends Base implements Intf
 	/**
 	/**
 	 * @var string $charset 数据库默认编码
 	 * @var string $charset 数据库默认编码
 	 */
 	 */
-	public $charset = 'UTF8';
+	public $charset = 'utf8mb4';
 	/**
 	/**
 	 * @var array $currentDB 当前数据库信息
 	 * @var array $currentDB 当前数据库信息
 	 */
 	 */
@@ -113,7 +113,7 @@ class Driver extends Base implements Intf
 		if (!empty($this->sysConfigure['charset'])) {
 		if (!empty($this->sysConfigure['charset'])) {
 			\mysql_query($this->db['CURRENT'], "SET CHARACTER SET {$this->sysConfigure['charset']}");
 			\mysql_query($this->db['CURRENT'], "SET CHARACTER SET {$this->sysConfigure['charset']}");
 		} else {
 		} else {
-			\mysql_query($this->db['CURRENT'], "SET CHARACTER SET UTF8");
+			\mysql_query($this->db['CURRENT'], "SET CHARACTER SET utf8mb4");
 		}
 		}
 
 
 		$this->rs = $rs = \mysql_query($this->db['CURRENT'], $sql);
 		$this->rs = $rs = \mysql_query($this->db['CURRENT'], $sql);

+ 1 - 1
src/Driver/Mysqli/Driver.php

@@ -118,7 +118,7 @@ class Driver extends Base implements Intf
 		if (!empty($this->sysConfigure['charset'])) {
 		if (!empty($this->sysConfigure['charset'])) {
 			\mysqli_query($this->db['CURRENT'], "SET CHARACTER SET {$this->sysConfigure['charset']}");
 			\mysqli_query($this->db['CURRENT'], "SET CHARACTER SET {$this->sysConfigure['charset']}");
 		} else {
 		} else {
-			\mysqli_query($this->db['CURRENT'], "SET CHARACTER SET UTF8");
+			\mysqli_query($this->db['CURRENT'], "SET CHARACTER SET utf8mb4");
 		}
 		}
 
 
 		$this->rs = $rs = \mysqli_query($this->db['CURRENT'], $sql);
 		$this->rs = $rs = \mysqli_query($this->db['CURRENT'], $sql);

+ 1 - 1
src/Driver/Pdo/Driver.php

@@ -56,7 +56,7 @@ class Driver extends Base implements Intf
     /**
     /**
      * @var string $charset 数据库默认编码
      * @var string $charset 数据库默认编码
      */
      */
-    public $charset = 'UTF8';
+    public $charset = 'utf8mb4';
     /**
     /**
      * 当前使用的db信息
      * 当前使用的db信息
      */
      */