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 |
||
| 17 | final class DB |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var int |
||
| 21 | */ |
||
| 22 | public $query_count = 0; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var \mysqli|null |
||
| 26 | */ |
||
| 27 | private $mysqli_link; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var bool |
||
| 31 | */ |
||
| 32 | private $connected = false; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | private $mysqlDefaultTimeFunctions; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | private $hostname = ''; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $username = ''; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | private $password = ''; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | private $database = ''; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var int |
||
| 61 | */ |
||
| 62 | private $port = 3306; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | private $charset = 'utf8'; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | private $socket = ''; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var int|null |
||
| 76 | */ |
||
| 77 | private $flags; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var bool |
||
| 81 | */ |
||
| 82 | private $session_to_db = false; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var bool |
||
| 86 | */ |
||
| 87 | private $in_transaction = false; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var bool |
||
| 91 | */ |
||
| 92 | private $convert_null_to_empty_string = false; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var bool |
||
| 96 | */ |
||
| 97 | private $ssl = false; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * The path name to the key file |
||
| 101 | * |
||
| 102 | * @var string |
||
| 103 | */ |
||
| 104 | private $clientkey; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * The path name to the certificate file |
||
| 108 | * |
||
| 109 | * @var string |
||
| 110 | */ |
||
| 111 | private $clientcert; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * The path name to the certificate authority file |
||
| 115 | * |
||
| 116 | * @var string |
||
| 117 | */ |
||
| 118 | private $cacert; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @var Debug |
||
| 122 | */ |
||
| 123 | private $debug; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @var \Doctrine\DBAL\Connection|null |
||
| 127 | */ |
||
| 128 | private $doctrine_connection; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @var int |
||
| 132 | */ |
||
| 133 | private $affected_rows = 0; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * __construct() |
||
| 137 | * |
||
| 138 | * @param string $hostname |
||
| 139 | * @param string $username |
||
| 140 | * @param string $password |
||
| 141 | * @param string $database |
||
| 142 | * @param int $port |
||
| 143 | * @param string $charset |
||
| 144 | * @param bool $exit_on_error <p>Throw a 'Exception' when a query failed, otherwise it will return |
||
| 145 | * 'false'. Use false to disable it.</p> |
||
| 146 | * @param bool $echo_on_error <p>Echo the error if "checkForDev()" returns true. |
||
| 147 | * Use false to disable it.</p> |
||
| 148 | * @param string $logger_class_name |
||
| 149 | * @param string $logger_level <p>'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'</p> |
||
| 150 | * @param array $extra_config <p> |
||
| 151 | * 'session_to_db' => bool<br> |
||
| 152 | * 'doctrine' => \Doctrine\DBAL\Connection<br> |
||
| 153 | * 'socket' => string (path)<br> |
||
| 154 | * 'flags' => null|int<br> |
||
| 155 | * 'ssl' => bool<br> |
||
| 156 | * 'clientkey' => string (path)<br> |
||
| 157 | * 'clientcert' => string (path)<br> |
||
| 158 | * 'cacert' => string (path)<br> |
||
| 159 | * </p> |
||
| 160 | */ |
||
| 161 | 24 | private function __construct( |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Prevent the instance from being cloned. |
||
| 224 | * |
||
| 225 | * @return void |
||
| 226 | */ |
||
| 227 | private function __clone() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * __destruct |
||
| 233 | */ |
||
| 234 | public function __destruct() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @param string|null $sql |
||
| 244 | * @param array $bindings |
||
| 245 | * |
||
| 246 | * @return bool|DB|int|Result|string |
||
| 247 | * <p> |
||
| 248 | * "DB" by "$sql" === null<br /> |
||
| 249 | * "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 250 | * "int|string" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 251 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 252 | * "true" by e.g. "DROP"-queries<br /> |
||
| 253 | * "false" on error |
||
| 254 | * </p> |
||
| 255 | */ |
||
| 256 | 4 | public function __invoke(string $sql = null, array $bindings = []) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * __wakeup |
||
| 263 | * |
||
| 264 | * @return void |
||
| 265 | */ |
||
| 266 | 4 | public function __wakeup() |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Load the config from the constructor. |
||
| 273 | * |
||
| 274 | * @param string $hostname |
||
| 275 | * @param string $username |
||
| 276 | * @param string $password |
||
| 277 | * @param string $database |
||
| 278 | * @param int $port <p>default is (int)3306</p> |
||
| 279 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 280 | * @param bool $exit_on_error <p>Throw a 'Exception' when a query failed, otherwise it will return |
||
| 281 | * 'false'. Use false to disable it.</p> |
||
| 282 | * @param bool $echo_on_error <p>Echo the error if "checkForDev()" returns true. |
||
| 283 | * Use false to disable it.</p> |
||
| 284 | * @param string $logger_class_name |
||
| 285 | * @param string $logger_level |
||
| 286 | * @param array $extra_config <p> |
||
| 287 | * 'session_to_db' => bool<br> |
||
| 288 | * 'doctrine' => \Doctrine\DBAL\Connection<br> |
||
| 289 | * 'socket' => string (path)<br> |
||
| 290 | * 'flags' => null|int<br> |
||
| 291 | * 'ssl' => bool<br> |
||
| 292 | * 'clientkey' => string (path)<br> |
||
| 293 | * 'clientcert' => string (path)<br> |
||
| 294 | * 'cacert' => string (path)<br> |
||
| 295 | * </p> |
||
| 296 | * |
||
| 297 | * @return bool |
||
| 298 | */ |
||
| 299 | 24 | private function _loadConfig( |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Parses arrays with value pairs and generates SQL to use in queries. |
||
| 357 | * |
||
| 358 | * @param array $arrayPair |
||
| 359 | * @param string $glue <p>This is the separator.</p> |
||
| 360 | * |
||
| 361 | * @return string |
||
| 362 | * |
||
| 363 | * @internal |
||
| 364 | */ |
||
| 365 | 72 | public function _parseArrayPair(array $arrayPair, string $glue = ','): string |
|
| 528 | |||
| 529 | /** |
||
| 530 | * _parseQueryParams |
||
| 531 | * |
||
| 532 | * @param string $sql |
||
| 533 | * @param array $params |
||
| 534 | * |
||
| 535 | * @return array |
||
| 536 | * <p>with the keys -> 'sql', 'params'</p> |
||
| 537 | */ |
||
| 538 | 7 | private function _parseQueryParams(string $sql, array $params = []): array |
|
| 572 | |||
| 573 | /** |
||
| 574 | * Returns the SQL by replacing :placeholders with SQL-escaped values. |
||
| 575 | * |
||
| 576 | * @param string $sql <p>The SQL string.</p> |
||
| 577 | * @param array $params <p>An array of key-value bindings.</p> |
||
| 578 | * |
||
| 579 | * @return array |
||
| 580 | * <p>with the keys -> 'sql', 'params'</p> |
||
| 581 | */ |
||
| 582 | 10 | private function _parseQueryParamsByName(string $sql, array $params = []): array |
|
| 628 | |||
| 629 | /** |
||
| 630 | * Gets the number of affected rows in a previous MySQL operation. |
||
| 631 | * |
||
| 632 | * @return int |
||
| 633 | */ |
||
| 634 | 25 | public function affected_rows(): int |
|
| 646 | |||
| 647 | /** |
||
| 648 | * Begins a transaction, by turning off auto commit. |
||
| 649 | * |
||
| 650 | * @return bool |
||
| 651 | * <p>This will return true or false indicating success of transaction</p> |
||
| 652 | */ |
||
| 653 | 18 | View Code Duplication | public function beginTransaction(): bool |
| 685 | |||
| 686 | /** |
||
| 687 | * Clear the errors in "_debug->_errors". |
||
| 688 | * |
||
| 689 | * @return bool |
||
| 690 | */ |
||
| 691 | 18 | public function clearErrors(): bool |
|
| 695 | |||
| 696 | /** |
||
| 697 | * Closes a previously opened database connection. |
||
| 698 | * |
||
| 699 | * @return bool |
||
| 700 | * Will return "true", if the connection was closed, |
||
| 701 | * otherwise (e.g. if the connection was already closed) "false". |
||
| 702 | */ |
||
| 703 | 6 | public function close(): bool |
|
| 740 | |||
| 741 | /** |
||
| 742 | * Commits the current transaction and end the transaction. |
||
| 743 | * |
||
| 744 | * @return bool |
||
| 745 | * <p>bool true on success, false otherwise.</p> |
||
| 746 | */ |
||
| 747 | 9 | View Code Duplication | public function commit(): bool |
| 775 | |||
| 776 | /** |
||
| 777 | * Open a new connection to the MySQL server. |
||
| 778 | * |
||
| 779 | * @throws DBConnectException |
||
| 780 | * |
||
| 781 | * @return bool |
||
| 782 | */ |
||
| 783 | 21 | public function connect(): bool |
|
| 878 | |||
| 879 | /** |
||
| 880 | * @return bool |
||
| 881 | */ |
||
| 882 | private function connect_helper(): bool |
||
| 901 | |||
| 902 | /** |
||
| 903 | * Execute a "delete"-query. |
||
| 904 | * |
||
| 905 | * @param string $table |
||
| 906 | * @param array|string $where |
||
| 907 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 908 | * |
||
| 909 | * @throws QueryException |
||
| 910 | * |
||
| 911 | * @return false|int |
||
| 912 | * <p>false on error</p> |
||
| 913 | */ |
||
| 914 | 4 | View Code Duplication | public function delete(string $table, $where, string $databaseName = null) |
| 945 | |||
| 946 | /** |
||
| 947 | * Ends a transaction and commits if no errors, then ends autocommit. |
||
| 948 | * |
||
| 949 | * @return bool |
||
| 950 | * <p>This will return true or false indicating success of transactions.</p> |
||
| 951 | */ |
||
| 952 | 12 | public function endTransaction(): bool |
|
| 983 | |||
| 984 | /** |
||
| 985 | * Get all errors from "$this->errors". |
||
| 986 | * |
||
| 987 | * @return array|false |
||
| 988 | * <p>false === on errors</p> |
||
| 989 | */ |
||
| 990 | 12 | public function errors() |
|
| 996 | |||
| 997 | /** |
||
| 998 | * Escape: Use "mysqli_real_escape_string" and clean non UTF-8 chars + some extra optional stuff. |
||
| 999 | * |
||
| 1000 | * @param mixed $var bool: convert into "integer"<br /> |
||
| 1001 | * int: int (don't change it)<br /> |
||
| 1002 | * float: float (don't change it)<br /> |
||
| 1003 | * null: null (don't change it)<br /> |
||
| 1004 | * array: run escape() for every key => value<br /> |
||
| 1005 | * string: run UTF8::cleanup() and mysqli_real_escape_string()<br /> |
||
| 1006 | * @param bool $stripe_non_utf8 |
||
| 1007 | * @param bool $html_entity_decode |
||
| 1008 | * @param bool|null $convert_array <strong>false</strong> => Keep the array.<br /> |
||
| 1009 | * <strong>true</strong> => Convert to string var1,var2,var3...<br /> |
||
| 1010 | * <strong>null</strong> => Convert the array into null, every time. |
||
| 1011 | * |
||
| 1012 | * @return mixed |
||
| 1013 | */ |
||
| 1014 | 110 | public function escape($var = '', bool $stripe_non_utf8 = true, bool $html_entity_decode = false, $convert_array = false) |
|
| 1121 | |||
| 1122 | /** |
||
| 1123 | * Execute select/insert/update/delete sql-queries. |
||
| 1124 | * |
||
| 1125 | * @param string $query <p>sql-query</p> |
||
| 1126 | * @param bool $useCache optional <p>use cache?</p> |
||
| 1127 | * @param int $cacheTTL optional <p>cache-ttl in seconds</p> |
||
| 1128 | * @param DB|null $db optional <p>the database connection</p> |
||
| 1129 | * |
||
| 1130 | * @throws QueryException |
||
| 1131 | * |
||
| 1132 | * @return mixed |
||
| 1133 | * <ul> |
||
| 1134 | * <li>"array" by "<b>SELECT</b>"-queries</li> |
||
| 1135 | * <li>"int|string" (insert_id) by "<b>INSERT</b>"-queries</li> |
||
| 1136 | * <li>"int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries</li> |
||
| 1137 | * <li>"true" by e.g. "DROP"-queries</li> |
||
| 1138 | * <li>"false" on error</li> |
||
| 1139 | * </ul> |
||
| 1140 | */ |
||
| 1141 | 9 | public static function execSQL(string $query, bool $useCache = false, int $cacheTTL = 3600, self $db = null) |
|
| 1187 | |||
| 1188 | /** |
||
| 1189 | * Get all table-names via "SHOW TABLES". |
||
| 1190 | * |
||
| 1191 | * @return array |
||
| 1192 | */ |
||
| 1193 | 3 | public function getAllTables(): array |
|
| 1202 | |||
| 1203 | /** |
||
| 1204 | * @return array |
||
| 1205 | */ |
||
| 1206 | 49 | public function getConfig(): array |
|
| 1227 | |||
| 1228 | /** |
||
| 1229 | * @return Debug |
||
| 1230 | */ |
||
| 1231 | 10 | public function getDebugger(): Debug |
|
| 1235 | |||
| 1236 | /** |
||
| 1237 | * @return \Doctrine\DBAL\Connection|null|null |
||
| 1238 | */ |
||
| 1239 | 2 | public function getDoctrineConnection() |
|
| 1243 | |||
| 1244 | /** |
||
| 1245 | * @return \Doctrine\DBAL\Driver\Connection|false |
||
| 1246 | */ |
||
| 1247 | View Code Duplication | private function getDoctrinePDOConnection() |
|
| 1258 | |||
| 1259 | /** |
||
| 1260 | * Get errors from "$this->errors". |
||
| 1261 | * |
||
| 1262 | * @return array |
||
| 1263 | */ |
||
| 1264 | 3 | public function getErrors(): array |
|
| 1268 | |||
| 1269 | /** |
||
| 1270 | * @param string $hostname <p>Hostname of the mysql server</p> |
||
| 1271 | * @param string $username <p>Username for the mysql connection</p> |
||
| 1272 | * @param string $password <p>Password for the mysql connection</p> |
||
| 1273 | * @param string $database <p>Database for the mysql connection</p> |
||
| 1274 | * @param int $port <p>default is (int)3306</p> |
||
| 1275 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 1276 | * @param bool $exit_on_error <p>Throw a 'Exception' when a query failed, otherwise it will return |
||
| 1277 | * 'false'. Use false to disable it.</p> |
||
| 1278 | * @param bool $echo_on_error <p>Echo the error if "checkForDev()" returns true. |
||
| 1279 | * Use false to disable it.</p> |
||
| 1280 | * @param string $logger_class_name |
||
| 1281 | * @param string $logger_level <p>'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'</p> |
||
| 1282 | * @param array $extra_config <p> |
||
| 1283 | * 're_connect' => bool<br> |
||
| 1284 | * 'session_to_db' => bool<br> |
||
| 1285 | * 'doctrine' => \Doctrine\DBAL\Connection<br> |
||
| 1286 | * 'socket' => string (path)<br> |
||
| 1287 | * 'flags' => null|int<br> |
||
| 1288 | * 'ssl' => bool<br> |
||
| 1289 | * 'clientkey' => string (path)<br> |
||
| 1290 | * 'clientcert' => string (path)<br> |
||
| 1291 | * 'cacert' => string (path)<br> |
||
| 1292 | * </p> |
||
| 1293 | * |
||
| 1294 | * @return self |
||
| 1295 | */ |
||
| 1296 | 207 | public static function getInstance( |
|
| 1376 | |||
| 1377 | /** |
||
| 1378 | * @param \Doctrine\DBAL\Connection $doctrine |
||
| 1379 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 1380 | * @param bool $exit_on_error <p>Throw a 'Exception' when a query failed, otherwise it will |
||
| 1381 | * return 'false'. Use false to disable it.</p> |
||
| 1382 | * @param bool $echo_on_error <p>Echo the error if "checkForDev()" returns true. |
||
| 1383 | * Use false to disable it.</p> |
||
| 1384 | * @param string $logger_class_name |
||
| 1385 | * @param string $logger_level <p>'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'</p> |
||
| 1386 | * @param array $extra_config <p> |
||
| 1387 | * 're_connect' => bool<br> |
||
| 1388 | * 'session_to_db' => bool<br> |
||
| 1389 | * 'socket' => string (path)<br> |
||
| 1390 | * 'flags' => null|int<br> |
||
| 1391 | * 'ssl' => bool<br> |
||
| 1392 | * 'clientkey' => string (path)<br> |
||
| 1393 | * 'clientcert' => string (path)<br> |
||
| 1394 | * 'cacert' => string (path)<br> |
||
| 1395 | * </p> |
||
| 1396 | * |
||
| 1397 | * @return self |
||
| 1398 | */ |
||
| 1399 | 55 | public static function getInstanceDoctrineHelper( |
|
| 1424 | |||
| 1425 | /** |
||
| 1426 | * Get the mysqli-link (link identifier returned by mysqli-connect). |
||
| 1427 | * |
||
| 1428 | * @return \mysqli|null |
||
| 1429 | */ |
||
| 1430 | 15 | public function getLink() |
|
| 1434 | |||
| 1435 | /** |
||
| 1436 | * Get the current charset. |
||
| 1437 | * |
||
| 1438 | * @return string |
||
| 1439 | */ |
||
| 1440 | 3 | public function get_charset(): string |
|
| 1444 | |||
| 1445 | /** |
||
| 1446 | * Check if we are in a transaction. |
||
| 1447 | * |
||
| 1448 | * @return bool |
||
| 1449 | */ |
||
| 1450 | public function inTransaction(): bool |
||
| 1454 | |||
| 1455 | /** |
||
| 1456 | * Execute a "insert"-query. |
||
| 1457 | * |
||
| 1458 | * @param string $table |
||
| 1459 | * @param array $data |
||
| 1460 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1461 | * |
||
| 1462 | * @throws QueryException |
||
| 1463 | * |
||
| 1464 | * @return false|int|string |
||
| 1465 | * <p>false on error</p> |
||
| 1466 | */ |
||
| 1467 | 74 | public function insert(string $table, array $data = [], string $databaseName = null) |
|
| 1501 | |||
| 1502 | /** |
||
| 1503 | * Returns the auto generated id used in the last query. |
||
| 1504 | * |
||
| 1505 | * @return false|int|string |
||
| 1506 | */ |
||
| 1507 | 100 | public function insert_id() |
|
| 1520 | |||
| 1521 | /** |
||
| 1522 | * @return bool |
||
| 1523 | */ |
||
| 1524 | public function isDoctrineMySQLiConnection(): bool |
||
| 1535 | |||
| 1536 | /** |
||
| 1537 | * @return bool |
||
| 1538 | */ |
||
| 1539 | View Code Duplication | public function isDoctrinePDOConnection(): bool |
|
| 1550 | |||
| 1551 | /** |
||
| 1552 | * Check if db-connection is ready. |
||
| 1553 | * |
||
| 1554 | * @return bool |
||
| 1555 | */ |
||
| 1556 | 162 | public function isReady(): bool |
|
| 1560 | |||
| 1561 | /** |
||
| 1562 | * Get the last sql-error. |
||
| 1563 | * |
||
| 1564 | * @return false|string |
||
| 1565 | * <p>false === there was no error</p> |
||
| 1566 | */ |
||
| 1567 | 3 | public function lastError() |
|
| 1573 | |||
| 1574 | /** |
||
| 1575 | * Execute a sql-multi-query. |
||
| 1576 | * |
||
| 1577 | * @param string $sql |
||
| 1578 | * |
||
| 1579 | * @throws QueryException |
||
| 1580 | * |
||
| 1581 | * @return bool|Result[] |
||
| 1582 | * <ul> |
||
| 1583 | * <li>"Result"-Array by "<b>SELECT</b>"-queries</li> |
||
| 1584 | * <li>"bool" by only "<b>INSERT</b>"-queries</li> |
||
| 1585 | * <li>"bool" by only (affected_rows) by "<b>UPDATE / DELETE</b>"-queries</li> |
||
| 1586 | * <li>"bool" by only by e.g. "DROP"-queries</li> |
||
| 1587 | * </ul> |
||
| 1588 | */ |
||
| 1589 | 3 | public function multi_query(string $sql) |
|
| 1715 | |||
| 1716 | /** |
||
| 1717 | * Count number of rows found matching a specific query. |
||
| 1718 | * |
||
| 1719 | * @param string $query |
||
| 1720 | * |
||
| 1721 | * @return int |
||
| 1722 | */ |
||
| 1723 | 3 | public function num_rows(string $query): int |
|
| 1737 | |||
| 1738 | /** |
||
| 1739 | * Pings a server connection, or tries to reconnect |
||
| 1740 | * if the connection has gone down. |
||
| 1741 | * |
||
| 1742 | * @return bool |
||
| 1743 | */ |
||
| 1744 | 9 | public function ping(): bool |
|
| 1764 | |||
| 1765 | /** |
||
| 1766 | * Get a new "Prepare"-Object for your sql-query. |
||
| 1767 | * |
||
| 1768 | * @param string $query |
||
| 1769 | * |
||
| 1770 | * @return Prepare |
||
| 1771 | */ |
||
| 1772 | 2 | public function prepare(string $query): Prepare |
|
| 1776 | |||
| 1777 | /** |
||
| 1778 | * Execute a sql-query and return the result-array for select-statements. |
||
| 1779 | * |
||
| 1780 | * @param string $query |
||
| 1781 | * |
||
| 1782 | * @throws \Exception |
||
| 1783 | * |
||
| 1784 | * @return mixed |
||
| 1785 | * |
||
| 1786 | * @deprecated |
||
| 1787 | */ |
||
| 1788 | 3 | public static function qry(string $query) |
|
| 1813 | |||
| 1814 | /** |
||
| 1815 | * Execute a sql-query. |
||
| 1816 | * |
||
| 1817 | * example: |
||
| 1818 | * <code> |
||
| 1819 | * $sql = "INSERT INTO TABLE_NAME_HERE |
||
| 1820 | * SET |
||
| 1821 | * foo = :foo, |
||
| 1822 | * bar = :bar |
||
| 1823 | * "; |
||
| 1824 | * $insert_id = $db->query( |
||
| 1825 | * $sql, |
||
| 1826 | * [ |
||
| 1827 | * 'foo' => 1.1, |
||
| 1828 | * 'bar' => 1, |
||
| 1829 | * ] |
||
| 1830 | * ); |
||
| 1831 | * </code> |
||
| 1832 | * |
||
| 1833 | * @param string $sql <p>The sql query-string.</p> |
||
| 1834 | * @param array|bool $params <p> |
||
| 1835 | * "array" of sql-query-parameters<br/> |
||
| 1836 | * "false" if you don't need any parameter (default)<br/> |
||
| 1837 | * </p> |
||
| 1838 | * |
||
| 1839 | * @throws QueryException |
||
| 1840 | * |
||
| 1841 | * @return bool|int|Result|string |
||
| 1842 | * <p> |
||
| 1843 | * "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 1844 | * "int|string" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 1845 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1846 | * "true" by e.g. "DROP"-queries<br /> |
||
| 1847 | * "false" on error |
||
| 1848 | * </p> |
||
| 1849 | */ |
||
| 1850 | 140 | public function query(string $sql = '', $params = false) |
|
| 1991 | |||
| 1992 | /** |
||
| 1993 | * Error-handling for the sql-query. |
||
| 1994 | * |
||
| 1995 | * @param string $errorMessage |
||
| 1996 | * @param int $errorNumber |
||
| 1997 | * @param string $sql |
||
| 1998 | * @param array|bool $sqlParams <p>false if there wasn't any parameter</p> |
||
| 1999 | * @param bool $sqlMultiQuery |
||
| 2000 | * |
||
| 2001 | * @throws QueryException |
||
| 2002 | * @throws DBGoneAwayException |
||
| 2003 | * |
||
| 2004 | * @return false|mixed |
||
| 2005 | */ |
||
| 2006 | 39 | private function queryErrorHandling(string $errorMessage, int $errorNumber, string $sql, $sqlParams = false, bool $sqlMultiQuery = false) |
|
| 2050 | |||
| 2051 | /** |
||
| 2052 | * Quote && Escape e.g. a table name string. |
||
| 2053 | * |
||
| 2054 | * @param mixed $str |
||
| 2055 | * |
||
| 2056 | * @return string |
||
| 2057 | */ |
||
| 2058 | 86 | public function quote_string($str): string |
|
| 2071 | |||
| 2072 | /** |
||
| 2073 | * Reconnect to the MySQL-Server. |
||
| 2074 | * |
||
| 2075 | * @param bool $checkViaPing |
||
| 2076 | * |
||
| 2077 | * @return bool |
||
| 2078 | */ |
||
| 2079 | 7 | public function reconnect(bool $checkViaPing = false): bool |
|
| 2093 | |||
| 2094 | /** |
||
| 2095 | * Execute a "replace"-query. |
||
| 2096 | * |
||
| 2097 | * @param string $table |
||
| 2098 | * @param array $data |
||
| 2099 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 2100 | * |
||
| 2101 | * @throws QueryException |
||
| 2102 | * |
||
| 2103 | * @return false|int |
||
| 2104 | * <p>false on error</p> |
||
| 2105 | */ |
||
| 2106 | 3 | public function replace(string $table, array $data = [], string $databaseName = null) |
|
| 2148 | |||
| 2149 | /** |
||
| 2150 | * Rollback in a transaction and end the transaction. |
||
| 2151 | * |
||
| 2152 | * @return bool |
||
| 2153 | * <p>bool true on success, false otherwise.</p> |
||
| 2154 | */ |
||
| 2155 | 12 | View Code Duplication | public function rollback(): bool |
| 2184 | |||
| 2185 | /** |
||
| 2186 | * Try to secure a variable, so can you use it in sql-queries. |
||
| 2187 | * |
||
| 2188 | * <p> |
||
| 2189 | * <strong>int:</strong> (also strings that contains only an int-value)<br /> |
||
| 2190 | * 1. parse into "int" |
||
| 2191 | * </p><br /> |
||
| 2192 | * |
||
| 2193 | * <p> |
||
| 2194 | * <strong>float:</strong><br /> |
||
| 2195 | * 1. return "float" |
||
| 2196 | * </p><br /> |
||
| 2197 | * |
||
| 2198 | * <p> |
||
| 2199 | * <strong>string:</strong><br /> |
||
| 2200 | * 1. check if the string isn't a default mysql-time-function e.g. 'CURDATE()'<br /> |
||
| 2201 | * 2. trim '<br /> |
||
| 2202 | * 3. escape the string (and remove non utf-8 chars)<br /> |
||
| 2203 | * 4. trim ' again (because we maybe removed some chars)<br /> |
||
| 2204 | * 5. add ' around the new string<br /> |
||
| 2205 | * </p><br /> |
||
| 2206 | * |
||
| 2207 | * <p> |
||
| 2208 | * <strong>array:</strong><br /> |
||
| 2209 | * 1. return null |
||
| 2210 | * </p><br /> |
||
| 2211 | * |
||
| 2212 | * <p> |
||
| 2213 | * <strong>object:</strong><br /> |
||
| 2214 | * 1. return false |
||
| 2215 | * </p><br /> |
||
| 2216 | * |
||
| 2217 | * <p> |
||
| 2218 | * <strong>null:</strong><br /> |
||
| 2219 | * 1. return null |
||
| 2220 | * </p> |
||
| 2221 | * |
||
| 2222 | * @param mixed $var |
||
| 2223 | * @param bool|null $convert_array <strong>false</strong> => Keep the array.<br /> |
||
| 2224 | * <strong>true</strong> => Convert to string var1,var2,var3...<br /> |
||
| 2225 | * <strong>null</strong> => Convert the array into null, every time. |
||
| 2226 | * |
||
| 2227 | * @return mixed |
||
| 2228 | */ |
||
| 2229 | 87 | public function secure($var, $convert_array = true) |
|
| 2292 | |||
| 2293 | /** |
||
| 2294 | * Execute a "select"-query. |
||
| 2295 | * |
||
| 2296 | * @param string $table |
||
| 2297 | * @param array|string $where |
||
| 2298 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 2299 | * |
||
| 2300 | * @throws QueryException |
||
| 2301 | * |
||
| 2302 | * @return false|Result |
||
| 2303 | * <p>false on error</p> |
||
| 2304 | */ |
||
| 2305 | 62 | View Code Duplication | public function select(string $table, $where = '1=1', string $databaseName = null) |
| 2335 | |||
| 2336 | /** |
||
| 2337 | * Selects a different database than the one specified on construction. |
||
| 2338 | * |
||
| 2339 | * @param string $database <p>Database name to switch to.</p> |
||
| 2340 | * |
||
| 2341 | * @return bool |
||
| 2342 | * <p>bool true on success, false otherwise.</p> |
||
| 2343 | */ |
||
| 2344 | public function select_db(string $database): bool |
||
| 2363 | |||
| 2364 | /** |
||
| 2365 | * @param array $extra_config <p> |
||
| 2366 | * 'session_to_db' => bool<br> |
||
| 2367 | * 'doctrine' => \Doctrine\DBAL\Connection<br> |
||
| 2368 | * 'socket' => string (path)<br> |
||
| 2369 | * 'flags' => null|int<br> |
||
| 2370 | * 'ssl' => bool<br> |
||
| 2371 | * 'clientkey' => string (path)<br> |
||
| 2372 | * 'clientcert' => string (path)<br> |
||
| 2373 | * 'cacert' => string (path)<br> |
||
| 2374 | * </p> |
||
| 2375 | */ |
||
| 2376 | 24 | public function setConfigExtra(array $extra_config) |
|
| 2414 | |||
| 2415 | /** |
||
| 2416 | * Set the current charset. |
||
| 2417 | * |
||
| 2418 | * @param string $charset |
||
| 2419 | * |
||
| 2420 | * @return bool |
||
| 2421 | */ |
||
| 2422 | 15 | public function set_charset(string $charset): bool |
|
| 2463 | |||
| 2464 | /** |
||
| 2465 | * Set the option to convert null to "''" (empty string). |
||
| 2466 | * |
||
| 2467 | * Used in secure() => select(), insert(), update(), delete() |
||
| 2468 | * |
||
| 2469 | * @deprecated It's not recommended to convert NULL into an empty string! |
||
| 2470 | * |
||
| 2471 | * @param bool $bool |
||
| 2472 | * |
||
| 2473 | * @return self |
||
| 2474 | */ |
||
| 2475 | 3 | public function set_convert_null_to_empty_string(bool $bool): self |
|
| 2481 | |||
| 2482 | /** |
||
| 2483 | * Enables or disables internal report functions |
||
| 2484 | * |
||
| 2485 | * @see http://php.net/manual/en/function.mysqli-report.php |
||
| 2486 | * |
||
| 2487 | * @param int $flags <p> |
||
| 2488 | * <table> |
||
| 2489 | * Supported flags |
||
| 2490 | * <tr valign="top"> |
||
| 2491 | * <td>Name</td> |
||
| 2492 | * <td>Description</td> |
||
| 2493 | * </tr> |
||
| 2494 | * <tr valign="top"> |
||
| 2495 | * <td><b>MYSQLI_REPORT_OFF</b></td> |
||
| 2496 | * <td>Turns reporting off</td> |
||
| 2497 | * </tr> |
||
| 2498 | * <tr valign="top"> |
||
| 2499 | * <td><b>MYSQLI_REPORT_ERROR</b></td> |
||
| 2500 | * <td>Report errors from mysqli function calls</td> |
||
| 2501 | * </tr> |
||
| 2502 | * <tr valign="top"> |
||
| 2503 | * <td><b>MYSQLI_REPORT_STRICT</b></td> |
||
| 2504 | * <td> |
||
| 2505 | * Throw <b>mysqli_sql_exception</b> for errors |
||
| 2506 | * instead of warnings |
||
| 2507 | * </td> |
||
| 2508 | * </tr> |
||
| 2509 | * <tr valign="top"> |
||
| 2510 | * <td><b>MYSQLI_REPORT_INDEX</b></td> |
||
| 2511 | * <td>Report if no index or bad index was used in a query</td> |
||
| 2512 | * </tr> |
||
| 2513 | * <tr valign="top"> |
||
| 2514 | * <td><b>MYSQLI_REPORT_ALL</b></td> |
||
| 2515 | * <td>Set all options (report all)</td> |
||
| 2516 | * </tr> |
||
| 2517 | * </table> |
||
| 2518 | * </p> |
||
| 2519 | * |
||
| 2520 | * @return bool |
||
| 2521 | */ |
||
| 2522 | public function set_mysqli_report(int $flags): bool |
||
| 2534 | |||
| 2535 | /** |
||
| 2536 | * Show config errors by throw exceptions. |
||
| 2537 | * |
||
| 2538 | * @throws \InvalidArgumentException |
||
| 2539 | * |
||
| 2540 | * @return bool |
||
| 2541 | */ |
||
| 2542 | 24 | public function showConfigError(): bool |
|
| 2577 | |||
| 2578 | /** |
||
| 2579 | * alias: "beginTransaction()" |
||
| 2580 | */ |
||
| 2581 | 3 | public function startTransaction(): bool |
|
| 2585 | |||
| 2586 | /** |
||
| 2587 | * Determine if database table exists |
||
| 2588 | * |
||
| 2589 | * @param string $table |
||
| 2590 | * |
||
| 2591 | * @return bool |
||
| 2592 | */ |
||
| 2593 | 3 | public function table_exists(string $table): bool |
|
| 2603 | |||
| 2604 | /** |
||
| 2605 | * Execute a callback inside a transaction. |
||
| 2606 | * |
||
| 2607 | * @param \Closure $callback <p>The callback to run inside the transaction, if it's throws an "Exception" or if it's |
||
| 2608 | * returns "false", all SQL-statements in the callback will be rollbacked.</p> |
||
| 2609 | * |
||
| 2610 | * @return bool |
||
| 2611 | * <p>bool true on success, false otherwise.</p> |
||
| 2612 | */ |
||
| 2613 | 3 | public function transact($callback): bool |
|
| 2636 | |||
| 2637 | /** |
||
| 2638 | * Execute a "update"-query. |
||
| 2639 | * |
||
| 2640 | * @param string $table |
||
| 2641 | * @param array $data |
||
| 2642 | * @param array|string $where |
||
| 2643 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 2644 | * |
||
| 2645 | * @throws QueryException |
||
| 2646 | * |
||
| 2647 | * @return false|int |
||
| 2648 | * <p>false on error</p> |
||
| 2649 | */ |
||
| 2650 | 21 | public function update(string $table, array $data = [], $where = '1=1', string $databaseName = null) |
|
| 2694 | } |
||
| 2695 |
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.