Total Complexity | 63 |
Total Lines | 471 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like CI_DB_mysqli_driver often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CI_DB_mysqli_driver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
56 | class CI_DB_mysqli_driver extends CI_DB_driver { |
||
57 | /** |
||
58 | * Database driver |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | public $dbdriver = 'mysqli'; |
||
63 | /** |
||
64 | * Compression flag |
||
65 | * |
||
66 | * @var bool |
||
67 | */ |
||
68 | public $compress = FALSE; |
||
69 | /** |
||
70 | * DELETE hack flag |
||
71 | * |
||
72 | * Whether to use the MySQL "delete hack" which allows the number |
||
73 | * of affected rows to be shown. Uses a preg_replace when enabled, |
||
74 | * adding a bit more processing to all queries. |
||
75 | * |
||
76 | * @var bool |
||
77 | */ |
||
78 | public $delete_hack = TRUE; |
||
79 | /** |
||
80 | * Strict ON flag |
||
81 | * |
||
82 | * Whether we're running in strict SQL mode. |
||
83 | * |
||
84 | * @var bool |
||
85 | */ |
||
86 | public $stricton; |
||
87 | /** |
||
88 | * Identifier escape character |
||
89 | * |
||
90 | * @var string |
||
91 | */ |
||
92 | protected $_escape_char = '`'; |
||
93 | /** |
||
94 | * MySQLi object |
||
|
|||
95 | * |
||
96 | * Has to be preserved without being assigned to $conn_id. |
||
97 | * |
||
98 | * @var MySQLi |
||
99 | */ |
||
100 | protected $_mysqli; |
||
101 | /** |
||
102 | * |
||
103 | * @param array $params |
||
104 | */ |
||
105 | public function __construct(array $params) |
||
106 | { |
||
107 | parent::__construct($params); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Database connection |
||
112 | * |
||
113 | * @param bool $persistent |
||
114 | * @return object |
||
115 | **/ |
||
116 | public function db_connect($persistent = FALSE) |
||
218 | } |
||
219 | |||
220 | // -------------------------------------------------------------------- |
||
221 | |||
222 | /** |
||
223 | * Reconnect |
||
224 | * Keep / reestablish the db connection if no queries have been |
||
225 | * sent for a length of time exceeding the server's idle timeout |
||
226 | * @return void |
||
227 | */ |
||
228 | public function reconnect() |
||
229 | { |
||
230 | if ($this->conn_id !== FALSE && $this->conn_id->ping() === FALSE) { |
||
231 | $this->conn_id = FALSE; |
||
232 | } |
||
233 | } |
||
234 | /** |
||
235 | * Select the database |
||
236 | * |
||
237 | * @param string $database |
||
238 | * @return bool |
||
239 | */ |
||
240 | public function db_select($database = '') |
||
241 | { |
||
242 | if ($database === '') |
||
243 | { |
||
244 | $database = $this->database; |
||
245 | } |
||
246 | |||
247 | if ($this->conn_id->select_db($database)) |
||
248 | { |
||
249 | $this->database = $database; |
||
250 | $this->data_cache = array(); |
||
251 | return TRUE; |
||
252 | } |
||
253 | |||
254 | return FALSE; |
||
255 | } |
||
256 | |||
257 | // -------------------------------------------------------------------- |
||
258 | |||
259 | /** |
||
260 | * Set client character set |
||
261 | * |
||
262 | * @param string $charset |
||
263 | * @return bool |
||
264 | */ |
||
265 | protected function _db_set_charset($charset) |
||
266 | { |
||
267 | return $this->conn_id->set_charset($charset); |
||
268 | } |
||
269 | |||
270 | // -------------------------------------------------------------------- |
||
271 | |||
272 | /** |
||
273 | * Database version number |
||
274 | * |
||
275 | * @return string |
||
276 | */ |
||
277 | public function version() |
||
278 | { |
||
279 | if (isset($this->data_cache['version'])) |
||
280 | { |
||
281 | return $this->data_cache['version']; |
||
282 | } |
||
283 | |||
284 | return $this->data_cache['version'] = $this->conn_id->server_info; |
||
285 | } |
||
286 | |||
287 | // -------------------------------------------------------------------- |
||
288 | |||
289 | /** |
||
290 | * Execute the query |
||
291 | * |
||
292 | * @param string $sql an SQL query |
||
293 | * @return mixed |
||
294 | */ |
||
295 | protected function _execute($sql) |
||
296 | { |
||
297 | return $this->conn_id->query($this->_prep_query($sql)); |
||
298 | } |
||
299 | |||
300 | // -------------------------------------------------------------------- |
||
301 | |||
302 | /** |
||
303 | * Prep the query |
||
304 | * |
||
305 | * If needed, each database adapter can prep the query string |
||
306 | * |
||
307 | * @param string $sql an SQL query |
||
308 | * @return string |
||
309 | */ |
||
310 | protected function _prep_query($sql) |
||
311 | { |
||
312 | // mysqli_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack |
||
313 | // modifies the query so that it a proper number of affected rows is returned. |
||
314 | if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql)) { |
||
315 | return trim($sql).' WHERE 1=1'; |
||
316 | } |
||
317 | return $sql; |
||
318 | } |
||
319 | /** |
||
320 | * Begin Transaction |
||
321 | * |
||
322 | * @return bool |
||
323 | */ |
||
324 | protected function _trans_begin() |
||
325 | { |
||
326 | $this->conn_id->autocommit(FALSE); |
||
327 | return $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK |
||
328 | } |
||
329 | |||
330 | // -------------------------------------------------------------------- |
||
331 | |||
332 | /** |
||
333 | * Commit Transaction |
||
334 | * |
||
335 | * @return bool |
||
336 | */ |
||
337 | protected function _trans_commit() |
||
338 | { |
||
339 | if ($this->conn_id->commit()) |
||
340 | { |
||
341 | $this->conn_id->autocommit(TRUE); |
||
342 | return TRUE; |
||
343 | } |
||
344 | |||
345 | return FALSE; |
||
346 | } |
||
347 | |||
348 | // -------------------------------------------------------------------- |
||
349 | |||
350 | /** |
||
351 | * Rollback Transaction |
||
352 | * |
||
353 | * @return bool |
||
354 | */ |
||
355 | protected function _trans_rollback() |
||
356 | { |
||
357 | if ($this->conn_id->rollback()) |
||
358 | { |
||
359 | $this->conn_id->autocommit(TRUE); |
||
360 | return TRUE; |
||
361 | } |
||
362 | |||
363 | return FALSE; |
||
364 | } |
||
365 | |||
366 | // -------------------------------------------------------------------- |
||
367 | |||
368 | /** |
||
369 | * Platform-dependent string escape |
||
370 | * |
||
371 | * @param string |
||
372 | * @return string |
||
373 | */ |
||
374 | protected function _escape_str($str) |
||
375 | { |
||
376 | return $this->conn_id->real_escape_string($str); |
||
377 | } |
||
378 | |||
379 | // -------------------------------------------------------------------- |
||
380 | |||
381 | /** |
||
382 | * Affected Rows |
||
383 | * |
||
384 | * @return int |
||
385 | */ |
||
386 | public function affected_rows() |
||
387 | { |
||
388 | return $this->conn_id->affected_rows; |
||
389 | } |
||
390 | |||
391 | // -------------------------------------------------------------------- |
||
392 | |||
393 | /** |
||
394 | * Insert ID |
||
395 | * |
||
396 | * @return int |
||
397 | */ |
||
398 | public function insert_id() |
||
399 | { |
||
400 | return $this->conn_id->insert_id; |
||
401 | } |
||
402 | |||
403 | // -------------------------------------------------------------------- |
||
404 | |||
405 | /** |
||
406 | * List table query |
||
407 | * |
||
408 | * Generates a platform-specific query string so that the table names can be fetched |
||
409 | * |
||
410 | * @param bool $prefix_limit |
||
411 | * @return string |
||
412 | */ |
||
413 | protected function _list_tables($prefix_limit = FALSE) |
||
414 | { |
||
415 | $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database); |
||
416 | |||
417 | if ($prefix_limit !== FALSE && $this->dbprefix !== '') |
||
418 | { |
||
419 | return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'"; |
||
420 | } |
||
421 | |||
422 | return $sql; |
||
423 | } |
||
424 | |||
425 | // -------------------------------------------------------------------- |
||
426 | |||
427 | /** |
||
428 | * Show column query |
||
429 | * |
||
430 | * Generates a platform-specific query string so that the column names can be fetched |
||
431 | * |
||
432 | * @param string $table |
||
433 | * @return string |
||
434 | */ |
||
435 | protected function _list_columns($table = '') |
||
436 | { |
||
437 | return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE); |
||
438 | } |
||
439 | |||
440 | // -------------------------------------------------------------------- |
||
441 | |||
442 | /** |
||
443 | * Returns an object with field data |
||
444 | * |
||
445 | * @param string $table |
||
446 | * @return array |
||
447 | */ |
||
448 | public function field_data($table) |
||
449 | { |
||
450 | if (($query = $this->query('SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE))) === FALSE) |
||
451 | { |
||
452 | return FALSE; |
||
453 | } |
||
454 | $query = $query->result_object(); |
||
455 | |||
456 | $retval = array(); |
||
457 | for ($i = 0, $c = count($query); $i < $c; $i++) |
||
458 | { |
||
459 | $retval[$i] = new stdClass(); |
||
460 | $retval[$i]->name = $query[$i]->Field; |
||
461 | |||
462 | sscanf($query[$i]->Type, '%[a-z](%d)', |
||
463 | $retval[$i]->type, |
||
464 | $retval[$i]->max_length |
||
465 | ); |
||
466 | |||
467 | $retval[$i]->default = $query[$i]->Default; |
||
468 | $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI'); |
||
469 | } |
||
470 | |||
471 | return $retval; |
||
472 | } |
||
473 | |||
474 | // -------------------------------------------------------------------- |
||
475 | |||
476 | /** |
||
477 | * Error |
||
478 | * |
||
479 | * Returns an array containing code and message of the last |
||
480 | * database error that has occurred. |
||
481 | * |
||
482 | * @return array |
||
483 | */ |
||
484 | public function error() |
||
485 | { |
||
486 | if ( ! empty($this->_mysqli->connect_errno)) |
||
487 | { |
||
488 | return array( |
||
489 | 'code' => $this->_mysqli->connect_errno, |
||
490 | 'message' => $this->_mysqli->connect_error |
||
491 | ); |
||
492 | } |
||
493 | |||
494 | return array('code' => $this->conn_id->errno, 'message' => $this->conn_id->error); |
||
495 | } |
||
496 | |||
497 | // -------------------------------------------------------------------- |
||
498 | |||
499 | /** |
||
500 | * FROM tables |
||
501 | * |
||
502 | * Groups tables in FROM clauses if needed, so there is no confusion |
||
503 | * about operator precedence. |
||
504 | * |
||
505 | * @return string |
||
506 | */ |
||
507 | protected function _from_tables() |
||
515 | } |
||
516 | |||
517 | // -------------------------------------------------------------------- |
||
518 | |||
519 | /** |
||
520 | * Close DB Connection |
||
521 | * |
||
522 | * @return void |
||
523 | */ |
||
524 | protected function _close() |
||
527 | } |
||
528 | |||
529 | } |
||
530 |