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