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 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 | * @param string $query |
||
| 284 | * |
||
| 285 | * @return array|null |
||
| 286 | */ |
||
| 287 | public function doSelectFetch($query) { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param string $query |
||
| 293 | * |
||
| 294 | * @return mixed|null |
||
| 295 | */ |
||
| 296 | public function doSelectFetchValue($query) { |
||
| 301 | |||
| 302 | |||
| 303 | // INSERT/REPLACE |
||
| 304 | protected function doSet($table, $fieldsAndValues, $replace = DB_INSERT_PLAIN) { |
||
| 312 | |||
| 313 | // TODO - batch insert and replace here |
||
| 314 | // TODO - перед тем, как переделывать данные из депрекейтов - убедится, что |
||
| 315 | // null - это null, а не строка'NULL' |
||
| 316 | /** |
||
| 317 | * Values should be passed as-is |
||
| 318 | * |
||
| 319 | * DANGER! Values should be properly escaped before passing here |
||
| 320 | * |
||
| 321 | * @param string $table |
||
| 322 | * @param array $fields |
||
| 323 | * @param string[] $valuesDanger |
||
| 324 | * @param bool $replace |
||
| 325 | * |
||
| 326 | * @return array|bool|mysqli_result|null |
||
| 327 | * @deprecated |
||
| 328 | */ |
||
| 329 | protected function doInsertBatchDanger($table, $fields, &$valuesDanger, $replace = DB_INSERT_PLAIN) { |
||
| 338 | |||
| 339 | |||
| 340 | // INSERTERS |
||
| 341 | public function doInsertComplex($query) { |
||
| 344 | /** |
||
| 345 | * @param string $table |
||
| 346 | * @param array $fieldsAndValues |
||
| 347 | * @param int $replace - DB_INSERT_PLAIN || DB_INSERT_IGNORE |
||
| 348 | * |
||
| 349 | * @return array|bool|mysqli_result|null |
||
| 350 | */ |
||
| 351 | public function doInsertSet($table, $fieldsAndValues, $replace = DB_INSERT_PLAIN) { |
||
| 354 | /** |
||
| 355 | * Values should be passed as-is |
||
| 356 | * |
||
| 357 | * @param string $table |
||
| 358 | * @param array $fields |
||
| 359 | * @param string[] $values |
||
| 360 | * |
||
| 361 | * @return array|bool|mysqli_result|null |
||
| 362 | * @deprecated |
||
| 363 | */ |
||
| 364 | public function doInsertValuesDeprecated($table, $fields, &$values) { |
||
| 367 | |||
| 368 | |||
| 369 | |||
| 370 | // REPLACERS |
||
| 371 | /** |
||
| 372 | * Replaces record in DB |
||
| 373 | * |
||
| 374 | * There are no DANGER replace operations |
||
| 375 | * |
||
| 376 | * @param string $table |
||
| 377 | * @param array $fieldsAndValues |
||
| 378 | * |
||
| 379 | * @return array|bool|mysqli_result|null |
||
| 380 | */ |
||
| 381 | public function doReplaceSet($table, $fieldsAndValues) { |
||
| 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 | // UPDATERS |
||
| 400 | public function doUpdateReallyComplex($query) { |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Executes self-contained SQL UPDATE query |
||
| 406 | * |
||
| 407 | * Self-contained - means no params used |
||
| 408 | * Such queries usually used to make large amount of in-base calculations |
||
| 409 | * |
||
| 410 | * @param $query |
||
| 411 | * |
||
| 412 | * @return array|bool|mysqli_result|null |
||
| 413 | */ |
||
| 414 | public function doUpdateSqlNoParam($query) { |
||
| 417 | |||
| 418 | |||
| 419 | |||
| 420 | /** |
||
| 421 | * @param $DbQuery DbQuery |
||
| 422 | */ |
||
| 423 | protected function doUpdateDbQuery($DbQuery) { |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @param $DbQuery DbQuery |
||
| 429 | */ |
||
| 430 | public function doUpdateDbQueryAdjust($DbQuery) { |
||
| 433 | |||
| 434 | |||
| 435 | protected function doUpdateWhere($table, $fieldsSet, $fieldsAdjust = array(), $where = array(), $isOneRecord = DB_RECORDS_ALL, $whereDanger = array()) { |
||
| 469 | |||
| 470 | public function doUpdateRowSet($table, $fieldsAndValues, $where) { |
||
| 473 | |||
| 474 | public function doUpdateTableSet($table, $fieldsAndValues, $where = array()) { |
||
| 477 | |||
| 478 | public function doUpdateRowAdjust($table, $fieldsSet, $fieldsAdjust, $where) { |
||
| 481 | |||
| 482 | public function doUpdateTableAdjust($table, $fieldsSet, $fieldsAdjust, $where, $whereDanger = array()) { |
||
| 485 | |||
| 486 | |||
| 487 | // DELETERS |
||
| 488 | /** |
||
| 489 | * @param string $table |
||
| 490 | * @param array $where |
||
| 491 | * @param bool $isOneRecord |
||
| 492 | * |
||
| 493 | * @return DbQuery |
||
| 494 | */ |
||
| 495 | protected function buildDeleteQuery($table, $where, $isOneRecord = DB_RECORDS_ALL) { |
||
| 501 | |||
| 502 | /** |
||
| 503 | * @param string $table |
||
| 504 | * @param array $where - simple WHERE statement list which can be combined with AND |
||
| 505 | * @param bool $isOneRecord |
||
| 506 | * |
||
| 507 | * @return array|bool|mysqli_result|null |
||
| 508 | */ |
||
| 509 | public function doDeleteWhere($table, $where, $isOneRecord = DB_RECORDS_ALL) { |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Early deprecated function for complex delete conditions |
||
| 515 | * |
||
| 516 | * Used for malformed $where conditions |
||
| 517 | * Also whereDanger can contain references for other {{tables}} |
||
| 518 | * |
||
| 519 | * @param string $table |
||
| 520 | * @param array $where |
||
| 521 | * @param array $whereDanger |
||
| 522 | * |
||
| 523 | * @return array|bool|mysqli_result|null |
||
| 524 | * @deprecated |
||
| 525 | */ |
||
| 526 | public function doDeleteDanger($table, $where, $whereDanger) { |
||
| 529 | |||
| 530 | /** |
||
| 531 | * @param string $table |
||
| 532 | * @param array $where - simple WHERE statement list which can be combined with AND |
||
| 533 | * |
||
| 534 | * @return array|bool|mysqli_result|null |
||
| 535 | */ |
||
| 536 | public function doDeleteRow($table, $where) { |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Perform simple delete queries on fixed tables w/o params |
||
| 542 | * |
||
| 543 | * @param string $query |
||
| 544 | * |
||
| 545 | * @return array|bool|mysqli_result|null |
||
| 546 | */ |
||
| 547 | public function doDeleteSql($query) { |
||
| 550 | |||
| 551 | |||
| 552 | // Misc functions |
||
| 553 | // |
||
| 554 | View Code Duplication | protected function castAsDbValue($value) { |
|
| 584 | |||
| 585 | /** |
||
| 586 | * Make field list safe |
||
| 587 | * |
||
| 588 | * Support expressions - expression index should be strictly integer! |
||
| 589 | * |
||
| 590 | * @param array $fields - array of pair $fieldName => $fieldValue |
||
| 591 | * |
||
| 592 | * @return array |
||
| 593 | */ |
||
| 594 | View Code Duplication | protected function safeFieldsEqualValues($fields) { |
|
| 612 | |||
| 613 | /** |
||
| 614 | * Make fields adjustment safe |
||
| 615 | * |
||
| 616 | * Convert "key => value" pair to string "`key` = `key` + (value)" |
||
| 617 | * Supports expressions - expression index should be strictly integer! |
||
| 618 | * |
||
| 619 | * @param array $fields - array of pair $fieldName => $fieldValue |
||
| 620 | * |
||
| 621 | * @return array |
||
| 622 | */ |
||
| 623 | View Code Duplication | protected function safeFieldsAdjust($fields) { |
|
| 641 | |||
| 642 | |||
| 643 | /** |
||
| 644 | * Returns iterator to iterate through mysqli_result |
||
| 645 | * |
||
| 646 | * @param string $query |
||
| 647 | * |
||
| 648 | * return DbResultIterator |
||
| 649 | * |
||
| 650 | * @return DbEmptyIterator|DbMysqliResultIterator |
||
| 651 | */ |
||
| 652 | public function doSelectIterator($query) { |
||
| 663 | |||
| 664 | /** |
||
| 665 | * @param DbQueryConstructor $stmt |
||
| 666 | * @param bool $skip_query_check |
||
| 667 | */ |
||
| 668 | public function doStmtLockAll($stmt, $skip_query_check = false) { |
||
| 678 | |||
| 679 | // TODO Заменить это на новый логгер |
||
| 680 | protected function security_watch_user_queries($query) { |
||
| 701 | |||
| 702 | |||
| 703 | public function security_query_check_bad_words($query) { |
||
| 760 | |||
| 761 | /** |
||
| 762 | * @param bool $prefixed_only |
||
| 763 | * |
||
| 764 | * @return array |
||
| 765 | */ |
||
| 766 | public function db_get_table_list($prefixed_only = true) { |
||
| 786 | |||
| 787 | /** |
||
| 788 | * @param string $statement |
||
| 789 | * |
||
| 790 | * @return bool|mysqli_stmt |
||
| 791 | */ |
||
| 792 | View Code Duplication | public function db_prepare($statement) { |
|
| 799 | |||
| 800 | |||
| 801 | /** |
||
| 802 | * L1 perform the query |
||
| 803 | * |
||
| 804 | * @param $query_string |
||
| 805 | * |
||
| 806 | * @return bool|mysqli_result |
||
| 807 | */ |
||
| 808 | View Code Duplication | public function db_sql_query($query_string) { |
|
| 815 | |||
| 816 | /** |
||
| 817 | * L1 fetch assoc array |
||
| 818 | * |
||
| 819 | * @param mysqli_result $query |
||
| 820 | * |
||
| 821 | * @return array|null |
||
| 822 | */ |
||
| 823 | View Code Duplication | public function db_fetch(&$query) { |
|
| 830 | |||
| 831 | public function db_fetch_row(&$query) { |
||
| 834 | |||
| 835 | public function db_escape($unescaped_string) { |
||
| 838 | |||
| 839 | public function driver_disconnect() { |
||
| 842 | |||
| 843 | public function db_error() { |
||
| 846 | |||
| 847 | public function db_insert_id() { |
||
| 850 | |||
| 851 | public function db_num_rows(&$result) { |
||
| 854 | |||
| 855 | public function db_affected_rows() { |
||
| 858 | |||
| 859 | /** |
||
| 860 | * @return string |
||
| 861 | */ |
||
| 862 | public function db_get_client_info() { |
||
| 865 | |||
| 866 | /** |
||
| 867 | * @return string |
||
| 868 | */ |
||
| 869 | public function db_get_server_info() { |
||
| 872 | |||
| 873 | /** |
||
| 874 | * @return string |
||
| 875 | */ |
||
| 876 | public function db_get_host_info() { |
||
| 879 | |||
| 880 | public function db_get_server_stat() { |
||
| 891 | |||
| 892 | /** |
||
| 893 | * @return array |
||
| 894 | * @throws Exception |
||
| 895 | */ |
||
| 896 | public function db_core_show_status() { |
||
| 909 | |||
| 910 | public function mysql_get_table_list() { |
||
| 913 | |||
| 914 | public function mysql_get_innodb_status() { |
||
| 917 | |||
| 918 | // Some wrappers to DbTransaction |
||
| 919 | // Unused for now |
||
| 920 | /** |
||
| 921 | * @return \DBAL\DbTransaction |
||
| 922 | */ |
||
| 923 | public function getTransaction() { |
||
| 926 | |||
| 927 | public function transactionCheck($status = null) { |
||
| 930 | |||
| 931 | public function transactionStart($level = '') { |
||
| 934 | |||
| 935 | public function transactionCommit() { |
||
| 938 | |||
| 939 | public function transactionRollback() { |
||
| 942 | |||
| 943 | } |
||
| 944 |