正在显示
1 个修改的文件
包含
33 行增加
和
18 行删除
| @@ -11,7 +11,7 @@ class MysqlCli | @@ -11,7 +11,7 @@ class MysqlCli | ||
| 11 | { | 11 | { |
| 12 | 12 | ||
| 13 | private $pool = null; | 13 | private $pool = null; |
| 14 | - private $prefix = '';// 前缀 | 14 | + private $prefix = ''; // 前缀 |
| 15 | 15 | ||
| 16 | private int $reconnectCount = 0; //period时间内重连次数 | 16 | private int $reconnectCount = 0; //period时间内重连次数 |
| 17 | private int $period = 300; | 17 | private int $period = 300; |
| @@ -42,13 +42,13 @@ class MysqlCli | @@ -42,13 +42,13 @@ class MysqlCli | ||
| 42 | $this->pool->close(); | 42 | $this->pool->close(); |
| 43 | } | 43 | } |
| 44 | $this->pool = new MysqliPool((new MysqliConfig) | 44 | $this->pool = new MysqliPool((new MysqliConfig) |
| 45 | - ->withHost($this->connectConfig['host']) | ||
| 46 | - ->withPort($this->connectConfig['port']) | ||
| 47 | - ->withDbName($this->connectConfig['database']) | ||
| 48 | - ->withCharset($this->connectConfig['charset']) | ||
| 49 | - ->withUsername($this->connectConfig['username']) | ||
| 50 | - ->withPassword($this->connectConfig['password']) | ||
| 51 | - , $this->connectConfig['connectCount'] | 45 | + ->withHost($this->connectConfig['host']) |
| 46 | + ->withPort($this->connectConfig['port']) | ||
| 47 | + ->withDbName($this->connectConfig['database']) | ||
| 48 | + ->withCharset($this->connectConfig['charset']) | ||
| 49 | + ->withUsername($this->connectConfig['username']) | ||
| 50 | + ->withPassword($this->connectConfig['password']), | ||
| 51 | + $this->connectConfig['connectCount'] | ||
| 52 | ); | 52 | ); |
| 53 | $this->prefix = $this->connectConfig['prefix']; | 53 | $this->prefix = $this->connectConfig['prefix']; |
| 54 | } | 54 | } |
| @@ -89,7 +89,6 @@ class MysqlCli | @@ -89,7 +89,6 @@ class MysqlCli | ||
| 89 | $ret = $result; | 89 | $ret = $result; |
| 90 | } | 90 | } |
| 91 | } | 91 | } |
| 92 | - | ||
| 93 | } finally { | 92 | } finally { |
| 94 | if ($db) { | 93 | if ($db) { |
| 95 | $this->pool->put($db); | 94 | $this->pool->put($db); |
| @@ -109,9 +108,9 @@ class MysqlCli | @@ -109,9 +108,9 @@ class MysqlCli | ||
| 109 | } | 108 | } |
| 110 | 109 | ||
| 111 | //select数据 | 110 | //select数据 |
| 112 | - public function select($table, $where = [], $col = [], $orderBy = '', $limit = '') | 111 | + public function select($table, $where = [], $col = [], $orderBy = '', $limit = '', $groupBy = '') |
| 113 | { | 112 | { |
| 114 | - $sql = $this->parseSelect($table, $where, $col, $orderBy, $limit); | 113 | + $sql = $this->parseSelect($table, $where, $col, $orderBy, $limit, $groupBy); |
| 115 | if (!$sql) { | 114 | if (!$sql) { |
| 116 | return false; | 115 | return false; |
| 117 | } | 116 | } |
| @@ -131,13 +130,23 @@ class MysqlCli | @@ -131,13 +130,23 @@ class MysqlCli | ||
| 131 | } | 130 | } |
| 132 | } | 131 | } |
| 133 | 132 | ||
| 134 | - if ($isAggregate) { | ||
| 135 | - // ✅ 聚合查询直接使用 fetch_assoc 风格 | 133 | + if ($isAggregate && empty($groupBy)) { |
| 134 | + // 没有 group by 的单行聚合查询 | ||
| 136 | $db = $this->pool->get(); | 135 | $db = $this->pool->get(); |
| 137 | $stmt = $db->query($sql); | 136 | $stmt = $db->query($sql); |
| 138 | $result = $stmt->fetch_assoc(); | 137 | $result = $stmt->fetch_assoc(); |
| 139 | $this->pool->put($db); | 138 | $this->pool->put($db); |
| 140 | return [$result]; | 139 | return [$result]; |
| 140 | + } elseif ($isAggregate && !empty($groupBy)) { | ||
| 141 | + // 有 group by 的聚合查询,多行返回 | ||
| 142 | + $db = $this->pool->get(); | ||
| 143 | + $stmt = $db->query($sql); | ||
| 144 | + $results = []; | ||
| 145 | + while ($row = $stmt->fetch_assoc()) { | ||
| 146 | + $results[] = $row; | ||
| 147 | + } | ||
| 148 | + $this->pool->put($db); | ||
| 149 | + return $results; | ||
| 141 | } | 150 | } |
| 142 | 151 | ||
| 143 | // ✅ 普通查询才查字段结构 | 152 | // ✅ 普通查询才查字段结构 |
| @@ -202,21 +211,28 @@ class MysqlCli | @@ -202,21 +211,28 @@ class MysqlCli | ||
| 202 | } | 211 | } |
| 203 | 212 | ||
| 204 | //解析select | 213 | //解析select |
| 205 | - private function parseSelect($table, $where, $col, $orderBy, $limit) | 214 | + private function parseSelect($table, $where, $col, $orderBy, $limit, $groupBy = '') |
| 206 | { | 215 | { |
| 207 | $table = $this->parseTable($table); | 216 | $table = $this->parseTable($table); |
| 217 | + | ||
| 208 | if (!empty($col) && !is_array($col)) { | 218 | if (!empty($col) && !is_array($col)) { |
| 209 | output('select where col 不合法'); | 219 | output('select where col 不合法'); |
| 210 | return false; | 220 | return false; |
| 211 | } | 221 | } |
| 222 | + | ||
| 212 | $colTxt = empty($col) ? '*' : implode(',', $col); | 223 | $colTxt = empty($col) ? '*' : implode(',', $col); |
| 213 | $whereTxt = $this->parseWhere($where); | 224 | $whereTxt = $this->parseWhere($where); |
| 214 | - $orderByTxt = !empty($orderBy) && isset($orderBy[0]) && isset($orderBy[1]) ? 'order by ' . $orderBy[0] . ' ' . $orderBy[1] : ''; | 225 | + $groupByTxt = !empty($groupBy) ? 'group by ' . $groupBy : ''; |
| 226 | + $orderByTxt = (!empty($orderBy) && isset($orderBy[0]) && isset($orderBy[1])) | ||
| 227 | + ? 'order by ' . $orderBy[0] . ' ' . $orderBy[1] | ||
| 228 | + : ''; | ||
| 215 | $limitTxt = !empty($limit) ? 'limit ' . $limit : ''; | 229 | $limitTxt = !empty($limit) ? 'limit ' . $limit : ''; |
| 216 | - $sql = "select {$colTxt} from {$table} {$whereTxt} {$orderByTxt} {$limitTxt}"; | 230 | + |
| 231 | + $sql = "select {$colTxt} from {$table} {$whereTxt} {$groupByTxt} {$orderByTxt} {$limitTxt}"; | ||
| 217 | return $sql; | 232 | return $sql; |
| 218 | } | 233 | } |
| 219 | 234 | ||
| 235 | + | ||
| 220 | //解析where | 236 | //解析where |
| 221 | private function parseWhere($where) | 237 | private function parseWhere($where) |
| 222 | { | 238 | { |
| @@ -303,5 +319,4 @@ class MysqlCli | @@ -303,5 +319,4 @@ class MysqlCli | ||
| 303 | } | 319 | } |
| 304 | return $valueNew; | 320 | return $valueNew; |
| 305 | } | 321 | } |
| 306 | - | ||
| 307 | -} | ||
| 322 | +} |
-
请 注册 或 登录 后发表评论