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 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, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | final class DB |
||
| 19 | { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var int |
||
| 23 | */ |
||
| 24 | public $query_count = 0; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var \mysqli|null |
||
| 28 | */ |
||
| 29 | private $link; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var bool |
||
| 33 | */ |
||
| 34 | private $connected = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | private $mysqlDefaultTimeFunctions; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | private $hostname = ''; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | private $username = ''; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | private $password = ''; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | private $database = ''; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var int |
||
| 63 | */ |
||
| 64 | private $port = 3306; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | private $charset = 'utf8'; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | private $socket = ''; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var bool |
||
| 78 | */ |
||
| 79 | private $session_to_db = false; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var bool |
||
| 83 | */ |
||
| 84 | private $_in_transaction = false; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var bool |
||
| 88 | */ |
||
| 89 | private $_convert_null_to_empty_string = false; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var bool |
||
| 93 | */ |
||
| 94 | private $_ssl = false; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * The path name to the key file |
||
| 98 | * |
||
| 99 | * @var string |
||
| 100 | */ |
||
| 101 | private $_clientkey; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * The path name to the certificate file |
||
| 105 | * |
||
| 106 | * @var string |
||
| 107 | */ |
||
| 108 | private $_clientcert; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * The path name to the certificate authority file |
||
| 112 | * |
||
| 113 | * @var string |
||
| 114 | */ |
||
| 115 | private $_cacert; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var Debug |
||
| 119 | */ |
||
| 120 | private $_debug; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * __construct() |
||
| 124 | * |
||
| 125 | * @param string $hostname |
||
| 126 | * @param string $username |
||
| 127 | * @param string $password |
||
| 128 | * @param string $database |
||
| 129 | * @param int $port |
||
| 130 | * @param string $charset |
||
| 131 | * @param bool $exit_on_error <p>Throw a 'Exception' when a query failed, otherwise it will return 'false'. |
||
| 132 | * Use false to disable it.</p> |
||
| 133 | * @param bool $echo_on_error <p>Echo the error if "checkForDev()" returns true. |
||
| 134 | * Use false to disable it.</p> |
||
| 135 | * @param string $logger_class_name |
||
| 136 | * @param string $logger_level <p>'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'</p> |
||
| 137 | * @param array $extra_config <p> |
||
| 138 | * 'session_to_db' => false|true<br> |
||
| 139 | * 'socket' => 'string (path)'<br> |
||
| 140 | * 'ssl' => 'bool'<br> |
||
| 141 | * 'clientkey' => 'string (path)'<br> |
||
| 142 | * 'clientcert' => 'string (path)'<br> |
||
| 143 | * 'cacert' => 'string (path)'<br> |
||
| 144 | * </p> |
||
| 145 | */ |
||
| 146 | 11 | private function __construct(string $hostname, string $username, string $password, string $database, $port, string $charset, bool $exit_on_error, bool $echo_on_error, string $logger_class_name, string $logger_level, array $extra_config = []) |
|
| 147 | { |
||
| 148 | 11 | $this->_debug = new Debug($this); |
|
| 149 | |||
| 150 | 11 | $this->_loadConfig( |
|
| 151 | 11 | $hostname, |
|
| 152 | 11 | $username, |
|
| 153 | 11 | $password, |
|
| 154 | 11 | $database, |
|
| 155 | 11 | $port, |
|
| 156 | 11 | $charset, |
|
| 157 | 11 | $exit_on_error, |
|
| 158 | 11 | $echo_on_error, |
|
| 159 | 11 | $logger_class_name, |
|
| 160 | 11 | $logger_level, |
|
| 161 | 11 | $extra_config |
|
| 162 | ); |
||
| 163 | |||
| 164 | 8 | $this->connect(); |
|
| 165 | |||
| 166 | 5 | $this->mysqlDefaultTimeFunctions = [ |
|
| 167 | // Returns the current date. |
||
| 168 | 'CURDATE()', |
||
| 169 | // CURRENT_DATE | Synonyms for CURDATE() |
||
| 170 | 'CURRENT_DATE()', |
||
| 171 | // CURRENT_TIME | Synonyms for CURTIME() |
||
| 172 | 'CURRENT_TIME()', |
||
| 173 | // CURRENT_TIMESTAMP | Synonyms for NOW() |
||
| 174 | 'CURRENT_TIMESTAMP()', |
||
| 175 | // Returns the current time. |
||
| 176 | 'CURTIME()', |
||
| 177 | // Synonym for NOW() |
||
| 178 | 'LOCALTIME()', |
||
| 179 | // Synonym for NOW() |
||
| 180 | 'LOCALTIMESTAMP()', |
||
| 181 | // Returns the current date and time. |
||
| 182 | 'NOW()', |
||
| 183 | // Returns the time at which the function executes. |
||
| 184 | 'SYSDATE()', |
||
| 185 | // Returns a UNIX timestamp. |
||
| 186 | 'UNIX_TIMESTAMP()', |
||
| 187 | // Returns the current UTC date. |
||
| 188 | 'UTC_DATE()', |
||
| 189 | // Returns the current UTC time. |
||
| 190 | 'UTC_TIME()', |
||
| 191 | // Returns the current UTC date and time. |
||
| 192 | 'UTC_TIMESTAMP()', |
||
| 193 | ]; |
||
| 194 | 5 | } |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Prevent the instance from being cloned. |
||
| 198 | * |
||
| 199 | * @return void |
||
| 200 | */ |
||
| 201 | private function __clone() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * __destruct |
||
| 207 | * |
||
| 208 | */ |
||
| 209 | public function __destruct() |
||
| 210 | { |
||
| 211 | // close the connection only if we don't save PHP-SESSION's in DB |
||
| 212 | if ($this->session_to_db === false) { |
||
| 213 | $this->close(); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param null|string $sql |
||
| 219 | * @param array $bindings |
||
| 220 | * |
||
| 221 | * @return bool|int|Result|DB <p> |
||
| 222 | * "DB" by "$sql" === null<br /> |
||
| 223 | * "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 224 | * "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 225 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 226 | * "true" by e.g. "DROP"-queries<br /> |
||
| 227 | * "false" on error |
||
| 228 | * </p> |
||
| 229 | */ |
||
| 230 | 2 | public function __invoke(string $sql = null, array $bindings = []) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * __wakeup |
||
| 237 | * |
||
| 238 | * @return void |
||
| 239 | */ |
||
| 240 | 2 | public function __wakeup() |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Load the config from the constructor. |
||
| 247 | * |
||
| 248 | * @param string $hostname |
||
| 249 | * @param string $username |
||
| 250 | * @param string $password |
||
| 251 | * @param string $database |
||
| 252 | * @param int $port <p>default is (int)3306</p> |
||
| 253 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 254 | * @param bool $exit_on_error <p>Throw a 'Exception' when a query failed, otherwise it will return 'false'. |
||
| 255 | * Use false to disable it.</p> |
||
| 256 | * @param bool $echo_on_error <p>Echo the error if "checkForDev()" returns true. |
||
| 257 | * Use false to disable it.</p> |
||
| 258 | * @param string $logger_class_name |
||
| 259 | * @param string $logger_level |
||
| 260 | * @param array $extra_config <p> |
||
| 261 | * 'session_to_db' => false|true<br> |
||
| 262 | * 'socket' => 'string (path)'<br> |
||
| 263 | * 'ssl' => 'bool'<br> |
||
| 264 | * 'clientkey' => 'string (path)'<br> |
||
| 265 | * 'clientcert' => 'string (path)'<br> |
||
| 266 | * 'cacert' => 'string (path)'<br> |
||
| 267 | * </p> |
||
| 268 | * |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | 11 | private function _loadConfig(string $hostname, string $username, string $password, string $database, $port, string $charset, bool $exit_on_error, bool $echo_on_error, string $logger_class_name, string $logger_level, array $extra_config = []): bool |
|
| 318 | |||
| 319 | |||
| 320 | /** |
||
| 321 | * @param array $extra_config <p> |
||
| 322 | * 'session_to_db' => false|true<br> |
||
| 323 | * 'socket' => 'string (path)'<br> |
||
| 324 | * 'ssl' => 'bool'<br> |
||
| 325 | * 'clientkey' => 'string (path)'<br> |
||
| 326 | * 'clientcert' => 'string (path)'<br> |
||
| 327 | * 'cacert' => 'string (path)'<br> |
||
| 328 | * </p> |
||
| 329 | */ |
||
| 330 | 11 | public function setConfigExtra(array $extra_config) |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Parses arrays with value pairs and generates SQL to use in queries. |
||
| 359 | * |
||
| 360 | * @param array $arrayPair |
||
| 361 | * @param string $glue <p>This is the separator.</p> |
||
| 362 | * |
||
| 363 | * @return string |
||
| 364 | * |
||
| 365 | * @internal |
||
| 366 | */ |
||
| 367 | 30 | public function _parseArrayPair(array $arrayPair, string $glue = ','): string |
|
| 514 | |||
| 515 | /** |
||
| 516 | * _parseQueryParams |
||
| 517 | * |
||
| 518 | * @param string $sql |
||
| 519 | * @param array $params |
||
| 520 | * |
||
| 521 | * @return array <p>with the keys -> 'sql', 'params'</p> |
||
| 522 | */ |
||
| 523 | 13 | private function _parseQueryParams(string $sql, array $params = []): array |
|
| 554 | |||
| 555 | /** |
||
| 556 | * Gets the number of affected rows in a previous MySQL operation. |
||
| 557 | * |
||
| 558 | * @return int |
||
| 559 | */ |
||
| 560 | 12 | public function affected_rows(): int |
|
| 564 | |||
| 565 | /** |
||
| 566 | * Begins a transaction, by turning off auto commit. |
||
| 567 | * |
||
| 568 | * @return bool <p>This will return true or false indicating success of transaction</p> |
||
| 569 | */ |
||
| 570 | 6 | View Code Duplication | public function beginTransaction(): bool |
| 587 | |||
| 588 | /** |
||
| 589 | * Clear the errors in "_debug->_errors". |
||
| 590 | * |
||
| 591 | * @return bool |
||
| 592 | */ |
||
| 593 | 6 | public function clearErrors(): bool |
|
| 597 | |||
| 598 | /** |
||
| 599 | * Closes a previously opened database connection. |
||
| 600 | */ |
||
| 601 | 2 | public function close(): bool |
|
| 618 | |||
| 619 | /** |
||
| 620 | * Commits the current transaction and end the transaction. |
||
| 621 | * |
||
| 622 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 623 | */ |
||
| 624 | 2 | View Code Duplication | public function commit(): bool |
| 638 | |||
| 639 | /** |
||
| 640 | * Open a new connection to the MySQL server. |
||
| 641 | * |
||
| 642 | * @return bool |
||
| 643 | * |
||
| 644 | * @throws DBConnectException |
||
| 645 | */ |
||
| 646 | 10 | public function connect(): bool |
|
| 721 | |||
| 722 | /** |
||
| 723 | * Execute a "delete"-query. |
||
| 724 | * |
||
| 725 | * @param string $table |
||
| 726 | * @param string|array $where |
||
| 727 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 728 | * |
||
| 729 | * @return false|int <p>false on error</p> |
||
| 730 | * |
||
| 731 | * * @throws QueryException |
||
| 732 | */ |
||
| 733 | 2 | View Code Duplication | public function delete(string $table, $where, string $databaseName = null) |
| 760 | |||
| 761 | /** |
||
| 762 | * Ends a transaction and commits if no errors, then ends autocommit. |
||
| 763 | * |
||
| 764 | * @return bool <p>This will return true or false indicating success of transactions.</p> |
||
| 765 | */ |
||
| 766 | 4 | View Code Duplication | public function endTransaction(): bool |
| 786 | |||
| 787 | /** |
||
| 788 | * Get all errors from "$this->_errors". |
||
| 789 | * |
||
| 790 | * @return array|false <p>false === on errors</p> |
||
| 791 | */ |
||
| 792 | 4 | public function errors() |
|
| 798 | |||
| 799 | /** |
||
| 800 | * Returns the SQL by replacing :placeholders with SQL-escaped values. |
||
| 801 | * |
||
| 802 | * @param mixed $sql <p>The SQL string.</p> |
||
| 803 | * @param array $params <p>An array of key-value bindings.</p> |
||
| 804 | * |
||
| 805 | * @return array <p>with the keys -> 'sql', 'params'</p> |
||
| 806 | */ |
||
| 807 | 14 | public function _parseQueryParamsByName(string $sql, array $params = []): array |
|
| 855 | |||
| 856 | /** |
||
| 857 | * Escape: Use "mysqli_real_escape_string" and clean non UTF-8 chars + some extra optional stuff. |
||
| 858 | * |
||
| 859 | * @param mixed $var boolean: convert into "integer"<br /> |
||
| 860 | * int: int (don't change it)<br /> |
||
| 861 | * float: float (don't change it)<br /> |
||
| 862 | * null: null (don't change it)<br /> |
||
| 863 | * array: run escape() for every key => value<br /> |
||
| 864 | * string: run UTF8::cleanup() and mysqli_real_escape_string()<br /> |
||
| 865 | * @param bool $stripe_non_utf8 |
||
| 866 | * @param bool $html_entity_decode |
||
| 867 | * @param bool|null $convert_array <strong>false</strong> => Keep the array.<br /> |
||
| 868 | * <strong>true</strong> => Convert to string var1,var2,var3...<br /> |
||
| 869 | * <strong>null</strong> => Convert the array into null, every time. |
||
| 870 | * |
||
| 871 | * @return mixed |
||
| 872 | */ |
||
| 873 | 55 | public function escape($var = '', bool $stripe_non_utf8 = true, bool $html_entity_decode = false, $convert_array = false) |
|
| 985 | |||
| 986 | /** |
||
| 987 | * Execute select/insert/update/delete sql-queries. |
||
| 988 | * |
||
| 989 | * @param string $query <p>sql-query</p> |
||
| 990 | * @param bool $useCache <p>use cache?</p> |
||
| 991 | * @param int $cacheTTL <p>cache-ttl in seconds</p> |
||
| 992 | * @param DB $db optional <p>the database connection</p> |
||
| 993 | * |
||
| 994 | * @return mixed "array" by "<b>SELECT</b>"-queries<br /> |
||
| 995 | * "int" (insert_id) by "<b>INSERT</b>"-queries<br /> |
||
| 996 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 997 | * "true" by e.g. "DROP"-queries<br /> |
||
| 998 | * "false" on error |
||
| 999 | * |
||
| 1000 | * @throws QueryException |
||
| 1001 | */ |
||
| 1002 | 3 | public static function execSQL(string $query, bool $useCache = false, int $cacheTTL = 3600, self $db = null) |
|
| 1051 | |||
| 1052 | /** |
||
| 1053 | * Get all table-names via "SHOW TABLES". |
||
| 1054 | * |
||
| 1055 | * @return array |
||
| 1056 | */ |
||
| 1057 | 1 | public function getAllTables(): array |
|
| 1064 | |||
| 1065 | /** |
||
| 1066 | * @return Debug |
||
| 1067 | */ |
||
| 1068 | 9 | public function getDebugger(): Debug |
|
| 1072 | |||
| 1073 | /** |
||
| 1074 | * Get errors from "$this->_errors". |
||
| 1075 | * |
||
| 1076 | * @return array |
||
| 1077 | */ |
||
| 1078 | 1 | public function getErrors(): array |
|
| 1082 | |||
| 1083 | /** |
||
| 1084 | * getInstance() |
||
| 1085 | * |
||
| 1086 | * @param string $hostname |
||
| 1087 | * @param string $username |
||
| 1088 | * @param string $password |
||
| 1089 | * @param string $database |
||
| 1090 | * @param int $port <p>default is (int)3306</p> |
||
| 1091 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 1092 | * @param bool $exit_on_error <p>Throw a 'Exception' when a query failed, otherwise it will return 'false'. |
||
| 1093 | * Use false to disable it.</p> |
||
| 1094 | * @param bool $echo_on_error <p>Echo the error if "checkForDev()" returns true. |
||
| 1095 | * Use false to disable it.</p> |
||
| 1096 | * @param string $logger_class_name |
||
| 1097 | * @param string $logger_level <p>'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'</p> |
||
| 1098 | * @param array $extra_config <p> |
||
| 1099 | * 'session_to_db' => false|true<br> |
||
| 1100 | * 'socket' => 'string (path)'<br> |
||
| 1101 | * 'ssl' => 'bool'<br> |
||
| 1102 | * 'clientkey' => 'string (path)'<br> |
||
| 1103 | * 'clientcert' => 'string (path)'<br> |
||
| 1104 | * 'cacert' => 'string (path)'<br> |
||
| 1105 | * </p> |
||
| 1106 | * |
||
| 1107 | * @return \voku\db\DB |
||
| 1108 | */ |
||
| 1109 | 97 | public static function getInstance(string $hostname = '', string $username = '', string $password = '', string $database = '', $port = 3306, string $charset = 'utf8', bool $exit_on_error = true, bool $echo_on_error = true, string $logger_class_name = '', string $logger_level = '', array $extra_config = []): self |
|
| 1165 | |||
| 1166 | /** |
||
| 1167 | * Get the mysqli-link (link identifier returned by mysqli-connect). |
||
| 1168 | * |
||
| 1169 | * @return \mysqli |
||
| 1170 | */ |
||
| 1171 | 56 | public function getLink(): \mysqli |
|
| 1175 | |||
| 1176 | /** |
||
| 1177 | * Get the current charset. |
||
| 1178 | * |
||
| 1179 | * @return string |
||
| 1180 | */ |
||
| 1181 | 1 | public function get_charset(): string |
|
| 1185 | |||
| 1186 | /** |
||
| 1187 | * Check if we are in a transaction. |
||
| 1188 | * |
||
| 1189 | * @return bool |
||
| 1190 | */ |
||
| 1191 | public function inTransaction(): bool |
||
| 1195 | |||
| 1196 | /** |
||
| 1197 | * Execute a "insert"-query. |
||
| 1198 | * |
||
| 1199 | * @param string $table |
||
| 1200 | * @param array $data |
||
| 1201 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1202 | * |
||
| 1203 | * @return false|int <p>false on error</p> |
||
| 1204 | * |
||
| 1205 | * @throws QueryException |
||
| 1206 | */ |
||
| 1207 | 28 | public function insert(string $table, array $data = [], string $databaseName = null) |
|
| 1234 | |||
| 1235 | /** |
||
| 1236 | * Returns the auto generated id used in the last query. |
||
| 1237 | * |
||
| 1238 | * @return int|string |
||
| 1239 | */ |
||
| 1240 | 58 | public function insert_id() |
|
| 1244 | |||
| 1245 | /** |
||
| 1246 | * Check if db-connection is ready. |
||
| 1247 | * |
||
| 1248 | * @return boolean |
||
| 1249 | */ |
||
| 1250 | 106 | public function isReady(): bool |
|
| 1254 | |||
| 1255 | /** |
||
| 1256 | * Get the last sql-error. |
||
| 1257 | * |
||
| 1258 | * @return string|false <p>false === there was no error</p> |
||
| 1259 | */ |
||
| 1260 | 1 | public function lastError() |
|
| 1266 | |||
| 1267 | /** |
||
| 1268 | * Execute a sql-multi-query. |
||
| 1269 | * |
||
| 1270 | * @param string $sql |
||
| 1271 | * |
||
| 1272 | * @return false|Result[] "Result"-Array by "<b>SELECT</b>"-queries<br /> |
||
| 1273 | * "boolean" by only "<b>INSERT</b>"-queries<br /> |
||
| 1274 | * "boolean" by only (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1275 | * "boolean" by only by e.g. "DROP"-queries<br /> |
||
| 1276 | * |
||
| 1277 | * @throws QueryException |
||
| 1278 | */ |
||
| 1279 | 1 | public function multi_query(string $sql) |
|
| 1346 | |||
| 1347 | /** |
||
| 1348 | * Pings a server connection, or tries to reconnect |
||
| 1349 | * if the connection has gone down. |
||
| 1350 | * |
||
| 1351 | * @return boolean |
||
| 1352 | */ |
||
| 1353 | 3 | public function ping(): bool |
|
| 1366 | |||
| 1367 | /** |
||
| 1368 | * Get a new "Prepare"-Object for your sql-query. |
||
| 1369 | * |
||
| 1370 | * @param string $query |
||
| 1371 | * |
||
| 1372 | * @return Prepare |
||
| 1373 | */ |
||
| 1374 | 2 | public function prepare(string $query): Prepare |
|
| 1378 | |||
| 1379 | /** |
||
| 1380 | * Execute a sql-query and return the result-array for select-statements. |
||
| 1381 | * |
||
| 1382 | * @param string $query |
||
| 1383 | * |
||
| 1384 | * @return mixed |
||
| 1385 | * @deprecated |
||
| 1386 | * @throws \Exception |
||
| 1387 | */ |
||
| 1388 | 1 | public static function qry(string $query) |
|
| 1413 | |||
| 1414 | /** |
||
| 1415 | * Execute a sql-query. |
||
| 1416 | * |
||
| 1417 | * @param string $sql <p>The sql query-string.</p> |
||
| 1418 | * |
||
| 1419 | * @param array|boolean $params <p> |
||
| 1420 | * "array" of sql-query-parameters<br/> |
||
| 1421 | * "false" if you don't need any parameter (default)<br/> |
||
| 1422 | * </p> |
||
| 1423 | * |
||
| 1424 | * @return bool|int|Result <p> |
||
| 1425 | * "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 1426 | * "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 1427 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1428 | * "true" by e.g. "DROP"-queries<br /> |
||
| 1429 | * "false" on error |
||
| 1430 | * </p> |
||
| 1431 | * |
||
| 1432 | * @throws QueryException |
||
| 1433 | */ |
||
| 1434 | 96 | public function query(string $sql = '', $params = false) |
|
| 1513 | |||
| 1514 | /** |
||
| 1515 | * Error-handling for the sql-query. |
||
| 1516 | * |
||
| 1517 | * @param string $errorMessage |
||
| 1518 | * @param int $errorNumber |
||
| 1519 | * @param string $sql |
||
| 1520 | * @param array|bool $sqlParams <p>false if there wasn't any parameter</p> |
||
| 1521 | * @param bool $sqlMultiQuery |
||
| 1522 | * |
||
| 1523 | * @throws QueryException |
||
| 1524 | * @throws DBGoneAwayException |
||
| 1525 | * |
||
| 1526 | * @return mixed|false |
||
| 1527 | */ |
||
| 1528 | 13 | private function queryErrorHandling(string $errorMessage, int $errorNumber, string $sql, $sqlParams = false, bool $sqlMultiQuery = false) |
|
| 1571 | |||
| 1572 | /** |
||
| 1573 | * Quote && Escape e.g. a table name string. |
||
| 1574 | * |
||
| 1575 | * @param mixed $str |
||
| 1576 | * |
||
| 1577 | * @return string |
||
| 1578 | */ |
||
| 1579 | 36 | public function quote_string($str): string |
|
| 1592 | |||
| 1593 | /** |
||
| 1594 | * Reconnect to the MySQL-Server. |
||
| 1595 | * |
||
| 1596 | * @param bool $checkViaPing |
||
| 1597 | * |
||
| 1598 | * @return bool |
||
| 1599 | */ |
||
| 1600 | 3 | public function reconnect(bool $checkViaPing = false): bool |
|
| 1615 | |||
| 1616 | /** |
||
| 1617 | * Execute a "replace"-query. |
||
| 1618 | * |
||
| 1619 | * @param string $table |
||
| 1620 | * @param array $data |
||
| 1621 | * @param null|string $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1622 | * |
||
| 1623 | * @return false|int <p>false on error</p> |
||
| 1624 | * |
||
| 1625 | * @throws QueryException |
||
| 1626 | */ |
||
| 1627 | 1 | public function replace(string $table, array $data = [], string $databaseName = null) |
|
| 1668 | |||
| 1669 | /** |
||
| 1670 | * Rollback in a transaction and end the transaction. |
||
| 1671 | * |
||
| 1672 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 1673 | */ |
||
| 1674 | 4 | View Code Duplication | public function rollback(): bool |
| 1688 | |||
| 1689 | /** |
||
| 1690 | * Try to secure a variable, so can you use it in sql-queries. |
||
| 1691 | * |
||
| 1692 | * <p> |
||
| 1693 | * <strong>int:</strong> (also strings that contains only an int-value)<br /> |
||
| 1694 | * 1. parse into "int" |
||
| 1695 | * </p><br /> |
||
| 1696 | * |
||
| 1697 | * <p> |
||
| 1698 | * <strong>float:</strong><br /> |
||
| 1699 | * 1. return "float" |
||
| 1700 | * </p><br /> |
||
| 1701 | * |
||
| 1702 | * <p> |
||
| 1703 | * <strong>string:</strong><br /> |
||
| 1704 | * 1. check if the string isn't a default mysql-time-function e.g. 'CURDATE()'<br /> |
||
| 1705 | * 2. \trim whitespace<br /> |
||
| 1706 | * 3. \trim '<br /> |
||
| 1707 | * 4. escape the string (and remove non utf-8 chars)<br /> |
||
| 1708 | * 5. \trim ' again (because we maybe removed some chars)<br /> |
||
| 1709 | * 6. add ' around the new string<br /> |
||
| 1710 | * </p><br /> |
||
| 1711 | * |
||
| 1712 | * <p> |
||
| 1713 | * <strong>array:</strong><br /> |
||
| 1714 | * 1. return null |
||
| 1715 | * </p><br /> |
||
| 1716 | * |
||
| 1717 | * <p> |
||
| 1718 | * <strong>object:</strong><br /> |
||
| 1719 | * 1. return false |
||
| 1720 | * </p><br /> |
||
| 1721 | * |
||
| 1722 | * <p> |
||
| 1723 | * <strong>null:</strong><br /> |
||
| 1724 | * 1. return null |
||
| 1725 | * </p> |
||
| 1726 | * |
||
| 1727 | * @param mixed $var |
||
| 1728 | * |
||
| 1729 | * @return mixed |
||
| 1730 | */ |
||
| 1731 | 44 | public function secure($var) |
|
| 1767 | |||
| 1768 | /** |
||
| 1769 | * Execute a "select"-query. |
||
| 1770 | * |
||
| 1771 | * @param string $table |
||
| 1772 | * @param string|array $where |
||
| 1773 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1774 | * |
||
| 1775 | * @return false|Result <p>false on error</p> |
||
| 1776 | * |
||
| 1777 | * @throws QueryException |
||
| 1778 | */ |
||
| 1779 | 24 | View Code Duplication | public function select(string $table, $where = '1=1', string $databaseName = null) |
| 1806 | |||
| 1807 | /** |
||
| 1808 | * Selects a different database than the one specified on construction. |
||
| 1809 | * |
||
| 1810 | * @param string $database <p>Database name to switch to.</p> |
||
| 1811 | * |
||
| 1812 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 1813 | */ |
||
| 1814 | public function select_db(string $database): bool |
||
| 1822 | |||
| 1823 | /** |
||
| 1824 | * Set the current charset. |
||
| 1825 | * |
||
| 1826 | * @param string $charset |
||
| 1827 | * |
||
| 1828 | * @return bool |
||
| 1829 | */ |
||
| 1830 | 8 | public function set_charset(string $charset): bool |
|
| 1850 | |||
| 1851 | /** |
||
| 1852 | * Set the option to convert null to "''" (empty string). |
||
| 1853 | * |
||
| 1854 | * Used in secure() => select(), insert(), update(), delete() |
||
| 1855 | * |
||
| 1856 | * @deprecated It's not recommended to convert NULL into an empty string! |
||
| 1857 | * |
||
| 1858 | * @param $bool |
||
| 1859 | * |
||
| 1860 | * @return $this |
||
| 1861 | */ |
||
| 1862 | 1 | public function set_convert_null_to_empty_string(bool $bool) |
|
| 1868 | |||
| 1869 | /** |
||
| 1870 | * Enables or disables internal report functions |
||
| 1871 | * |
||
| 1872 | * @link http://php.net/manual/en/function.mysqli-report.php |
||
| 1873 | * |
||
| 1874 | * @param int $flags <p> |
||
| 1875 | * <table> |
||
| 1876 | * Supported flags |
||
| 1877 | * <tr valign="top"> |
||
| 1878 | * <td>Name</td> |
||
| 1879 | * <td>Description</td> |
||
| 1880 | * </tr> |
||
| 1881 | * <tr valign="top"> |
||
| 1882 | * <td><b>MYSQLI_REPORT_OFF</b></td> |
||
| 1883 | * <td>Turns reporting off</td> |
||
| 1884 | * </tr> |
||
| 1885 | * <tr valign="top"> |
||
| 1886 | * <td><b>MYSQLI_REPORT_ERROR</b></td> |
||
| 1887 | * <td>Report errors from mysqli function calls</td> |
||
| 1888 | * </tr> |
||
| 1889 | * <tr valign="top"> |
||
| 1890 | * <td><b>MYSQLI_REPORT_STRICT</b></td> |
||
| 1891 | * <td> |
||
| 1892 | * Throw <b>mysqli_sql_exception</b> for errors |
||
| 1893 | * instead of warnings |
||
| 1894 | * </td> |
||
| 1895 | * </tr> |
||
| 1896 | * <tr valign="top"> |
||
| 1897 | * <td><b>MYSQLI_REPORT_INDEX</b></td> |
||
| 1898 | * <td>Report if no index or bad index was used in a query</td> |
||
| 1899 | * </tr> |
||
| 1900 | * <tr valign="top"> |
||
| 1901 | * <td><b>MYSQLI_REPORT_ALL</b></td> |
||
| 1902 | * <td>Set all options (report all)</td> |
||
| 1903 | * </tr> |
||
| 1904 | * </table> |
||
| 1905 | * </p> |
||
| 1906 | * |
||
| 1907 | * @return bool |
||
| 1908 | */ |
||
| 1909 | public function set_mysqli_report(int $flags): bool |
||
| 1913 | |||
| 1914 | /** |
||
| 1915 | * Show config errors by throw exceptions. |
||
| 1916 | * |
||
| 1917 | * @return bool |
||
| 1918 | * |
||
| 1919 | * @throws \InvalidArgumentException |
||
| 1920 | */ |
||
| 1921 | 11 | public function showConfigError(): bool |
|
| 1949 | |||
| 1950 | /** |
||
| 1951 | * alias: "beginTransaction()" |
||
| 1952 | */ |
||
| 1953 | 1 | public function startTransaction(): bool |
|
| 1957 | |||
| 1958 | /** |
||
| 1959 | * Execute a callback inside a transaction. |
||
| 1960 | * |
||
| 1961 | * @param callback $callback <p>The callback to run inside the transaction, if it's throws an "Exception" or if it's |
||
| 1962 | * returns "false", all SQL-statements in the callback will be rollbacked.</p> |
||
| 1963 | * |
||
| 1964 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 1965 | */ |
||
| 1966 | 1 | public function transact($callback): bool |
|
| 1992 | |||
| 1993 | /** |
||
| 1994 | * Execute a "update"-query. |
||
| 1995 | * |
||
| 1996 | * @param string $table |
||
| 1997 | * @param array $data |
||
| 1998 | * @param array|string $where |
||
| 1999 | * @param null|string $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 2000 | * |
||
| 2001 | * @return false|int <p>false on error</p> |
||
| 2002 | * |
||
| 2003 | * @throws QueryException |
||
| 2004 | */ |
||
| 2005 | 7 | public function update(string $table, array $data = [], $where = '1=1', string $databaseName = null) |
|
| 2040 | |||
| 2041 | /** |
||
| 2042 | * Determine if database table exists |
||
| 2043 | * |
||
| 2044 | * @param string $table |
||
| 2045 | * |
||
| 2046 | * @return bool |
||
| 2047 | */ |
||
| 2048 | 1 | public function table_exists(string $table): bool |
|
| 2058 | |||
| 2059 | /** |
||
| 2060 | * Count number of rows found matching a specific query. |
||
| 2061 | * |
||
| 2062 | * @param string $query |
||
| 2063 | * |
||
| 2064 | * @return int |
||
| 2065 | */ |
||
| 2066 | 1 | public function num_rows(string $query): int |
|
| 2080 | } |
||
| 2081 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.