Complex classes like AbstractDriver 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 AbstractDriver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | abstract class AbstractDriver extends \PDO implements DriverInterface { |
||
29 | |||
30 | /** |
||
31 | * Reference to the last executed query |
||
32 | * @var \PDOStatement |
||
33 | */ |
||
34 | protected $statement; |
||
35 | |||
36 | /** |
||
37 | * Character to escape identifiers |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $escape_char = '"'; |
||
41 | |||
42 | /** |
||
43 | * Reference to sql class |
||
44 | * @var SQLInterface |
||
45 | */ |
||
46 | protected $sql; |
||
47 | |||
48 | /** |
||
49 | * Reference to util class |
||
50 | * @var AbstractUtil |
||
51 | */ |
||
52 | protected $util; |
||
53 | |||
54 | /** |
||
55 | * Last query executed |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $last_query; |
||
59 | |||
60 | /** |
||
61 | * Prefix to apply to table names |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $table_prefix = ''; |
||
65 | |||
66 | /** |
||
67 | * Whether the driver supports 'TRUNCATE' |
||
68 | * @var bool |
||
69 | */ |
||
70 | protected $has_truncate = TRUE; |
||
71 | |||
72 | /** |
||
73 | * PDO constructor wrapper |
||
74 | * |
||
75 | * @param string $dsn |
||
76 | * @param string $username |
||
77 | * @param string $password |
||
78 | * @param array $driver_options |
||
79 | */ |
||
80 | public function __construct($dsn, $username=NULL, $password=NULL, array $driver_options=array()) |
||
88 | |||
89 | // -------------------------------------------------------------------------- |
||
90 | |||
91 | /** |
||
92 | * Loads the subclasses for the driver |
||
93 | * |
||
94 | * @return void |
||
95 | */ |
||
96 | protected function _load_sub_classes() |
||
109 | |||
110 | // -------------------------------------------------------------------------- |
||
111 | |||
112 | /** |
||
113 | * Allow invoke to work on table object |
||
114 | * |
||
115 | * @codeCoverageIgnore |
||
116 | * @param string $name |
||
117 | * @param array $args |
||
118 | * @return mixed |
||
119 | */ |
||
120 | public function __call($name, $args = array()) |
||
131 | |||
132 | // -------------------------------------------------------------------------- |
||
133 | // ! Accessors / Mutators |
||
134 | // -------------------------------------------------------------------------- |
||
135 | |||
136 | /** |
||
137 | * Get the last sql query exexcuted |
||
138 | * |
||
139 | * @return string |
||
140 | */ |
||
141 | public function get_last_query() |
||
145 | |||
146 | // -------------------------------------------------------------------------- |
||
147 | |||
148 | /** |
||
149 | * Set the last query sql |
||
150 | * |
||
151 | * @param string $query_string |
||
152 | * @return void |
||
153 | */ |
||
154 | public function set_last_query($query_string) |
||
158 | |||
159 | // -------------------------------------------------------------------------- |
||
160 | |||
161 | /** |
||
162 | * Get the SQL class for the current driver |
||
163 | * |
||
164 | * @return SQLInterface |
||
165 | */ |
||
166 | public function get_sql() |
||
170 | |||
171 | // -------------------------------------------------------------------------- |
||
172 | |||
173 | /** |
||
174 | * Get the Util class for the current driver |
||
175 | * |
||
176 | * @return AbstractUtil |
||
177 | */ |
||
178 | public function get_util() |
||
182 | |||
183 | // -------------------------------------------------------------------------- |
||
184 | |||
185 | /** |
||
186 | * Set the common table name prefix |
||
187 | * |
||
188 | * @param string $prefix |
||
189 | * @return void |
||
190 | */ |
||
191 | public function set_table_prefix($prefix) |
||
195 | |||
196 | // -------------------------------------------------------------------------- |
||
197 | // ! Concrete functions that can be overridden in child classes |
||
198 | // -------------------------------------------------------------------------- |
||
199 | |||
200 | /** |
||
201 | * Simplifies prepared statements for database queries |
||
202 | * |
||
203 | * @param string $sql |
||
204 | * @param array $data |
||
205 | * @return \PDOStatement | FALSE |
||
206 | * @throws \InvalidArgumentException |
||
207 | */ |
||
208 | public function prepare_query($sql, $data) |
||
232 | |||
233 | // ------------------------------------------------------------------------- |
||
234 | |||
235 | /** |
||
236 | * Create and execute a prepared statement with the provided parameters |
||
237 | * |
||
238 | * @param string $sql |
||
239 | * @param array $params |
||
240 | * @return \PDOStatement |
||
241 | */ |
||
242 | public function prepare_execute($sql, $params) |
||
249 | |||
250 | // ------------------------------------------------------------------------- |
||
251 | |||
252 | /** |
||
253 | * Returns number of rows affected by an INSERT, UPDATE, DELETE type query |
||
254 | * |
||
255 | * @return int |
||
256 | */ |
||
257 | public function affected_rows() |
||
262 | |||
263 | // -------------------------------------------------------------------------- |
||
264 | |||
265 | /** |
||
266 | * Prefixes a table if it is not already prefixed |
||
267 | * @param string $table |
||
268 | * @return string |
||
269 | */ |
||
270 | public function prefix_table($table) |
||
293 | |||
294 | // -------------------------------------------------------------------------- |
||
295 | |||
296 | /** |
||
297 | * Quote database table name, and set prefix |
||
298 | * |
||
299 | * @param string $table |
||
300 | * @return string |
||
301 | */ |
||
302 | public function quote_table($table) |
||
309 | |||
310 | // -------------------------------------------------------------------------- |
||
311 | |||
312 | /** |
||
313 | * Surrounds the string with the databases identifier escape characters |
||
314 | * |
||
315 | * @param mixed $ident |
||
316 | * @return string |
||
317 | */ |
||
318 | public function quote_ident($ident) |
||
355 | |||
356 | // ------------------------------------------------------------------------- |
||
357 | |||
358 | /** |
||
359 | * Return schemas for databases that list them |
||
360 | * |
||
361 | * @return array |
||
362 | */ |
||
363 | public function get_schemas() |
||
367 | |||
368 | // ------------------------------------------------------------------------- |
||
369 | |||
370 | /** |
||
371 | * Return list of tables for the current database |
||
372 | * |
||
373 | * @return array |
||
374 | */ |
||
375 | public function get_tables() |
||
381 | |||
382 | // ------------------------------------------------------------------------- |
||
383 | |||
384 | /** |
||
385 | * Return list of dbs for the current connection, if possible |
||
386 | * |
||
387 | * @return array |
||
388 | */ |
||
389 | public function get_dbs() |
||
393 | |||
394 | // ------------------------------------------------------------------------- |
||
395 | |||
396 | /** |
||
397 | * Return list of views for the current database |
||
398 | * |
||
399 | * @return array |
||
400 | */ |
||
401 | public function get_views() |
||
407 | |||
408 | // ------------------------------------------------------------------------- |
||
409 | |||
410 | /** |
||
411 | * Return list of sequences for the current database, if they exist |
||
412 | * |
||
413 | * @return array |
||
414 | */ |
||
415 | public function get_sequences() |
||
419 | |||
420 | // ------------------------------------------------------------------------- |
||
421 | |||
422 | /** |
||
423 | * Return list of functions for the current database |
||
424 | * |
||
425 | * @return array |
||
426 | */ |
||
427 | public function get_functions() |
||
431 | |||
432 | // ------------------------------------------------------------------------- |
||
433 | |||
434 | /** |
||
435 | * Return list of stored procedures for the current database |
||
436 | * |
||
437 | * @return array |
||
438 | */ |
||
439 | public function get_procedures() |
||
443 | |||
444 | // ------------------------------------------------------------------------- |
||
445 | |||
446 | /** |
||
447 | * Return list of triggers for the current database |
||
448 | * |
||
449 | * @return array |
||
450 | */ |
||
451 | public function get_triggers() |
||
455 | |||
456 | // ------------------------------------------------------------------------- |
||
457 | |||
458 | /** |
||
459 | * Retrieves an array of non-user-created tables for |
||
460 | * the connection/database |
||
461 | * |
||
462 | * @return array |
||
463 | */ |
||
464 | public function get_system_tables() |
||
468 | |||
469 | // -------------------------------------------------------------------------- |
||
470 | |||
471 | /** |
||
472 | * Retrieve column information for the current database table |
||
473 | * |
||
474 | * @param string $table |
||
475 | * @return array |
||
476 | */ |
||
477 | public function get_columns($table) |
||
481 | |||
482 | // -------------------------------------------------------------------------- |
||
483 | |||
484 | /** |
||
485 | * Retrieve foreign keys for the table |
||
486 | * |
||
487 | * @param string $table |
||
488 | * @return array |
||
489 | */ |
||
490 | public function get_fks($table) |
||
494 | |||
495 | // -------------------------------------------------------------------------- |
||
496 | |||
497 | /** |
||
498 | * Retrieve indexes for the table |
||
499 | * |
||
500 | * @param string $table |
||
501 | * @return array |
||
502 | */ |
||
503 | public function get_indexes($table) |
||
507 | |||
508 | // -------------------------------------------------------------------------- |
||
509 | |||
510 | /** |
||
511 | * Retrieve list of data types for the database |
||
512 | * |
||
513 | * @return array |
||
514 | */ |
||
515 | public function get_types() |
||
519 | |||
520 | // ------------------------------------------------------------------------- |
||
521 | |||
522 | /** |
||
523 | * Method to simplify retrieving db results for meta-data queries |
||
524 | * |
||
525 | * @param string|array|null $query |
||
526 | * @param bool $filtered_index |
||
527 | * @return array |
||
528 | */ |
||
529 | public function driver_query($query, $filtered_index=TRUE) |
||
552 | |||
553 | // -------------------------------------------------------------------------- |
||
554 | |||
555 | /** |
||
556 | * Return the number of rows returned for a SELECT query |
||
557 | * |
||
558 | * @see http://us3.php.net/manual/en/pdostatement.rowcount.php#87110 |
||
559 | * @return int |
||
560 | */ |
||
561 | public function num_rows() |
||
574 | |||
575 | // -------------------------------------------------------------------------- |
||
576 | |||
577 | /** |
||
578 | * Create sql for batch insert |
||
579 | * |
||
580 | * @param string $table |
||
581 | * @param array $data |
||
582 | * @return null|array<string|array|null> |
||
583 | */ |
||
584 | public function insert_batch($table, $data=array()) |
||
615 | |||
616 | // -------------------------------------------------------------------------- |
||
617 | |||
618 | /** |
||
619 | * Helper method for quote_ident |
||
620 | * |
||
621 | * @param mixed $str |
||
622 | * @return mixed |
||
623 | */ |
||
624 | public function _quote($str) |
||
638 | |||
639 | // -------------------------------------------------------------------------- |
||
640 | |||
641 | /** |
||
642 | * Sets the table prefix on the passed string |
||
643 | * |
||
644 | * @param string $str |
||
645 | * @return string |
||
646 | */ |
||
647 | protected function _prefix($str) |
||
657 | |||
658 | // ------------------------------------------------------------------------- |
||
659 | |||
660 | /** |
||
661 | * Empty the passed table |
||
662 | * |
||
663 | * @param string $table |
||
664 | * @return \PDOStatement |
||
665 | */ |
||
666 | public function truncate($table) |
||
677 | |||
678 | } |
||
679 | // End of db_pdo.php |