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 |
||
| 6 | class db_mysql { |
||
| 7 | const TRANSACTION_SERIALIZABLE = 'SERIALIZABLE'; |
||
| 8 | const TRANSACTION_REPEATABLE_READ = 'REPEATABLE READ'; |
||
| 9 | const TRANSACTION_READ_COMMITTED = 'READ COMMITTED'; |
||
| 10 | const TRANSACTION_READ_UNCOMMITTED = 'READ UNCOMMITTED'; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Статус соеднения с MySQL |
||
| 14 | * |
||
| 15 | * @var bool |
||
| 16 | */ |
||
| 17 | public $connected = false; |
||
| 18 | /** |
||
| 19 | * Префикс названий таблиц в БД |
||
| 20 | * |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | public $db_prefix = ''; |
||
| 24 | /** |
||
| 25 | * Список таблиц в БД |
||
| 26 | * |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | public $table_list = array(); |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Настройки БД |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $dbsettings = array(); |
||
| 37 | /** |
||
| 38 | * Драйвер для прямого обращения к MySQL |
||
| 39 | * |
||
| 40 | * @var db_mysql_v5 $driver |
||
| 41 | */ |
||
| 42 | public $driver = null; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Общее время запросов |
||
| 46 | * |
||
| 47 | * @var float $time_mysql_total |
||
| 48 | */ |
||
| 49 | public $time_mysql_total = 0.0; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Amount of queries on this DB |
||
| 53 | * |
||
| 54 | * @var int |
||
| 55 | */ |
||
| 56 | public $queryCount = 0; |
||
| 57 | |||
| 58 | public $isWatching = false; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var \DBAL\DbTransaction $transaction |
||
| 62 | */ |
||
| 63 | protected $transaction; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Should query check be skipped? |
||
| 67 | * |
||
| 68 | * Used for altering scheme of DB |
||
| 69 | * |
||
| 70 | * @var bool $skipQueryCheck |
||
| 71 | */ |
||
| 72 | protected $skipQueryCheck = false; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var SnCache $snCache |
||
| 76 | */ |
||
| 77 | public $snCache; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * db_mysql constructor. |
||
| 81 | * |
||
| 82 | * @param \Common\GlobalContainer $gc |
||
| 83 | */ |
||
| 84 | public function __construct($gc) { |
||
| 88 | |||
| 89 | public function load_db_settings($configFile = '') { |
||
| 90 | $dbsettings = array(); |
||
| 91 | |||
| 92 | empty($configFile) ? $configFile = SN_ROOT_PHYSICAL . "config" . DOT_PHP_EX : false; |
||
| 93 | |||
| 94 | require $configFile; |
||
| 95 | |||
| 96 | $this->dbsettings = $dbsettings; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param null|array $external_db_settings |
||
| 101 | * |
||
| 102 | * @return bool |
||
| 103 | */ |
||
| 104 | public function sn_db_connect($external_db_settings = null) { |
||
| 105 | $this->db_disconnect(); |
||
| 106 | |||
| 107 | if (!empty($external_db_settings) && is_array($external_db_settings)) { |
||
| 108 | $this->dbsettings = $external_db_settings; |
||
| 109 | } |
||
| 110 | |||
| 111 | if (empty($this->dbsettings)) { |
||
| 112 | $this->load_db_settings(SN_ROOT_PHYSICAL . "config" . DOT_PHP_EX); |
||
| 113 | } |
||
| 114 | |||
| 115 | // TODO - фатальные (?) ошибки на каждом шагу. Хотя - скорее Эксепшны |
||
| 116 | if (!empty($this->dbsettings)) { |
||
| 117 | $driver_name = empty($this->dbsettings['sn_driver']) ? 'db_mysql_v5' : $this->dbsettings['sn_driver']; |
||
| 118 | $this->driver = new $driver_name(); |
||
| 119 | $this->db_prefix = $this->dbsettings['prefix']; |
||
| 120 | |||
| 121 | $this->connected = $this->connected || $this->driver_connect(); |
||
| 122 | |||
| 123 | if ($this->connected) { |
||
| 124 | $this->table_list = $this->db_get_table_list(); |
||
| 125 | // TODO Проверка на пустоту |
||
| 126 | } |
||
| 127 | } else { |
||
| 128 | $this->connected = false; |
||
| 129 | } |
||
| 130 | |||
| 131 | return $this->connected; |
||
| 132 | } |
||
| 133 | |||
| 134 | protected function driver_connect() { |
||
| 145 | |||
| 146 | public function db_disconnect() { |
||
| 147 | if ($this->connected) { |
||
| 148 | $this->connected = !$this->driver_disconnect(); |
||
| 149 | $this->connected = false; |
||
| 150 | } |
||
| 151 | |||
| 152 | return !$this->connected; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param string $query |
||
| 157 | * |
||
| 158 | * @return mixed|string |
||
| 159 | */ |
||
| 160 | public function replaceTablePlaceholders($query) { |
||
| 161 | $sql = $query; |
||
| 162 | if (strpos($sql, '{{') !== false) { |
||
| 163 | foreach ($this->table_list as $tableName) { |
||
| 164 | $sql = str_replace("{{{$tableName}}}", $this->db_prefix . $tableName, $sql); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | return $sql; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param $query |
||
| 173 | */ |
||
| 174 | protected function logQuery($query) { |
||
| 175 | if (!classSupernova::$config->debug) { |
||
| 176 | return; |
||
| 177 | } |
||
| 178 | |||
| 179 | $this->queryCount++; |
||
| 180 | $arr = debug_backtrace(); |
||
| 181 | $file = end(explode('/', $arr[0]['file'])); |
||
|
|
|||
| 182 | $line = $arr[0]['line']; |
||
| 183 | classSupernova::$debug->add("<tr><th>Query {$this->queryCount}: </th><th>$query</th><th>{$file} @ {$line}</th><th> </th></tr>"); |
||
| 184 | } |
||
| 185 | |||
| 186 | |||
| 187 | /** |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public function queryTrace() { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param string $query |
||
| 212 | * |
||
| 213 | * @return array|bool|mysqli_result|null |
||
| 214 | * @internal param bool $skip_query_check |
||
| 215 | * |
||
| 216 | */ |
||
| 217 | protected function doquery($query) { |
||
| 218 | if (!$this->connected) { |
||
| 219 | $this->sn_db_connect(); |
||
| 220 | } |
||
| 221 | |||
| 222 | $stringQuery = $query; |
||
| 223 | $stringQuery = trim($stringQuery); |
||
| 224 | // You can't do it - 'cause you can break commented statement with line-end comments |
||
| 225 | // $stringQuery = preg_replace("/\s+/", ' ', $stringQuery); |
||
| 226 | |||
| 227 | $this->security_watch_user_queries($stringQuery); |
||
| 228 | $this->security_query_check_bad_words($stringQuery); |
||
| 229 | $this->logQuery($stringQuery); |
||
| 230 | |||
| 231 | $stringQuery = $this->replaceTablePlaceholders($stringQuery); |
||
| 232 | |||
| 233 | $queryTrace = $this->queryTrace(); |
||
| 234 | |||
| 235 | $queryResult = null; |
||
| 236 | try { |
||
| 237 | $queryResult = $this->db_sql_query($stringQuery . DbSqlHelper::quoteComment($queryTrace)); |
||
| 238 | if (!$queryResult) { |
||
| 239 | throw new Exception(); |
||
| 240 | } |
||
| 241 | } catch (Exception $e) { |
||
| 242 | classSupernova::$debug->error($this->db_error() . "<br />{$query}<br />", 'SQL Error'); |
||
| 243 | } |
||
| 244 | |||
| 245 | return $queryResult; |
||
| 246 | } |
||
| 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 doExecute($query, $skip_query_check = false) { |
||
| 274 | |||
| 275 | |||
| 276 | public function doSelect($query) { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param string $query |
||
| 282 | * |
||
| 283 | * @return array|null |
||
| 284 | */ |
||
| 285 | public function doSelectFetch($query) { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param string $query |
||
| 291 | * |
||
| 292 | * @return mixed|null |
||
| 293 | */ |
||
| 294 | public function doSelectFetchValue($query) { |
||
| 299 | |||
| 300 | |||
| 301 | public function doInsert($query) { |
||
| 304 | |||
| 305 | protected function doSet($table, $fieldsAndValues, $replace = false) { |
||
| 306 | $tableSafe = $this->db_escape($table); |
||
| 307 | $safeFieldsAndValues = implode(',', $this->safeFieldsAndValues($fieldsAndValues)); |
||
| 308 | $command = $replace ? 'REPLACE' : 'INSERT'; |
||
| 309 | $query = "{$command} INTO `{{{$tableSafe}}}` SET {$safeFieldsAndValues}"; |
||
| 310 | |||
| 313 | |||
| 314 | public function doInsertSet($table, $fieldsAndValues) { |
||
| 317 | |||
| 318 | public function doReplaceSet($table, $fieldsAndValues) { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Values should be passed as-is |
||
| 324 | * |
||
| 325 | * @param string $table |
||
| 326 | * @param array $fields |
||
| 327 | * @param string[] $values |
||
| 328 | * @param bool $replace |
||
| 329 | * |
||
| 330 | * @return array|bool|mysqli_result|null |
||
| 331 | * @deprecated |
||
| 332 | */ |
||
| 333 | protected function doValuesDeprecated($table, $fields, &$values, $replace = false) { |
||
| 342 | |||
| 343 | // TODO - batch insert and replace here |
||
| 344 | |||
| 345 | // TODO - перед тем, как переделывать данные из депрекейтов - убедится, что |
||
| 346 | // null - это null, а не строка'NULL' |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Values should be passed as-is |
||
| 350 | * |
||
| 351 | * @param string $table |
||
| 352 | * @param array $fields |
||
| 353 | * @param string[] $values |
||
| 354 | * |
||
| 355 | * @return array|bool|mysqli_result|null |
||
| 356 | * @deprecated |
||
| 357 | */ |
||
| 358 | public function doInsertValuesDeprecated($table, $fields, &$values) { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Values should be passed as-is |
||
| 364 | * |
||
| 365 | * @param string $table |
||
| 366 | * @param array $fields |
||
| 367 | * @param string[] $values |
||
| 368 | * |
||
| 369 | * @return array|bool|mysqli_result|null |
||
| 370 | * @deprecated |
||
| 371 | */ |
||
| 372 | public function doReplaceValuesDeprecated($table, $fields, &$values) { |
||
| 375 | |||
| 376 | public function doUpdate($query) { |
||
| 379 | |||
| 380 | |||
| 381 | /** |
||
| 382 | * @param string $query |
||
| 383 | * |
||
| 384 | * @return array|bool|mysqli_result|null |
||
| 385 | */ |
||
| 386 | public function doDelete($query) { |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @param string $table |
||
| 392 | * @param array $where - simple WHERE statement list which can be combined with AND |
||
| 393 | * @param bool $isOneRecord |
||
| 394 | * |
||
| 395 | * @return array|bool|mysqli_result|null |
||
| 396 | */ |
||
| 397 | public function doDeleteWhere($table, $where, $isOneRecord = false) { |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @param string $table |
||
| 408 | * @param array $where - simple WHERE statement list which can be combined with AND |
||
| 409 | * |
||
| 410 | * @return array|bool|mysqli_result|null |
||
| 411 | */ |
||
| 412 | public function doDeleteRowWhere($table, $where) { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * @param string $query |
||
| 418 | * |
||
| 419 | * @return array|bool|mysqli_result|null |
||
| 420 | */ |
||
| 421 | public function doDeleteComplex($query) { |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Early deprecated function for complex delete conditions |
||
| 427 | * |
||
| 428 | * Usually used for mallformed $where conditions |
||
| 429 | * |
||
| 430 | * @param $table |
||
| 431 | * @param $where |
||
| 432 | * |
||
| 433 | * @return array|bool|mysqli_result|null |
||
| 434 | * @deprecated |
||
| 435 | */ |
||
| 436 | public function doDeleteDeprecated($table, $where) { |
||
| 439 | |||
| 440 | |||
| 441 | protected function castAsDbValue($value) { |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Make field list safe |
||
| 474 | * |
||
| 475 | * Support expressions - expression index should be strictly integer! |
||
| 476 | * |
||
| 477 | * @param array $fields - array of pair $fieldName => $fieldValue |
||
| 478 | * |
||
| 479 | * @return array |
||
| 480 | */ |
||
| 481 | protected function safeFieldsAndValues($fields) { |
||
| 499 | |||
| 500 | // TODO - redo as callable usage with array_map/array_walk |
||
| 501 | View Code Duplication | public function safeValues($values) { |
|
| 514 | |||
| 515 | // TODO - redo as callable usage with array_map/array_walk |
||
| 516 | View Code Duplication | public function safeFields($fields) { |
|
| 529 | |||
| 530 | |||
| 531 | /** |
||
| 532 | * Returns iterator to iterate through mysqli_result |
||
| 533 | * |
||
| 534 | * @param string $query |
||
| 535 | * |
||
| 536 | * return DbResultIterator |
||
| 537 | * |
||
| 538 | * @return DbEmptyIterator|DbMysqliResultIterator |
||
| 539 | */ |
||
| 540 | public function doSelectIterator($query) { |
||
| 551 | |||
| 552 | /** |
||
| 553 | * @param DbQueryConstructor $stmt |
||
| 554 | * @param bool $skip_query_check |
||
| 555 | */ |
||
| 556 | public function doStmtLockAll($stmt, $skip_query_check = false) { |
||
| 566 | |||
| 567 | // TODO Заменить это на новый логгер |
||
| 568 | protected function security_watch_user_queries($query) { |
||
| 589 | |||
| 590 | |||
| 591 | public function security_query_check_bad_words($query) { |
||
| 648 | |||
| 649 | /** |
||
| 650 | * @param bool $prefixed_only |
||
| 651 | * |
||
| 652 | * @return array |
||
| 653 | */ |
||
| 654 | public function db_get_table_list($prefixed_only = true) { |
||
| 674 | |||
| 675 | /** |
||
| 676 | * @param string $statement |
||
| 677 | * |
||
| 678 | * @return bool|mysqli_stmt |
||
| 679 | */ |
||
| 680 | View Code Duplication | public function db_prepare($statement) { |
|
| 687 | |||
| 688 | |||
| 689 | /** |
||
| 690 | * L1 perform the query |
||
| 691 | * |
||
| 692 | * @param $query_string |
||
| 693 | * |
||
| 694 | * @return bool|mysqli_result |
||
| 695 | */ |
||
| 696 | View Code Duplication | public function db_sql_query($query_string) { |
|
| 703 | |||
| 704 | /** |
||
| 705 | * L1 fetch assoc array |
||
| 706 | * |
||
| 707 | * @param mysqli_result $query |
||
| 708 | * |
||
| 709 | * @return array|null |
||
| 710 | */ |
||
| 711 | View Code Duplication | public function db_fetch(&$query) { |
|
| 718 | |||
| 719 | public function db_fetch_row(&$query) { |
||
| 722 | |||
| 723 | public function db_escape($unescaped_string) { |
||
| 726 | |||
| 727 | public function driver_disconnect() { |
||
| 730 | |||
| 731 | public function db_error() { |
||
| 734 | |||
| 735 | public function db_insert_id() { |
||
| 738 | |||
| 739 | public function db_num_rows(&$result) { |
||
| 742 | |||
| 743 | public function db_affected_rows() { |
||
| 746 | |||
| 747 | /** |
||
| 748 | * @return string |
||
| 749 | */ |
||
| 750 | public function db_get_client_info() { |
||
| 753 | |||
| 754 | /** |
||
| 755 | * @return string |
||
| 756 | */ |
||
| 757 | public function db_get_server_info() { |
||
| 760 | |||
| 761 | /** |
||
| 762 | * @return string |
||
| 763 | */ |
||
| 764 | public function db_get_host_info() { |
||
| 767 | |||
| 768 | public function db_get_server_stat() { |
||
| 779 | |||
| 780 | /** |
||
| 781 | * @return array |
||
| 782 | * @throws Exception |
||
| 783 | */ |
||
| 784 | public function db_core_show_status() { |
||
| 797 | |||
| 798 | public function mysql_get_table_list() { |
||
| 801 | |||
| 802 | public function mysql_get_innodb_status() { |
||
| 805 | |||
| 806 | /** |
||
| 807 | * @return \DBAL\DbTransaction |
||
| 808 | */ |
||
| 809 | public function getTransaction() { |
||
| 812 | |||
| 813 | // Some wrappers to DbTransaction |
||
| 814 | // Unused for now |
||
| 815 | public function transactionCheck($status = null) { |
||
| 818 | |||
| 819 | public function transactionStart($level = '') { |
||
| 822 | |||
| 823 | public function transactionCommit() { |
||
| 826 | |||
| 827 | public function transactionRollback() { |
||
| 830 | |||
| 831 | } |
||
| 832 |