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 |
||
| 8 | class db_mysql { |
||
| 9 | const TRANSACTION_SERIALIZABLE = 'SERIALIZABLE'; |
||
| 10 | const TRANSACTION_REPEATABLE_READ = 'REPEATABLE READ'; |
||
| 11 | const TRANSACTION_READ_COMMITTED = 'READ COMMITTED'; |
||
| 12 | const TRANSACTION_READ_UNCOMMITTED = 'READ UNCOMMITTED'; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Статус соеднения с MySQL |
||
| 16 | * |
||
| 17 | * @var bool |
||
| 18 | */ |
||
| 19 | public $connected = false; |
||
| 20 | /** |
||
| 21 | * Префикс названий таблиц в БД |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | public $db_prefix = ''; |
||
| 26 | /** |
||
| 27 | * Список таблиц в БД |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | public $table_list = array(); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Настройки БД |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $dbsettings = array(); |
||
| 39 | /** |
||
| 40 | * Драйвер для прямого обращения к MySQL |
||
| 41 | * |
||
| 42 | * @var db_mysql_v5 $driver |
||
| 43 | */ |
||
| 44 | public $driver = null; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Общее время запросов |
||
| 48 | * |
||
| 49 | * @var float $time_mysql_total |
||
| 50 | */ |
||
| 51 | public $time_mysql_total = 0.0; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Amount of queries on this DB |
||
| 55 | * |
||
| 56 | * @var int |
||
| 57 | */ |
||
| 58 | public $queryCount = 0; |
||
| 59 | |||
| 60 | public $isWatching = false; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var \DBAL\DbTransaction $transaction |
||
| 64 | */ |
||
| 65 | protected $transaction; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Should query check be skipped? |
||
| 69 | * |
||
| 70 | * Used for altering scheme of DB |
||
| 71 | * |
||
| 72 | * @var bool $skipQueryCheck |
||
| 73 | */ |
||
| 74 | protected $skipQueryCheck = false; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var SnCache $snCache |
||
| 78 | */ |
||
| 79 | public $snCache; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * db_mysql constructor. |
||
| 83 | * |
||
| 84 | * @param \Common\GlobalContainer $gc |
||
| 85 | */ |
||
| 86 | public function __construct($gc) { |
||
| 90 | |||
| 91 | public function load_db_settings($configFile = '') { |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param null|array $external_db_settings |
||
| 103 | * |
||
| 104 | * @return bool |
||
| 105 | */ |
||
| 106 | public function sn_db_connect($external_db_settings = null) { |
||
| 135 | |||
| 136 | protected function driver_connect() { |
||
| 147 | |||
| 148 | public function db_disconnect() { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param string $query |
||
| 159 | * |
||
| 160 | * @return mixed|string |
||
| 161 | */ |
||
| 162 | public function replaceTablePlaceholders($query) { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param $query |
||
| 175 | */ |
||
| 176 | protected function logQuery($query) { |
||
| 187 | |||
| 188 | |||
| 189 | /** |
||
| 190 | * @return string |
||
| 191 | */ |
||
| 192 | public function queryTrace() { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param string $query |
||
| 214 | * |
||
| 215 | * @return array|bool|mysqli_result|null |
||
| 216 | * @internal param bool $skip_query_check |
||
| 217 | * |
||
| 218 | */ |
||
| 219 | protected function queryDriver($query) { |
||
| 249 | |||
| 250 | |||
| 251 | // Just wrappers to distinguish query types |
||
| 252 | /** |
||
| 253 | * Executes non-data manipulation statements |
||
| 254 | * |
||
| 255 | * Can execute queries with check skip |
||
| 256 | * Honor current state of query checking |
||
| 257 | * |
||
| 258 | * @param string $query |
||
| 259 | * @param bool $skip_query_check |
||
| 260 | * |
||
| 261 | * @return array|bool|mysqli_result|null |
||
| 262 | */ |
||
| 263 | public function doSql($query, $skip_query_check = false) { |
||
| 277 | |||
| 278 | |||
| 279 | public function doSelect($query) { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param string $query |
||
| 285 | * |
||
| 286 | * @return array|null |
||
| 287 | */ |
||
| 288 | public function doSelectFetch($query) { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param string $query |
||
| 294 | * |
||
| 295 | * @return mixed|null |
||
| 296 | */ |
||
| 297 | public function doSelectFetchValue($query) { |
||
| 302 | |||
| 303 | |||
| 304 | public function doInsertComplex($query) { |
||
| 307 | |||
| 308 | protected function doSet($table, $fieldsAndValues, $replace = DB_INSERT_PLAIN) { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @param string $table |
||
| 331 | * @param array $fieldsAndValues |
||
| 332 | * @param int $replace - DB_INSERT_PLAIN || DB_INSERT_IGNORE |
||
| 333 | * |
||
| 334 | * @return array|bool|mysqli_result|null |
||
| 335 | */ |
||
| 336 | public function doInsertSet($table, $fieldsAndValues, $replace = DB_INSERT_PLAIN) { |
||
| 339 | |||
| 340 | public function doReplaceSet($table, $fieldsAndValues) { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Values should be passed as-is |
||
| 346 | * |
||
| 347 | * @param string $table |
||
| 348 | * @param array $fields |
||
| 349 | * @param string[] $values |
||
| 350 | * @param bool $replace |
||
| 351 | * |
||
| 352 | * @return array|bool|mysqli_result|null |
||
| 353 | * @deprecated |
||
| 354 | */ |
||
| 355 | protected function doValuesDeprecated($table, $fields, &$values, $replace = DB_INSERT_PLAIN) { |
||
| 364 | |||
| 365 | // TODO - batch insert and replace here |
||
| 366 | |||
| 367 | // TODO - перед тем, как переделывать данные из депрекейтов - убедится, что |
||
| 368 | // null - это null, а не строка'NULL' |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Values should be passed as-is |
||
| 372 | * |
||
| 373 | * @param string $table |
||
| 374 | * @param array $fields |
||
| 375 | * @param string[] $values |
||
| 376 | * |
||
| 377 | * @return array|bool|mysqli_result|null |
||
| 378 | * @deprecated |
||
| 379 | */ |
||
| 380 | public function doInsertValuesDeprecated($table, $fields, &$values) { |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Values should be passed as-is |
||
| 386 | * |
||
| 387 | * @param string $table |
||
| 388 | * @param array $fields |
||
| 389 | * @param string[] $values |
||
| 390 | * |
||
| 391 | * @return array|bool|mysqli_result|null |
||
| 392 | * @deprecated |
||
| 393 | */ |
||
| 394 | public function doReplaceValuesDeprecated($table, $fields, &$values) { |
||
| 397 | |||
| 398 | |||
| 399 | public function doUpdateComplex($query) { |
||
| 402 | |||
| 403 | public function doUpdateReallyComplex($query) { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Executes self-contained SQL UPDATE query |
||
| 409 | * |
||
| 410 | * Self-contained - means no params used |
||
| 411 | * Such queries usually used to make large amount of in-base calculations |
||
| 412 | * |
||
| 413 | * @param $query |
||
| 414 | * |
||
| 415 | * @return array|bool|mysqli_result|null |
||
| 416 | */ |
||
| 417 | public function doUpdateSqlNoParam($query) { |
||
| 420 | |||
| 421 | protected function doUpdateWhere($table, $fieldsSet, $fieldsAdjust = array(), $where = array(), $isOneRecord = DB_RECORDS_ALL) { |
||
| 443 | |||
| 444 | public function doUpdateRowSet($table, $fieldsAndValues, $where) { |
||
| 447 | |||
| 448 | public function doUpdateTableSet($table, $fieldsAndValues, $where = array()) { |
||
| 451 | |||
| 452 | public function doUpdateRowAdjust($table, $fieldsSet, $fieldsAdjust, $where) { |
||
| 455 | |||
| 456 | public function doUpdateTableAdjust($table, $fieldsSet, $fieldsAdjust, $where) { |
||
| 459 | |||
| 460 | /** |
||
| 461 | * In this call means that used some conditions in $where different than field = value |
||
| 462 | * |
||
| 463 | * It should be rewrote later |
||
| 464 | * |
||
| 465 | * @param $table |
||
| 466 | * @param $fieldsSet |
||
| 467 | * @param $fieldsAdjust |
||
| 468 | * @param $where |
||
| 469 | * |
||
| 470 | * @return array|bool|mysqli_result|null |
||
| 471 | * @deprecated |
||
| 472 | */ |
||
| 473 | public function doUpdateTableAdjustDanger($table, $fieldsSet, $fieldsAdjust, $where) { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * For update_old - would be deprecated |
||
| 479 | * |
||
| 480 | * @param string $query |
||
| 481 | * |
||
| 482 | * @return array|bool|mysqli_result|null |
||
| 483 | * @deprecated |
||
| 484 | */ |
||
| 485 | public function doUpdateOld($query) { |
||
| 488 | |||
| 489 | |||
| 490 | |||
| 491 | // DELETERS |
||
| 492 | /** |
||
| 493 | * @param string $table |
||
| 494 | * @param array $where |
||
| 495 | * @param bool $isOneRecord |
||
| 496 | * |
||
| 497 | * @return DbQuery |
||
| 498 | */ |
||
| 499 | protected function buildDeleteQuery($table, $where, $isOneRecord = DB_RECORDS_ALL) { |
||
| 505 | |||
| 506 | /** |
||
| 507 | * @param string $table |
||
| 508 | * @param array $where - simple WHERE statement list which can be combined with AND |
||
| 509 | * @param bool $isOneRecord |
||
| 510 | * |
||
| 511 | * @return array|bool|mysqli_result|null |
||
| 512 | */ |
||
| 513 | public function doDeleteWhere($table, $where, $isOneRecord = DB_RECORDS_ALL) { |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Early deprecated function for complex delete conditions |
||
| 519 | * |
||
| 520 | * Used for malformed $where conditions |
||
| 521 | * Also whereDanger can contain references for other {{tables}} |
||
| 522 | * |
||
| 523 | * @param string $table |
||
| 524 | * @param array $where |
||
| 525 | * @param array $whereDanger |
||
| 526 | * |
||
| 527 | * @return array|bool|mysqli_result|null |
||
| 528 | * @deprecated |
||
| 529 | */ |
||
| 530 | public function doDeleteDanger($table, $where, $whereDanger) { |
||
| 533 | |||
| 534 | /** |
||
| 535 | * @param string $table |
||
| 536 | * @param array $where - simple WHERE statement list which can be combined with AND |
||
| 537 | * |
||
| 538 | * @return array|bool|mysqli_result|null |
||
| 539 | */ |
||
| 540 | public function doDeleteRow($table, $where) { |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Perform simple delete queries on fixed tables w/o params |
||
| 546 | * |
||
| 547 | * @param string $query |
||
| 548 | * |
||
| 549 | * @return array|bool|mysqli_result|null |
||
| 550 | */ |
||
| 551 | public function doDeleteSimple($query) { |
||
| 554 | |||
| 555 | |||
| 556 | // Misc functions |
||
| 557 | // |
||
| 558 | View Code Duplication | protected function castAsDbValue($value) { |
|
| 588 | |||
| 589 | /** |
||
| 590 | * Make field list safe |
||
| 591 | * |
||
| 592 | * Support expressions - expression index should be strictly integer! |
||
| 593 | * |
||
| 594 | * @param array $fields - array of pair $fieldName => $fieldValue |
||
| 595 | * |
||
| 596 | * @return array |
||
| 597 | */ |
||
| 598 | View Code Duplication | protected function safeFieldsEqualValues($fields) { |
|
| 616 | |||
| 617 | /** |
||
| 618 | * Make fields adjustment safe |
||
| 619 | * |
||
| 620 | * Convert "key => value" pair to string "`key` = `key` + (value)" |
||
| 621 | * Supports expressions - expression index should be strictly integer! |
||
| 622 | * |
||
| 623 | * @param array $fields - array of pair $fieldName => $fieldValue |
||
| 624 | * |
||
| 625 | * @return array |
||
| 626 | */ |
||
| 627 | View Code Duplication | protected function safeFieldsAdjust($fields) { |
|
| 645 | |||
| 646 | // TODO - redo as callable usage with array_map/array_walk |
||
| 647 | View Code Duplication | public function safeValues($values) { |
|
| 660 | |||
| 661 | // TODO - redo as callable usage with array_map/array_walk |
||
| 662 | View Code Duplication | public function safeFields($fields) { |
|
| 675 | |||
| 676 | |||
| 677 | /** |
||
| 678 | * Returns iterator to iterate through mysqli_result |
||
| 679 | * |
||
| 680 | * @param string $query |
||
| 681 | * |
||
| 682 | * return DbResultIterator |
||
| 683 | * |
||
| 684 | * @return DbEmptyIterator|DbMysqliResultIterator |
||
| 685 | */ |
||
| 686 | public function doSelectIterator($query) { |
||
| 697 | |||
| 698 | /** |
||
| 699 | * @param DbQueryConstructor $stmt |
||
| 700 | * @param bool $skip_query_check |
||
| 701 | */ |
||
| 702 | public function doStmtLockAll($stmt, $skip_query_check = false) { |
||
| 712 | |||
| 713 | // TODO Заменить это на новый логгер |
||
| 714 | protected function security_watch_user_queries($query) { |
||
| 735 | |||
| 736 | |||
| 737 | public function security_query_check_bad_words($query) { |
||
| 794 | |||
| 795 | /** |
||
| 796 | * @param bool $prefixed_only |
||
| 797 | * |
||
| 798 | * @return array |
||
| 799 | */ |
||
| 800 | public function db_get_table_list($prefixed_only = true) { |
||
| 820 | |||
| 821 | /** |
||
| 822 | * @param string $statement |
||
| 823 | * |
||
| 824 | * @return bool|mysqli_stmt |
||
| 825 | */ |
||
| 826 | View Code Duplication | public function db_prepare($statement) { |
|
| 833 | |||
| 834 | |||
| 835 | /** |
||
| 836 | * L1 perform the query |
||
| 837 | * |
||
| 838 | * @param $query_string |
||
| 839 | * |
||
| 840 | * @return bool|mysqli_result |
||
| 841 | */ |
||
| 842 | View Code Duplication | public function db_sql_query($query_string) { |
|
| 849 | |||
| 850 | /** |
||
| 851 | * L1 fetch assoc array |
||
| 852 | * |
||
| 853 | * @param mysqli_result $query |
||
| 854 | * |
||
| 855 | * @return array|null |
||
| 856 | */ |
||
| 857 | View Code Duplication | public function db_fetch(&$query) { |
|
| 864 | |||
| 865 | public function db_fetch_row(&$query) { |
||
| 868 | |||
| 869 | public function db_escape($unescaped_string) { |
||
| 872 | |||
| 873 | public function driver_disconnect() { |
||
| 876 | |||
| 877 | public function db_error() { |
||
| 880 | |||
| 881 | public function db_insert_id() { |
||
| 884 | |||
| 885 | public function db_num_rows(&$result) { |
||
| 888 | |||
| 889 | public function db_affected_rows() { |
||
| 892 | |||
| 893 | /** |
||
| 894 | * @return string |
||
| 895 | */ |
||
| 896 | public function db_get_client_info() { |
||
| 899 | |||
| 900 | /** |
||
| 901 | * @return string |
||
| 902 | */ |
||
| 903 | public function db_get_server_info() { |
||
| 906 | |||
| 907 | /** |
||
| 908 | * @return string |
||
| 909 | */ |
||
| 910 | public function db_get_host_info() { |
||
| 913 | |||
| 914 | public function db_get_server_stat() { |
||
| 925 | |||
| 926 | /** |
||
| 927 | * @return array |
||
| 928 | * @throws Exception |
||
| 929 | */ |
||
| 930 | public function db_core_show_status() { |
||
| 943 | |||
| 944 | public function mysql_get_table_list() { |
||
| 947 | |||
| 948 | public function mysql_get_innodb_status() { |
||
| 951 | |||
| 952 | /** |
||
| 953 | * @return \DBAL\DbTransaction |
||
| 954 | */ |
||
| 955 | public function getTransaction() { |
||
| 958 | |||
| 959 | // Some wrappers to DbTransaction |
||
| 960 | // Unused for now |
||
| 961 | public function transactionCheck($status = null) { |
||
| 964 | |||
| 965 | public function transactionStart($level = '') { |
||
| 968 | |||
| 969 | public function transactionCommit() { |
||
| 972 | |||
| 973 | public function transactionRollback() { |
||
| 976 | |||
| 977 | } |
||
| 978 |