Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
96.67% |
29 / 30 |
|
90.91% |
10 / 11 |
CRAP | |
0.00% |
0 / 1 |
AbstractClause | |
96.67% |
29 / 30 |
|
90.91% |
10 / 11 |
22 | |
0.00% |
0 / 1 |
setTable | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
hasAlias | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getAlias | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setAlias | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getTable | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setValues | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
addValue | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
4.13 | |||
addNamedValue | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
getValues | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isSupportedFunction | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
render | n/a |
0 / 0 |
n/a |
0 / 0 |
0 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (http://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@nolainteractive.com> |
7 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
8 | * @license http://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Db\Sql; |
15 | |
16 | /** |
17 | * Abstract clause class |
18 | * |
19 | * @category Pop |
20 | * @package Pop\Db |
21 | * @author Nick Sagona, III <dev@nolainteractive.com> |
22 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
23 | * @license http://www.popphp.org/license New BSD License |
24 | * @version 6.5.0 |
25 | */ |
26 | abstract class AbstractClause extends AbstractSql |
27 | { |
28 | |
29 | /** |
30 | * Table |
31 | * @var mixed |
32 | */ |
33 | protected mixed $table = null; |
34 | |
35 | /** |
36 | * Alias |
37 | * @var ?string |
38 | */ |
39 | protected ?string $alias = null; |
40 | |
41 | /** |
42 | * Values |
43 | * @var array |
44 | */ |
45 | protected array $values = []; |
46 | |
47 | /** |
48 | * Supported standard SQL aggregate functions |
49 | * @var array |
50 | */ |
51 | protected static array $aggregateFunctions = [ |
52 | 'AVG', 'COUNT', 'MAX', 'MIN', 'SUM' |
53 | ]; |
54 | |
55 | /** |
56 | * Supported standard SQL math functions |
57 | * @var array |
58 | */ |
59 | protected static array $mathFunctions = [ |
60 | 'ABS', 'RAND', 'SQRT', 'POW', 'POWER', 'EXP', 'LN', 'LOG', 'LOG10', 'GREATEST', 'LEAST', |
61 | 'DIV', 'MOD', 'ROUND', 'TRUNC', 'CEIL', 'CEILING', 'FLOOR', 'COS', 'ACOS', 'ACOSH', 'SIN', |
62 | 'SINH', 'ASIN', 'ASINH', 'TAN', 'TANH', 'ATANH', 'ATAN2', |
63 | ]; |
64 | |
65 | /** |
66 | * Supported standard SQL string functions |
67 | * @var array |
68 | */ |
69 | protected static array $stringFunctions = [ |
70 | 'CONCAT', 'FORMAT', 'INSTR', 'LCASE', 'LEFT', 'LENGTH', 'LOCATE', 'LOWER', 'LPAD', |
71 | 'LTRIM', 'POSITION', 'QUOTE', 'REGEXP', 'REPEAT', 'REPLACE', 'REVERSE', 'RIGHT', 'RPAD', |
72 | 'RTRIM', 'SPACE', 'STRCMP', 'SUBSTRING', 'SUBSTR', 'TRIM', 'UCASE', 'UPPER' |
73 | ]; |
74 | |
75 | /** |
76 | * Set the table |
77 | * |
78 | * @param mixed $table |
79 | * @return AbstractClause |
80 | */ |
81 | public function setTable(mixed $table): AbstractClause |
82 | { |
83 | $this->table = $table; |
84 | return $this; |
85 | } |
86 | |
87 | /** |
88 | * Determine if there is an alias |
89 | * |
90 | * @return bool |
91 | */ |
92 | public function hasAlias(): bool |
93 | { |
94 | return ($this->alias !== null); |
95 | } |
96 | |
97 | /** |
98 | * Get the alias |
99 | * |
100 | * @return ?string |
101 | */ |
102 | public function getAlias(): ?string |
103 | { |
104 | return $this->alias; |
105 | } |
106 | |
107 | /** |
108 | * Set the alias |
109 | * |
110 | * @param string $alias |
111 | * @return AbstractClause |
112 | */ |
113 | public function setAlias(string $alias): AbstractClause |
114 | { |
115 | $this->alias = $alias; |
116 | return $this; |
117 | } |
118 | |
119 | /** |
120 | * Get the table |
121 | * |
122 | * @return ?string |
123 | */ |
124 | public function getTable(): ?string |
125 | { |
126 | return $this->table; |
127 | } |
128 | |
129 | /** |
130 | * Set the values |
131 | * |
132 | * @param array $values |
133 | * @return AbstractClause |
134 | */ |
135 | public function setValues(array $values): AbstractClause |
136 | { |
137 | foreach ($values as $column => $value) { |
138 | if ($this->isParameter($value, $column)) { |
139 | $values[$column] = $this->getParameter($value, $column); |
140 | } |
141 | } |
142 | |
143 | $this->values = $values; |
144 | return $this; |
145 | } |
146 | |
147 | /** |
148 | * Add a value |
149 | * |
150 | * @param mixed $value |
151 | * @return AbstractClause |
152 | */ |
153 | public function addValue(mixed $value): AbstractClause |
154 | { |
155 | if (!is_array($value) && !is_object($value)) { |
156 | if ($this->isParameter($value)) { |
157 | $value = $this->getParameter($value); |
158 | } |
159 | $this->values[] = $value; |
160 | } |
161 | return $this; |
162 | } |
163 | |
164 | /** |
165 | * Add a named value |
166 | * |
167 | * @param string $name |
168 | * @param mixed $value |
169 | * @return AbstractClause |
170 | */ |
171 | public function addNamedValue(string $name, mixed $value): AbstractClause |
172 | { |
173 | if (!is_array($value) && !is_object($value)) { |
174 | if ($this->isParameter($value, $name)) { |
175 | $value = $this->getParameter($value, $name); |
176 | } |
177 | $this->values[$name] = $value; |
178 | } |
179 | return $this; |
180 | } |
181 | |
182 | /** |
183 | * Get the values |
184 | * |
185 | * @return array |
186 | */ |
187 | public function getValues(): array |
188 | { |
189 | return $this->values; |
190 | } |
191 | |
192 | /** |
193 | * Get a value |
194 | * |
195 | * @param string $name |
196 | * @return mixed |
197 | */ |
198 | public function getValue(string $name): mixed |
199 | { |
200 | return $this->values[$name] ?? null; |
201 | } |
202 | |
203 | /** |
204 | * Check if value contains a standard SQL supported function |
205 | * |
206 | * @param string $value |
207 | * @return bool |
208 | */ |
209 | public static function isSupportedFunction(string $value): bool |
210 | { |
211 | if (str_contains($value, '(')) { |
212 | $value = trim(substr($value, 0, strpos($value, '('))); |
213 | } |
214 | $value = strtoupper($value); |
215 | |
216 | return (in_array($value, static::$aggregateFunctions) || |
217 | in_array($value, static::$mathFunctions) || |
218 | in_array($value, static::$stringFunctions)); |
219 | } |
220 | |
221 | /** |
222 | * Render the statement |
223 | * |
224 | * @return string |
225 | */ |
226 | abstract public function render(): string; |
227 | |
228 | } |