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) { |
||
| 275 | |||
| 276 | |||
| 277 | // SELECTS |
||
| 278 | public function doSelect($query) { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * DANGER! Fields and Where can be danger |
||
| 284 | * |
||
| 285 | * @param string $table |
||
| 286 | * @param array $fields |
||
| 287 | * @param array $where |
||
| 288 | * @param bool $isOneRecord |
||
| 289 | * |
||
| 290 | * @return array|bool|mysqli_result|null |
||
| 291 | */ |
||
| 292 | public function doSelectDanger($table, $fields, $where = array(), $isOneRecord = DB_RECORDS_ALL, $forUpdate = DB_SELECT_PLAIN) { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param string $query |
||
| 315 | * |
||
| 316 | * @return array|null |
||
| 317 | */ |
||
| 318 | public function doSelectFetch($query) { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param string $query |
||
| 324 | * |
||
| 325 | * @return mixed|null |
||
| 326 | */ |
||
| 327 | public function doSelectFetchValue($query) { |
||
| 332 | |||
| 333 | |||
| 334 | // INSERT/REPLACE |
||
| 335 | protected function doSet($table, $fieldsAndValues, $replace = DB_INSERT_PLAIN) { |
||
| 343 | |||
| 344 | // TODO - batch insert and replace here |
||
| 345 | // TODO - перед тем, как переделывать данные из депрекейтов - убедится, что |
||
| 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 bool $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 | * @param string $table |
||
| 377 | * @param array $fieldsAndValues |
||
| 378 | * @param int $replace - DB_INSERT_PLAIN || DB_INSERT_IGNORE |
||
| 379 | * |
||
| 380 | * @return array|bool|mysqli_result|null |
||
| 381 | */ |
||
| 382 | public function doInsertSet($table, $fieldsAndValues, $replace = DB_INSERT_PLAIN) { |
||
| 385 | /** |
||
| 386 | * Values should be passed as-is |
||
| 387 | * |
||
| 388 | * @param string $table |
||
| 389 | * @param array $fields |
||
| 390 | * @param string[] $values |
||
| 391 | * |
||
| 392 | * @return array|bool|mysqli_result|null |
||
| 393 | * @deprecated |
||
| 394 | */ |
||
| 395 | public function doInsertValuesDeprecated($table, $fields, &$values) { |
||
| 398 | |||
| 399 | |||
| 400 | |||
| 401 | // REPLACERS |
||
| 402 | /** |
||
| 403 | * Replaces record in DB |
||
| 404 | * |
||
| 405 | * There are no DANGER replace operations |
||
| 406 | * |
||
| 407 | * @param string $table |
||
| 408 | * @param array $fieldsAndValues |
||
| 409 | * |
||
| 410 | * @return array|bool|mysqli_result|null |
||
| 411 | */ |
||
| 412 | public function doReplaceSet($table, $fieldsAndValues) { |
||
| 415 | /** |
||
| 416 | * Values should be passed as-is |
||
| 417 | * |
||
| 418 | * @param string $table |
||
| 419 | * @param array $fields |
||
| 420 | * @param string[] $values |
||
| 421 | * |
||
| 422 | * @return array|bool|mysqli_result|null |
||
| 423 | * @deprecated |
||
| 424 | */ |
||
| 425 | public function doReplaceValuesDeprecated($table, $fields, &$values) { |
||
| 428 | |||
| 429 | |||
| 430 | // UPDATERS |
||
| 431 | public function doUpdateReallyComplex($query) { |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Executes self-contained SQL UPDATE query |
||
| 437 | * |
||
| 438 | * Self-contained - means no params used |
||
| 439 | * Such queries usually used to make large amount of in-base calculations |
||
| 440 | * |
||
| 441 | * @param $query |
||
| 442 | * |
||
| 443 | * @return array|bool|mysqli_result|null |
||
| 444 | */ |
||
| 445 | public function doUpdateSqlNoParam($query) { |
||
| 448 | |||
| 449 | |||
| 450 | |||
| 451 | /** |
||
| 452 | * @param $DbQuery DbQuery |
||
| 453 | */ |
||
| 454 | protected function doUpdateDbQuery($DbQuery) { |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @param $DbQuery DbQuery |
||
| 460 | */ |
||
| 461 | public function doUpdateDbQueryAdjust($DbQuery) { |
||
| 464 | |||
| 465 | |||
| 466 | protected function doUpdateWhere($table, $fieldsSet, $fieldsAdjust = array(), $where = array(), $isOneRecord = DB_RECORDS_ALL, $whereDanger = array()) { |
||
| 481 | |||
| 482 | public function doUpdateRowSet($table, $fieldsAndValues, $where) { |
||
| 485 | |||
| 486 | public function doUpdateTableSet($table, $fieldsAndValues, $where = array()) { |
||
| 489 | |||
| 490 | public function doUpdateRowAdjust($table, $fieldsSet, $fieldsAdjust, $where) { |
||
| 493 | |||
| 494 | public function doUpdateTableAdjust($table, $fieldsSet, $fieldsAdjust, $where, $whereDanger = array()) { |
||
| 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) { |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @param string $table |
||
| 516 | * @param array $where - simple WHERE statement list which can be combined with AND |
||
| 517 | * @param bool $isOneRecord |
||
| 518 | * |
||
| 519 | * @return array|bool|mysqli_result|null |
||
| 520 | */ |
||
| 521 | public function doDeleteWhere($table, $where, $isOneRecord = DB_RECORDS_ALL) { |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Early deprecated function for complex delete conditions |
||
| 527 | * |
||
| 528 | * Used for malformed $where conditions |
||
| 529 | * Also whereDanger can contain references for other {{tables}} |
||
| 530 | * |
||
| 531 | * @param string $table |
||
| 532 | * @param array $where |
||
| 533 | * @param array $whereDanger |
||
| 534 | * |
||
| 535 | * @return array|bool|mysqli_result|null |
||
| 536 | * @deprecated |
||
| 537 | */ |
||
| 538 | public function doDeleteDanger($table, $where, $whereDanger) { |
||
| 541 | |||
| 542 | /** |
||
| 543 | * @param string $table |
||
| 544 | * @param array $where - simple WHERE statement list which can be combined with AND |
||
| 545 | * |
||
| 546 | * @return array|bool|mysqli_result|null |
||
| 547 | */ |
||
| 548 | public function doDeleteRow($table, $where) { |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Perform simple delete queries on fixed tables w/o params |
||
| 554 | * |
||
| 555 | * @param string $query |
||
| 556 | * |
||
| 557 | * @return array|bool|mysqli_result|null |
||
| 558 | */ |
||
| 559 | public function doDeleteSql($query) { |
||
| 562 | |||
| 563 | |||
| 564 | |||
| 565 | |||
| 566 | |||
| 567 | /** |
||
| 568 | * Returns iterator to iterate through mysqli_result |
||
| 569 | * |
||
| 570 | * @param string $query |
||
| 571 | * |
||
| 572 | * return DbResultIterator |
||
| 573 | * |
||
| 574 | * @return DbEmptyIterator|DbMysqliResultIterator |
||
| 575 | */ |
||
| 576 | public function doSelectIterator($query) { |
||
| 587 | |||
| 588 | /** |
||
| 589 | * @param DbQueryConstructor $stmt |
||
| 590 | * @param bool $skip_query_check |
||
| 591 | */ |
||
| 592 | public function doStmtLockAll($stmt, $skip_query_check = false) { |
||
| 602 | |||
| 603 | // TODO Заменить это на новый логгер |
||
| 604 | protected function security_watch_user_queries($query) { |
||
| 625 | |||
| 626 | |||
| 627 | public function security_query_check_bad_words($query) { |
||
| 684 | |||
| 685 | /** |
||
| 686 | * @param bool $prefixed_only |
||
| 687 | * |
||
| 688 | * @return array |
||
| 689 | */ |
||
| 690 | public function db_get_table_list($prefixed_only = true) { |
||
| 710 | |||
| 711 | /** |
||
| 712 | * @param string $statement |
||
| 713 | * |
||
| 714 | * @return bool|mysqli_stmt |
||
| 715 | */ |
||
| 716 | View Code Duplication | public function db_prepare($statement) { |
|
| 723 | |||
| 724 | |||
| 725 | /** |
||
| 726 | * L1 perform the query |
||
| 727 | * |
||
| 728 | * @param $query_string |
||
| 729 | * |
||
| 730 | * @return bool|mysqli_result |
||
| 731 | */ |
||
| 732 | View Code Duplication | public function db_sql_query($query_string) { |
|
| 739 | |||
| 740 | /** |
||
| 741 | * L1 fetch assoc array |
||
| 742 | * |
||
| 743 | * @param mysqli_result $query |
||
| 744 | * |
||
| 745 | * @return array|null |
||
| 746 | */ |
||
| 747 | View Code Duplication | public function db_fetch(&$query) { |
|
| 754 | |||
| 755 | public function db_fetch_row(&$query) { |
||
| 758 | |||
| 759 | public function db_escape($unescaped_string) { |
||
| 762 | |||
| 763 | public function driver_disconnect() { |
||
| 766 | |||
| 767 | public function db_error() { |
||
| 770 | |||
| 771 | public function db_insert_id() { |
||
| 774 | |||
| 775 | public function db_num_rows(&$result) { |
||
| 778 | |||
| 779 | public function db_affected_rows() { |
||
| 782 | |||
| 783 | /** |
||
| 784 | * @return string |
||
| 785 | */ |
||
| 786 | public function db_get_client_info() { |
||
| 789 | |||
| 790 | /** |
||
| 791 | * @return string |
||
| 792 | */ |
||
| 793 | public function db_get_server_info() { |
||
| 796 | |||
| 797 | /** |
||
| 798 | * @return string |
||
| 799 | */ |
||
| 800 | public function db_get_host_info() { |
||
| 803 | |||
| 804 | public function db_get_server_stat() { |
||
| 815 | |||
| 816 | /** |
||
| 817 | * @return array |
||
| 818 | * @throws Exception |
||
| 819 | */ |
||
| 820 | public function db_core_show_status() { |
||
| 833 | |||
| 834 | public function mysql_get_table_list() { |
||
| 837 | |||
| 838 | public function mysql_get_innodb_status() { |
||
| 841 | |||
| 842 | // Some wrappers to DbTransaction |
||
| 843 | // Unused for now |
||
| 844 | /** |
||
| 845 | * @return \DBAL\DbTransaction |
||
| 846 | */ |
||
| 847 | public function getTransaction() { |
||
| 850 | |||
| 851 | public function transactionCheck($status = null) { |
||
| 854 | |||
| 855 | public function transactionStart($level = '') { |
||
| 858 | |||
| 859 | public function transactionCommit() { |
||
| 862 | |||
| 863 | public function transactionRollback() { |
||
| 866 | |||
| 867 | } |
||
| 868 |