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 DBMysql 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 DBMysql, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class DBMysql extends DB |
||
| 15 | { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * prefix of a tablename (One or more XEs can be installed in a single DB) |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | var $prefix = 'xe_'; // / < |
||
| 22 | var $comment_syntax = '/* %s */'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Column type used in MySQL |
||
| 26 | * |
||
| 27 | * Becasue a common column type in schema/query xml is used for colum_type, |
||
| 28 | * it should be replaced properly for each DBMS |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | var $column_type = array( |
||
| 32 | 'bignumber' => 'bigint', |
||
| 33 | 'number' => 'bigint', |
||
| 34 | 'varchar' => 'varchar', |
||
| 35 | 'char' => 'char', |
||
| 36 | 'text' => 'text', |
||
| 37 | 'bigtext' => 'longtext', |
||
| 38 | 'date' => 'varchar(14)', |
||
| 39 | 'float' => 'float', |
||
| 40 | ); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Constructor |
||
| 44 | * @return void |
||
| 45 | */ |
||
| 46 | function DBMysql() |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Create an instance of this class |
||
| 54 | * @return DBMysql return DBMysql object instance |
||
| 55 | */ |
||
| 56 | function create() |
||
| 60 | |||
| 61 | /** |
||
| 62 | * DB Connect |
||
| 63 | * this method is private |
||
| 64 | * @param array $connection connection's value is db_hostname, db_port, db_database, db_userid, db_password |
||
| 65 | * @return resource |
||
| 66 | */ |
||
| 67 | function __connect($connection) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * If have a task after connection, add a taks in this method |
||
| 106 | * this method is private |
||
| 107 | * @param resource $connection |
||
| 108 | * @return void |
||
| 109 | */ |
||
| 110 | function _afterConnect($connection) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * DB disconnection |
||
| 118 | * this method is private |
||
| 119 | * @param resource $connection |
||
| 120 | * @return void |
||
| 121 | */ |
||
| 122 | function _close($connection) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Handles quatation of the string variables from the query |
||
| 129 | * @param string $string |
||
| 130 | * @return string |
||
| 131 | */ |
||
| 132 | View Code Duplication | function addQuotes($string) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * DB transaction start |
||
| 147 | * this method is private |
||
| 148 | * @return boolean |
||
| 149 | */ |
||
| 150 | function _begin($transactionLevel = 0) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * DB transaction rollback |
||
| 157 | * this method is private |
||
| 158 | * @return boolean |
||
| 159 | */ |
||
| 160 | function _rollback($transactionLevel = 0) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * DB transaction commit |
||
| 167 | * this method is private |
||
| 168 | * @return boolean |
||
| 169 | */ |
||
| 170 | function _commit() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Execute the query |
||
| 177 | * this method is private |
||
| 178 | * @param string $query |
||
| 179 | * @param resource $connection |
||
| 180 | * @return resource |
||
| 181 | */ |
||
| 182 | View Code Duplication | function __query($query, $connection) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Fetch the result |
||
| 201 | * @param resource $result |
||
| 202 | * @param int|NULL $arrayIndexEndValue |
||
| 203 | * @return array |
||
| 204 | */ |
||
| 205 | function _fetch($result, $arrayIndexEndValue = NULL) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Return the sequence value incremented by 1 |
||
| 240 | * Auto_increment column only used in the sequence table |
||
| 241 | * @return int |
||
| 242 | */ |
||
| 243 | function getNextSequence() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Function to obtain mysql old password(mysql only) |
||
| 259 | * @param string $password input password |
||
| 260 | * @param string $saved_password saved password in DBMS |
||
| 261 | * @return boolean |
||
| 262 | */ |
||
| 263 | function isValidOldPassword($password, $saved_password) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Check a table exists status |
||
| 277 | * @param string $target_name |
||
| 278 | * @return boolean |
||
| 279 | */ |
||
| 280 | function isTableExists($target_name) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Add a column to the table |
||
| 294 | * @param string $table_name table name |
||
| 295 | * @param string $column_name column name |
||
| 296 | * @param string $type column type, default value is 'number' |
||
| 297 | * @param int $size column size |
||
| 298 | * @param string|int $default default value |
||
| 299 | * @param boolean $notnull not null status, default value is false |
||
| 300 | * @return void |
||
| 301 | */ |
||
| 302 | View Code Duplication | function addColumn($table_name, $column_name, $type = 'number', $size = '', $default = null, $notnull = false) |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Drop a column from the table |
||
| 333 | * @param string $table_name table name |
||
| 334 | * @param string $column_name column name |
||
| 335 | * @return void |
||
| 336 | */ |
||
| 337 | function dropColumn($table_name, $column_name) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Check column exist status of the table |
||
| 345 | * @param string $table_name table name |
||
| 346 | * @param string $column_name column name |
||
| 347 | * @return boolean |
||
| 348 | */ |
||
| 349 | function isColumnExists($table_name, $column_name) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Add an index to the table |
||
| 375 | * $target_columns = array(col1, col2) |
||
| 376 | * $is_unique? unique : none |
||
| 377 | * @param string $table_name table name |
||
| 378 | * @param string $index_name index name |
||
| 379 | * @param string|array $target_columns target column or columns |
||
| 380 | * @param boolean $is_unique |
||
| 381 | * @return void |
||
| 382 | */ |
||
| 383 | View Code Duplication | function addIndex($table_name, $index_name, $target_columns, $is_unique = false) |
|
| 393 | |||
| 394 | /** |
||
| 395 | * Drop an index from the table |
||
| 396 | * @param string $table_name table name |
||
| 397 | * @param string $index_name index name |
||
| 398 | * @param boolean $is_unique |
||
| 399 | * @return void |
||
| 400 | */ |
||
| 401 | function dropIndex($table_name, $index_name, $is_unique = false) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Check index status of the table |
||
| 409 | * @param string $table_name table name |
||
| 410 | * @param string $index_name index name |
||
| 411 | * @return boolean |
||
| 412 | */ |
||
| 413 | function isIndexExists($table_name, $index_name) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Creates a table by using xml contents |
||
| 444 | * @param string $xml_doc xml schema contents |
||
| 445 | * @return void|object |
||
| 446 | */ |
||
| 447 | function createTableByXml($xml_doc) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Creates a table by using xml file path |
||
| 454 | * @param string $file_name xml schema file path |
||
| 455 | * @return void|object |
||
| 456 | */ |
||
| 457 | function createTableByXmlFile($file_name) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Create table by using the schema xml |
||
| 470 | * |
||
| 471 | * type : number, varchar, tinytext, text, bigtext, char, date, \n |
||
| 472 | * opt : notnull, default, size\n |
||
| 473 | * index : primary key, index, unique\n |
||
| 474 | * @param string $xml_doc xml schema contents |
||
| 475 | * @return void|object |
||
| 476 | */ |
||
| 477 | function _createTable($xml_doc) |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Handles insertAct |
||
| 561 | * @param Object $queryObject |
||
| 562 | * @param boolean $with_values |
||
| 563 | * @return resource |
||
| 564 | */ |
||
| 565 | View Code Duplication | function _executeInsertAct($queryObject, $with_values = true) |
|
| 575 | |||
| 576 | /** |
||
| 577 | * Handles updateAct |
||
| 578 | * @param Object $queryObject |
||
| 579 | * @param boolean $with_values |
||
| 580 | * @return resource |
||
| 581 | */ |
||
| 582 | View Code Duplication | function _executeUpdateAct($queryObject, $with_values = true) |
|
| 596 | |||
| 597 | /** |
||
| 598 | * Handles deleteAct |
||
| 599 | * @param Object $queryObject |
||
| 600 | * @param boolean $with_values |
||
| 601 | * @return resource |
||
| 602 | */ |
||
| 603 | View Code Duplication | function _executeDeleteAct($queryObject, $with_values = true) |
|
| 613 | |||
| 614 | /** |
||
| 615 | * Handle selectAct |
||
| 616 | * In order to get a list of pages easily when selecting \n |
||
| 617 | * it supports a method as navigation |
||
| 618 | * @param Object $queryObject |
||
| 619 | * @param resource $connection |
||
| 620 | * @param boolean $with_values |
||
| 621 | * @return Object |
||
| 622 | */ |
||
| 623 | function _executeSelectAct($queryObject, $connection = null, $with_values = true) |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Get the ID generated in the last query |
||
| 662 | * Return next sequence from sequence table |
||
| 663 | * This method use only mysql |
||
| 664 | * @return int |
||
| 665 | */ |
||
| 666 | function db_insert_id() |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Fetch a result row as an object |
||
| 674 | * @param resource $result |
||
| 675 | * @return object |
||
| 676 | */ |
||
| 677 | function db_fetch_object(&$result) |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Free result memory |
||
| 684 | * @param resource $result |
||
| 685 | * @return boolean Returns TRUE on success or FALSE on failure. |
||
| 686 | */ |
||
| 687 | function db_free_result(&$result) |
||
| 691 | |||
| 692 | /** |
||
| 693 | * Return the DBParser |
||
| 694 | * @param boolean $force |
||
| 695 | * @return DBParser |
||
| 696 | */ |
||
| 697 | function &getParser($force = FALSE) |
||
| 702 | |||
| 703 | /** |
||
| 704 | * If have a error, return error object |
||
| 705 | * @param Object $queryObject |
||
| 706 | * @return Object |
||
| 707 | */ |
||
| 708 | View Code Duplication | function queryError($queryObject) |
|
| 726 | |||
| 727 | /** |
||
| 728 | * If select query execute, return page info |
||
| 729 | * @param Object $queryObject |
||
| 730 | * @param resource $result |
||
| 731 | * @param resource $connection |
||
| 732 | * @param boolean $with_values |
||
| 733 | * @return Object Object with page info containing |
||
| 734 | */ |
||
| 735 | function queryPageLimit($queryObject, $result, $connection, $with_values = true) |
||
| 824 | |||
| 825 | /** |
||
| 826 | * If select query execute, return paging sql |
||
| 827 | * @param object $query |
||
| 828 | * @param boolean $with_values |
||
| 829 | * @param int $start_count |
||
| 830 | * @param int $list_count |
||
| 831 | * @return string select paging sql |
||
| 832 | */ |
||
| 833 | View Code Duplication | function getSelectPageSql($query, $with_values = true, $start_count = 0, $list_count = 0) |
|
| 875 | |||
| 876 | } |
||
| 877 | |||
| 882 |