Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like db_mysql 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 db_mysql, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class db_mysql { |
||
10 | const TRANSACTION_SERIALIZABLE = 'SERIALIZABLE'; |
||
11 | const TRANSACTION_REPEATABLE_READ = 'REPEATABLE READ'; |
||
12 | const TRANSACTION_READ_COMMITTED = 'READ COMMITTED'; |
||
13 | const TRANSACTION_READ_UNCOMMITTED = 'READ UNCOMMITTED'; |
||
14 | |||
15 | /** |
||
16 | * Статус соеднения с MySQL |
||
17 | * |
||
18 | * @var bool |
||
19 | */ |
||
20 | public $connected = false; |
||
21 | /** |
||
22 | * Префикс названий таблиц в БД |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | public $db_prefix = ''; |
||
27 | /** |
||
28 | * Список таблиц в БД |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | public $table_list = array(); |
||
33 | |||
34 | /** |
||
35 | * Настройки БД |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $dbsettings = array(); |
||
40 | /** |
||
41 | * Драйвер для прямого обращения к MySQL |
||
42 | * |
||
43 | * @var db_mysql_v5 $driver |
||
44 | */ |
||
45 | public $driver = null; |
||
46 | |||
47 | /** |
||
48 | * Общее время запросов |
||
49 | * |
||
50 | * @var float $time_mysql_total |
||
51 | */ |
||
52 | public $time_mysql_total = 0.0; |
||
53 | |||
54 | /** |
||
55 | * Amount of queries on this DB |
||
56 | * |
||
57 | * @var int |
||
58 | */ |
||
59 | public $queryCount = 0; |
||
60 | |||
61 | public $isWatching = false; |
||
62 | |||
63 | /** |
||
64 | * @var \DBAL\DbTransaction $transaction |
||
65 | */ |
||
66 | protected $transaction; |
||
67 | |||
68 | /** |
||
69 | * Should query check be skipped? |
||
70 | * |
||
71 | * Used for altering scheme of DB |
||
72 | * |
||
73 | * @var bool $skipQueryCheck |
||
74 | */ |
||
75 | protected $skipQueryCheck = false; |
||
76 | |||
77 | /** |
||
78 | * @var SnCache $snCache |
||
79 | */ |
||
80 | public $snCache; |
||
81 | |||
82 | /** |
||
83 | * @var DbRowDirectOperator $operator |
||
84 | */ |
||
85 | protected $operator; |
||
86 | |||
87 | /** |
||
88 | * db_mysql constructor. |
||
89 | * |
||
90 | * @param \Common\GlobalContainer $gc |
||
91 | */ |
||
92 | public function __construct($gc) { |
||
97 | |||
98 | public function load_db_settings($configFile = '') { |
||
107 | |||
108 | /** |
||
109 | * @param null|array $external_db_settings |
||
110 | * |
||
111 | * @return bool |
||
112 | */ |
||
113 | public function sn_db_connect($external_db_settings = null) { |
||
142 | |||
143 | protected function driver_connect() { |
||
154 | |||
155 | public function db_disconnect() { |
||
163 | |||
164 | /** |
||
165 | * @param string $query |
||
166 | * |
||
167 | * @return mixed|string |
||
168 | */ |
||
169 | public function replaceTablePlaceholders($query) { |
||
179 | |||
180 | /** |
||
181 | * @param $query |
||
182 | */ |
||
183 | protected function logQuery($query) { |
||
194 | |||
195 | |||
196 | /** |
||
197 | * @return string |
||
198 | */ |
||
199 | public function traceQuery() { |
||
218 | |||
219 | /** |
||
220 | * @param string $query |
||
221 | * |
||
222 | * @return array|bool|mysqli_result|null |
||
223 | */ |
||
224 | protected function queryDriver($query) { |
||
254 | |||
255 | |||
256 | // Just wrappers to distinguish query types |
||
257 | /** |
||
258 | * Executes non-data manipulation statements |
||
259 | * |
||
260 | * Can execute queries with check skip |
||
261 | * Honor current state of query checking |
||
262 | * |
||
263 | * @param string $query |
||
264 | * @param bool $skip_query_check |
||
265 | * |
||
266 | * @return array|bool|mysqli_result|null |
||
267 | */ |
||
268 | public function doSql($query, $skip_query_check = false) { |
||
277 | |||
278 | |||
279 | // TODO - should be in DbRowDirectOperator eventually |
||
280 | // SQL operations ==================================================================================================== |
||
281 | // SELECTS ----------------------------------------------------------------------------------------------------------- |
||
282 | public function doSelect($query) { |
||
285 | |||
286 | /** |
||
287 | * DANGER! Fields and Where can be danger |
||
288 | * |
||
289 | * @param string $table |
||
290 | * @param array $fields |
||
291 | * @param array $where |
||
292 | * @param bool $isOneRecord |
||
293 | * |
||
294 | * @return array|bool|mysqli_result|null |
||
295 | * |
||
296 | * TODO - replace with appropriate DbQuery when it's be ready |
||
297 | */ |
||
298 | public function doSelectDanger($table, $fields, $where = array(), $isOneRecord = DB_RECORDS_ALL, $forUpdate = DB_SELECT_PLAIN) { |
||
317 | |||
318 | /** |
||
319 | * @param string $strSql |
||
320 | * |
||
321 | * @return array|null |
||
322 | * |
||
323 | * @deprecated |
||
324 | * TODO - УДАЛИТЬ |
||
325 | * Метод временно находится здесь - слишком много переписывать, что бы его отсюда вынести |
||
326 | */ |
||
327 | public function doSelectFetchArray($strSql) { |
||
330 | |||
331 | |||
332 | |||
333 | // |
||
334 | // INSERT/REPLACE ---------------------------------------------------------------------------------------------------- |
||
335 | protected function doSet($table, $fieldsAndValues, $replace = DB_INSERT_PLAIN) { |
||
343 | |||
344 | // TODO - batch insert and replace here |
||
345 | // перед тем, как переделывать данные из депрекейтов - убедится, что |
||
346 | // null - это null, а не строка'NULL' |
||
347 | /** |
||
348 | * Values should be passed as-is |
||
349 | * |
||
350 | * DANGER! Values should be properly escaped before passing here |
||
351 | * |
||
352 | * @param string $table |
||
353 | * @param array $fields |
||
354 | * @param string[] $valuesDanger |
||
355 | * @param int $replace |
||
356 | * |
||
357 | * @return array|bool|mysqli_result|null |
||
358 | * @deprecated |
||
359 | */ |
||
360 | protected function doInsertBatchDanger($table, $fields, &$valuesDanger, $replace = DB_INSERT_PLAIN) { |
||
369 | |||
370 | |||
371 | // INSERTERS |
||
372 | public function doInsertComplex($query) { |
||
375 | |||
376 | /** |
||
377 | * @param string $table |
||
378 | * @param array $fieldsAndValues |
||
379 | * @param int $replace - DB_INSERT_PLAIN || DB_INSERT_IGNORE |
||
380 | * |
||
381 | * @return array|bool|mysqli_result|null |
||
382 | */ |
||
383 | public function doInsertSet($table, $fieldsAndValues, $replace = DB_INSERT_PLAIN) { |
||
386 | |||
387 | /** |
||
388 | * Values should be passed as-is |
||
389 | * |
||
390 | * @param string $table |
||
391 | * @param array $fields |
||
392 | * @param string[] $values |
||
393 | * |
||
394 | * @return array|bool|mysqli_result|null |
||
395 | * @deprecated |
||
396 | */ |
||
397 | public function doInsertValuesDeprecated($table, $fields, &$values) { |
||
400 | |||
401 | |||
402 | // REPLACERS |
||
403 | /** |
||
404 | * Replaces record in DB |
||
405 | * |
||
406 | * There are no DANGER replace operations |
||
407 | * |
||
408 | * @param string $table |
||
409 | * @param array $fieldsAndValues |
||
410 | * |
||
411 | * @return array|bool|mysqli_result|null |
||
412 | */ |
||
413 | public function doReplaceSet($table, $fieldsAndValues) { |
||
416 | |||
417 | /** |
||
418 | * Values should be passed as-is |
||
419 | * |
||
420 | * @param string $table |
||
421 | * @param array $fields |
||
422 | * @param string[] $values |
||
423 | * |
||
424 | * @return array|bool|mysqli_result|null |
||
425 | * @deprecated |
||
426 | */ |
||
427 | public function doReplaceValuesDeprecated($table, $fields, &$values) { |
||
430 | |||
431 | |||
432 | // UPDATERS |
||
433 | public function doUpdateReallyComplex($query) { |
||
436 | |||
437 | /** |
||
438 | * Executes self-contained SQL UPDATE query |
||
439 | * |
||
440 | * Self-contained - means no params used |
||
441 | * Such queries usually used to make large amount of in-base calculations |
||
442 | * |
||
443 | * @param $query |
||
444 | * |
||
445 | * @return array|bool|mysqli_result|null |
||
446 | */ |
||
447 | public function doUpdateSqlNoParam($query) { |
||
450 | |||
451 | |||
452 | /** |
||
453 | * @param $DbQuery DbQuery |
||
454 | */ |
||
455 | protected function doUpdateDbQuery($DbQuery) { |
||
458 | |||
459 | /** |
||
460 | * @param $DbQuery DbQuery |
||
461 | */ |
||
462 | public function doUpdateDbQueryAdjust($DbQuery) { |
||
465 | |||
466 | |||
467 | protected function doUpdateWhere($table, $fieldsSet, $fieldsAdjust = array(), $where = array(), $isOneRecord = DB_RECORDS_ALL, $whereDanger = array()) { |
||
480 | |||
481 | public function doUpdateRowSet($table, $fieldsAndValues, $where) { |
||
484 | |||
485 | public function doUpdateTableSet($table, $fieldsAndValues, $where = array()) { |
||
488 | |||
489 | public function doUpdateRowAdjust($table, $fieldsSet, $fieldsAdjust, $where) { |
||
492 | |||
493 | public function doUpdateTableAdjust($table, $fieldsSet, $fieldsAdjust, $where, $whereDanger = array()) { |
||
496 | |||
497 | |||
498 | // |
||
499 | // DELETERS ---------------------------------------------------------------------------------------------------------- |
||
500 | /** |
||
501 | * @param string $table |
||
502 | * @param array $where |
||
503 | * @param bool $isOneRecord |
||
504 | * |
||
505 | * @return DbQuery |
||
506 | */ |
||
507 | protected function buildDeleteQuery($table, $where, $isOneRecord = DB_RECORDS_ALL) { |
||
514 | |||
515 | /** |
||
516 | * @param string $table |
||
517 | * @param array $where - simple WHERE statement list which can be combined with AND |
||
518 | * @param bool $isOneRecord |
||
519 | * |
||
520 | * @return array|bool|mysqli_result|null |
||
521 | */ |
||
522 | public function doDeleteWhere($table, $where, $isOneRecord = DB_RECORDS_ALL) { |
||
529 | |||
530 | /** |
||
531 | * Early deprecated function for complex delete conditions |
||
532 | * |
||
533 | * Used for malformed $where conditions |
||
534 | * Also whereDanger can contain references for other {{tables}} |
||
535 | * |
||
536 | * @param string $table |
||
537 | * @param array $where |
||
538 | * @param array $whereDanger |
||
539 | * |
||
540 | * @return array|bool|mysqli_result|null |
||
541 | * @deprecated |
||
542 | */ |
||
543 | public function doDeleteDanger($table, $where, $whereDanger) { |
||
551 | |||
552 | /** |
||
553 | * @param string $table |
||
554 | * @param array $where - simple WHERE statement list which can be combined with AND |
||
555 | * |
||
556 | * @return array|bool|mysqli_result|null |
||
557 | */ |
||
558 | public function doDeleteRow($table, $where) { |
||
561 | |||
562 | /** |
||
563 | * Perform simple delete queries on fixed tables w/o params |
||
564 | * |
||
565 | * @param string $query |
||
566 | * |
||
567 | * @return array|bool|mysqli_result|null |
||
568 | */ |
||
569 | public function doDeleteSql($query) { |
||
572 | |||
573 | |||
574 | // LOCKERS ---------------------------------------------------------------------------------------------------------- |
||
575 | /** |
||
576 | * @param DbQueryConstructor $stmt |
||
577 | * @param bool $skip_query_check |
||
578 | */ |
||
579 | public function doStmtLockAll($stmt, $skip_query_check = false) { |
||
589 | |||
590 | |||
591 | // |
||
592 | // OTHER FUNCTIONS ---------------------------------------------------------------------------------------------------------- |
||
593 | // TODO Заменить это на новый логгер |
||
594 | protected function security_watch_user_queries($query) { |
||
615 | |||
616 | |||
617 | public function security_query_check_bad_words($query) { |
||
674 | |||
675 | /** |
||
676 | * @param bool $prefixed_only |
||
677 | * |
||
678 | * @return array |
||
679 | */ |
||
680 | public function db_get_table_list($prefixed_only = true) { |
||
700 | |||
701 | /** |
||
702 | * @param string $statement |
||
703 | * |
||
704 | * @return bool|mysqli_stmt |
||
705 | */ |
||
706 | View Code Duplication | public function db_prepare($statement) { |
|
713 | |||
714 | |||
715 | /** |
||
716 | * L1 perform the query |
||
717 | * |
||
718 | * @param $query_string |
||
719 | * |
||
720 | * @return bool|mysqli_result |
||
721 | */ |
||
722 | View Code Duplication | public function db_sql_query($query_string) { |
|
729 | |||
730 | /** |
||
731 | * L1 fetch assoc array |
||
732 | * |
||
733 | * @param mysqli_result $query |
||
734 | * |
||
735 | * @return array|null |
||
736 | */ |
||
737 | View Code Duplication | public function db_fetch($query) { |
|
744 | |||
745 | public function db_fetch_row(&$query) { |
||
748 | |||
749 | public function db_escape($unescaped_string) { |
||
752 | |||
753 | public function driver_disconnect() { |
||
756 | |||
757 | public function db_error() { |
||
760 | |||
761 | public function db_insert_id() { |
||
764 | |||
765 | public function db_num_rows(&$result) { |
||
768 | |||
769 | /** |
||
770 | * @return int -1 means error |
||
771 | */ |
||
772 | public function db_affected_rows() { |
||
775 | |||
776 | /** |
||
777 | * @return string |
||
778 | */ |
||
779 | public function db_get_client_info() { |
||
782 | |||
783 | /** |
||
784 | * @return string |
||
785 | */ |
||
786 | public function db_get_server_info() { |
||
789 | |||
790 | /** |
||
791 | * @return string |
||
792 | */ |
||
793 | public function db_get_host_info() { |
||
796 | |||
797 | public function db_get_server_stat() { |
||
808 | |||
809 | /** |
||
810 | * @return array |
||
811 | * @throws Exception |
||
812 | */ |
||
813 | public function db_core_show_status() { |
||
826 | |||
827 | public function mysql_get_table_list() { |
||
830 | |||
831 | public function mysql_get_innodb_status() { |
||
834 | |||
835 | /** |
||
836 | * @return DbRowDirectOperator |
||
837 | */ |
||
838 | public function getOperator() { |
||
841 | |||
842 | |||
843 | |||
844 | // Some wrappers to DbTransaction |
||
845 | // Unused for now |
||
846 | /** |
||
847 | * @return DbTransaction |
||
848 | */ |
||
849 | public function getTransaction() { |
||
852 | |||
853 | public function transactionCheck($status = null) { |
||
856 | |||
857 | public function transactionStart($level = '') { |
||
860 | |||
861 | public function transactionCommit() { |
||
864 | |||
865 | public function transactionRollback() { |
||
868 | |||
869 | } |
||
870 |