DebugTrait::renderExecutedQuery()   A
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.0592

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 24
ccs 13
cts 15
cp 0.8667
rs 9.4888
cc 5
nc 9
nop 0
crap 5.0592
1
<?php namespace AgelxNash\Modx\Evo\Database\Traits;
2
3
use AgelxNash\Modx\Evo\Database\Exceptions;
4
5
trait DebugTrait
6
{
7
    /**
8
     * @var bool
9
     */
10
    protected $debug = false;
11
12
    /**
13
     * @var array
14
     */
15
    protected $queryCollection = [];
16
17
    /**
18
     * @var int
19
     */
20
    protected $queriesTime = 0;
21
22
    /**
23
     * @var string
24
     */
25
    protected $lastQuery = '';
26
27
    /**
28
     * @var int
29
     */
30
    protected $connectionTime = 0;
31
32
    /**
33
     * @var array
34
     */
35
    protected $ignoreErrors = [
36
        '42S22', // SQLSTATE: 42S22 (ER_BAD_FIELD_ERROR) Unknown column '%s' in '%s'
37
        '42S21', // SQLSTATE: 42S21 (ER_DUP_FIELDNAME) Duplicate column name '%s'
38
        '42000', // SQLSTATE: 42000 (ER_DUP_KEYNAME) Duplicate key name '%s'
39
        '23000', // SQLSTATE: 23000 (ER_DUP_ENTRY) Duplicate entry '%s' for key %d
40
        '42000' // SQLSTATE: 42000 (ER_CANT_DROP_FIELD_OR_KEY) Can't DROP '%s'; check that column/key exists
41
    ];
42
43
    /**
44
     * @var string
45
     */
46
    protected $timeFormat = '%2.5f';
47
48
    /**
49
     * {@inheritDoc}
50
     */
51
    abstract public function getDriver();
52
53
    /**
54
     * {@inheritDoc}
55
     */
56
    abstract public function getLastError();
57
58
    /**
59
     * {@inheritDoc}
60
     */
61
    abstract public function getLastErrorNo();
62
63
    /**
64
     * {@inheritDoc}
65
     */
66
    abstract public function getRecordCount($result);
67
68
    /**
69
     * {@inheritDoc}
70
     */
71
    abstract public function getAffectedRows();
72
73
    /**
74
     * {@inheritDoc}
75
     */
76 59
    public function isDebug()
77
    {
78 59
        return $this->debug;
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     */
84 59
    public function setDebug($flag)
85
    {
86 59
        $this->debug = $flag;
87
88 59
        return $this;
89
    }
90
91
    /**
92
     * {@inheritDoc}
93
     */
94 4
    public function checkLastError($query = null)
95
    {
96 4
        if ($this->getDriver()->isConnected() === false || $this->getLastErrorNo() === '') {
97 2
            return false;
98
        }
99
100 4
        if (\in_array($this->getLastErrorNo(), $this->getIgnoreErrors(), true)) {
101 2
            return true;
102
        }
103
104 2
        throw (new Exceptions\QueryException($this->getLastError()))
105 2
            ->setQuery($query);
106
    }
107
108
    /**
109
     * {@inheritDoc}
110
     */
111 43
    public function collectQuery($result, $sql, $time)
112
    {
113 43
        $debug = debug_backtrace();
114 43
        array_shift($debug);
115 43
        array_shift($debug);
116 43
        $path = [];
117 43
        foreach ($debug as $line) {
118 43
            $path[] = ($line['class'] ? ($line['class'] . '::') : null) . $line['function'];
119
            /*$path[] = [
120
                'method' => ($line['class'] ? ($line['class'] . '::') : null) . $line['function'],
121
                'file' => ($line['file'] ? ($line['file'] . ':') : null) . ($line['line'] ?? 0)
122
            ];*/
123
        }
124 43
        $path = implode(' > ', array_reverse($path));
125
126
        $data = [
127 43
            'sql' => $sql,
128 43
            'time' => $time,
129 43
            'rows' => (stripos($sql, 'SELECT') === 0 && $this->getDriver()->isResult($result)) ?
130 43
                $this->getRecordCount($result) : $this->getAffectedRows(),
131 43
            'path' => $path,
132
            //'event' => $modx->event->name,
133
            //'element' => [
134
            //      'name' => $modx->event->activePlugin ?? ($modx->currentSnippet ?? null),
135
            //      'type' => $modx->event->activePlugin ? 'Plugin' : ($modx->currentSnippet ? 'Snippet' : 'Source code')
136
            // ]
137
        ];
138
139 43
        $this->queryCollection[] = $data;
140
141 43
        return $data;
142
    }
143
144
    /**
145
     * {@inheritDoc}
146
     */
147 47
    public function setLastQuery($query)
148
    {
149 47
        $this->lastQuery = (string)$query;
150
151 47
        return $query;
152
    }
153
154
    /**
155
     * {@inheritDoc}
156
     */
157 46
    public function getLastQuery()
158
    {
159 46
        return (string)$this->lastQuery;
160
    }
161
162
    /**
163
     * {@inheritDoc}
164
     */
165 15
    public function getAllExecutedQuery()
166
    {
167 15
        return $this->queryCollection;
168
    }
169
170
    /**
171
     * {@inheritDoc}
172
     */
173 14
    public function flushExecutedQuery()
174
    {
175 14
        $this->queryCollection = [];
176 14
        $this->queriesTime = 0;
177 14
        $this->lastQuery = $this->setLastQuery('');
178
179 14
        return true;
180
    }
181
182
    /**
183
     * {@inheritDoc}
184
     */
185 58
    public function setConnectionTime($value)
186
    {
187 58
        $this->connectionTime = $value;
188
189 58
        return $value;
190
    }
191
192
    /**
193
     * {@inheritDoc}
194
     */
195 3
    public function getConnectionTime($format = false)
196
    {
197 3
        return $format ? sprintf($this->timeFormat, $this->connectionTime) : $this->connectionTime;
198
    }
199
200
    /**
201
     * {@inheritDoc}
202
     */
203 2
    public function getQueriesTime()
204
    {
205 2
        return $this->queriesTime;
206
    }
207
208
    /**
209
     * {@inheritDoc}
210
     */
211 43
    public function addQueriesTime($time)
212
    {
213 43
        $this->queriesTime += $time;
214
215 43
        return $time;
216
    }
217
218
    /**
219
     * @return string
220
     */
221 1
    public function renderConnectionTime()
222
    {
223
        return '<fieldset style="text-align:left">' .
224
            '<legend>Database connection</legend>' .
225 1
            'Database connection was created in ' . $this->getConnectionTime(true) . ' s.' .
226 1
            '</fieldset>' .
227 1
            '<br />';
228
    }
229
230
    /**
231
     * @return array
232
     */
233 5
    public function getIgnoreErrors()
234
    {
235 5
        return $this->ignoreErrors;
236
    }
237
238
    /**
239
     * @param string $error
240
     * @return string
241
     */
242 3
    public function addIgnoreErrors($error)
243
    {
244 3
        $this->ignoreErrors[] = $error;
245
246 3
        return $error;
247
    }
248
249
    /**
250
     * @return bool
251
     */
252 3
    public function flushIgnoreErrors()
253
    {
254 3
        $this->ignoreErrors = [];
255
256 3
        return true;
257
    }
258
259
    /**
260
     * @param array $errors
261
     * @return array
262
     */
263 3
    public function setIgnoreErrors(array $errors)
264
    {
265 3
        $this->flushIgnoreErrors();
266
267 3
        foreach ($errors as $error) {
268 3
            $this->addIgnoreErrors($error);
269
        }
270
271 3
        return $this->getIgnoreErrors();
272
    }
273
274
    /**
275
     * @return string
276
     */
277 3
    public function renderExecutedQuery()
278
    {
279 3
        $out = '';
280
281 3
        foreach ($this->getAllExecutedQuery() as $i => $query) {
282 2
            $out .= '<fieldset style="text-align:left">';
283 2
            $out .= '<legend>Query ' . $i . ' - ' . sprintf($this->timeFormat, $query['time']) . '</legend>';
284 2
            $out .= $query['sql'] . '<br><br>';
285 2
            if (! empty($query['element'])) {
286
                $out .= $query['element']['type'] . '  => ' . $query['element']['name'] . '<br>';
287
            }
288 2
            if (! empty($query['event'])) {
289
                $out .= 'Current Event  => ' . $query['event'] . '<br>';
290
            }
291 2
            $out .= 'Affected Rows => ' . $query['rows'] . '<br>';
292 2
            if (! empty($query['path'])) {
293 2
                $out .= 'Functions Path => ' . $query['path'] . '<br>';
294
            }
295
            /*$out .= 'Functions Path => ' . $query['path']['method'] . '<br>';
296
            $out .= empty($query['path']['file']) ?: $query['path']['file'] . '<br />';*/
297 2
            $out .= '</fieldset><br />';
298
        }
299
300 3
        return $out;
301
    }
302
}
303