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()) { |
||
| 500 | |||
| 501 | public function doUpdateRowSet($table, $fieldsAndValues, $where) { |
||
| 504 | |||
| 505 | public function doUpdateTableSet($table, $fieldsAndValues, $where = array()) { |
||
| 508 | |||
| 509 | public function doUpdateRowAdjust($table, $fieldsSet, $fieldsAdjust, $where) { |
||
| 512 | |||
| 513 | public function doUpdateTableAdjust($table, $fieldsSet, $fieldsAdjust, $where, $whereDanger = array()) { |
||
| 516 | |||
| 517 | |||
| 518 | // DELETERS |
||
| 519 | /** |
||
| 520 | * @param string $table |
||
| 521 | * @param array $where |
||
| 522 | * @param bool $isOneRecord |
||
| 523 | * |
||
| 524 | * @return DbQuery |
||
| 525 | */ |
||
| 526 | protected function buildDeleteQuery($table, $where, $isOneRecord = DB_RECORDS_ALL) { |
||
| 532 | |||
| 533 | /** |
||
| 534 | * @param string $table |
||
| 535 | * @param array $where - simple WHERE statement list which can be combined with AND |
||
| 536 | * @param bool $isOneRecord |
||
| 537 | * |
||
| 538 | * @return array|bool|mysqli_result|null |
||
| 539 | */ |
||
| 540 | public function doDeleteWhere($table, $where, $isOneRecord = DB_RECORDS_ALL) { |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Early deprecated function for complex delete conditions |
||
| 546 | * |
||
| 547 | * Used for malformed $where conditions |
||
| 548 | * Also whereDanger can contain references for other {{tables}} |
||
| 549 | * |
||
| 550 | * @param string $table |
||
| 551 | * @param array $where |
||
| 552 | * @param array $whereDanger |
||
| 553 | * |
||
| 554 | * @return array|bool|mysqli_result|null |
||
| 555 | * @deprecated |
||
| 556 | */ |
||
| 557 | public function doDeleteDanger($table, $where, $whereDanger) { |
||
| 560 | |||
| 561 | /** |
||
| 562 | * @param string $table |
||
| 563 | * @param array $where - simple WHERE statement list which can be combined with AND |
||
| 564 | * |
||
| 565 | * @return array|bool|mysqli_result|null |
||
| 566 | */ |
||
| 567 | public function doDeleteRow($table, $where) { |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Perform simple delete queries on fixed tables w/o params |
||
| 573 | * |
||
| 574 | * @param string $query |
||
| 575 | * |
||
| 576 | * @return array|bool|mysqli_result|null |
||
| 577 | */ |
||
| 578 | public function doDeleteSql($query) { |
||
| 581 | |||
| 582 | |||
| 583 | // Misc functions |
||
| 584 | // |
||
| 585 | View Code Duplication | protected function castAsDbValue($value) { |
|
| 615 | |||
| 616 | /** |
||
| 617 | * Make field list safe |
||
| 618 | * |
||
| 619 | * Support expressions - expression index should be strictly integer! |
||
| 620 | * |
||
| 621 | * @param array $fields - array of pair $fieldName => $fieldValue |
||
| 622 | * |
||
| 623 | * @return array |
||
| 624 | */ |
||
| 625 | View Code Duplication | protected function safeFieldsEqualValues($fields) { |
|
| 643 | |||
| 644 | /** |
||
| 645 | * Make fields adjustment safe |
||
| 646 | * |
||
| 647 | * Convert "key => value" pair to string "`key` = `key` + (value)" |
||
| 648 | * Supports expressions - expression index should be strictly integer! |
||
| 649 | * |
||
| 650 | * @param array $fields - array of pair $fieldName => $fieldValue |
||
| 651 | * |
||
| 652 | * @return array |
||
| 653 | */ |
||
| 654 | View Code Duplication | protected function safeFieldsAdjust($fields) { |
|
| 672 | |||
| 673 | |||
| 674 | /** |
||
| 675 | * Returns iterator to iterate through mysqli_result |
||
| 676 | * |
||
| 677 | * @param string $query |
||
| 678 | * |
||
| 679 | * return DbResultIterator |
||
| 680 | * |
||
| 681 | * @return DbEmptyIterator|DbMysqliResultIterator |
||
| 682 | */ |
||
| 683 | public function doSelectIterator($query) { |
||
| 694 | |||
| 695 | /** |
||
| 696 | * @param DbQueryConstructor $stmt |
||
| 697 | * @param bool $skip_query_check |
||
| 698 | */ |
||
| 699 | public function doStmtLockAll($stmt, $skip_query_check = false) { |
||
| 709 | |||
| 710 | // TODO Заменить это на новый логгер |
||
| 711 | protected function security_watch_user_queries($query) { |
||
| 732 | |||
| 733 | |||
| 734 | public function security_query_check_bad_words($query) { |
||
| 791 | |||
| 792 | /** |
||
| 793 | * @param bool $prefixed_only |
||
| 794 | * |
||
| 795 | * @return array |
||
| 796 | */ |
||
| 797 | public function db_get_table_list($prefixed_only = true) { |
||
| 817 | |||
| 818 | /** |
||
| 819 | * @param string $statement |
||
| 820 | * |
||
| 821 | * @return bool|mysqli_stmt |
||
| 822 | */ |
||
| 823 | View Code Duplication | public function db_prepare($statement) { |
|
| 830 | |||
| 831 | |||
| 832 | /** |
||
| 833 | * L1 perform the query |
||
| 834 | * |
||
| 835 | * @param $query_string |
||
| 836 | * |
||
| 837 | * @return bool|mysqli_result |
||
| 838 | */ |
||
| 839 | View Code Duplication | public function db_sql_query($query_string) { |
|
| 846 | |||
| 847 | /** |
||
| 848 | * L1 fetch assoc array |
||
| 849 | * |
||
| 850 | * @param mysqli_result $query |
||
| 851 | * |
||
| 852 | * @return array|null |
||
| 853 | */ |
||
| 854 | View Code Duplication | public function db_fetch(&$query) { |
|
| 861 | |||
| 862 | public function db_fetch_row(&$query) { |
||
| 865 | |||
| 866 | public function db_escape($unescaped_string) { |
||
| 869 | |||
| 870 | public function driver_disconnect() { |
||
| 873 | |||
| 874 | public function db_error() { |
||
| 877 | |||
| 878 | public function db_insert_id() { |
||
| 881 | |||
| 882 | public function db_num_rows(&$result) { |
||
| 885 | |||
| 886 | public function db_affected_rows() { |
||
| 889 | |||
| 890 | /** |
||
| 891 | * @return string |
||
| 892 | */ |
||
| 893 | public function db_get_client_info() { |
||
| 896 | |||
| 897 | /** |
||
| 898 | * @return string |
||
| 899 | */ |
||
| 900 | public function db_get_server_info() { |
||
| 903 | |||
| 904 | /** |
||
| 905 | * @return string |
||
| 906 | */ |
||
| 907 | public function db_get_host_info() { |
||
| 910 | |||
| 911 | public function db_get_server_stat() { |
||
| 922 | |||
| 923 | /** |
||
| 924 | * @return array |
||
| 925 | * @throws Exception |
||
| 926 | */ |
||
| 927 | public function db_core_show_status() { |
||
| 940 | |||
| 941 | public function mysql_get_table_list() { |
||
| 944 | |||
| 945 | public function mysql_get_innodb_status() { |
||
| 948 | |||
| 949 | // Some wrappers to DbTransaction |
||
| 950 | // Unused for now |
||
| 951 | /** |
||
| 952 | * @return \DBAL\DbTransaction |
||
| 953 | */ |
||
| 954 | public function getTransaction() { |
||
| 957 | |||
| 958 | public function transactionCheck($status = null) { |
||
| 961 | |||
| 962 | public function transactionStart($level = '') { |
||
| 965 | |||
| 966 | public function transactionCommit() { |
||
| 969 | |||
| 970 | public function transactionRollback() { |
||
| 973 | |||
| 974 | } |
||
| 975 |