Skip to content

Commit a672147

Browse files
committed
改进join自关联查询 改进聚合查询对json字段的支持
1 parent ad42187 commit a672147

File tree

9 files changed

+69
-91
lines changed

9 files changed

+69
-91
lines changed

src/db/Builder.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,12 @@ protected function parseArrayData($data)
199199

200200
/**
201201
* 字段名分析
202-
* @access protected
202+
* @access public
203203
* @param Query $query 查询对象
204204
* @param string $key
205205
* @return string
206206
*/
207-
protected function parseKey(Query $query, $key)
207+
public function parseKey(Query $query, $key)
208208
{
209209
return $key;
210210
}
@@ -249,12 +249,9 @@ protected function parseTable(Query $query, $tables)
249249
{
250250
$item = [];
251251
$options = $query->getOptions();
252+
252253
foreach ((array) $tables as $key => $table) {
253254
if (!is_numeric($key)) {
254-
if (strpos($key, '@think')) {
255-
$key = strstr($key, '@think', true);
256-
}
257-
258255
$key = $this->connection->parseSqlTable($key);
259256
$item[] = $this->parseKey($query, $key) . ' ' . $this->parseKey($query, $table);
260257
} else {

src/db/Connection.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,21 @@ public function column(Query $query, $field, $key = '')
13661366
return $result;
13671367
}
13681368

1369+
/**
1370+
* 得到某个字段的值
1371+
* @access public
1372+
* @param Query $query 查询对象
1373+
* @param string $aggregate 聚合方法
1374+
* @param string $field 字段名
1375+
* @return mixed
1376+
*/
1377+
public function aggregate(Query $query, $aggregate, $field)
1378+
{
1379+
$field = $aggregate . '(' . $this->builder->parseKey($query, $field) . ') AS tp_' . strtolower($aggregate);
1380+
1381+
return $this->value($query, $field, 0);
1382+
}
1383+
13691384
/**
13701385
* 执行查询但只返回PDOStatement对象
13711386
* @access public

src/db/Mongo.php

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,14 @@ public function count($field = null)
174174
* @access public
175175
* @param string $aggregate 聚合指令
176176
* @param string $field 字段名
177-
* @param bool $force 强制转为数字类型
177+
* @param bool $force 强制转为数字类型
178178
* @return mixed
179179
*/
180180
public function aggregate($aggregate, $field, $force = false)
181181
{
182182
$this->parseOptions();
183183

184-
$result = $this->cmd('aggregate', [$aggregate, $field]);
184+
$result = $this->cmd('aggregate', [strtolower($aggregate), $field]);
185185
$value = isset($result[0]['result'][0]['aggregate']) ? $result[0]['result'][0]['aggregate'] : 0;
186186

187187
if ($force) {
@@ -191,52 +191,6 @@ public function aggregate($aggregate, $field, $force = false)
191191
return $value;
192192
}
193193

194-
/**
195-
* MAX查询
196-
* @access public
197-
* @param string $field 字段名
198-
* @param bool $force 强制转为数字类型
199-
* @return float
200-
*/
201-
public function max($field, $force = true)
202-
{
203-
return $this->aggregate('max', $field, $force);
204-
}
205-
206-
/**
207-
* MIN查询
208-
* @access public
209-
* @param string $field 字段名
210-
* @param bool $force 强制转为数字类型
211-
* @return mixed
212-
*/
213-
public function min($field, $force = true)
214-
{
215-
return $this->aggregate('min', $field, $force);
216-
}
217-
218-
/**
219-
* SUM查询
220-
* @access public
221-
* @param string $field 字段名
222-
* @return float
223-
*/
224-
public function sum($field)
225-
{
226-
return $this->aggregate('sum', $field);
227-
}
228-
229-
/**
230-
* AVG查询
231-
* @access public
232-
* @param string $field 字段名
233-
* @return float
234-
*/
235-
public function avg($field)
236-
{
237-
return $this->aggregate('avg', $field);
238-
}
239-
240194
/**
241195
* 字段值(延迟)增长
242196
* @access public

src/db/Query.php

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -480,19 +480,16 @@ public function getPartitionTableName($data, $field, $rule = [])
480480
* @access public
481481
* @param string $field 字段名
482482
* @param mixed $default 默认值
483-
* @param bool $force 强制转为数字类型
484483
* @return mixed
485484
*/
486-
public function value($field, $default = null, $force = false)
485+
public function value($field, $default = null)
487486
{
488487
$this->parseOptions();
489488

490489
$result = $this->connection->value($this, $field, $default);
491490

492491
if (!empty($this->options['fetch_sql'])) {
493492
return $result;
494-
} elseif ($force) {
495-
$result += 0;
496493
}
497494

498495
return $result;
@@ -512,6 +509,29 @@ public function column($field, $key = '')
512509
return $this->connection->column($this, $field, $key);
513510
}
514511

512+
/**
513+
* 聚合查询
514+
* @access public
515+
* @param string $aggregate 聚合方法
516+
* @param string $field 字段名
517+
* @param bool $force 强制转为数字类型
518+
* @return mixed
519+
*/
520+
public function aggregate($aggregate, $field, $force = false)
521+
{
522+
$this->parseOptions();
523+
524+
$result = $this->connection->aggregate($this, $aggregate, $field);
525+
526+
if (!empty($this->options['fetch_sql'])) {
527+
return $result;
528+
} elseif ($force) {
529+
$result += 0;
530+
}
531+
532+
return $result;
533+
}
534+
515535
/**
516536
* COUNT查询
517537
* @access public
@@ -531,56 +551,56 @@ public function count($field = '*')
531551
$query->fetchSql(true);
532552
}
533553

534-
return $query->value('COUNT(*) AS tp_count', 0, true);
554+
return $query->aggregate('COUNT', '*', true);
535555
}
536556

537-
return $this->value('COUNT(' . $field . ') AS tp_count', 0, true);
557+
return $this->aggregate('COUNT', $field, true);
538558
}
539559

540560
/**
541561
* SUM查询
542562
* @access public
543-
* @param string $field 字段名
563+
* @param string $field 字段名
544564
* @return float|int
545565
*/
546566
public function sum($field)
547567
{
548-
return $this->value('SUM(' . $field . ') AS tp_sum', 0, true);
568+
return $this->aggregate('SUM', $field, true);
549569
}
550570

551571
/**
552572
* MIN查询
553573
* @access public
554-
* @param string $field 字段名
555-
* @param bool $force 强制转为数字类型
574+
* @param string $field 字段名
575+
* @param bool $force 强制转为数字类型
556576
* @return mixed
557577
*/
558578
public function min($field, $force = true)
559579
{
560-
return $this->value('MIN(' . $field . ') AS tp_min', 0, $force);
580+
return $this->aggregate('MIN', $field, $force);
561581
}
562582

563583
/**
564584
* MAX查询
565585
* @access public
566-
* @param string $field 字段名
567-
* @param bool $force 强制转为数字类型
586+
* @param string $field 字段名
587+
* @param bool $force 强制转为数字类型
568588
* @return mixed
569589
*/
570590
public function max($field, $force = true)
571591
{
572-
return $this->value('MAX(' . $field . ') AS tp_max', 0, $force);
592+
return $this->aggregate('MAX', $field, $force);
573593
}
574594

575595
/**
576596
* AVG查询
577597
* @access public
578-
* @param string $field 字段名
598+
* @param string $field 字段名
579599
* @return float|int
580600
*/
581601
public function avg($field)
582602
{
583-
return $this->value('AVG(' . $field . ') AS tp_avg', 0, true);
603+
return $this->aggregate('AVG', $field, true);
584604
}
585605

586606
/**
@@ -708,8 +728,7 @@ protected function getJoinTable($join, &$alias = null)
708728
{
709729
// 传入的表名为数组
710730
if (is_array($join)) {
711-
$table = key($join);
712-
$alias = current($join);
731+
$table = $join;
713732
} else {
714733
$join = trim($join);
715734

@@ -732,17 +751,10 @@ protected function getJoinTable($join, &$alias = null)
732751
$table = $this->getTable($table);
733752
}
734753
}
735-
}
736754

737-
if (isset($alias) && $table != $alias) {
738-
if (isset($this->options['alias'][$table])) {
739-
$table = $table . '@think' . uniqid();
740-
} elseif ($this->gettable() == $table) {
741-
$table = $table . '@think' . uniqid();
755+
if (isset($alias) && $table != $alias) {
756+
$table = [$table => $alias];
742757
}
743-
744-
$table = [$table => $alias];
745-
$this->alias($table);
746758
}
747759

748760
return $table;

src/db/builder/Mysql.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ protected function parseRegexp(Query $query, $key, $exp, $value, $field)
102102

103103
/**
104104
* 字段和表名处理
105-
* @access protected
105+
* @access public
106106
* @param Query $query 查询对象
107107
* @param string $key
108108
* @return string
109109
*/
110-
protected function parseKey(Query $query, $key)
110+
public function parseKey(Query $query, $key)
111111
{
112112
$key = trim($key);
113113

src/db/builder/Oracle.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ protected function parseLock(Query $query, $lock = false)
6161

6262
/**
6363
* 字段和表名处理
64-
* @access protected
64+
* @access public
6565
* @param Query $query 查询对象
6666
* @param string $key
6767
* @return string
6868
*/
69-
protected function parseKey(Query $query, $key)
69+
public function parseKey(Query $query, $key)
7070
{
7171
$key = trim($key);
7272

src/db/builder/Pgsql.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ public function parseLimit(Query $query, $limit)
4848

4949
/**
5050
* 字段和表名处理
51-
* @access protected
51+
* @access public
5252
* @param Query $query 查询对象
5353
* @param string $key
5454
* @return string
5555
*/
56-
protected function parseKey(Query $query, $key)
56+
public function parseKey(Query $query, $key)
5757
{
5858
$key = trim($key);
5959

src/db/builder/Sqlite.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ protected function parseRand(Query $query)
5656

5757
/**
5858
* 字段和表名处理
59-
* @access protected
59+
* @access public
6060
* @param Query $query 查询对象
6161
* @param string $key
6262
* @return string
6363
*/
64-
protected function parseKey(Query $query, $key)
64+
public function parseKey(Query $query, $key)
6565
{
6666
$key = trim($key);
6767
if (strpos($key, '.')) {

src/db/builder/Sqlsrv.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ protected function parseRand(Query $query)
7272

7373
/**
7474
* 字段和表名处理
75-
* @access protected
75+
* @access public
7676
* @param Query $query 查询对象
7777
* @param string $key
7878
* @return string
7979
*/
80-
protected function parseKey(Query $query, $key)
80+
public function parseKey(Query $query, $key)
8181
{
8282
$key = trim($key);
8383

0 commit comments

Comments
 (0)