Переглянути джерело

不自动记录sql,不开启记录的情况下只记录最后一条sql

zjh 3 тижнів тому
батько
коміт
f26d83d696
2 змінених файлів з 55 додано та 1 видалено
  1. 18 0
      src/Driver/Entity/Base.php
  2. 37 1
      src/Driver/Traits/SQL.php

+ 18 - 0
src/Driver/Entity/Base.php

@@ -834,6 +834,7 @@ class Base {
                 $fields[$field] = null;
             }
         }
+        unset($class, $method, $properties);
         return $fields;
     }
 
@@ -1481,6 +1482,23 @@ class Base {
         }
         return $fields;
     }
+
+    /**
+     * 开始记录SQL
+     *
+     * @return void
+     */
+    final public function startRecord(){
+        $this->db()->startRecord();
+    }
+
+    /**
+     * 不再记录SQL
+     * @return void
+     */
+    final public function endRecord(){
+        $this->db()->endRecord();
+    }
     /**
      * 获取where及or条件并创建查询条件
      *

+ 37 - 1
src/Driver/Traits/SQL.php

@@ -104,6 +104,17 @@ trait SQL
      * @var null $table 表名
      */
     private $table = null;
+    /**
+     * 是否记录SQL
+     *
+     * @var bool
+     */
+    private $recordSQL = false;
+    /**
+     * sql 记录上限
+     * @var int
+     */
+    private $recordLimit = -1;
     /**
      * 执行的SQL及耗时
      *
@@ -190,6 +201,23 @@ trait SQL
         return '`'.trim($field).'`';
     }
 
+    /**
+     * 开始记录SQL语句
+     *
+     * @return void
+     */
+    final public function startRecord(){
+         $this->recordSQL = true;
+    }
+
+    /**
+     * 记录结束
+     *
+     * @return void
+     */
+    final public function endRecord(){
+        $this->recordSQL = false;
+    }
     /**
      * sql执行的开始时间
      *
@@ -197,10 +225,18 @@ trait SQL
      * @return void
      */
     final public function startTime($sql){
-        $this->executionSQL[] = [
+        $record = [
             'sql' => $sql,
             'start' => microtime(true)
         ];
+        if(!$this->recordSQL) {
+            $this->executionSQL = [$record];
+            return;
+        }
+        if($this->recordLimit > 0 && count($this->executionSQL) >= $this->recordLimit) {
+            $this->executionSQL = array_slice($this->executionSQL, 0, $this->recordLimit);
+        }
+        $this->executionSQL[] = $record;
     }
 
     /**