1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Query |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Query\Traits\Clause; |
16
|
|
|
|
17
|
|
|
use Phossa2\Query\Misc\Template; |
18
|
|
|
use Phossa2\Query\Interfaces\Clause\GroupInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* GroupTrait |
22
|
|
|
* |
23
|
|
|
* @package Phossa2\Query |
24
|
|
|
* @author Hong Zhang <[email protected]> |
25
|
|
|
* @see GroupInterface |
26
|
|
|
* @version 2.0.0 |
27
|
|
|
* @since 2.0.0 added |
28
|
|
|
*/ |
29
|
|
|
trait GroupTrait |
30
|
|
|
{ |
31
|
|
|
use AbstractTrait; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritDoc} |
35
|
|
|
*/ |
36
|
|
|
public function group($col) |
37
|
|
|
{ |
38
|
|
|
// support multiple group by |
39
|
|
|
if (func_num_args() > 1) { |
40
|
|
|
$col = func_get_args(); |
41
|
|
|
} |
42
|
|
|
return $this->realGroup($col); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritDoc} |
47
|
|
|
*/ |
48
|
|
|
public function groupDesc($col) |
49
|
|
|
{ |
50
|
|
|
return $this->realGroup($col, 'DESC'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritDoc} |
55
|
|
|
*/ |
56
|
|
|
public function groupTpl(/*# string */ $template, $col) |
57
|
|
|
{ |
58
|
|
|
return $this->realGroup(new Template($template, $col), '', true); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritDoc} |
63
|
|
|
*/ |
64
|
|
|
public function groupRaw(/*# string */ $rawString) |
65
|
|
|
{ |
66
|
|
|
$rawString = $this->positionedParam($rawString, func_get_args(), 1); |
67
|
|
|
return $this->realGroup($rawString, '', true); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* real group by |
72
|
|
|
* @param string|string[]|Template $col column[s] |
73
|
|
|
* @param string $suffix ''|ASC|DESC |
74
|
|
|
* @param bool $rawMode |
75
|
|
|
* @return $this |
76
|
|
|
* @access protected |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
protected function realGroup( |
79
|
|
|
$col, |
80
|
|
|
/*# sting */ $suffix = '', |
81
|
|
|
/*# bool */ $rawMode = false) |
82
|
|
|
{ |
83
|
|
|
if (is_array($col)) { |
84
|
|
|
$this->multipleGroup($col, $suffix); |
85
|
|
|
} else { |
86
|
|
|
$clause = &$this->getClause('GROUP BY'); |
87
|
|
|
$part = [$col, $this->isRaw($col, $rawMode)]; |
88
|
|
|
if (!empty($suffix)) { |
89
|
|
|
$part[] = $suffix; |
90
|
|
|
} |
91
|
|
|
$clause[] = $part; |
92
|
|
|
} |
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Multitple groupbys |
98
|
|
|
* |
99
|
|
|
* @param array $cols |
100
|
|
|
* @param string $suffix |
101
|
|
|
* @access protected |
102
|
|
|
*/ |
103
|
|
|
protected function multipleGroup(array $cols, /*# string */ $suffix) |
104
|
|
|
{ |
105
|
|
|
foreach ($cols as $col) { |
106
|
|
|
$this->realGroup($col, $suffix); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Build GROUP BY |
112
|
|
|
* |
113
|
|
|
* @param string $prefix |
114
|
|
|
* @param array $settings |
115
|
|
|
* @return string |
116
|
|
|
* @access protected |
117
|
|
|
*/ |
118
|
|
|
protected function buildGroup( |
119
|
|
|
/*# string */ $prefix, |
120
|
|
|
array $settings |
121
|
|
|
)/*# : string */ { |
122
|
|
|
return $this->buildClause('GROUP BY', $prefix, $settings); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|