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() { |
||
| 137 | if (!is_object($this->driver)) { |
||
| 138 | classSupernova::$debug->error_fatal('DB Error - No driver for MySQL found!'); |
||
| 139 | } |
||
| 140 | |||
| 141 | if (!method_exists($this->driver, 'mysql_connect')) { |
||
| 142 | classSupernova::$debug->error_fatal('DB Error - WRONG MySQL driver!'); |
||
| 143 | } |
||
| 144 | |||
| 145 | return $this->driver->mysql_connect($this->dbsettings); |
||
| 146 | } |
||
| 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 traceQuery() { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param string $query |
||
| 214 | * |
||
| 215 | * @return array|bool|mysqli_result|null |
||
| 216 | */ |
||
| 217 | protected function queryDriver($query) { |
||
| 247 | |||
| 248 | |||
| 249 | // Just wrappers to distinguish query types |
||
| 250 | /** |
||
| 251 | * Executes non-data manipulation statements |
||
| 252 | * |
||
| 253 | * Can execute queries with check skip |
||
| 254 | * Honor current state of query checking |
||
| 255 | * |
||
| 256 | * @param string $query |
||
| 257 | * @param bool $skip_query_check |
||
| 258 | * |
||
| 259 | * @return array|bool|mysqli_result|null |
||
| 260 | */ |
||
| 261 | public function doSql($query, $skip_query_check = false) { |
||
| 270 | |||
| 271 | |||
| 272 | // SELECTS |
||
| 273 | public function doSelect($query) { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * DANGER! Fields and Where can be danger |
||
| 279 | * |
||
| 280 | * @param string $table |
||
| 281 | * @param array $fields |
||
| 282 | * @param array $where |
||
| 283 | * @param bool $isOneRecord |
||
| 284 | * |
||
| 285 | * @return array|bool|mysqli_result|null |
||
| 286 | */ |
||
| 287 | public function doSelectDanger($table, $fields, $where = array(), $isOneRecord = DB_RECORDS_ALL, $forUpdate = DB_SELECT_PLAIN) { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param string $query |
||
| 309 | * |
||
| 310 | * @return array|null |
||
| 311 | */ |
||
| 312 | public function doSelectFetch($query) { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @param string $query |
||
| 318 | * |
||
| 319 | * @return mixed|null |
||
| 320 | */ |
||
| 321 | public function doSelectFetchValue($query) { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @param DbResultIterator $iterator |
||
| 329 | * |
||
| 330 | * @return mixed |
||
| 331 | */ |
||
| 332 | public function getDbIteratorFirstValue($iterator) { |
||
| 337 | |||
| 338 | |||
| 339 | // INSERT/REPLACE |
||
| 340 | protected function doSet($table, $fieldsAndValues, $replace = DB_INSERT_PLAIN) { |
||
| 348 | |||
| 349 | // TODO - batch insert and replace here |
||
| 350 | // TODO - перед тем, как переделывать данные из депрекейтов - убедится, что |
||
| 351 | // null - это null, а не строка'NULL' |
||
| 352 | /** |
||
| 353 | * Values should be passed as-is |
||
| 354 | * |
||
| 355 | * DANGER! Values should be properly escaped before passing here |
||
| 356 | * |
||
| 357 | * @param string $table |
||
| 358 | * @param array $fields |
||
| 359 | * @param string[] $valuesDanger |
||
| 360 | * @param int $replace |
||
| 361 | * |
||
| 362 | * @return array|bool|mysqli_result|null |
||
| 363 | * @deprecated |
||
| 364 | */ |
||
| 365 | protected function doInsertBatchDanger($table, $fields, &$valuesDanger, $replace = DB_INSERT_PLAIN) { |
||
| 374 | |||
| 375 | |||
| 376 | // INSERTERS |
||
| 377 | public function doInsertComplex($query) { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @param string $table |
||
| 383 | * @param array $fieldsAndValues |
||
| 384 | * @param int $replace - DB_INSERT_PLAIN || DB_INSERT_IGNORE |
||
| 385 | * |
||
| 386 | * @return array|bool|mysqli_result|null |
||
| 387 | */ |
||
| 388 | public function doInsertSet($table, $fieldsAndValues, $replace = DB_INSERT_PLAIN) { |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Values should be passed as-is |
||
| 394 | * |
||
| 395 | * @param string $table |
||
| 396 | * @param array $fields |
||
| 397 | * @param string[] $values |
||
| 398 | * |
||
| 399 | * @return array|bool|mysqli_result|null |
||
| 400 | * @deprecated |
||
| 401 | */ |
||
| 402 | public function doInsertValuesDeprecated($table, $fields, &$values) { |
||
| 405 | |||
| 406 | |||
| 407 | |||
| 408 | // REPLACERS |
||
| 409 | /** |
||
| 410 | * Replaces record in DB |
||
| 411 | * |
||
| 412 | * There are no DANGER replace operations |
||
| 413 | * |
||
| 414 | * @param string $table |
||
| 415 | * @param array $fieldsAndValues |
||
| 416 | * |
||
| 417 | * @return array|bool|mysqli_result|null |
||
| 418 | */ |
||
| 419 | public function doReplaceSet($table, $fieldsAndValues) { |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Values should be passed as-is |
||
| 425 | * |
||
| 426 | * @param string $table |
||
| 427 | * @param array $fields |
||
| 428 | * @param string[] $values |
||
| 429 | * |
||
| 430 | * @return array|bool|mysqli_result|null |
||
| 431 | * @deprecated |
||
| 432 | */ |
||
| 433 | public function doReplaceValuesDeprecated($table, $fields, &$values) { |
||
| 436 | |||
| 437 | |||
| 438 | // UPDATERS |
||
| 439 | public function doUpdateReallyComplex($query) { |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Executes self-contained SQL UPDATE query |
||
| 445 | * |
||
| 446 | * Self-contained - means no params used |
||
| 447 | * Such queries usually used to make large amount of in-base calculations |
||
| 448 | * |
||
| 449 | * @param $query |
||
| 450 | * |
||
| 451 | * @return array|bool|mysqli_result|null |
||
| 452 | */ |
||
| 453 | public function doUpdateSqlNoParam($query) { |
||
| 456 | |||
| 457 | |||
| 458 | /** |
||
| 459 | * @param $DbQuery DbQuery |
||
| 460 | */ |
||
| 461 | protected function doUpdateDbQuery($DbQuery) { |
||
| 464 | |||
| 465 | /** |
||
| 466 | * @param $DbQuery DbQuery |
||
| 467 | */ |
||
| 468 | public function doUpdateDbQueryAdjust($DbQuery) { |
||
| 471 | |||
| 472 | |||
| 473 | protected function doUpdateWhere($table, $fieldsSet, $fieldsAdjust = array(), $where = array(), $isOneRecord = DB_RECORDS_ALL, $whereDanger = array()) { |
||
| 486 | |||
| 487 | public function doUpdateRowSet($table, $fieldsAndValues, $where) { |
||
| 490 | |||
| 491 | public function doUpdateTableSet($table, $fieldsAndValues, $where = array()) { |
||
| 494 | |||
| 495 | public function doUpdateRowAdjust($table, $fieldsSet, $fieldsAdjust, $where) { |
||
| 498 | |||
| 499 | public function doUpdateTableAdjust($table, $fieldsSet, $fieldsAdjust, $where, $whereDanger = array()) { |
||
| 502 | |||
| 503 | |||
| 504 | // DELETERS |
||
| 505 | /** |
||
| 506 | * @param string $table |
||
| 507 | * @param array $where |
||
| 508 | * @param bool $isOneRecord |
||
| 509 | * |
||
| 510 | * @return DbQuery |
||
| 511 | */ |
||
| 512 | protected function buildDeleteQuery($table, $where, $isOneRecord = DB_RECORDS_ALL) { |
||
| 519 | |||
| 520 | /** |
||
| 521 | * @param string $table |
||
| 522 | * @param array $where - simple WHERE statement list which can be combined with AND |
||
| 523 | * @param bool $isOneRecord |
||
| 524 | * |
||
| 525 | * @return array|bool|mysqli_result|null |
||
| 526 | */ |
||
| 527 | public function doDeleteWhere($table, $where, $isOneRecord = DB_RECORDS_ALL) { |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Early deprecated function for complex delete conditions |
||
| 537 | * |
||
| 538 | * Used for malformed $where conditions |
||
| 539 | * Also whereDanger can contain references for other {{tables}} |
||
| 540 | * |
||
| 541 | * @param string $table |
||
| 542 | * @param array $where |
||
| 543 | * @param array $whereDanger |
||
| 544 | * |
||
| 545 | * @return array|bool|mysqli_result|null |
||
| 546 | * @deprecated |
||
| 547 | */ |
||
| 548 | public function doDeleteDanger($table, $where, $whereDanger) { |
||
| 556 | |||
| 557 | /** |
||
| 558 | * @param string $table |
||
| 559 | * @param array $where - simple WHERE statement list which can be combined with AND |
||
| 560 | * |
||
| 561 | * @return array|bool|mysqli_result|null |
||
| 562 | */ |
||
| 563 | public function doDeleteRow($table, $where) { |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Perform simple delete queries on fixed tables w/o params |
||
| 569 | * |
||
| 570 | * @param string $query |
||
| 571 | * |
||
| 572 | * @return array|bool|mysqli_result|null |
||
| 573 | */ |
||
| 574 | public function doDeleteSql($query) { |
||
| 577 | |||
| 578 | |||
| 579 | /** |
||
| 580 | * Returns iterator to iterate through mysqli_result |
||
| 581 | * |
||
| 582 | * @param string $query |
||
| 583 | * |
||
| 584 | * return DbResultIterator |
||
| 585 | * |
||
| 586 | * @return DbEmptyIterator|DbMysqliResultIterator |
||
| 587 | */ |
||
| 588 | public function doSelectIterator($query) { |
||
| 599 | |||
| 600 | /** |
||
| 601 | * @param DbQueryConstructor $stmt |
||
| 602 | * @param bool $skip_query_check |
||
| 603 | */ |
||
| 604 | public function doStmtLockAll($stmt, $skip_query_check = false) { |
||
| 614 | |||
| 615 | // TODO Заменить это на новый логгер |
||
| 616 | protected function security_watch_user_queries($query) { |
||
| 637 | |||
| 638 | |||
| 639 | public function security_query_check_bad_words($query) { |
||
| 696 | |||
| 697 | /** |
||
| 698 | * @param bool $prefixed_only |
||
| 699 | * |
||
| 700 | * @return array |
||
| 701 | */ |
||
| 702 | public function db_get_table_list($prefixed_only = true) { |
||
| 722 | |||
| 723 | /** |
||
| 724 | * @param string $statement |
||
| 725 | * |
||
| 726 | * @return bool|mysqli_stmt |
||
| 727 | */ |
||
| 728 | View Code Duplication | public function db_prepare($statement) { |
|
| 735 | |||
| 736 | |||
| 737 | /** |
||
| 738 | * L1 perform the query |
||
| 739 | * |
||
| 740 | * @param $query_string |
||
| 741 | * |
||
| 742 | * @return bool|mysqli_result |
||
| 743 | */ |
||
| 744 | View Code Duplication | public function db_sql_query($query_string) { |
|
| 751 | |||
| 752 | /** |
||
| 753 | * L1 fetch assoc array |
||
| 754 | * |
||
| 755 | * @param mysqli_result $query |
||
| 756 | * |
||
| 757 | * @return array|null |
||
| 758 | */ |
||
| 759 | View Code Duplication | public function db_fetch(&$query) { |
|
| 766 | |||
| 767 | public function db_fetch_row(&$query) { |
||
| 770 | |||
| 771 | public function db_escape($unescaped_string) { |
||
| 774 | |||
| 775 | public function driver_disconnect() { |
||
| 778 | |||
| 779 | public function db_error() { |
||
| 782 | |||
| 783 | public function db_insert_id() { |
||
| 786 | |||
| 787 | public function db_num_rows(&$result) { |
||
| 790 | |||
| 791 | /** |
||
| 792 | * @return int -1 means error |
||
| 793 | */ |
||
| 794 | public function db_affected_rows() { |
||
| 797 | |||
| 798 | /** |
||
| 799 | * @return string |
||
| 800 | */ |
||
| 801 | public function db_get_client_info() { |
||
| 804 | |||
| 805 | /** |
||
| 806 | * @return string |
||
| 807 | */ |
||
| 808 | public function db_get_server_info() { |
||
| 811 | |||
| 812 | /** |
||
| 813 | * @return string |
||
| 814 | */ |
||
| 815 | public function db_get_host_info() { |
||
| 818 | |||
| 819 | public function db_get_server_stat() { |
||
| 830 | |||
| 831 | /** |
||
| 832 | * @return array |
||
| 833 | * @throws Exception |
||
| 834 | */ |
||
| 835 | public function db_core_show_status() { |
||
| 848 | |||
| 849 | public function mysql_get_table_list() { |
||
| 852 | |||
| 853 | public function mysql_get_innodb_status() { |
||
| 856 | |||
| 857 | // Some wrappers to DbTransaction |
||
| 858 | // Unused for now |
||
| 859 | /** |
||
| 860 | * @return \DBAL\DbTransaction |
||
| 861 | */ |
||
| 862 | public function getTransaction() { |
||
| 865 | |||
| 866 | public function transactionCheck($status = null) { |
||
| 869 | |||
| 870 | public function transactionStart($level = '') { |
||
| 873 | |||
| 874 | public function transactionCommit() { |
||
| 877 | |||
| 878 | public function transactionRollback() { |
||
| 881 | |||
| 882 | } |
||
| 883 |