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) { |
||
| 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->traceQuery(); |
||
| 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 doSql($query, $skip_query_check = false) { |
||
| 262 | $prevState = false; |
||
| 263 | if ($skip_query_check) { |
||
| 264 | $prevState = $this->skipQueryCheck; |
||
| 265 | $this->skipQueryCheck = true; |
||
| 266 | } |
||
| 267 | // TODO - disable watch ?? |
||
| 268 | $result = $this->queryDriver($query); |
||
| 269 | if ($skip_query_check) { |
||
| 270 | $this->skipQueryCheck = $prevState; |
||
| 271 | } |
||
| 272 | |||
| 273 | return $result; |
||
| 274 | } |
||
| 275 | |||
| 276 | |||
| 277 | // SELECTS |
||
| 278 | public function doSelect($query) { |
||
| 279 | return $this->doSql($query); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @param string $query |
||
| 284 | * |
||
| 285 | * @return array|null |
||
| 286 | */ |
||
| 287 | public function doSelectFetch($query) { |
||
| 288 | return $this->db_fetch($this->doSelect($query)); |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param string $query |
||
| 293 | * |
||
| 294 | * @return mixed|null |
||
| 295 | */ |
||
| 296 | public function doSelectFetchValue($query) { |
||
| 297 | $row = $this->doSelectFetch($query); |
||
| 298 | |||
| 299 | return is_array($row) ? reset($row) : null; |
||
| 300 | } |
||
| 301 | |||
| 302 | |||
| 303 | // INSERT/REPLACE |
||
| 304 | protected function doSet($table, $fieldsAndValues, $replace = DB_INSERT_PLAIN) { |
||
| 305 | $query = DbQuery::build($this) |
||
| 306 | ->setTable($table) |
||
| 307 | ->setValues($fieldsAndValues) |
||
| 308 | ->insertSet($replace); |
||
| 309 | |||
| 310 | return $this->doSql($query); |
||
| 311 | } |
||
| 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) { |
||
| 330 | $query = DbQuery::build($this) |
||
| 331 | ->setTable($table) |
||
| 332 | ->setFields($fields) |
||
| 333 | ->setValuesDanger($valuesDanger) |
||
| 334 | ->insertBatch($replace); |
||
| 335 | |||
| 336 | return $this->doSql($query); |
||
| 337 | } |
||
| 338 | |||
| 339 | |||
| 340 | // INSERTERS |
||
| 341 | public function doInsertComplex($query) { |
||
| 342 | return $this->doSql($query); |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param string $table |
||
| 347 | * @param array $fieldsAndValues |
||
| 348 | * @param int $replace - DB_INSERT_PLAIN || DB_INSERT_IGNORE |
||
| 349 | * |
||
| 350 | * @return array|bool|mysqli_result|null |
||
| 351 | */ |
||
| 352 | public function doInsertSet($table, $fieldsAndValues, $replace = DB_INSERT_PLAIN) { |
||
| 353 | return $this->doSet($table, $fieldsAndValues, $replace); |
||
| 354 | } |
||
| 355 | |||
| 356 | |||
| 357 | /** |
||
| 358 | * Values should be passed as-is |
||
| 359 | * |
||
| 360 | * @param string $table |
||
| 361 | * @param array $fields |
||
| 362 | * @param string[] $values |
||
| 363 | * |
||
| 364 | * @return array|bool|mysqli_result|null |
||
| 365 | * @deprecated |
||
| 366 | */ |
||
| 367 | public function doInsertValuesDeprecated($table, $fields, &$values) { |
||
| 368 | return $this->doInsertBatchDanger($table, $fields, $values, DB_INSERT_PLAIN); |
||
| 369 | } |
||
| 370 | |||
| 371 | |||
| 372 | |||
| 373 | // REPLACERS |
||
| 374 | /** |
||
| 375 | * Replaces record in DB |
||
| 376 | * |
||
| 377 | * There are no DANGER replace operations |
||
| 378 | * |
||
| 379 | * @param string $table |
||
| 380 | * @param array $fieldsAndValues |
||
| 381 | * |
||
| 382 | * @return array|bool|mysqli_result|null |
||
| 383 | */ |
||
| 384 | public function doReplaceSet($table, $fieldsAndValues) { |
||
| 385 | return $this->doSet($table, $fieldsAndValues, DB_INSERT_REPLACE); |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Values should be passed as-is |
||
| 390 | * |
||
| 391 | * @param string $table |
||
| 392 | * @param array $fields |
||
| 393 | * @param string[] $values |
||
| 394 | * |
||
| 395 | * @return array|bool|mysqli_result|null |
||
| 396 | * @deprecated |
||
| 397 | */ |
||
| 398 | public function doReplaceValuesDeprecated($table, $fields, &$values) { |
||
| 399 | return $this->doInsertBatchDanger($table, $fields, $values, DB_INSERT_REPLACE); |
||
| 400 | } |
||
| 401 | |||
| 402 | |||
| 403 | // UPDATERS |
||
| 404 | public function doUpdateComplex($query) { |
||
| 405 | return $this->doSql($query); |
||
| 406 | } |
||
| 407 | |||
| 408 | public function doUpdateReallyComplex($query) { |
||
| 409 | return $this->doSql($query); |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Executes self-contained SQL UPDATE query |
||
| 414 | * |
||
| 415 | * Self-contained - means no params used |
||
| 416 | * Such queries usually used to make large amount of in-base calculations |
||
| 417 | * |
||
| 418 | * @param $query |
||
| 419 | * |
||
| 420 | * @return array|bool|mysqli_result|null |
||
| 421 | */ |
||
| 422 | public function doUpdateSqlNoParam($query) { |
||
| 423 | return $this->doSql($query); |
||
| 424 | } |
||
| 425 | |||
| 426 | protected function doUpdateWhere($table, $fieldsSet, $fieldsAdjust = array(), $where = array(), $isOneRecord = DB_RECORDS_ALL) { |
||
| 427 | // $query = DbQuery::build($this) |
||
| 428 | // ->setTable($table) |
||
| 429 | // ->setValues($fieldsSet) |
||
| 430 | // ->setAdjustDanger($fieldsAdjust) |
||
| 431 | // |
||
| 432 | // // TODO - separate danger WHEREs |
||
| 433 | // ->setWhereArray($where) |
||
| 434 | // ->setOneRow($isOneRecord) |
||
| 435 | // |
||
| 436 | // ->update(); |
||
| 437 | |||
| 438 | $tableSafe = $this->db_escape($table); |
||
| 439 | |||
| 440 | $safeFields = array(); |
||
| 441 | // Adjusts overwritten by Sets |
||
| 442 | if ($safeAdjust = implode(',', $this->safeFieldsAdjust($fieldsAdjust))) { |
||
| 443 | $safeFields[] = &$safeAdjust; |
||
| 444 | } |
||
| 445 | if ($safeFieldsEqualValues = implode(',', $this->safeFieldsEqualValues($fieldsSet))) { |
||
| 446 | $safeFields[] = &$safeFieldsEqualValues; |
||
| 447 | } |
||
| 448 | $safeFieldsString = implode(',', $safeFields); |
||
| 449 | |||
| 450 | // TODO - Exception of $safeFieldsString |
||
| 451 | |||
| 452 | $safeWhereAnd = implode(' AND ', $this->safeFieldsEqualValues($where)); |
||
| 453 | $query = "UPDATE `{{{$tableSafe}}}` SET {$safeFieldsString}" |
||
| 454 | . (!empty($safeWhereAnd) ? " WHERE {$safeWhereAnd}" : '') |
||
| 455 | . ($isOneRecord == DB_RECORD_ONE ? ' LIMIT 1' : ''); |
||
| 456 | |||
| 457 | return $this->doSql($query); |
||
| 458 | } |
||
| 459 | |||
| 460 | public function doUpdateRowSet($table, $fieldsAndValues, $where) { |
||
| 463 | |||
| 464 | public function doUpdateTableSet($table, $fieldsAndValues, $where = array()) { |
||
| 467 | |||
| 468 | public function doUpdateRowAdjust($table, $fieldsSet, $fieldsAdjust, $where) { |
||
| 471 | |||
| 472 | public function doUpdateTableAdjust($table, $fieldsSet, $fieldsAdjust, $where) { |
||
| 475 | |||
| 476 | /** |
||
| 477 | * For update_old - would be deprecated |
||
| 478 | * |
||
| 479 | * @param string $query |
||
| 480 | * |
||
| 481 | * @return array|bool|mysqli_result|null |
||
| 482 | * @deprecated |
||
| 483 | */ |
||
| 484 | public function doUpdateOld($query) { |
||
| 485 | return $this->doSql($query); |
||
| 486 | } |
||
| 487 | |||
| 488 | |||
| 489 | |||
| 490 | // DELETERS |
||
| 491 | /** |
||
| 492 | * @param string $table |
||
| 493 | * @param array $where |
||
| 494 | * @param bool $isOneRecord |
||
| 495 | * |
||
| 496 | * @return DbQuery |
||
| 497 | */ |
||
| 498 | protected function buildDeleteQuery($table, $where, $isOneRecord = DB_RECORDS_ALL) { |
||
| 504 | |||
| 505 | /** |
||
| 506 | * @param string $table |
||
| 507 | * @param array $where - simple WHERE statement list which can be combined with AND |
||
| 508 | * @param bool $isOneRecord |
||
| 509 | * |
||
| 510 | * @return array|bool|mysqli_result|null |
||
| 511 | */ |
||
| 512 | public function doDeleteWhere($table, $where, $isOneRecord = DB_RECORDS_ALL) { |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Early deprecated function for complex delete conditions |
||
| 518 | * |
||
| 519 | * Used for malformed $where conditions |
||
| 520 | * Also whereDanger can contain references for other {{tables}} |
||
| 521 | * |
||
| 522 | * @param string $table |
||
| 523 | * @param array $where |
||
| 524 | * @param array $whereDanger |
||
| 525 | * |
||
| 526 | * @return array|bool|mysqli_result|null |
||
| 527 | * @deprecated |
||
| 528 | */ |
||
| 529 | public function doDeleteDanger($table, $where, $whereDanger) { |
||
| 532 | |||
| 533 | /** |
||
| 534 | * @param string $table |
||
| 535 | * @param array $where - simple WHERE statement list which can be combined with AND |
||
| 536 | * |
||
| 537 | * @return array|bool|mysqli_result|null |
||
| 538 | */ |
||
| 539 | public function doDeleteRow($table, $where) { |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Perform simple delete queries on fixed tables w/o params |
||
| 545 | * |
||
| 546 | * @param string $query |
||
| 547 | * |
||
| 548 | * @return array|bool|mysqli_result|null |
||
| 549 | */ |
||
| 550 | public function doDeleteSimple($query) { |
||
| 553 | |||
| 554 | |||
| 555 | // Misc functions |
||
| 556 | // |
||
| 557 | View Code Duplication | protected function castAsDbValue($value) { |
|
| 587 | |||
| 588 | /** |
||
| 589 | * Make field list safe |
||
| 590 | * |
||
| 591 | * Support expressions - expression index should be strictly integer! |
||
| 592 | * |
||
| 593 | * @param array $fields - array of pair $fieldName => $fieldValue |
||
| 594 | * |
||
| 595 | * @return array |
||
| 596 | */ |
||
| 597 | View Code Duplication | protected function safeFieldsEqualValues($fields) { |
|
| 615 | |||
| 616 | /** |
||
| 617 | * Make fields adjustment safe |
||
| 618 | * |
||
| 619 | * Convert "key => value" pair to string "`key` = `key` + (value)" |
||
| 620 | * Supports expressions - expression index should be strictly integer! |
||
| 621 | * |
||
| 622 | * @param array $fields - array of pair $fieldName => $fieldValue |
||
| 623 | * |
||
| 624 | * @return array |
||
| 625 | */ |
||
| 626 | View Code Duplication | protected function safeFieldsAdjust($fields) { |
|
| 644 | |||
| 645 | |||
| 646 | /** |
||
| 647 | * Returns iterator to iterate through mysqli_result |
||
| 648 | * |
||
| 649 | * @param string $query |
||
| 650 | * |
||
| 651 | * return DbResultIterator |
||
| 652 | * |
||
| 653 | * @return DbEmptyIterator|DbMysqliResultIterator |
||
| 654 | */ |
||
| 655 | public function doSelectIterator($query) { |
||
| 666 | |||
| 667 | /** |
||
| 668 | * @param DbQueryConstructor $stmt |
||
| 669 | * @param bool $skip_query_check |
||
| 670 | */ |
||
| 671 | public function doStmtLockAll($stmt, $skip_query_check = false) { |
||
| 681 | |||
| 682 | // TODO Заменить это на новый логгер |
||
| 683 | protected function security_watch_user_queries($query) { |
||
| 704 | |||
| 705 | |||
| 706 | public function security_query_check_bad_words($query) { |
||
| 763 | |||
| 764 | /** |
||
| 765 | * @param bool $prefixed_only |
||
| 766 | * |
||
| 767 | * @return array |
||
| 768 | */ |
||
| 769 | public function db_get_table_list($prefixed_only = true) { |
||
| 789 | |||
| 790 | /** |
||
| 791 | * @param string $statement |
||
| 792 | * |
||
| 793 | * @return bool|mysqli_stmt |
||
| 794 | */ |
||
| 795 | View Code Duplication | public function db_prepare($statement) { |
|
| 802 | |||
| 803 | |||
| 804 | /** |
||
| 805 | * L1 perform the query |
||
| 806 | * |
||
| 807 | * @param $query_string |
||
| 808 | * |
||
| 809 | * @return bool|mysqli_result |
||
| 810 | */ |
||
| 811 | View Code Duplication | public function db_sql_query($query_string) { |
|
| 818 | |||
| 819 | /** |
||
| 820 | * L1 fetch assoc array |
||
| 821 | * |
||
| 822 | * @param mysqli_result $query |
||
| 823 | * |
||
| 824 | * @return array|null |
||
| 825 | */ |
||
| 826 | View Code Duplication | public function db_fetch(&$query) { |
|
| 833 | |||
| 834 | public function db_fetch_row(&$query) { |
||
| 837 | |||
| 838 | public function db_escape($unescaped_string) { |
||
| 841 | |||
| 842 | public function driver_disconnect() { |
||
| 845 | |||
| 846 | public function db_error() { |
||
| 849 | |||
| 850 | public function db_insert_id() { |
||
| 853 | |||
| 854 | public function db_num_rows(&$result) { |
||
| 857 | |||
| 858 | public function db_affected_rows() { |
||
| 861 | |||
| 862 | /** |
||
| 863 | * @return string |
||
| 864 | */ |
||
| 865 | public function db_get_client_info() { |
||
| 868 | |||
| 869 | /** |
||
| 870 | * @return string |
||
| 871 | */ |
||
| 872 | public function db_get_server_info() { |
||
| 875 | |||
| 876 | /** |
||
| 877 | * @return string |
||
| 878 | */ |
||
| 879 | public function db_get_host_info() { |
||
| 882 | |||
| 883 | public function db_get_server_stat() { |
||
| 894 | |||
| 895 | /** |
||
| 896 | * @return array |
||
| 897 | * @throws Exception |
||
| 898 | */ |
||
| 899 | public function db_core_show_status() { |
||
| 912 | |||
| 913 | public function mysql_get_table_list() { |
||
| 916 | |||
| 917 | public function mysql_get_innodb_status() { |
||
| 920 | |||
| 921 | /** |
||
| 922 | * @return \DBAL\DbTransaction |
||
| 923 | */ |
||
| 924 | public function getTransaction() { |
||
| 927 | |||
| 928 | // Some wrappers to DbTransaction |
||
| 929 | // Unused for now |
||
| 930 | public function transactionCheck($status = null) { |
||
| 933 | |||
| 934 | public function transactionStart($level = '') { |
||
| 937 | |||
| 938 | public function transactionCommit() { |
||
| 941 | |||
| 942 | public function transactionRollback() { |
||
| 945 | |||
| 946 | } |
||
| 947 |