|
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\ColInterface; |
|
19
|
|
|
use Phossa2\Query\Interfaces\Statement\SelectStatementInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* ColTrait |
|
23
|
|
|
* |
|
24
|
|
|
* Implementation of ColInterface |
|
25
|
|
|
* |
|
26
|
|
|
* @package Phossa2\Query |
|
27
|
|
|
* @author Hong Zhang <[email protected]> |
|
28
|
|
|
* @see ColInterface |
|
29
|
|
|
* @version 2.0.0 |
|
30
|
|
|
* @since 2.0.0 added |
|
31
|
|
|
*/ |
|
32
|
|
|
trait ColTrait |
|
33
|
|
|
{ |
|
34
|
|
|
use AbstractTrait; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var bool |
|
38
|
|
|
* @access protected |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $is_distinct = false; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritDoc} |
|
44
|
|
|
*/ |
|
45
|
|
|
public function col($col = '', /*# string */ $alias = '') |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->realCol($col, $alias); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritDoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
public function distinct() |
|
54
|
|
|
{ |
|
55
|
|
|
$this->is_distinct = true; |
|
56
|
|
|
return $this->col(func_get_args()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritDoc} |
|
61
|
|
|
*/ |
|
62
|
|
|
public function cnt(/*# string */ $col, /*# string */ $alias = '') |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->colTpl('COUNT(%s)', $col, $alias); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritDoc} |
|
69
|
|
|
*/ |
|
70
|
|
|
public function min(/*# string */ $col, /*# string */ $alias = '') |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->colTpl('MIN(%s)', $col, $alias); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* {@inheritDoc} |
|
77
|
|
|
*/ |
|
78
|
|
|
public function max(/*# string */ $col, /*# string */ $alias = '') |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->colTpl('MAX(%s)', $col, $alias); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* {@inheritDoc} |
|
85
|
|
|
*/ |
|
86
|
|
|
public function avg(/*# string */ $col, /*# string */ $alias = '') |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->colTpl('AVG(%s)', $col, $alias); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritDoc} |
|
93
|
|
|
*/ |
|
94
|
|
|
public function sum(/*# string */ $col, /*# string */ $alias = '') |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->colTpl('SUM(%s)', $col, $alias); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritDoc} |
|
101
|
|
|
*/ |
|
102
|
|
|
public function colTpl( |
|
103
|
|
|
/*# string */ $template, |
|
104
|
|
|
$col, |
|
105
|
|
|
/*# string */ $alias = '' |
|
106
|
|
|
) { |
|
107
|
|
|
return $this->realCol(new Template($template, $col), $alias); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* {@inheritDoc} |
|
112
|
|
|
*/ |
|
113
|
|
|
public function colRaw(/*# string */ $rawString, array $params = []) |
|
114
|
|
|
{ |
|
115
|
|
|
$rawString = $this->positionedParam($rawString, $params); |
|
116
|
|
|
return $this->realCol($rawString, '', true); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param mixed $col column/field specification[s] |
|
121
|
|
|
* @param string $alias column alias name |
|
122
|
|
|
* @param bool $rawMode raw mode |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function realCol( |
|
125
|
|
|
$col, |
|
126
|
|
|
/*# string */ $alias = '', |
|
127
|
|
|
$rawMode = false |
|
128
|
|
|
) { |
|
129
|
|
|
if (empty($col)) { |
|
130
|
|
|
return $this; |
|
131
|
|
|
} elseif (is_array($col)) { |
|
132
|
|
|
$this->multipleCol($col, $rawMode); |
|
133
|
|
|
} else { |
|
134
|
|
|
$clause = &$this->getClause('COL'); |
|
135
|
|
|
$raw = $this->isRaw($col, $rawMode); |
|
136
|
|
|
if ('' === $alias) { |
|
137
|
|
|
$clause[] = [$col, $raw]; |
|
138
|
|
|
} else { |
|
139
|
|
|
$clause[(string) $alias] = [$col, $raw]; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
return $this; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* from multiple tables |
|
147
|
|
|
* |
|
148
|
|
|
* @param array $cols |
|
149
|
|
|
* @param bool $rawMode |
|
150
|
|
|
* @access protected |
|
151
|
|
|
*/ |
|
152
|
|
|
protected function multipleCol(array $cols, /*# bool */ $rawMode) |
|
153
|
|
|
{ |
|
154
|
|
|
foreach ($cols as $key => $val) { |
|
155
|
|
|
if (is_int($key)) { |
|
156
|
|
|
$this->realCol($val, '', $rawMode); |
|
157
|
|
|
} else { |
|
158
|
|
|
$this->realCol($key, $val, $rawMode); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Build fields |
|
165
|
|
|
* |
|
166
|
|
|
* @param strin $prefix prefix afront of the clause |
|
167
|
|
|
* @param array $settings |
|
168
|
|
|
* @return string |
|
169
|
|
|
* @access protected |
|
170
|
|
|
*/ |
|
171
|
|
|
protected function buildCol( |
|
172
|
|
|
/*# string */ $prefix, |
|
173
|
|
|
array $settings |
|
174
|
|
|
)/*# : string */ { |
|
175
|
|
|
$clause = &$this->getClause('COL'); |
|
176
|
|
|
if ($this instanceof SelectStatementInterface && empty($clause)) { |
|
177
|
|
|
$clauseParts = ['*']; |
|
178
|
|
|
} else { |
|
179
|
|
|
$clauseParts = []; |
|
180
|
|
|
} |
|
181
|
|
|
return $this->buildClause('COL', $prefix, $settings, $clauseParts); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Build DISTINCT |
|
186
|
|
|
* |
|
187
|
|
|
* @param string $prefix |
|
188
|
|
|
* @param array $settings |
|
189
|
|
|
* @return string |
|
190
|
|
|
* @access protected |
|
191
|
|
|
*/ |
|
192
|
|
|
protected function buildDistinct( |
|
193
|
|
|
/*# string */ $prefix, |
|
194
|
|
|
array $settings |
|
195
|
|
|
)/*# : string */ { |
|
196
|
|
|
return $this->is_distinct ? ' DISTINCT' : $prefix; |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|