Complex classes like DoliDBMysqli 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 DoliDBMysqli, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class DoliDBMysqli extends DoliDB |
||
| 34 | { |
||
| 35 | /** @var mysqli Database object */ |
||
| 36 | public $db; |
||
| 37 | //! Database type |
||
| 38 | public $type='mysqli'; |
||
| 39 | //! Database label |
||
| 40 | const LABEL='MySQL or MariaDB'; |
||
| 41 | //! Version min database |
||
| 42 | const VERSIONMIN='5.0.3'; |
||
| 43 | /** @var mysqli_result Resultset of last query */ |
||
| 44 | private $_results; |
||
|
|
|||
| 45 | |||
| 46 | /** |
||
| 47 | * Constructor. |
||
| 48 | * This create an opened connexion to a database server and eventually to a database |
||
| 49 | * |
||
| 50 | * @param string $type Type of database (mysql, pgsql...) |
||
| 51 | * @param string $host Address of database server |
||
| 52 | * @param string $user Nom de l'utilisateur autorise |
||
| 53 | * @param string $pass Mot de passe |
||
| 54 | * @param string $name Nom de la database |
||
| 55 | * @param int $port Port of database server |
||
| 56 | */ |
||
| 57 | function __construct($type, $host, $user, $pass, $name='', $port=0) |
||
| 146 | |||
| 147 | |||
| 148 | /** |
||
| 149 | * Convert a SQL request in Mysql syntax to native syntax |
||
| 150 | * |
||
| 151 | * @param string $line SQL request line to convert |
||
| 152 | * @param string $type Type of SQL order ('ddl' for insert, update, select, delete or 'dml' for create, alter...) |
||
| 153 | * @return string SQL request line converted |
||
| 154 | */ |
||
| 155 | static function convertSQLFromMysql($line,$type='ddl') |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Select a database |
||
| 162 | * |
||
| 163 | * @param string $database Name of database |
||
| 164 | * @return boolean true if OK, false if KO |
||
| 165 | */ |
||
| 166 | function select_db($database) |
||
| 171 | |||
| 172 | |||
| 173 | /** |
||
| 174 | * Connect to server |
||
| 175 | * |
||
| 176 | * @param string $host database server host |
||
| 177 | * @param string $login login |
||
| 178 | * @param string $passwd password |
||
| 179 | * @param string $name name of database (not used for mysql, used for pgsql) |
||
| 180 | * @param integer $port Port of database server |
||
| 181 | * @return mysqli Database access object |
||
| 182 | * @see close |
||
| 183 | */ |
||
| 184 | function connect($host, $login, $passwd, $name, $port = 0) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Return version of database server |
||
| 193 | * |
||
| 194 | * @return string Version string |
||
| 195 | */ |
||
| 196 | function getVersion() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Return version of database client driver |
||
| 203 | * |
||
| 204 | * @return string Version string |
||
| 205 | */ |
||
| 206 | function getDriverInfo() |
||
| 210 | |||
| 211 | |||
| 212 | /** |
||
| 213 | * Close database connexion |
||
| 214 | * |
||
| 215 | * @return bool True if disconnect successfull, false otherwise |
||
| 216 | * @see connect |
||
| 217 | */ |
||
| 218 | function close() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Execute a SQL request and return the resultset |
||
| 231 | * |
||
| 232 | * @param string $query SQL query string |
||
| 233 | * @param int $usesavepoint 0=Default mode, 1=Run a savepoint before and a rollbock to savepoint if error (this allow to have some request with errors inside global transactions). |
||
| 234 | * Note that with Mysql, this parameter is not used as Myssql can already commit a transaction even if one request is in error, without using savepoints. |
||
| 235 | * @param string $type Type of SQL order ('ddl' for insert, update, select, delete or 'dml' for create, alter...) |
||
| 236 | * @return bool|mysqli_result Resultset of answer |
||
| 237 | */ |
||
| 238 | function query($query,$usesavepoint=0,$type='auto') |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Renvoie la ligne courante (comme un objet) pour le curseur resultset |
||
| 277 | * |
||
| 278 | * @param mysqli_result $resultset Curseur de la requete voulue |
||
| 279 | * @return object|null Object result line or null if KO or end of cursor |
||
| 280 | */ |
||
| 281 | function fetch_object($resultset) |
||
| 287 | |||
| 288 | |||
| 289 | /** |
||
| 290 | * Return datas as an array |
||
| 291 | * |
||
| 292 | * @param mysqli_result $resultset Resultset of request |
||
| 293 | * @return array|null Array or null if KO or end of cursor |
||
| 294 | */ |
||
| 295 | function fetch_array($resultset) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Return datas as an array |
||
| 304 | * |
||
| 305 | * @param mysqli_result $resultset Resultset of request |
||
| 306 | * @return array|null|0 Array or null if KO or end of cursor or 0 if resultset is bool |
||
| 307 | */ |
||
| 308 | function fetch_row($resultset) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Return number of lines for result of a SELECT |
||
| 325 | * |
||
| 326 | * @param mysqli_result $resultset Resulset of requests |
||
| 327 | * @return int Nb of lines |
||
| 328 | * @see affected_rows |
||
| 329 | */ |
||
| 330 | function num_rows($resultset) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Renvoie le nombre de lignes dans le resultat d'une requete INSERT, DELETE ou UPDATE |
||
| 339 | * |
||
| 340 | * @param mysqli_result $resultset Curseur de la requete voulue |
||
| 341 | * @return int Nombre de lignes |
||
| 342 | * @see num_rows |
||
| 343 | */ |
||
| 344 | function affected_rows($resultset) |
||
| 352 | |||
| 353 | |||
| 354 | /** |
||
| 355 | * Libere le dernier resultset utilise sur cette connexion |
||
| 356 | * |
||
| 357 | * @param mysqli_result $resultset Curseur de la requete voulue |
||
| 358 | * @return void |
||
| 359 | */ |
||
| 360 | function free($resultset=null) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Escape a string to insert data |
||
| 370 | * |
||
| 371 | * @param string $stringtoencode String to escape |
||
| 372 | * @return string String escaped |
||
| 373 | */ |
||
| 374 | function escape($stringtoencode) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Return generic error code of last operation. |
||
| 381 | * |
||
| 382 | * @return string Error code (Exemples: DB_ERROR_TABLE_ALREADY_EXISTS, DB_ERROR_RECORD_ALREADY_EXISTS...) |
||
| 383 | */ |
||
| 384 | function errno() |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Return description of last error |
||
| 432 | * |
||
| 433 | * @return string Error text |
||
| 434 | */ |
||
| 435 | function error() |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Get last ID after an insert INSERT |
||
| 448 | * |
||
| 449 | * @param string $tab Table name concerned by insert. Ne sert pas sous MySql mais requis pour compatibilite avec Postgresql |
||
| 450 | * @param string $fieldid Field name |
||
| 451 | * @return int|string Id of row |
||
| 452 | */ |
||
| 453 | function last_insert_id($tab,$fieldid='rowid') |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Encrypt sensitive data in database |
||
| 460 | * Warning: This function includes the escape, so it must use direct value |
||
| 461 | * |
||
| 462 | * @param string $fieldorvalue Field name or value to encrypt |
||
| 463 | * @param int $withQuotes Return string with quotes |
||
| 464 | * @return string XXX(field) or XXX('value') or field or 'value' |
||
| 465 | * |
||
| 466 | */ |
||
| 467 | function encrypt($fieldorvalue, $withQuotes=0) |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Decrypt sensitive data in database |
||
| 496 | * |
||
| 497 | * @param string $value Value to decrypt |
||
| 498 | * @return string Decrypted value if used |
||
| 499 | */ |
||
| 500 | function decrypt($value) |
||
| 526 | |||
| 527 | |||
| 528 | /** |
||
| 529 | * Return connexion ID |
||
| 530 | * |
||
| 531 | * @return string Id connexion |
||
| 532 | */ |
||
| 533 | function DDLGetConnectId() |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Create a new database |
||
| 546 | * Do not use function xxx_create_db (xxx=mysql, ...) as they are deprecated |
||
| 547 | * We force to create database with charset this->forcecharset and collate this->forcecollate |
||
| 548 | * |
||
| 549 | * @param string $database Database name to create |
||
| 550 | * @param string $charset Charset used to store data |
||
| 551 | * @param string $collation Charset used to sort data |
||
| 552 | * @param string $owner Username of database owner |
||
| 553 | * @return bool|mysqli_result resource defined if OK, null if KO |
||
| 554 | */ |
||
| 555 | function DDLCreateDb($database,$charset='',$collation='',$owner='') |
||
| 575 | |||
| 576 | /** |
||
| 577 | * List tables into a database |
||
| 578 | * |
||
| 579 | * @param string $database Name of database |
||
| 580 | * @param string $table Nmae of table filter ('xxx%') |
||
| 581 | * @return array List of tables in an array |
||
| 582 | */ |
||
| 583 | function DDLListTables($database, $table='') |
||
| 601 | |||
| 602 | /** |
||
| 603 | * List information of columns into a table. |
||
| 604 | * |
||
| 605 | * @param string $table Name of table |
||
| 606 | * @return array Tableau des informations des champs de la table |
||
| 607 | */ |
||
| 608 | function DDLInfoTable($table) |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Create a table into database |
||
| 628 | * |
||
| 629 | * @param string $table Nom de la table |
||
| 630 | * @param array $fields Tableau associatif [nom champ][tableau des descriptions] |
||
| 631 | * @param string $primary_key Nom du champ qui sera la clef primaire |
||
| 632 | * @param string $type Type de la table |
||
| 633 | * @param array $unique_keys Tableau associatifs Nom de champs qui seront clef unique => valeur |
||
| 634 | * @param array $fulltext_keys Tableau des Nom de champs qui seront indexes en fulltext |
||
| 635 | * @param array $keys Tableau des champs cles noms => valeur |
||
| 636 | * @return int <0 if KO, >=0 if OK |
||
| 637 | */ |
||
| 638 | function DDLCreateTable($table,$fields,$primary_key,$type,$unique_keys=null,$fulltext_keys=null,$keys=null) |
||
| 708 | |||
| 709 | /** |
||
| 710 | * Return a pointer of line with description of a table or field |
||
| 711 | * |
||
| 712 | * @param string $table Name of table |
||
| 713 | * @param string $field Optionnel : Name of field if we want description of field |
||
| 714 | * @return bool|mysqli_result Resultset x (x->Field, x->Type, ...) |
||
| 715 | */ |
||
| 716 | function DDLDescTable($table,$field="") |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Create a new field into table |
||
| 727 | * |
||
| 728 | * @param string $table Name of table |
||
| 729 | * @param string $field_name Name of field to add |
||
| 730 | * @param string $field_desc Tableau associatif de description du champ a inserer[nom du parametre][valeur du parametre] |
||
| 731 | * @param string $field_position Optionnel ex.: "after champtruc" |
||
| 732 | * @return int <0 if KO, >0 if OK |
||
| 733 | */ |
||
| 734 | function DDLAddField($table,$field_name,$field_desc,$field_position="") |
||
| 766 | |||
| 767 | /** |
||
| 768 | * Update format of a field into a table |
||
| 769 | * |
||
| 770 | * @param string $table Name of table |
||
| 771 | * @param string $field_name Name of field to modify |
||
| 772 | * @param string $field_desc Array with description of field format |
||
| 773 | * @return int <0 if KO, >0 if OK |
||
| 774 | */ |
||
| 775 | function DDLUpdateField($table,$field_name,$field_desc) |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Drop a field from table |
||
| 814 | * |
||
| 815 | * @param string $table Name of table |
||
| 816 | * @param string $field_name Name of field to drop |
||
| 817 | * @return int <0 if KO, >0 if OK |
||
| 818 | */ |
||
| 819 | function DDLDropField($table,$field_name) |
||
| 829 | |||
| 830 | |||
| 831 | /** |
||
| 832 | * Create a user and privileges to connect to database (even if database does not exists yet) |
||
| 833 | * |
||
| 834 | * @param string $dolibarr_main_db_host Ip server or '%' |
||
| 835 | * @param string $dolibarr_main_db_user Nom user a creer |
||
| 836 | * @param string $dolibarr_main_db_pass Mot de passe user a creer |
||
| 837 | * @param string $dolibarr_main_db_name Database name where user must be granted |
||
| 838 | * @return int <0 if KO, >=0 if OK |
||
| 839 | */ |
||
| 840 | function DDLCreateUser($dolibarr_main_db_host,$dolibarr_main_db_user,$dolibarr_main_db_pass,$dolibarr_main_db_name) |
||
| 876 | |||
| 877 | /** |
||
| 878 | * Return charset used to store data in current database (same result than using SELECT default_character_set_name FROM information_schema.SCHEMATA WHERE schema_name = "databasename";) |
||
| 879 | * |
||
| 880 | * @return string Charset |
||
| 881 | * @see getDefaultCollationDatabase |
||
| 882 | */ |
||
| 883 | function getDefaultCharacterSetDatabase() |
||
| 896 | |||
| 897 | /** |
||
| 898 | * Return list of available charset that can be used to store data in database |
||
| 899 | * |
||
| 900 | * @return array|null List of Charset |
||
| 901 | */ |
||
| 902 | function getListOfCharacterSet() |
||
| 922 | |||
| 923 | /** |
||
| 924 | * Return collation used in current database |
||
| 925 | * |
||
| 926 | * @return string Collation value |
||
| 927 | * @see getDefaultCharacterSetDatabase |
||
| 928 | */ |
||
| 929 | function getDefaultCollationDatabase() |
||
| 942 | |||
| 943 | /** |
||
| 944 | * Return list of available collation that can be used for database |
||
| 945 | * |
||
| 946 | * @return array|null Liste of Collation |
||
| 947 | */ |
||
| 948 | function getListOfCollation() |
||
| 967 | |||
| 968 | /** |
||
| 969 | * Return full path of dump program |
||
| 970 | * |
||
| 971 | * @return string Full path of dump program |
||
| 972 | */ |
||
| 973 | function getPathOfDump() |
||
| 986 | |||
| 987 | /** |
||
| 988 | * Return full path of restore program |
||
| 989 | * |
||
| 990 | * @return string Full path of restore program |
||
| 991 | */ |
||
| 992 | function getPathOfRestore() |
||
| 1005 | |||
| 1006 | /** |
||
| 1007 | * Return value of server parameters |
||
| 1008 | * |
||
| 1009 | * @param string $filter Filter list on a particular value |
||
| 1010 | * @return array Array of key-values (key=>value) |
||
| 1011 | */ |
||
| 1012 | function getServerParametersValues($filter='') |
||
| 1026 | |||
| 1027 | /** |
||
| 1028 | * Return value of server status (current indicators on memory, cache...) |
||
| 1029 | * |
||
| 1030 | * @param string $filter Filter list on a particular value |
||
| 1031 | * @return array Array of key-values (key=>value) |
||
| 1032 | */ |
||
| 1033 | function getServerStatusValues($filter='') |
||
| 1047 | } |
||
| 1048 | |||
| 1049 |