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 |
||
| 10 | class db_mysql { |
||
| 11 | const TRANSACTION_SERIALIZABLE = 'SERIALIZABLE'; |
||
| 12 | const TRANSACTION_REPEATABLE_READ = 'REPEATABLE READ'; |
||
| 13 | const TRANSACTION_READ_COMMITTED = 'READ COMMITTED'; |
||
| 14 | const TRANSACTION_READ_UNCOMMITTED = 'READ UNCOMMITTED'; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Статус соеднения с MySQL |
||
| 18 | * |
||
| 19 | * @var bool |
||
| 20 | */ |
||
| 21 | public $connected = false; |
||
| 22 | /** |
||
| 23 | * Префикс названий таблиц в БД |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | public $db_prefix = ''; |
||
| 28 | /** |
||
| 29 | * Список таблиц в БД |
||
| 30 | * |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | public $table_list = array(); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Настройки БД |
||
| 37 | * |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | protected $dbsettings = array(); |
||
| 41 | /** |
||
| 42 | * Драйвер для прямого обращения к MySQL |
||
| 43 | * |
||
| 44 | * @var db_mysql_v5 $driver |
||
| 45 | */ |
||
| 46 | public $driver = null; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Общее время запросов |
||
| 50 | * |
||
| 51 | * @var float $time_mysql_total |
||
| 52 | */ |
||
| 53 | public $time_mysql_total = 0.0; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Amount of queries on this DB |
||
| 57 | * |
||
| 58 | * @var int |
||
| 59 | */ |
||
| 60 | public $queryCount = 0; |
||
| 61 | |||
| 62 | public $isWatching = false; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var \DBAL\DbTransaction $transaction |
||
| 66 | */ |
||
| 67 | protected $transaction; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Should query check be skipped? |
||
| 71 | * |
||
| 72 | * Used for altering scheme of DB |
||
| 73 | * |
||
| 74 | * @var bool $skipQueryCheck |
||
| 75 | */ |
||
| 76 | protected $skipQueryCheck = false; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var SnCache $snCache |
||
| 80 | */ |
||
| 81 | public $snCache; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * db_mysql constructor. |
||
| 85 | * |
||
| 86 | * @param \Common\GlobalContainer $gc |
||
| 87 | */ |
||
| 88 | public function __construct($gc) { |
||
| 92 | |||
| 93 | public function load_db_settings($configFile = '') { |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param null|array $external_db_settings |
||
| 105 | * |
||
| 106 | * @return bool |
||
| 107 | */ |
||
| 108 | public function sn_db_connect($external_db_settings = null) { |
||
| 137 | |||
| 138 | protected function driver_connect() { |
||
| 149 | |||
| 150 | public function db_disconnect() { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @param string $query |
||
| 161 | * |
||
| 162 | * @return mixed|string |
||
| 163 | */ |
||
| 164 | public function replaceTablePlaceholders($query) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param $query |
||
| 177 | */ |
||
| 178 | protected function logQuery($query) { |
||
| 189 | |||
| 190 | |||
| 191 | /** |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | public function queryTrace() { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param string $query |
||
| 216 | * |
||
| 217 | * @return array|bool|mysqli_result|null |
||
| 218 | * @internal param bool $skip_query_check |
||
| 219 | * |
||
| 220 | */ |
||
| 221 | protected function doquery($query) { |
||
| 251 | |||
| 252 | |||
| 253 | // Just wrappers to distinguish query types |
||
| 254 | /** |
||
| 255 | * Executes non-data manipulation statements |
||
| 256 | * |
||
| 257 | * Can execute queries with check skip |
||
| 258 | * Honor current state of query checking |
||
| 259 | * |
||
| 260 | * @param string $query |
||
| 261 | * @param bool $skip_query_check |
||
| 262 | * |
||
| 263 | * @return array|bool|mysqli_result|null |
||
| 264 | */ |
||
| 265 | public function doExecute($query, $skip_query_check = false) { |
||
| 278 | |||
| 279 | |||
| 280 | public function doSelect($query) { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param string $query |
||
| 286 | * |
||
| 287 | * @return array|null |
||
| 288 | */ |
||
| 289 | public function doSelectFetch($query) { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param string $query |
||
| 295 | * |
||
| 296 | * @return mixed|null |
||
| 297 | */ |
||
| 298 | public function doSelectFetchValue($query) { |
||
| 303 | |||
| 304 | |||
| 305 | public function doInsertComplex($query) { |
||
| 308 | |||
| 309 | |||
| 310 | protected function doSet($table, $fieldsAndValues, $replace = DB_INSERT_PLAIN) { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param string $table |
||
| 333 | * @param array $fieldsAndValues |
||
| 334 | * @param int $replace - DB_INSERT_PLAIN || DB_INSERT_IGNORE |
||
| 335 | * |
||
| 336 | * @return array|bool|mysqli_result|null |
||
| 337 | */ |
||
| 338 | public function doInsertSet($table, $fieldsAndValues, $replace = DB_INSERT_PLAIN) { |
||
| 341 | |||
| 342 | public function doReplaceSet($table, $fieldsAndValues) { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Values should be passed as-is |
||
| 348 | * |
||
| 349 | * @param string $table |
||
| 350 | * @param array $fields |
||
| 351 | * @param string[] $values |
||
| 352 | * @param bool $replace |
||
| 353 | * |
||
| 354 | * @return array|bool|mysqli_result|null |
||
| 355 | * @deprecated |
||
| 356 | */ |
||
| 357 | protected function doValuesDeprecated($table, $fields, &$values, $replace = DB_INSERT_PLAIN) { |
||
| 366 | |||
| 367 | // TODO - batch insert and replace here |
||
| 368 | |||
| 369 | // TODO - перед тем, как переделывать данные из депрекейтов - убедится, что |
||
| 370 | // null - это null, а не строка'NULL' |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Values should be passed as-is |
||
| 374 | * |
||
| 375 | * @param string $table |
||
| 376 | * @param array $fields |
||
| 377 | * @param string[] $values |
||
| 378 | * |
||
| 379 | * @return array|bool|mysqli_result|null |
||
| 380 | * @deprecated |
||
| 381 | */ |
||
| 382 | public function doInsertValuesDeprecated($table, $fields, &$values) { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Values should be passed as-is |
||
| 388 | * |
||
| 389 | * @param string $table |
||
| 390 | * @param array $fields |
||
| 391 | * @param string[] $values |
||
| 392 | * |
||
| 393 | * @return array|bool|mysqli_result|null |
||
| 394 | * @deprecated |
||
| 395 | */ |
||
| 396 | public function doReplaceValuesDeprecated($table, $fields, &$values) { |
||
| 399 | |||
| 400 | public function doUpdate($query) { |
||
| 403 | |||
| 404 | |||
| 405 | /** |
||
| 406 | * @param string $query |
||
| 407 | * |
||
| 408 | * @return array|bool|mysqli_result|null |
||
| 409 | */ |
||
| 410 | public function doDelete($query) { |
||
| 413 | |||
| 414 | /** |
||
| 415 | * @param string $table |
||
| 416 | * @param array $where - simple WHERE statement list which can be combined with AND |
||
| 417 | * @param bool $isOneRecord |
||
| 418 | * |
||
| 419 | * @return array|bool|mysqli_result|null |
||
| 420 | */ |
||
| 421 | public function doDeleteWhere($table, $where, $isOneRecord = false) { |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @param string $table |
||
| 432 | * @param array $where - simple WHERE statement list which can be combined with AND |
||
| 433 | * |
||
| 434 | * @return array|bool|mysqli_result|null |
||
| 435 | */ |
||
| 436 | public function doDeleteRowWhere($table, $where) { |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @param string $query |
||
| 442 | * |
||
| 443 | * @return array|bool|mysqli_result|null |
||
| 444 | */ |
||
| 445 | public function doDeleteComplex($query) { |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Early deprecated function for complex delete conditions |
||
| 451 | * |
||
| 452 | * Usually used for mallformed $where conditions |
||
| 453 | * |
||
| 454 | * @param $table |
||
| 455 | * @param $where |
||
| 456 | * |
||
| 457 | * @return array|bool|mysqli_result|null |
||
| 458 | * @deprecated |
||
| 459 | */ |
||
| 460 | public function doDeleteDeprecated($table, $where) { |
||
| 463 | |||
| 464 | |||
| 465 | protected function castAsDbValue($value) { |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Make field list safe |
||
| 498 | * |
||
| 499 | * Support expressions - expression index should be strictly integer! |
||
| 500 | * |
||
| 501 | * @param array $fields - array of pair $fieldName => $fieldValue |
||
| 502 | * |
||
| 503 | * @return array |
||
| 504 | */ |
||
| 505 | protected function safeFieldsAndValues($fields) { |
||
| 523 | |||
| 524 | // TODO - redo as callable usage with array_map/array_walk |
||
| 525 | View Code Duplication | public function safeValues($values) { |
|
| 538 | |||
| 539 | // TODO - redo as callable usage with array_map/array_walk |
||
| 540 | View Code Duplication | public function safeFields($fields) { |
|
| 553 | |||
| 554 | |||
| 555 | /** |
||
| 556 | * Returns iterator to iterate through mysqli_result |
||
| 557 | * |
||
| 558 | * @param string $query |
||
| 559 | * |
||
| 560 | * return DbResultIterator |
||
| 561 | * |
||
| 562 | * @return DbEmptyIterator|DbMysqliResultIterator |
||
| 563 | */ |
||
| 564 | public function doSelectIterator($query) { |
||
| 575 | |||
| 576 | /** |
||
| 577 | * @param DbQueryConstructor $stmt |
||
| 578 | * @param bool $skip_query_check |
||
| 579 | */ |
||
| 580 | public function doStmtLockAll($stmt, $skip_query_check = false) { |
||
| 590 | |||
| 591 | // TODO Заменить это на новый логгер |
||
| 592 | protected function security_watch_user_queries($query) { |
||
| 613 | |||
| 614 | |||
| 615 | public function security_query_check_bad_words($query) { |
||
| 672 | |||
| 673 | /** |
||
| 674 | * @param bool $prefixed_only |
||
| 675 | * |
||
| 676 | * @return array |
||
| 677 | */ |
||
| 678 | public function db_get_table_list($prefixed_only = true) { |
||
| 698 | |||
| 699 | /** |
||
| 700 | * @param string $statement |
||
| 701 | * |
||
| 702 | * @return bool|mysqli_stmt |
||
| 703 | */ |
||
| 704 | View Code Duplication | public function db_prepare($statement) { |
|
| 711 | |||
| 712 | |||
| 713 | /** |
||
| 714 | * L1 perform the query |
||
| 715 | * |
||
| 716 | * @param $query_string |
||
| 717 | * |
||
| 718 | * @return bool|mysqli_result |
||
| 719 | */ |
||
| 720 | View Code Duplication | public function db_sql_query($query_string) { |
|
| 727 | |||
| 728 | /** |
||
| 729 | * L1 fetch assoc array |
||
| 730 | * |
||
| 731 | * @param mysqli_result $query |
||
| 732 | * |
||
| 733 | * @return array|null |
||
| 734 | */ |
||
| 735 | View Code Duplication | public function db_fetch(&$query) { |
|
| 742 | |||
| 743 | public function db_fetch_row(&$query) { |
||
| 746 | |||
| 747 | public function db_escape($unescaped_string) { |
||
| 750 | |||
| 751 | public function driver_disconnect() { |
||
| 754 | |||
| 755 | public function db_error() { |
||
| 758 | |||
| 759 | public function db_insert_id() { |
||
| 762 | |||
| 763 | public function db_num_rows(&$result) { |
||
| 766 | |||
| 767 | public function db_affected_rows() { |
||
| 770 | |||
| 771 | /** |
||
| 772 | * @return string |
||
| 773 | */ |
||
| 774 | public function db_get_client_info() { |
||
| 777 | |||
| 778 | /** |
||
| 779 | * @return string |
||
| 780 | */ |
||
| 781 | public function db_get_server_info() { |
||
| 784 | |||
| 785 | /** |
||
| 786 | * @return string |
||
| 787 | */ |
||
| 788 | public function db_get_host_info() { |
||
| 791 | |||
| 792 | public function db_get_server_stat() { |
||
| 803 | |||
| 804 | /** |
||
| 805 | * @return array |
||
| 806 | * @throws Exception |
||
| 807 | */ |
||
| 808 | public function db_core_show_status() { |
||
| 821 | |||
| 822 | public function mysql_get_table_list() { |
||
| 825 | |||
| 826 | public function mysql_get_innodb_status() { |
||
| 829 | |||
| 830 | /** |
||
| 831 | * @return \DBAL\DbTransaction |
||
| 832 | */ |
||
| 833 | public function getTransaction() { |
||
| 836 | |||
| 837 | // Some wrappers to DbTransaction |
||
| 838 | // Unused for now |
||
| 839 | public function transactionCheck($status = null) { |
||
| 842 | |||
| 843 | public function transactionStart($level = '') { |
||
| 846 | |||
| 847 | public function transactionCommit() { |
||
| 850 | |||
| 851 | public function transactionRollback() { |
||
| 854 | |||
| 855 | } |
||
| 856 |