Browse Source

fix db error for simps (#5796)

沈唁 5 years ago
parent
commit
f312161c0b

+ 4 - 4
frameworks/PHP/simps/README.md

@@ -1,13 +1,13 @@
 <p align="center">
     <a href="https://simps.io" target="_blank">
-        <img src="https://cdn.jsdelivr.net/gh/sy-records/staticfile/images/simps.png" alt="Simps" />
+        <img src="https://cdn.jsdelivr.net/gh/sy-records/staticfile@master/images/simps.png" alt="Simps" />
     </a>
 </p>
 
-[![Simps License](https://img.shields.io/packagist/l/simple-swoole/simps?color=blue)](https://github.com/simple-swoole/simps/blob/master/LICENSE)
+[![Simps License](https://poser.pugx.org/simple-swoole/simps/license)](https://github.com/simple-swoole/simps/blob/master/LICENSE)
 [![Latest Version](https://img.shields.io/packagist/v/simple-swoole/simps.svg)](https://packagist.org/packages/simple-swoole/simps)
 [![Simps Doc](https://img.shields.io/badge/docs-passing-blue.svg)](https://doc.simps.io)
-[![Contact Simps Team](https://img.shields.io/badge/[email protected]?style=flat)](mailto:[email protected])
+[![Contact Simps Team](https://img.shields.io/badge/contact-@Simps%20Team-blue.svg)](mailto:[email protected])
 [![Php Version](https://img.shields.io/badge/php-%3E=7.1-brightgreen.svg)](https://www.php.net)
 [![Swoole Version](https://img.shields.io/badge/swoole-%3E=4.4.0-brightgreen.svg)](https://github.com/swoole/swoole-src)
 
@@ -18,7 +18,7 @@
 🚀 A simple, lightweight and high-performance PHP coroutine framework.
 
 [Home Page](https://simps.io)
-[Document](https://doc.simps.io)
+[Documentation](https://doc.simps.io)
 
 ## Important Libraries
 

+ 4 - 3
frameworks/PHP/simps/app/Controller/IndexController.php

@@ -12,6 +12,7 @@ declare(strict_types=1);
 namespace App\Controller;
 
 use App\Model\DbModel;
+use App\Model\MicroModel;
 use Simps\Server\Protocol\HTTP\SimpleResponse;
 
 class IndexController
@@ -123,7 +124,7 @@ class IndexController
 
     public function microDb($server, $fd)
     {
-        $db = new DbModel();
+        $db = new MicroModel();
         $res = $db->microDb();
 
         $server->send(
@@ -138,7 +139,7 @@ class IndexController
 
     public function microQueries($server, $fd, $data)
     {
-        $db = new DbModel();
+        $db = new MicroModel();
         if (isset($data['queries'])) {
             $res = $db->microQueries((int)$data['queries']);
         } else {
@@ -157,7 +158,7 @@ class IndexController
 
     public function microUpdates($server, $fd, $data)
     {
-        $db = new DbModel();
+        $db = new MicroModel();
         if (isset($data['queries'])) {
             $res = $db->microUpdates((int)$data['queries']);
         } else {

+ 8 - 107
frameworks/PHP/simps/app/Model/DbModel.php

@@ -11,16 +11,14 @@ declare(strict_types=1);
 
 namespace App\Model;
 
-use Simps\DB\BaseModel;
+use Simps\DB\DB;
 
-class DbModel extends BaseModel
+class DbModel extends DB
 {
     public function fortunes()
     {
         $fortune = [];
-        $this->pdo->fortune_test = $this->pdo->fortune_test ?? $this->pdo->prepare('SELECT id, message FROM Fortune');
-        $this->pdo->fortune_test->execute();
-        $arr = $this->pdo->fortune_test->fetchAll();
+        $arr = $this->query('SELECT id, message FROM Fortune');
         foreach ($arr as $row) {
             $fortune[$row['id']] = $row['message'];
         }
@@ -37,23 +35,18 @@ class DbModel extends BaseModel
         }
 
         $arr = [];
-        $this->pdo->updates_test_select = $this->pdo->updates_test_select ?? $this->pdo->prepare(
-                'SELECT id, randomNumber FROM World WHERE id = ?'
-            );
-        $this->pdo->updates_test_update = $this->pdo->updates_test_update ?? $this->pdo->prepare(
-                'UPDATE World SET randomNumber = ? WHERE id = ?'
-            );
 
         while ($query_count--) {
             $id = mt_rand(1, 10000);
             $randomNumber = mt_rand(1, 10000);
-            $this->pdo->updates_test_select->execute([$id]);
-            $ret = $this->pdo->updates_test_select->fetchAll();
+
+            $ret = $this->query('SELECT id, randomNumber FROM World WHERE id = ?', [$id]);
 
             // Store result in array.
             $world = ['id' => $id, 'randomNumber' => $ret[0]['randomNumber']];
             $world['randomNumber'] = $randomNumber;
-            $this->pdo->updates_test_update->execute([$randomNumber, $id]);
+
+            $this->execute('UPDATE World SET randomNumber = ? WHERE id = ?', [$randomNumber, $id]);
 
             $arr[] = $world;
         }
@@ -71,16 +64,11 @@ class DbModel extends BaseModel
 
         // Create an array with the response string.
         $arr = [];
-        // Define query
-        $this->pdo->db_test = $this->pdo->db_test ?? $this->pdo->prepare(
-                'SELECT id, randomNumber FROM World WHERE id = ?'
-            );
 
         // For each query, store the result set values in the response array
         while ($query_count--) {
             $id = mt_rand(1, 10000);
-            $this->pdo->db_test->execute([$id]);
-            $data = $this->pdo->db_test->fetchAll();
+            $data = $this->query('SELECT id, randomNumber FROM World WHERE id = ?', [$id]);
 
             // Store result in array.
             $arr[] = ['id' => $id, 'randomNumber' => $data[0]['randomNumber']];
@@ -94,91 +82,4 @@ class DbModel extends BaseModel
 
         return \json_encode($arr, JSON_NUMERIC_CHECK);
     }
-
-    public function microDb()
-    {
-        $id = mt_rand(1, 10000);
-        $data = $this->get(
-            "World",
-            [
-                'id',
-                'randomNumber'
-            ],
-            [
-                "id" => $id
-            ]
-        );
-        return \json_encode($data, JSON_NUMERIC_CHECK);
-    }
-
-    public function microQueries(int $queries = 0)
-    {
-        $query_count = 1;
-        if ($queries > 1) {
-            $query_count = $queries > 500 ? 500 : $queries;
-        }
-
-        $arr = [];
-
-        while ($query_count--) {
-            $id = mt_rand(1, 10000);
-            $data = $this->get(
-                "World",
-                [
-                    'id',
-                    'randomNumber'
-                ],
-                [
-                    "id" => $id
-                ]
-            );
-
-            // Store result in array.
-            $arr[] = $data;
-        }
-
-        return \json_encode($arr, JSON_NUMERIC_CHECK);
-    }
-
-    public function microUpdates(int $queries = 0)
-    {
-        $query_count = 1;
-        if ($queries > 1) {
-            $query_count = $queries > 500 ? 500 : $queries;
-        }
-
-        $arr = [];
-
-        while ($query_count--) {
-            $id = mt_rand(1, 10000);
-            $randomNumber = mt_rand(1, 10000);
-            $data = $this->get(
-                "World",
-                [
-                    'id',
-                    'randomNumber'
-                ],
-                [
-                    "id" => $id
-                ]
-            );
-
-            $world = ['id' => $id, 'randomNumber' => $data['randomNumber']];
-            $world['randomNumber'] = $randomNumber;
-
-            $this->update(
-                "World",
-                [
-                    'randomNumber' => $randomNumber
-                ],
-                [
-                    "id" => $id
-                ]
-            );
-
-            $arr[] = $world;
-        }
-
-        return \json_encode($arr, JSON_NUMERIC_CHECK);
-    }
 }

+ 100 - 0
frameworks/PHP/simps/app/Model/MicroModel.php

@@ -0,0 +1,100 @@
+<?php
+/**
+ * User: lufei
+ * Date: 2020/6/24
+ * Email: [email protected]
+ */
+
+namespace App\Model;
+
+use Simps\DB\BaseModel;
+
+class MicroModel extends BaseModel
+{
+    public function microDb()
+    {
+        $id = mt_rand(1, 10000);
+        $data = $this->get(
+            "World",
+            [
+                'id',
+                'randomNumber'
+            ],
+            [
+                "id" => $id
+            ]
+        );
+        return \json_encode($data, JSON_NUMERIC_CHECK);
+    }
+
+    public function microQueries(int $queries = 0)
+    {
+        $query_count = 1;
+        if ($queries > 1) {
+            $query_count = $queries > 500 ? 500 : $queries;
+        }
+
+        $arr = [];
+
+        while ($query_count--) {
+            $id = mt_rand(1, 10000);
+            $data = $this->get(
+                "World",
+                [
+                    'id',
+                    'randomNumber'
+                ],
+                [
+                    "id" => $id
+                ]
+            );
+
+            // Store result in array.
+            $arr[] = $data;
+        }
+
+        return \json_encode($arr, JSON_NUMERIC_CHECK);
+    }
+
+    public function microUpdates(int $queries = 0)
+    {
+        $query_count = 1;
+        if ($queries > 1) {
+            $query_count = $queries > 500 ? 500 : $queries;
+        }
+
+        $arr = [];
+
+        while ($query_count--) {
+            $id = mt_rand(1, 10000);
+            $randomNumber = mt_rand(1, 10000);
+            $data = $this->get(
+                "World",
+                [
+                    'id',
+                    'randomNumber'
+                ],
+                [
+                    "id" => $id
+                ]
+            );
+
+            $world = ['id' => $id, 'randomNumber' => $data['randomNumber']];
+            $world['randomNumber'] = $randomNumber;
+
+            $this->update(
+                "World",
+                [
+                    'randomNumber' => $randomNumber
+                ],
+                [
+                    "id" => $id
+                ]
+            );
+
+            $arr[] = $world;
+        }
+
+        return \json_encode($arr, JSON_NUMERIC_CHECK);
+    }
+}