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\Interfaces\Clause\ColInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* ColTrait |
21
|
|
|
* |
22
|
|
|
* Implementation of ColInterface |
23
|
|
|
* |
24
|
|
|
* @package Phossa2\Query |
25
|
|
|
* @author Hong Zhang <[email protected]> |
26
|
|
|
* @see ColInterface |
27
|
|
|
* @version 2.0.0 |
28
|
|
|
* @since 2.0.0 added |
29
|
|
|
*/ |
30
|
|
|
trait ColTrait |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var bool |
34
|
|
|
* @access protected |
35
|
|
|
*/ |
36
|
|
|
protected $is_distinct = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritDoc} |
40
|
|
|
*/ |
41
|
|
|
public function col($col, /*# string */ $alias = '') |
42
|
|
|
{ |
43
|
|
|
return $this->realCol($col, $alias); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritDoc} |
48
|
|
|
*/ |
49
|
|
|
public function distinct($col = '', /*# string */ $alias = '') |
50
|
|
|
{ |
51
|
|
|
$this->is_distinct = true; |
52
|
|
|
return empty($col) ? $this : $this->col($col, $alias); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritDoc} |
57
|
|
|
*/ |
58
|
|
|
public function count(/*# string */ $col, /*# string */ $alias = '') |
59
|
|
|
{ |
60
|
|
|
return $this->colTpl('COUNT(%s)', $col, $alias); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritDoc} |
65
|
|
|
*/ |
66
|
|
|
public function min(/*# string */ $col, /*# string */ $alias = '') |
67
|
|
|
{ |
68
|
|
|
return $this->colTpl('MIN(%s)', $col, $alias); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritDoc} |
73
|
|
|
*/ |
74
|
|
|
public function max(/*# string */ $col, /*# string */ $alias = '') |
75
|
|
|
{ |
76
|
|
|
return $this->colTpl('MAX(%s)', $col, $alias); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritDoc} |
81
|
|
|
*/ |
82
|
|
|
public function avg(/*# string */ $col, /*# string */ $alias = '') |
83
|
|
|
{ |
84
|
|
|
return $this->colTpl('AVG(%s)', $col, $alias); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritDoc} |
89
|
|
|
*/ |
90
|
|
|
public function sum(/*# string */ $col, /*# string */ $alias = '') |
91
|
|
|
{ |
92
|
|
|
return $this->colTpl('SUM(%s)', $col, $alias); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritDoc} |
97
|
|
|
*/ |
98
|
|
|
public function colTpl( |
99
|
|
|
/*# string */ $template, |
100
|
|
|
$col, |
101
|
|
|
/*# string */ $alias = '' |
102
|
|
|
) { |
103
|
|
|
return $this->colRaw($this->clauseTpl($template, $col), $alias); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritDoc} |
108
|
|
|
*/ |
109
|
|
|
public function colRaw($rawString, /*# string */ $alias = '') |
110
|
|
|
{ |
111
|
|
|
return $this->realCol($rawString, $alias, true); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param mixed $col column/field specification[s] |
116
|
|
|
* @param string $alias column alias name |
117
|
|
|
* @param bool $rawMode raw mode |
118
|
|
|
*/ |
119
|
|
|
protected function realCol( |
120
|
|
|
$col, |
121
|
|
|
/*# string */ $alias = '', |
122
|
|
|
$rawMode = false |
123
|
|
|
) { |
124
|
|
|
if (empty($col)) { |
125
|
|
|
return $this; |
126
|
|
|
} elseif (is_array($col)) { |
127
|
|
|
$this->multipleCol($col, $rawMode); |
128
|
|
|
} else { |
129
|
|
|
$clause = &$this->getClause('COL'); |
130
|
|
|
$raw = $this->isRaw($col, $rawMode); |
131
|
|
|
if ('' === $alias) { |
132
|
|
|
$clause[] = [$col, $raw]; |
133
|
|
|
} else { |
134
|
|
|
$clause[(string) $alias] = [$col, $raw]; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* from multiple tables |
142
|
|
|
* |
143
|
|
|
* @param array $cols |
144
|
|
|
* @param bool $rawMode |
145
|
|
|
* @access protected |
146
|
|
|
*/ |
147
|
|
|
protected function multipleCol(array $cols, /*# bool */ $rawMode) |
148
|
|
|
{ |
149
|
|
|
foreach ($cols as $key => $val) { |
150
|
|
|
if (is_int($key)) { |
151
|
|
|
$this->realCol($val, '', $rawMode); |
152
|
|
|
} else { |
153
|
|
|
$this->realCol($key, $val, $rawMode); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Build fields |
160
|
|
|
* |
161
|
|
|
* @param array $settings |
162
|
|
|
* @return string |
163
|
|
|
* @access protected |
164
|
|
|
*/ |
165
|
|
|
protected function buildCol(array $settings)/*# : string */ |
166
|
|
|
{ |
167
|
|
|
$result = []; |
168
|
|
|
$clause = &$this->getClause('COL'); |
169
|
|
|
|
170
|
|
|
if (empty($clause)) { |
171
|
|
|
$result[] = '*'; |
172
|
|
|
} else { |
173
|
|
View Code Duplication |
foreach ($clause as $as => $col) { |
|
|
|
|
174
|
|
|
$alias = $this->quoteAlias($as); |
175
|
|
|
$field = $this->quoteItem($col[0], $col[1]); |
176
|
|
|
$result[] = $field . $alias; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $this->joinClause('', ',', $result, $settings); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Build DISTINCT |
185
|
|
|
* |
186
|
|
|
* @param array $settings |
187
|
|
|
* @return string |
188
|
|
|
* @access protected |
189
|
|
|
*/ |
190
|
|
|
protected function buildDistinct(array $settings)/*# : string */ |
|
|
|
|
191
|
|
|
{ |
192
|
|
|
return $this->is_distinct ? ' DISTINCT' : ''; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
abstract protected function isRaw($str, /*# bool */ $rawMode)/*# : bool */; |
196
|
|
|
abstract protected function quoteAlias($alias)/*# : string */; |
197
|
|
|
abstract protected function quoteItem($item, /*# bool */ $rawMode = false)/*# : string */; |
198
|
|
|
abstract protected function clauseTpl(/*# string */ $template, $col)/*# : string */; |
199
|
|
|
abstract protected function &getClause(/*# string */ $clauseName)/*# : array */; |
200
|
|
|
abstract protected function joinClause( |
201
|
|
|
/*# : string */ $prefix, |
202
|
|
|
/*# : string */ $seperator, |
203
|
|
|
array $clause, |
204
|
|
|
array $settings |
205
|
|
|
)/*# : string */; |
206
|
|
|
} |
207
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.