Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
34 / 34 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
State | |
100.00% |
34 / 34 |
|
100.00% |
9 / 9 |
15 | |
100.00% |
1 / 1 |
__call | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
5 | |||
appendSelectString | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
appendSetArrayKeys | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
setOrderArray | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
appendGroupArray | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
appendValues | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
appendWhereValues | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
appendMap | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
appendHavingMap | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 |
1 | <?php declare(strict_types=1); |
2 | /** |
3 | * Query |
4 | * |
5 | * SQL Query Builder / Database Abstraction Layer |
6 | * |
7 | * PHP version 8.1 |
8 | * |
9 | * @package Query |
10 | * @author Timothy J. Warren <tim@timshome.page> |
11 | * @copyright 2012 - 2023 Timothy J. Warren |
12 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
13 | * @link https://git.timshomepage.net/aviat/Query |
14 | * @version 4.0.0 |
15 | */ |
16 | |
17 | namespace Query; |
18 | |
19 | use function is_array; |
20 | |
21 | /** |
22 | * Query builder state |
23 | * |
24 | * @method getFromString(): string |
25 | * @method getGroupArray(): array |
26 | * @method getGroupString(): string |
27 | * @method getHavingMap(): array |
28 | * @method getLimit(): int|null |
29 | * @method getOffset() |
30 | * @method getOrderArray(): array |
31 | * @method getOrderString(): string |
32 | * @method getQueryMap(): array |
33 | * @method getSelectString(): string |
34 | * @method getSetArrayKeys(): array |
35 | * @method getSetString(): string |
36 | * @method getValues(): array |
37 | * @method getWhereValues(): array |
38 | * |
39 | * @method setFromString(string $fromString): self |
40 | * @method setGroupArray(array $array): self |
41 | * @method setGroupString(string $groupString): self |
42 | * @method setLimit(int $limit): self |
43 | * @method setOffset(?int $offset): self |
44 | * @method setOrderString(string $orderString): self |
45 | * @method setSelectString(string $selectString): self |
46 | * @method setSetArrayKeys(array $arrayKeys): self |
47 | * @method setSetString(string $setString): self |
48 | */ |
49 | class State |
50 | { |
51 | // -------------------------------------------------------------------------- |
52 | // ! SQL Clause Strings |
53 | // -------------------------------------------------------------------------- |
54 | /** |
55 | * Compiled 'select' clause |
56 | */ |
57 | protected string $selectString = ''; |
58 | |
59 | /** |
60 | * Compiled 'from' clause |
61 | */ |
62 | protected string $fromString = ''; |
63 | |
64 | /** |
65 | * Compiled arguments for insert / update |
66 | */ |
67 | protected string $setString = ''; |
68 | |
69 | /** |
70 | * Order by clause |
71 | */ |
72 | protected string $orderString = ''; |
73 | |
74 | /** |
75 | * Group by clause |
76 | */ |
77 | protected string $groupString = ''; |
78 | |
79 | // -------------------------------------------------------------------------- |
80 | // ! SQL Clause Arrays |
81 | // -------------------------------------------------------------------------- |
82 | /** |
83 | * Keys for insert/update statement |
84 | */ |
85 | protected array $setArrayKeys = []; |
86 | |
87 | /** |
88 | * Key/val pairs for order by clause |
89 | */ |
90 | protected array $orderArray = []; |
91 | |
92 | /** |
93 | * Key/val pairs for group by clause |
94 | */ |
95 | protected array $groupArray = []; |
96 | |
97 | // -------------------------------------------------------------------------- |
98 | // ! Other Class vars |
99 | // -------------------------------------------------------------------------- |
100 | /** |
101 | * Values to apply to prepared statements |
102 | */ |
103 | protected array $values = []; |
104 | |
105 | /** |
106 | * Values to apply to where clauses in prepared statements |
107 | */ |
108 | protected array $whereValues = []; |
109 | |
110 | /** |
111 | * Value for limit string |
112 | */ |
113 | protected ?int $limit = NULL; |
114 | |
115 | /** |
116 | * Value for offset in limit string |
117 | */ |
118 | protected ?int $offset = NULL; |
119 | |
120 | /** |
121 | * Query component order mapping |
122 | * for complex select queries |
123 | * |
124 | * Format: |
125 | * [ |
126 | * 'type' => 'where', |
127 | * 'conjunction' => ' AND ', |
128 | * 'string' => 'k=?' |
129 | * ] |
130 | */ |
131 | protected array $queryMap = []; |
132 | |
133 | /** |
134 | * Map for having clause |
135 | */ |
136 | protected array $havingMap = []; |
137 | |
138 | public function __call(string $name, array $arguments) |
139 | { |
140 | if (str_starts_with($name, 'get')) |
141 | { |
142 | $maybeProp = lcfirst(substr($name, 3)); |
143 | if (isset($this->$maybeProp)) |
144 | { |
145 | return $this->$maybeProp; |
146 | } |
147 | } |
148 | |
149 | if (str_starts_with($name, 'set')) |
150 | { |
151 | $maybeProp = lcfirst(substr($name, 3)); |
152 | if (isset($this->$maybeProp)) |
153 | { |
154 | $this->$maybeProp = $arguments[0]; |
155 | |
156 | return $this; |
157 | } |
158 | } |
159 | |
160 | return NULL; |
161 | } |
162 | |
163 | public function appendSelectString(string $str): self |
164 | { |
165 | $this->selectString .= $str; |
166 | |
167 | return $this; |
168 | } |
169 | |
170 | public function appendSetArrayKeys(array $setArrayKeys): self |
171 | { |
172 | $this->setArrayKeys = array_merge($this->setArrayKeys, $setArrayKeys); |
173 | |
174 | return $this; |
175 | } |
176 | |
177 | public function setOrderArray(string $key, mixed $orderArray): self |
178 | { |
179 | $this->orderArray[$key] = $orderArray; |
180 | |
181 | return $this; |
182 | } |
183 | |
184 | public function appendGroupArray(string $groupArray): self |
185 | { |
186 | $this->groupArray[] = $groupArray; |
187 | |
188 | return $this; |
189 | } |
190 | |
191 | public function appendValues(array $values): self |
192 | { |
193 | $this->values = array_merge($this->values, $values); |
194 | |
195 | return $this; |
196 | } |
197 | |
198 | public function appendWhereValues(mixed $val): self |
199 | { |
200 | if (is_array($val)) |
201 | { |
202 | foreach ($val as $v) |
203 | { |
204 | $this->whereValues[] = $v; |
205 | } |
206 | |
207 | return $this; |
208 | } |
209 | |
210 | $this->whereValues[] = $val; |
211 | |
212 | return $this; |
213 | } |
214 | |
215 | /** |
216 | * Add an additional set of mapping pairs to a internal map |
217 | */ |
218 | public function appendMap(string $conjunction = '', string $string = '', MapType $type = MapType::WHERE): self |
219 | { |
220 | $this->queryMap[] = [ |
221 | 'type' => $type, |
222 | 'conjunction' => $conjunction, |
223 | 'string' => $string, |
224 | ]; |
225 | |
226 | return $this; |
227 | } |
228 | |
229 | public function appendHavingMap(array $item): self |
230 | { |
231 | $this->havingMap[] = $item; |
232 | |
233 | return $this; |
234 | } |
235 | } |