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