Test Failed
Pull Request — master (#4)
by Dmytro
04:37
created

MySqliDriver   A

Complexity

Total Complexity 38

Size/Duplication

Total Lines 262
Duplicated Lines 0 %

Test Coverage

Coverage 35.56%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 38
eloc 71
c 2
b 0
f 0
dl 0
loc 262
ccs 32
cts 90
cp 0.3556
rs 9.36

23 Methods

Rating   Name   Duplication   Size   Complexity  
A connect() 0 15 2
A __construct() 0 6 1
A commit() 0 3 1
A isConnected() 0 3 1
A getVersion() 0 3 1
A rollback() 0 3 1
A getConnect() 0 7 2
A begin() 0 3 1
A isResult() 0 3 1
A dataSeek() 0 3 2
A getLastError() 0 3 1
A escape() 0 3 1
A getRow() 0 22 5
A query() 0 13 2
A numFields() 0 3 2
A disconnect() 0 9 2
A getRecordCount() 0 3 2
A getAffectedRows() 0 3 1
A fieldName() 0 5 3
A setCharset() 0 9 2
A selectDb() 0 3 1
A getLastErrorNo() 0 9 2
A getInsertId() 0 3 1
1
<?php namespace AgelxNash\Modx\Evo\Database\Drivers;
2
3
use AgelxNash\Modx\Evo\Database\Exceptions;
4
use mysqli;
5
use mysqli_result;
6
use mysqli_sql_exception;
7
use mysqli_driver;
8
use ReflectionClass;
9
10
/**
11
 * @property mysqli $conn
12
 */
13
class MySqliDriver extends AbstractDriver
14
{
15
    /**
16
     * {@inheritDoc}
17
     */
18 23
    public function __construct(array $config = [])
19
    {
20 23
        $driver = new mysqli_driver();
21 23
        $driver->report_mode = MYSQLI_REPORT_STRICT | MYSQLI_REPORT_ERROR;
22
23 23
        $this->setConfig($config);
24 23
    }
25
26
    /**
27
     * {@inheritDoc}
28
     * @return mysqli
29
     */
30 3
    public function getConnect()
31
    {
32 3
        if (! $this->isConnected()) {
33 3
            return $this->connect();
34
        }
35
36 1
        return $this->conn;
37
    }
38
39
    /**
40
     * {@inheritDoc}
41
     */
42 3
    public function isConnected()
43
    {
44 3
        return ($this->conn instanceof mysqli);
45
    }
46
47
    /**
48
     * {@inheritDoc}
49
     */
50
    public function getLastError()
51
    {
52
        return $this->getConnect()->error;
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     */
58
    public function getLastErrorNo()
59
    {
60
        $out = (string)$this->getConnect()->sqlstate;
61
62
        if ($out === '00000') {
63
            $out = '';
64
        }
65
66
        return $out;
67
    }
68
69
    /**
70
     * {@inheritDoc}
71
     */
72 3
    public function connect()
73
    {
74
        try {
75 3
            $this->conn = new mysqli(
76 3
                $this->getConfig('host'),
77 3
                $this->getConfig('username'),
78 3
                $this->getConfig('password'),
79 3
                $this->getConfig('database')
80
            );
81 1
        } catch (mysqli_sql_exception $exception) {
82 1
            $this->conn = null;
83 1
            throw new Exceptions\ConnectException($exception->getMessage(), $exception->getCode());
84
        }
85
86 2
        return $this->conn;
87
    }
88
89
    /**
90
     * {@inheritDoc}
91
     */
92
    public function disconnect()
93
    {
94
        if ($this->isConnected()) {
95
            $this->conn->close();
96
        }
97
98
        $this->conn = null;
99
100
        return true;
101
    }
102
103
    /**
104
     * {@inheritDoc}
105
     */
106 1
    public function isResult($result)
107
    {
108 1
        return $result instanceof mysqli_result;
109
    }
110
111
    /**
112
     * @param mysqli_result $result
113
     * {@inheritDoc}
114
     */
115
    public function numFields($result)
116
    {
117
        return $this->isResult($result) ? $result->field_count : 0;
118
    }
119
120
    /**
121
     * @param mysqli_result $result
122
     * {@inheritDoc}
123
     */
124
    public function fieldName($result, $col = 0)
125
    {
126
        $field = $this->isResult($result) ? $result->fetch_field_direct($col) : [];
127
128
        return isset($field->name) ? $field->name : null;
129
    }
130
131
    /**
132
     * {@inheritDoc}
133
     * @throws \ReflectionException
134
     */
135 1
    public function setCharset($charset, $method = null)
136
    {
137 1
        if ($method === null) {
138
            $method = $this->getConfig('method');
139
        }
140
141 1
        $this->query($method . ' ' . $charset);
142
143 1
        return $this->getConnect()->set_charset($charset);
144
    }
145
146
    /**
147
     * {@inheritDoc}
148
     */
149
    public function selectDb($name)
150
    {
151
        return $this->getConnect()->select_db($name);
152
    }
153
154
    /**
155
     * {@inheritDoc}
156
     */
157
    public function escape($data)
158
    {
159
        return $this->getConnect()->escape_string($data);
160
    }
161
162
    /**
163
     * {@inheritDoc}
164
     * @return bool|mysqli_result
165
     * @throws \ReflectionException
166
     */
167 1
    public function query($sql)
168
    {
169
        try {
170 1
            $result = $this->getConnect()->query($sql);
171
        } catch (mysqli_sql_exception $exception) {
172
            $reflect = new ReflectionClass($exception);
173
            $property = $reflect->getProperty('sqlstate');
174
            $property->setAccessible(true);
175
            throw (new Exceptions\QueryException($exception->getMessage(), $property->getValue($exception)))
176
                ->setQuery($sql);
177
        }
178
179 1
        return $result;
180
    }
181
182
    /**
183
     * @param mysqli_result $result
184
     * {@inheritDoc}
185
     */
186
    public function getRecordCount($result)
187
    {
188
        return $this->isResult($result) ? $result->num_rows : 0;
189
    }
190
191
    /**
192
     * @param mysqli_result $result
193
     * {@inheritDoc}
194
     */
195
    public function getRow($result, $mode = 'assoc')
196
    {
197
        switch ($mode) {
198
            case 'assoc':
199
                $out = $result->fetch_assoc();
200
                break;
201
            case 'num':
202
                $out = $result->fetch_row();
203
                break;
204
            case 'object':
205
                $out = $result->fetch_object();
206
                break;
207
            case 'both':
208
                $out = $result->fetch_array(MYSQLI_BOTH);
209
                break;
210
            default:
211
                throw new Exceptions\UnknownFetchTypeException(
212
                    "Unknown get type ($mode) specified for fetchRow - must be empty, 'assoc', 'num' or 'both'."
213
                );
214
        }
215
216
        return $out;
217
    }
218
219
    /**
220
     * {@inheritDoc}
221
     */
222 1
    public function getVersion()
223
    {
224 1
        return $this->getConnect()->server_info;
225
    }
226
227
    /**
228
     * {@inheritDoc}
229
     */
230
    public function getInsertId()
231
    {
232
        return $this->getConnect()->insert_id;
233
    }
234
235
    /**
236
     * {@inheritDoc}
237
     */
238
    public function getAffectedRows()
239
    {
240
        return $this->getConnect()->affected_rows;
241
    }
242
243
    /**
244
     * @param mysqli_result $result
245
     * @param int $position
246
     * @return bool
247
     */
248
    public function dataSeek(&$result, $position)
249
    {
250
        return $this->isResult($result) ? $result->data_seek($position) : false;
251
    }
252
253
    /**
254
     * {@inheritDoc}
255
     */
256
    public function begin ($flag = 0, $name = null)
257
    {
258
        return $this->getConnect()->begin_transaction($flag, $name);
259
    }
260
261
    /**
262
     * {@inheritDoc}
263
     */
264
    public function commit ($flag = 0, $name = null)
265
    {
266
        return $this->getConnect()->commit($flag, $name);
267
    }
268
269
    /**
270
     * {@inheritDoc}
271
     */
272
    public function rollback ($flag = 0, $name = null)
273
    {
274
        return $this->getConnect()->rollback($flag, $name);
275
    }
276
}
277