Skip to content

Commit 974065c

Browse files
committed
增加 join, trace 功能
1 parent bcedb93 commit 974065c

File tree

2 files changed

+120
-17
lines changed

2 files changed

+120
-17
lines changed

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## ShardingQuery
22

3-
根据时间分表的数据查询类
3+
分表数据查询类
44

55
## 使用方法
66

@@ -14,10 +14,16 @@ $shardingQuery = new ShardingQuery([
1414
'order_201805',
1515
'order_201804',
1616
],
17-
'field' => '*',
18-
'where' => 'member_id = 10001',
19-
'offset' => 32,
20-
'limit' => 3,
17+
'field' => '{table}.*, u.name',
18+
'leftJoin' => [
19+
'user AS u ON u.member_id = {table}.member_id',
20+
],
21+
'where' => '{table}.member_id = 10001 AND status = 1',
22+
'order' => '{table}.add_time DESC',
23+
'offset' => 0,
24+
'limit' => 10,
2125
]);
2226
$res = $shardingQuery->select();
27+
$count = $shardingQuery->count();
28+
$trace = $shardingQuery->trace();
2329
```

ShardingQuery.php

Lines changed: 109 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?php
22

33
/**
4-
* 根据时间同构分表的查询类
5-
* @package app\common\library
4+
* 分表数据查询类
65
* @author LIUJIAN <coder.keda@gmail.com>
76
*/
87
class ShardingQuery
@@ -28,6 +27,30 @@ class ShardingQuery
2827
*/
2928
public $field;
3029

30+
/**
31+
* join
32+
* @var array
33+
*/
34+
public $innerJoin;
35+
36+
/**
37+
* join
38+
* @var array
39+
*/
40+
public $leftJoin;
41+
42+
/**
43+
* join
44+
* @var array
45+
*/
46+
public $rightJoin;
47+
48+
/**
49+
* join
50+
* @var array
51+
*/
52+
public $fullJoin;
53+
3154
/**
3255
* 条件
3356
* @var string
@@ -58,6 +81,12 @@ class ShardingQuery
5881
*/
5982
protected $stats;
6083

84+
/**
85+
* 追踪
86+
* @var array
87+
*/
88+
protected $trace;
89+
6190
/**
6291
* ShardingQuery constructor.
6392
* @param array $config
@@ -84,6 +113,30 @@ public function __construct(array $config)
84113
}
85114
$this->$key = $value;
86115
break;
116+
case 'innerJoin':
117+
if (!is_array($value)) {
118+
throw new \RuntimeException("'innerJoin' is not a array type.");
119+
}
120+
$this->$key = $value;
121+
break;
122+
case 'leftJoin':
123+
if (!is_array($value)) {
124+
throw new \RuntimeException("'leftJoin' is not a array type.");
125+
}
126+
$this->$key = $value;
127+
break;
128+
case 'rightJoin':
129+
if (!is_array($value)) {
130+
throw new \RuntimeException("'rightJoin' is not a array type.");
131+
}
132+
$this->$key = $value;
133+
break;
134+
case 'fullJoin':
135+
if (!is_array($value)) {
136+
throw new \RuntimeException("'fullJoin' is not a array type.");
137+
}
138+
$this->$key = $value;
139+
break;
87140
case 'where':
88141
if (!is_string($value)) {
89142
throw new \RuntimeException("'where' is not a string type.");
@@ -120,18 +173,17 @@ public function select()
120173
{
121174
$this->stats = $this->stats($this->table);
122175
$range = $this->range($this->stats);
176+
$sql = $this->sql();
123177
$data = [];
124178
foreach ($range as $tableName => $item) {
125-
$sql = "SELECT {$this->field} FROM `{$tableName}`";
126-
if (!empty($this->where)) {
127-
$sql .= " WHERE {$this->where}";
128-
}
129-
if (!empty($this->order)) {
130-
$sql .= " ORDER BY {$this->order}";
131-
}
132-
$sql = "{$sql} LIMIT {$item['limit']} OFFSET {$item['offset']}";
133-
$result = call_user_func($this->callback, $sql);
134-
$data = array_merge($data, $result);
179+
$sql = str_replace('{table}', $tableName, $sql);
180+
$sql = "{$sql} LIMIT {$item['limit']} OFFSET {$item['offset']}";
181+
$result = call_user_func($this->callback, $sql);
182+
$data = array_merge($data, $result);
183+
$this->trace[] = [
184+
'sql' => $sql,
185+
'rowCount' => count($result),
186+
];
135187
}
136188
return $data;
137189
}
@@ -214,6 +266,42 @@ protected function range($stats)
214266
return $tables;
215267
}
216268

269+
/**
270+
* 生成sql
271+
* @return string
272+
*/
273+
protected function sql()
274+
{
275+
$sql = "SELECT {$this->field} FROM `{table}`";
276+
if (!empty($this->innerJoin)) {
277+
foreach ($this->innerJoin as $item) {
278+
$sql .= " INNER JOIN {$item}";
279+
}
280+
}
281+
if (!empty($this->leftJoin)) {
282+
foreach ($this->leftJoin as $item) {
283+
$sql .= " LEFT JOIN {$item}";
284+
}
285+
}
286+
if (!empty($this->rightJoin)) {
287+
foreach ($this->rightJoin as $item) {
288+
$sql .= " RIGHT JOIN {$item}";
289+
}
290+
}
291+
if (!empty($this->fullJoin)) {
292+
foreach ($this->fullJoin as $item) {
293+
$sql .= " FULL JOIN {$item}";
294+
}
295+
}
296+
if (!empty($this->where)) {
297+
$sql .= " WHERE {$this->where}";
298+
}
299+
if (!empty($this->order)) {
300+
$sql .= " ORDER BY {$this->order}";
301+
}
302+
return $sql;
303+
}
304+
217305
/**
218306
* 获取数据总数
219307
* @return int
@@ -226,4 +314,13 @@ public function count()
226314
return $number ?: 0;
227315
}
228316

317+
/**
318+
* 获取追踪数据
319+
* @return array
320+
*/
321+
public function trace()
322+
{
323+
return $this->trace;
324+
}
325+
229326
}

0 commit comments

Comments
 (0)