Skip to content

Commit 6c24dac

Browse files
committed
优化
1 parent c05cef4 commit 6c24dac

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

ShardingQuery.php

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,18 @@ public function __construct(array $config)
178178
public function select()
179179
{
180180
$this->stats = $this->stats($this->table);
181-
$range = $this->range($this->stats);
182-
$sql = $this->sql();
183-
$data = [];
181+
$this->log(['stats' => $this->stats]);
182+
$range = $this->range($this->stats);
183+
$this->log(['range' => $range]);
184+
$sql = $this->sql();
185+
$data = [];
184186
foreach ($range as $tableName => $item) {
185-
$tmpSql = str_replace(static::$tableSymbol, $tableName, $sql);
186-
$tmpSql = "{$tmpSql} LIMIT {$item['limit']} OFFSET {$item['offset']}";
187-
$result = call_user_func($this->callback, $tmpSql);
188-
$data = array_merge($data, $result);
189-
$this->trace[] = [
190-
'sql' => $tmpSql,
191-
'rowCount' => count($result),
192-
];
187+
$tmpSql = str_replace(static::$tableSymbol, $tableName, $sql);
188+
$tmpSql = "{$tmpSql} LIMIT {$item['limit']} OFFSET {$item['offset']}";
189+
$this->log(['sql' => $tmpSql]);
190+
$result = call_user_func($this->callback, $tmpSql);
191+
$this->log(['resultCount' => count($result)]);
192+
$data = array_merge($data, $result);
193193
}
194194
return $data;
195195
}
@@ -208,10 +208,12 @@ protected function stats($table)
208208
if (!empty($this->where)) {
209209
$sql .= " WHERE {$this->where}";
210210
}
211-
$sql = str_replace(static::$tableSymbol, $tableName, $sql);
212-
$result = call_user_func($this->callback, $sql);
213-
$first = array_pop($result);
214-
$count = array_pop($first);
211+
$sql = str_replace(static::$tableSymbol, $tableName, $sql);
212+
$this->log(['sql' => $sql]);
213+
$result = call_user_func($this->callback, $sql);
214+
$first = array_pop($result);
215+
$count = array_pop($first);
216+
$this->log(['result' => $count]);
215217
$start = $end;
216218
$end += $count;
217219
$stats[$tableName] = [
@@ -243,31 +245,24 @@ protected function range($stats)
243245
'offset' => $start - $item['start'],
244246
'limit' => $end - $start,
245247
];
246-
continue;
247-
}
248-
if ($before) {
248+
} elseif ($before) {
249249
$tables[$table] = [
250250
'offset' => $start - $item['start'],
251251
'limit' => $item['end'] - $start,
252252
];
253-
if ($tables[$table]['limit'] == 0) {
254-
unset($tables[$table]);
255-
}
256-
continue;
257-
}
258-
if ($after) {
253+
} elseif ($after) {
259254
$tables[$table] = [
260255
'offset' => 0,
261256
'limit' => $end - $item['start'],
262257
];
263-
continue;
264-
}
265-
if ($center) {
258+
} elseif ($center) {
266259
$tables[$table] = [
267260
'offset' => 0,
268261
'limit' => $item['end'] - $item['start'],
269262
];
270-
continue;
263+
}
264+
if (isset($tables[$table]['limit']) && $tables[$table]['limit'] === 0) {
265+
unset($tables[$table]);
271266
}
272267
}
273268
return $tables;
@@ -321,6 +316,14 @@ public function count()
321316
return $number ?: 0;
322317
}
323318

319+
/**
320+
* 记录日志
321+
*/
322+
protected function log($message)
323+
{
324+
$this->trace[] = $message;
325+
}
326+
324327
/**
325328
* 获取追踪数据
326329
* @return array

0 commit comments

Comments
 (0)