1 | <?php |
||
2 | /** |
||
3 | * This file is part of the O2System Framework package. |
||
4 | * |
||
5 | * For the full copyright and license information, please view the LICENSE |
||
6 | * file that was distributed with this source code. |
||
7 | * |
||
8 | * @author Steeve Andrian Salim |
||
9 | * @copyright Copyright (c) Steeve Andrian Salim |
||
10 | */ |
||
11 | |||
12 | // ------------------------------------------------------------------------ |
||
13 | |||
14 | namespace O2System\Database\Sql\DataStructures; |
||
15 | |||
16 | // ------------------------------------------------------------------------ |
||
17 | |||
18 | use O2System\Spl\Traits\Collectors\ErrorCollectorTrait; |
||
19 | |||
20 | /** |
||
21 | * Class QueryStatement |
||
22 | * |
||
23 | * @package O2System\Database\Sql\DataStructures |
||
24 | */ |
||
25 | class QueryStatement |
||
26 | { |
||
27 | use ErrorCollectorTrait; |
||
28 | |||
29 | /** |
||
30 | * QueryStatement::$sqlStatement |
||
31 | * |
||
32 | * The Sql Statement. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | private $sqlStatement; |
||
37 | |||
38 | /** |
||
39 | * QueryStatement::$SqlBinds |
||
40 | * |
||
41 | * The Sql Statement bindings. |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | private $sqlBinds = []; |
||
46 | |||
47 | |||
48 | /** |
||
49 | * QueryStatement::$SqlFinalStatement |
||
50 | * |
||
51 | * The compiled Sql Statement with Sql Statement binders. |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | private $sqlFinalStatement; |
||
56 | |||
57 | /** |
||
58 | * QueryStatement::$startExecutionTime |
||
59 | * |
||
60 | * The start time in seconds with microseconds |
||
61 | * for when this query was executed. |
||
62 | * |
||
63 | * @var float |
||
64 | */ |
||
65 | private $startExecutionTime; |
||
66 | |||
67 | /** |
||
68 | * QueryStatement::$endExecutionTime |
||
69 | * |
||
70 | * The end time in seconds with microseconds |
||
71 | * for when this query was executed. |
||
72 | * |
||
73 | * @var float |
||
74 | */ |
||
75 | private $endExecutionTime; |
||
76 | |||
77 | private $hits = 0; |
||
78 | |||
79 | /** |
||
80 | * QueryStatement::$affectedRows |
||
81 | * |
||
82 | * The numbers of affected rows. |
||
83 | * |
||
84 | * @var int |
||
85 | */ |
||
86 | private $affectedRows; |
||
87 | |||
88 | /** |
||
89 | * QueryStatement::$lastInsertId |
||
90 | * |
||
91 | * The last insert id. |
||
92 | * |
||
93 | * @var mixed |
||
94 | */ |
||
95 | private $lastInsertId; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
96 | |||
97 | /** |
||
98 | * QueryStatement::$error |
||
99 | * |
||
100 | * The query execution error info. |
||
101 | * |
||
102 | * @var array |
||
103 | */ |
||
104 | private $error; |
||
105 | |||
106 | //-------------------------------------------------------------------- |
||
107 | |||
108 | /** |
||
109 | * QueryStatement::setBinds |
||
110 | * |
||
111 | * Will store the variables to bind into the query later. |
||
112 | * |
||
113 | * @param array $SqlBinds |
||
114 | * |
||
115 | * @return static |
||
116 | */ |
||
117 | public function setBinds(array $SqlBinds) |
||
118 | { |
||
119 | $this->sqlBinds = $SqlBinds; |
||
120 | |||
121 | return $this; |
||
122 | } |
||
123 | |||
124 | //-------------------------------------------------------------------- |
||
125 | |||
126 | public function getBinds() |
||
127 | { |
||
128 | return $this->sqlBinds; |
||
129 | } |
||
130 | |||
131 | //-------------------------------------------------------------------- |
||
132 | |||
133 | /** |
||
134 | * QueryStatement::setDuration |
||
135 | * |
||
136 | * Records the execution time of the statement using microtime(true) |
||
137 | * for it's start and end values. If no end value is present, will |
||
138 | * use the current time to determine total duration. |
||
139 | * |
||
140 | * @param int $start |
||
141 | * @param int|null $end |
||
142 | * |
||
143 | * @return static |
||
144 | */ |
||
145 | public function setDuration($start, $end = null) |
||
146 | { |
||
147 | $this->startExecutionTime = $start; |
||
148 | |||
149 | if (is_null($end)) { |
||
150 | $end = microtime(true); |
||
151 | } |
||
152 | |||
153 | $this->endExecutionTime = $end; |
||
154 | |||
155 | return $this; |
||
156 | } |
||
157 | |||
158 | //-------------------------------------------------------------------- |
||
159 | |||
160 | /** |
||
161 | * QueryStatement::getStartExecutionTime |
||
162 | * |
||
163 | * Returns the start time in seconds with microseconds. |
||
164 | * |
||
165 | * @param bool $numberFormat |
||
166 | * @param int $decimals |
||
167 | * |
||
168 | * @return mixed |
||
169 | */ |
||
170 | public function getStartExecutionTime($numberFormat = false, $decimals = 6) |
||
171 | { |
||
172 | if ( ! $numberFormat) { |
||
173 | return $this->startExecutionTime; |
||
174 | } |
||
175 | |||
176 | return number_format($this->startExecutionTime, $decimals); |
||
177 | } |
||
178 | |||
179 | //-------------------------------------------------------------------- |
||
180 | |||
181 | /** |
||
182 | * QueryStatement::getExecutionDuration |
||
183 | * |
||
184 | * Returns the duration of this query during execution, or null if |
||
185 | * the query has not been executed yet. |
||
186 | * |
||
187 | * @param int $decimals The accuracy of the returned time. |
||
188 | * |
||
189 | * @return mixed |
||
190 | */ |
||
191 | public function getExecutionDuration($decimals = 6) |
||
192 | { |
||
193 | return number_format(($this->endExecutionTime - $this->startExecutionTime), $decimals); |
||
194 | } |
||
195 | |||
196 | //-------------------------------------------------------------------- |
||
197 | |||
198 | /** |
||
199 | * QueryStatement::setErrorInfo |
||
200 | * |
||
201 | * Stores the occurred error information when the query was executed. |
||
202 | * |
||
203 | * @param int $errorCode |
||
204 | * @param string $errorMessage |
||
205 | * |
||
206 | * @return static |
||
207 | */ |
||
208 | public function setError($errorCode, $errorMessage) |
||
209 | { |
||
210 | $this->error[ $errorCode ] = $errorMessage; |
||
211 | |||
212 | return $this; |
||
213 | } |
||
214 | |||
215 | //-------------------------------------------------------------------- |
||
216 | |||
217 | /** |
||
218 | * QueryStatement::getAffectedRows |
||
219 | * |
||
220 | * Gets numbers of affected rows. |
||
221 | * |
||
222 | * @return int |
||
223 | */ |
||
224 | public function getAffectedRows() |
||
225 | { |
||
226 | return (int)$this->affectedRows; |
||
227 | } |
||
228 | |||
229 | //-------------------------------------------------------------------- |
||
230 | |||
231 | /** |
||
232 | * QueryStatement::setAffectedRows |
||
233 | * |
||
234 | * Sets numbers of affected rows. |
||
235 | * |
||
236 | * @param int $affectedRows Numbers of affected rows. |
||
237 | * |
||
238 | * @return static |
||
239 | */ |
||
240 | public function setAffectedRows($affectedRows) |
||
241 | { |
||
242 | $this->affectedRows = $affectedRows; |
||
243 | |||
244 | return $this; |
||
245 | } |
||
246 | |||
247 | //-------------------------------------------------------------------- |
||
248 | |||
249 | /** |
||
250 | * QueryStatement::getAffectedRows |
||
251 | * |
||
252 | * Gets numbers of affected rows. |
||
253 | * |
||
254 | * @return int |
||
255 | */ |
||
256 | public function getLastInsertId() |
||
257 | { |
||
258 | return $this->affectedRows; |
||
259 | } |
||
260 | |||
261 | //-------------------------------------------------------------------- |
||
262 | |||
263 | /** |
||
264 | * QueryStatement::setAffectedRows |
||
265 | * |
||
266 | * Sets numbers of affected rows. |
||
267 | * |
||
268 | * @param int $affectedRows Numbers of affected rows. |
||
269 | * |
||
270 | * @return static |
||
271 | */ |
||
272 | public function setLastInsertId($affectedRows) |
||
273 | { |
||
274 | $this->affectedRows = $affectedRows; |
||
275 | |||
276 | return $this; |
||
277 | } |
||
278 | |||
279 | //-------------------------------------------------------------------- |
||
280 | |||
281 | /** |
||
282 | * QueryStatement::isWriteStatement |
||
283 | * |
||
284 | * Determines if the Sql statement is a write-syntax query or not. |
||
285 | * |
||
286 | * @return bool |
||
287 | */ |
||
288 | public function isWriteStatement() |
||
289 | { |
||
290 | return (bool)preg_match( |
||
291 | '/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK|REINDEX)\s/i', |
||
292 | $this->sqlStatement |
||
293 | ); |
||
294 | } |
||
295 | |||
296 | //-------------------------------------------------------------------- |
||
297 | |||
298 | /** |
||
299 | * QueryStatement::replacePrefix |
||
300 | * |
||
301 | * Replace all table prefix with new prefix. |
||
302 | * |
||
303 | * @param string $search |
||
304 | * @param string $replace |
||
305 | * |
||
306 | * @return mixed |
||
307 | */ |
||
308 | public function swapTablePrefix($search, $replace) |
||
309 | { |
||
310 | $Sql = empty($this->sqlFinalStatement) ? $this->sqlStatement : $this->sqlFinalStatement; |
||
311 | |||
312 | $this->sqlFinalStatement = preg_replace('/(\W)' . $search . '(\S+?)/', '\\1' . $replace . '\\2', $Sql); |
||
313 | |||
314 | return $this; |
||
315 | } |
||
316 | |||
317 | //-------------------------------------------------------------------- |
||
318 | |||
319 | /** |
||
320 | * QueryStatement::getSqlStatement |
||
321 | * |
||
322 | * Get the original Sql statement. |
||
323 | * |
||
324 | * @return string The Sql statement string. |
||
325 | */ |
||
326 | public function getSqlStatement() |
||
327 | { |
||
328 | return $this->sqlStatement; |
||
329 | } |
||
330 | |||
331 | //-------------------------------------------------------------------- |
||
332 | |||
333 | /** |
||
334 | * QueryStatement::setStatement |
||
335 | * |
||
336 | * Sets the raw query string to use for this statement. |
||
337 | * |
||
338 | * @param string $sqlStatement The Sql Statement. |
||
339 | * @param array $SqlBinds The Sql Statement bindings. |
||
340 | * |
||
341 | * @return static |
||
342 | */ |
||
343 | public function setSqlStatement($sqlStatement, array $SqlBinds = []) |
||
344 | { |
||
345 | $this->sqlStatement = $sqlStatement; |
||
346 | $this->sqlBinds = $SqlBinds; |
||
347 | |||
348 | return $this; |
||
349 | } |
||
350 | |||
351 | //-------------------------------------------------------------------- |
||
352 | |||
353 | public function getKey() |
||
354 | { |
||
355 | return md5($this->getSqlFinalStatement()); |
||
356 | } |
||
357 | |||
358 | //-------------------------------------------------------------------- |
||
359 | |||
360 | /** |
||
361 | * QueryStatement::getSqlFinalStatement |
||
362 | * |
||
363 | * Returns the final, processed query string after binding, etal |
||
364 | * has been performed. |
||
365 | * |
||
366 | * @return string |
||
367 | */ |
||
368 | public function getSqlFinalStatement() |
||
369 | { |
||
370 | return $this->sqlFinalStatement; |
||
371 | } |
||
372 | |||
373 | //-------------------------------------------------------------------- |
||
374 | |||
375 | /** |
||
376 | * QueryStatement::setSqlFinalStatement |
||
377 | * |
||
378 | * @param string $finalStatement |
||
379 | * |
||
380 | * @return static |
||
381 | */ |
||
382 | public function setSqlFinalStatement($finalStatement) |
||
383 | { |
||
384 | $this->sqlFinalStatement = $finalStatement; |
||
385 | |||
386 | return $this; |
||
387 | } |
||
388 | |||
389 | //-------------------------------------------------------------------- |
||
390 | |||
391 | /** |
||
392 | * QueryStatement::getHits |
||
393 | * |
||
394 | * Gets num of hits. |
||
395 | * |
||
396 | * @return int |
||
397 | */ |
||
398 | public function getHits() |
||
399 | { |
||
400 | return $this->hits; |
||
401 | } |
||
402 | |||
403 | //-------------------------------------------------------------------- |
||
404 | |||
405 | /** |
||
406 | * QueryStatement::addHit |
||
407 | * |
||
408 | * @param int $hit |
||
409 | * |
||
410 | * @return $this |
||
411 | */ |
||
412 | public function addHit($hit = 1) |
||
413 | { |
||
414 | $this->hits = $this->hits + (int)$hit; |
||
415 | |||
416 | return $this; |
||
417 | } |
||
418 | |||
419 | //-------------------------------------------------------------------- |
||
420 | |||
421 | /** |
||
422 | * QueryStatement::__toString |
||
423 | * |
||
424 | * Convert this query into compiled Sql Statement string. |
||
425 | * |
||
426 | * @return string |
||
427 | */ |
||
428 | public function __toString() |
||
429 | { |
||
430 | return (string)$this->getSqlFinalStatement(); |
||
431 | } |
||
432 | } |
||
433 |