GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( deb718...3121b4 )
by Steeven
03:03
created

Statement::setDuration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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