作者 karlet

feat:优化mysqlCli

1 <?php 1 <?php
  2 +
2 namespace Jiaoyin; 3 namespace Jiaoyin;
3 4
4 use Swoole\Database\MysqliConfig; 5 use Swoole\Database\MysqliConfig;
@@ -10,24 +11,24 @@ class MysqlCli @@ -10,24 +11,24 @@ class MysqlCli
10 private $pool = null; 11 private $pool = null;
11 private $prefix = '';// 前缀 12 private $prefix = '';// 前缀
12 13
13 - private int $reconnectCount = 0; //period时间内重连次数  
14 - private int $period = 300; 14 + private int $reconnectCount = 0; //period时间内重连次数
  15 + private int $period = 300;
15 private int $lastReconnectTime = 0; 16 private int $lastReconnectTime = 0;
16 - private $connectConfig = null;  
17 - private $database = null; 17 + private $connectConfig = null;
  18 + private $database = null;
18 19
19 - public function __construct($host,$port,$database,$username,$password,$charset='utf8',$prefix='', $connectCount = 20) 20 + public function __construct($host, $port, $database, $username, $password, $charset = 'utf8', $prefix = '', $connectCount = 20)
20 { 21 {
21 - $this->database = $database;  
22 - $this->prefix = $prefix; 22 + $this->database = $database;
  23 + $this->prefix = $prefix;
23 $this->connectConfig = [ 24 $this->connectConfig = [
24 - 'host' => $host,  
25 - 'port' => $port,  
26 - 'database' => $database,  
27 - 'username' => $username,  
28 - 'password' => $password,  
29 - 'charset' => $charset,  
30 - 'prefix' => $prefix, 25 + 'host' => $host,
  26 + 'port' => $port,
  27 + 'database' => $database,
  28 + 'username' => $username,
  29 + 'password' => $password,
  30 + 'charset' => $charset,
  31 + 'prefix' => $prefix,
31 'connectCount' => $connectCount, 32 'connectCount' => $connectCount,
32 ]; 33 ];
33 $this->poolConnect(); 34 $this->poolConnect();
@@ -35,17 +36,17 @@ class MysqlCli @@ -35,17 +36,17 @@ class MysqlCli
35 36
36 private function poolConnect() 37 private function poolConnect()
37 { 38 {
38 - if($this->pool){ 39 + if ($this->pool) {
39 $this->pool->close(); 40 $this->pool->close();
40 } 41 }
41 - $this->pool = new MysqliPool((new MysqliConfig) 42 + $this->pool = new MysqliPool((new MysqliConfig)
42 ->withHost($this->connectConfig['host']) 43 ->withHost($this->connectConfig['host'])
43 ->withPort($this->connectConfig['port']) 44 ->withPort($this->connectConfig['port'])
44 ->withDbName($this->connectConfig['database']) 45 ->withDbName($this->connectConfig['database'])
45 ->withCharset($this->connectConfig['charset']) 46 ->withCharset($this->connectConfig['charset'])
46 ->withUsername($this->connectConfig['username']) 47 ->withUsername($this->connectConfig['username'])
47 ->withPassword($this->connectConfig['password']) 48 ->withPassword($this->connectConfig['password'])
48 - ,$this->connectConfig['connectCount'] 49 + , $this->connectConfig['connectCount']
49 ); 50 );
50 $this->prefix = $this->connectConfig['prefix']; 51 $this->prefix = $this->connectConfig['prefix'];
51 } 52 }
@@ -116,7 +117,7 @@ class MysqlCli @@ -116,7 +117,7 @@ class MysqlCli
116 $data = $this->execute('SELECT', $sql); 117 $data = $this->execute('SELECT', $sql);
117 $newData = []; 118 $newData = [];
118 if (empty($col)) { 119 if (empty($col)) {
119 - $newsql = 'SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = "'.$this->database.'" AND TABLE_NAME="' . $this->parseTable($table) . '" ORDER BY `ORDINAL_POSITION` ASC'; 120 + $newsql = 'SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = "' . $this->database . '" AND TABLE_NAME="' . $this->parseTable($table) . '" ORDER BY `ORDINAL_POSITION` ASC';
120 $coldata = $this->execute('SELECT', $newsql); 121 $coldata = $this->execute('SELECT', $newsql);
121 if (!$coldata) { 122 if (!$coldata) {
122 return false; 123 return false;
@@ -208,9 +209,13 @@ class MysqlCli @@ -208,9 +209,13 @@ class MysqlCli
208 return false; 209 return false;
209 } 210 }
210 $value[1] = $this->transferValue($value[1]); 211 $value[1] = $this->transferValue($value[1]);
211 - $symbol = isset($value[2]) ? $value[2] : '=';  
212 - $tmp .= $value[0] . ' ' . $symbol . ' ' . $value[1];  
213 - $tmp .= $key < count($where) - 1 ? ' and ' : ''; 212 + if ($value[1] === 'NULL') {
  213 + $tmp = $value[0] . ' is NULL';
  214 + } else {
  215 + $symbol = $value[2] ?? '=';
  216 + $tmp .= $value[0] . ' ' . $symbol . ' ' . $value[1];
  217 + }
  218 + $tmp .= $key < count($where) - 1 ? ' and ' : '';
214 } 219 }
215 $whereTxt .= $tmp; 220 $whereTxt .= $tmp;
216 } else { 221 } else {