|
...
|
...
|
@@ -11,7 +11,7 @@ class MysqlCli |
|
|
|
{
|
|
|
|
|
|
|
|
private $pool = null;
|
|
|
|
private $prefix = '';// 前缀
|
|
|
|
private $prefix = ''; // 前缀
|
|
|
|
|
|
|
|
private int $reconnectCount = 0; //period时间内重连次数
|
|
|
|
private int $period = 300;
|
|
...
|
...
|
@@ -42,13 +42,13 @@ class MysqlCli |
|
|
|
$this->pool->close();
|
|
|
|
}
|
|
|
|
$this->pool = new MysqliPool((new MysqliConfig)
|
|
|
|
->withHost($this->connectConfig['host'])
|
|
|
|
->withPort($this->connectConfig['port'])
|
|
|
|
->withDbName($this->connectConfig['database'])
|
|
|
|
->withCharset($this->connectConfig['charset'])
|
|
|
|
->withUsername($this->connectConfig['username'])
|
|
|
|
->withPassword($this->connectConfig['password'])
|
|
|
|
, $this->connectConfig['connectCount']
|
|
|
|
->withHost($this->connectConfig['host'])
|
|
|
|
->withPort($this->connectConfig['port'])
|
|
|
|
->withDbName($this->connectConfig['database'])
|
|
|
|
->withCharset($this->connectConfig['charset'])
|
|
|
|
->withUsername($this->connectConfig['username'])
|
|
|
|
->withPassword($this->connectConfig['password']),
|
|
|
|
$this->connectConfig['connectCount']
|
|
|
|
);
|
|
|
|
$this->prefix = $this->connectConfig['prefix'];
|
|
|
|
}
|
|
...
|
...
|
@@ -89,7 +89,6 @@ class MysqlCli |
|
|
|
$ret = $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
if ($db) {
|
|
|
|
$this->pool->put($db);
|
|
...
|
...
|
@@ -109,9 +108,9 @@ class MysqlCli |
|
|
|
}
|
|
|
|
|
|
|
|
//select数据
|
|
|
|
public function select($table, $where = [], $col = [], $orderBy = '', $limit = '')
|
|
|
|
public function select($table, $where = [], $col = [], $orderBy = '', $limit = '', $groupBy = '')
|
|
|
|
{
|
|
|
|
$sql = $this->parseSelect($table, $where, $col, $orderBy, $limit);
|
|
|
|
$sql = $this->parseSelect($table, $where, $col, $orderBy, $limit, $groupBy);
|
|
|
|
if (!$sql) {
|
|
|
|
return false;
|
|
|
|
}
|
|
...
|
...
|
@@ -131,13 +130,23 @@ class MysqlCli |
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($isAggregate) {
|
|
|
|
// ✅ 聚合查询直接使用 fetch_assoc 风格
|
|
|
|
if ($isAggregate && empty($groupBy)) {
|
|
|
|
// 没有 group by 的单行聚合查询
|
|
|
|
$db = $this->pool->get();
|
|
|
|
$stmt = $db->query($sql);
|
|
|
|
$result = $stmt->fetch_assoc();
|
|
|
|
$this->pool->put($db);
|
|
|
|
return [$result];
|
|
|
|
} elseif ($isAggregate && !empty($groupBy)) {
|
|
|
|
// 有 group by 的聚合查询,多行返回
|
|
|
|
$db = $this->pool->get();
|
|
|
|
$stmt = $db->query($sql);
|
|
|
|
$results = [];
|
|
|
|
while ($row = $stmt->fetch_assoc()) {
|
|
|
|
$results[] = $row;
|
|
|
|
}
|
|
|
|
$this->pool->put($db);
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ✅ 普通查询才查字段结构
|
|
...
|
...
|
@@ -202,21 +211,28 @@ class MysqlCli |
|
|
|
}
|
|
|
|
|
|
|
|
//解析select
|
|
|
|
private function parseSelect($table, $where, $col, $orderBy, $limit)
|
|
|
|
private function parseSelect($table, $where, $col, $orderBy, $limit, $groupBy = '')
|
|
|
|
{
|
|
|
|
$table = $this->parseTable($table);
|
|
|
|
|
|
|
|
if (!empty($col) && !is_array($col)) {
|
|
|
|
output('select where col 不合法');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$colTxt = empty($col) ? '*' : implode(',', $col);
|
|
|
|
$whereTxt = $this->parseWhere($where);
|
|
|
|
$orderByTxt = !empty($orderBy) && isset($orderBy[0]) && isset($orderBy[1]) ? 'order by ' . $orderBy[0] . ' ' . $orderBy[1] : '';
|
|
|
|
$groupByTxt = !empty($groupBy) ? 'group by ' . $groupBy : '';
|
|
|
|
$orderByTxt = (!empty($orderBy) && isset($orderBy[0]) && isset($orderBy[1]))
|
|
|
|
? 'order by ' . $orderBy[0] . ' ' . $orderBy[1]
|
|
|
|
: '';
|
|
|
|
$limitTxt = !empty($limit) ? 'limit ' . $limit : '';
|
|
|
|
$sql = "select {$colTxt} from {$table} {$whereTxt} {$orderByTxt} {$limitTxt}";
|
|
|
|
|
|
|
|
$sql = "select {$colTxt} from {$table} {$whereTxt} {$groupByTxt} {$orderByTxt} {$limitTxt}";
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//解析where
|
|
|
|
private function parseWhere($where)
|
|
|
|
{
|
|
...
|
...
|
@@ -303,5 +319,4 @@ class MysqlCli |
|
|
|
}
|
|
|
|
return $valueNew;
|
|
|
|
}
|
|
|
|
|
|
|
|
} |
|
|
\ No newline at end of file |
|
|
|
} |
...
|
...
|
|