Skip to content

Commit 0fd8544

Browse files
committed
优化
1 parent 6c24dac commit 0fd8544

File tree

2 files changed

+32
-50
lines changed

2 files changed

+32
-50
lines changed

README.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
## 使用方法
66

7-
ThinkPHP 5
7+
ThinkPHP 5 的范例,支持任何框架,只需修改 `query` 参数的闭包代码。
88

99
```php
1010
$shardingQuery = new ShardingQuery([
11-
'callback' => 'think\Db::query',
11+
'query' => function ($sql) {
12+
return \think\Db::query($sql);
13+
},
1214
'table' => [
1315
'order',
1416
'order_201805',
@@ -23,7 +25,22 @@ $shardingQuery = new ShardingQuery([
2325
'offset' => 0,
2426
'limit' => 10,
2527
]);
26-
$res = $shardingQuery->select();
28+
// 查询结果
29+
$result = $shardingQuery->select();
30+
// 总行数
2731
$count = $shardingQuery->count();
32+
// 追踪数据,用于调试
2833
$trace = $shardingQuery->trace();
2934
```
35+
36+
全部参数:
37+
38+
- `query`:执行 sql 返回结果的闭包,接收一个 $sql 参数,返回查询结果。
39+
- `table`:要查询的多个同构表的清单,数组类型。
40+
- `field`:select 选择的列名,字符串类型。
41+
- `innerJoin`:join信息,数组类型。
42+
- `leftJoin`:join信息,数组类型。
43+
- `where`:查询条件,字符串类型。
44+
- `order`:排序,字符串类型。
45+
- `offset`:偏移数,整数类型。
46+
- `limit`:限制数,整数类型。

ShardingQuery.php

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ class ShardingQuery
88
{
99

1010
/**
11-
* 数据库查询的回调
12-
* 回调必须返回数组类型
13-
* @var callable
11+
* 查询闭包
12+
* @var \Closure
1413
*/
15-
public $callback;
14+
public $query;
1615

1716
/**
1817
* 数据表列表
@@ -28,29 +27,17 @@ class ShardingQuery
2827
public $field;
2928

3029
/**
31-
* join
30+
* innerJoin
3231
* @var array
3332
*/
3433
public $innerJoin;
3534

3635
/**
37-
* join
36+
* leftJoin
3837
* @var array
3938
*/
4039
public $leftJoin;
4140

42-
/**
43-
* join
44-
* @var array
45-
*/
46-
public $rightJoin;
47-
48-
/**
49-
* join
50-
* @var array
51-
*/
52-
public $fullJoin;
53-
5441
/**
5542
* 条件
5643
* @var string
@@ -101,9 +88,9 @@ public function __construct(array $config)
10188
{
10289
foreach ($config as $key => $value) {
10390
switch ($key) {
104-
case 'callback':
105-
if (!is_callable($value)) {
106-
throw new \RuntimeException("'callback' is not a callable type.");
91+
case 'query':
92+
if (!($value instanceof \Closure)) {
93+
throw new \RuntimeException("'query' is not a closure type.");
10794
}
10895
$this->$key = $value;
10996
break;
@@ -131,18 +118,6 @@ public function __construct(array $config)
131118
}
132119
$this->$key = $value;
133120
break;
134-
case 'rightJoin':
135-
if (!is_array($value)) {
136-
throw new \RuntimeException("'rightJoin' is not a array type.");
137-
}
138-
$this->$key = $value;
139-
break;
140-
case 'fullJoin':
141-
if (!is_array($value)) {
142-
throw new \RuntimeException("'fullJoin' is not a array type.");
143-
}
144-
$this->$key = $value;
145-
break;
146121
case 'where':
147122
if (!is_string($value)) {
148123
throw new \RuntimeException("'where' is not a string type.");
@@ -187,7 +162,7 @@ public function select()
187162
$tmpSql = str_replace(static::$tableSymbol, $tableName, $sql);
188163
$tmpSql = "{$tmpSql} LIMIT {$item['limit']} OFFSET {$item['offset']}";
189164
$this->log(['sql' => $tmpSql]);
190-
$result = call_user_func($this->callback, $tmpSql);
165+
$result = call_user_func($this->query, $tmpSql);
191166
$this->log(['resultCount' => count($result)]);
192167
$data = array_merge($data, $result);
193168
}
@@ -210,8 +185,8 @@ protected function stats($table)
210185
}
211186
$sql = str_replace(static::$tableSymbol, $tableName, $sql);
212187
$this->log(['sql' => $sql]);
213-
$result = call_user_func($this->callback, $sql);
214-
$first = array_pop($result);
188+
$result = call_user_func($this->query, $sql);
189+
$first = (array)array_pop($result);
215190
$count = array_pop($first);
216191
$this->log(['result' => $count]);
217192
$start = $end;
@@ -285,16 +260,6 @@ protected function sql()
285260
$sql .= " LEFT JOIN {$item}";
286261
}
287262
}
288-
if (!empty($this->rightJoin)) {
289-
foreach ($this->rightJoin as $item) {
290-
$sql .= " RIGHT JOIN {$item}";
291-
}
292-
}
293-
if (!empty($this->fullJoin)) {
294-
foreach ($this->fullJoin as $item) {
295-
$sql .= " FULL JOIN {$item}";
296-
}
297-
}
298263
if (!empty($this->where)) {
299264
$sql .= " WHERE {$this->where}";
300265
}
@@ -317,7 +282,7 @@ public function count()
317282
}
318283

319284
/**
320-
* 记录日志
285+
* 记录追踪数据
321286
*/
322287
protected function log($message)
323288
{

0 commit comments

Comments
 (0)