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 |
||
| 16 | final class DB |
||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var int |
||
| 21 | */ |
||
| 22 | public $query_count = 0; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var \mysqli |
||
| 26 | */ |
||
| 27 | private $link = false; |
||
| 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 bool |
||
| 76 | */ |
||
| 77 | private $session_to_db = false; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var bool |
||
| 81 | */ |
||
| 82 | private $_in_transaction = false; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var bool |
||
| 86 | */ |
||
| 87 | private $_convert_null_to_empty_string = false; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var bool |
||
| 91 | */ |
||
| 92 | private $_ssl = false; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * The path name to the key file |
||
| 96 | * |
||
| 97 | * @var string |
||
| 98 | */ |
||
| 99 | private $_clientkey; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * The path name to the certificate file |
||
| 103 | * |
||
| 104 | * @var string |
||
| 105 | */ |
||
| 106 | private $_clientcert; |
||
| 107 | |||
| 108 | /** |
||
| 109 | 10 | * The path name to the certificate authority file |
|
| 110 | * |
||
| 111 | 10 | * @var string |
|
| 112 | */ |
||
| 113 | 10 | private $_cacert; |
|
| 114 | |||
| 115 | 10 | /** |
|
| 116 | 10 | * @var Debug |
|
| 117 | 10 | */ |
|
| 118 | 10 | private $_debug; |
|
| 119 | 10 | ||
| 120 | 10 | /** |
|
| 121 | 10 | * __construct() |
|
| 122 | 10 | * |
|
| 123 | 10 | * @param string $hostname |
|
| 124 | 10 | * @param string $username |
|
| 125 | 10 | * @param string $password |
|
| 126 | * @param string $database |
||
| 127 | 10 | * @param int $port |
|
| 128 | * @param string $charset |
||
| 129 | 7 | * @param boolean|string $exit_on_error <p>Use a empty string "" or false to disable it.</p> |
|
| 130 | * @param boolean|string $echo_on_error <p>Use a empty string "" or false to disable it.</p> |
||
| 131 | 4 | * @param string $logger_class_name |
|
| 132 | * @param string $logger_level <p>'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'</p> |
||
| 133 | 4 | * @param array $extra_config <p> |
|
| 134 | * 'session_to_db' => false|true<br> |
||
| 135 | 4 | * 'socket' => 'string (path)'<br> |
|
| 136 | * 'ssl' => 'bool'<br> |
||
| 137 | 4 | * 'clientkey' => 'string (path)'<br> |
|
| 138 | * 'clientcert' => 'string (path)'<br> |
||
| 139 | 4 | * 'cacert' => 'string (path)'<br> |
|
| 140 | * </p> |
||
| 141 | 4 | */ |
|
| 142 | protected function __construct($hostname, $username, $password, $database, $port, $charset, $exit_on_error, $echo_on_error, $logger_class_name, $logger_level, $extra_config = array()) |
||
| 193 | |||
| 194 | 7 | /** |
|
| 195 | * Prevent the instance from being cloned. |
||
| 196 | * |
||
| 197 | * @return void |
||
| 198 | 10 | */ |
|
| 199 | private function __clone() |
||
| 202 | 10 | ||
| 203 | /** |
||
| 204 | 10 | * __destruct |
|
| 205 | 10 | * |
|
| 206 | */ |
||
| 207 | 10 | public function __destruct() |
|
| 214 | |||
| 215 | 10 | /** |
|
| 216 | 10 | * __wakeup |
|
| 217 | * |
||
| 218 | 10 | * @return void |
|
| 219 | */ |
||
| 220 | 10 | public function __wakeup() |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Load the config from the constructor. |
||
| 227 | * |
||
| 228 | * @param string $hostname |
||
| 229 | * @param string $username |
||
| 230 | 10 | * @param string $password |
|
| 231 | * @param string $database |
||
| 232 | * @param int|string $port <p>default is (int)3306</p> |
||
| 233 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 234 | 10 | * @param boolean|string $exit_on_error <p>Use a empty string "" or false to disable it.</p> |
|
| 235 | 10 | * @param boolean|string $echo_on_error <p>Use a empty string "" or false to disable it.</p> |
|
| 236 | 9 | * @param string $logger_class_name |
|
| 237 | 9 | * @param string $logger_level |
|
| 238 | 8 | * @param array $extra_config <p> |
|
| 239 | 10 | * 'session_to_db' => false|true<br> |
|
| 240 | * 'socket' => 'string (path)'<br> |
||
| 241 | 3 | * 'ssl' => 'bool'<br> |
|
| 242 | 1 | * 'clientkey' => 'string (path)'<br> |
|
| 243 | * 'clientcert' => 'string (path)'<br> |
||
| 244 | * 'cacert' => 'string (path)'<br> |
||
| 245 | 2 | * </p> |
|
| 246 | 1 | * |
|
| 247 | * @return bool |
||
| 248 | */ |
||
| 249 | 1 | private function _loadConfig($hostname, $username, $password, $database, $port, $charset, $exit_on_error, $echo_on_error, $logger_class_name, $logger_level, $extra_config) |
|
| 250 | 1 | { |
|
| 251 | $this->hostname = (string)$hostname; |
||
| 252 | $this->username = (string)$username; |
||
| 253 | $this->password = (string)$password; |
||
| 254 | $this->database = (string)$database; |
||
| 255 | |||
| 256 | 7 | if ($charset) { |
|
| 257 | $this->charset = (string)$charset; |
||
| 258 | } |
||
| 259 | |||
| 260 | if ($port) { |
||
| 261 | $this->port = (int)$port; |
||
| 262 | } else { |
||
| 263 | /** @noinspection PhpUsageOfSilenceOperatorInspection */ |
||
| 264 | /** @noinspection UsageOfSilenceOperatorInspection */ |
||
| 265 | $this->port = (int)@ini_get('mysqli.default_port'); |
||
| 266 | 9 | } |
|
| 267 | |||
| 268 | 9 | // fallback |
|
| 269 | 1 | if (!$this->port) { |
|
| 270 | $this->port = 3306; |
||
| 271 | } |
||
| 272 | 9 | ||
| 273 | if (!$this->socket) { |
||
| 274 | 9 | /** @noinspection PhpUsageOfSilenceOperatorInspection */ |
|
| 275 | $this->socket = @ini_get('mysqli.default_socket'); |
||
| 276 | 9 | } |
|
| 277 | 9 | ||
| 278 | 9 | if ($exit_on_error === true || $exit_on_error === false) { |
|
| 279 | $this->_debug->setExitOnError($exit_on_error); |
||
| 280 | } |
||
| 281 | 9 | ||
| 282 | 9 | if ($echo_on_error === true || $echo_on_error === false) { |
|
| 283 | 9 | $this->_debug->setEchoOnError($echo_on_error); |
|
| 284 | 9 | } |
|
| 285 | 9 | ||
| 286 | 9 | $this->_debug->setLoggerClassName($logger_class_name); |
|
| 287 | 9 | $this->_debug->setLoggerLevel($logger_level); |
|
| 288 | 9 | ||
| 289 | 9 | if (is_array($extra_config) === true) { |
|
| 290 | 9 | ||
| 291 | 3 | if (isset($extra_config['session_to_db'])) { |
|
| 292 | 3 | $this->session_to_db = (boolean)$extra_config['session_to_db']; |
|
| 293 | 3 | } |
|
| 294 | |||
| 295 | 6 | if (isset($extra_config['socket'])) { |
|
| 296 | $this->socket = $extra_config['socket']; |
||
| 297 | 6 | } |
|
| 298 | |||
| 299 | if (isset($extra_config['ssl'])) { |
||
| 300 | $this->_ssl = $extra_config['ssl']; |
||
| 301 | } |
||
| 302 | |||
| 303 | if (isset($extra_config['clientkey'])) { |
||
| 304 | 6 | $this->_clientkey = $extra_config['clientkey']; |
|
| 305 | } |
||
| 306 | 6 | ||
| 307 | if (isset($extra_config['clientcert'])) { |
||
| 308 | $this->_clientcert = $extra_config['clientcert']; |
||
| 309 | } |
||
| 310 | |||
| 311 | if (isset($extra_config['cacert'])) { |
||
| 312 | $this->_cacert = $extra_config['cacert']; |
||
| 313 | } |
||
| 314 | 45 | ||
| 315 | } else { |
||
|
|
|||
| 316 | 45 | // only for backward compatibility |
|
| 317 | //$this->session_to_db = (boolean)$extra_config; |
||
| 318 | } |
||
| 319 | |||
| 320 | return $this->showConfigError(); |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Parses arrays with value pairs and generates SQL to use in queries. |
||
| 325 | * |
||
| 326 | 2 | * @param array $arrayPair |
|
| 327 | * @param string $glue <p>This is the separator.</p> |
||
| 328 | 2 | * |
|
| 329 | * @return string |
||
| 330 | * |
||
| 331 | * @internal |
||
| 332 | */ |
||
| 333 | public function _parseArrayPair($arrayPair, $glue = ',') |
||
| 479 | 33 | ||
| 480 | 28 | /** |
|
| 481 | 28 | * _parseQueryParams |
|
| 482 | 24 | * |
|
| 483 | * @param string $sql |
||
| 484 | * @param array $params |
||
| 485 | 33 | * |
|
| 486 | * @return string |
||
| 487 | */ |
||
| 488 | 27 | private function _parseQueryParams($sql, array $params) |
|
| 509 | |||
| 510 | 8 | /** |
|
| 511 | * Gets the number of affected rows in a previous MySQL operation. |
||
| 512 | * |
||
| 513 | * @return int |
||
| 514 | */ |
||
| 515 | public function affected_rows() |
||
| 519 | |||
| 520 | 8 | /** |
|
| 521 | * Begins a transaction, by turning off auto commit. |
||
| 522 | 8 | * |
|
| 523 | * @return bool <p>This will return true or false indicating success of transaction</p> |
||
| 524 | */ |
||
| 525 | public function beginTransaction() |
||
| 543 | |||
| 544 | 3 | /** |
|
| 545 | 3 | * Clear the errors in "_debug->_errors". |
|
| 546 | 3 | * |
|
| 547 | 3 | * @return bool |
|
| 548 | 3 | */ |
|
| 549 | 3 | public function clearErrors() |
|
| 553 | |||
| 554 | /** |
||
| 555 | * Closes a previously opened database connection. |
||
| 556 | * |
||
| 557 | * @return bool |
||
| 558 | */ |
||
| 559 | public function close() |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Open a new connection to the MySQL server. |
||
| 577 | * |
||
| 578 | * @return bool |
||
| 579 | * |
||
| 580 | * @throws DBConnectException |
||
| 581 | */ |
||
| 582 | public function connect() |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Execute a "delete"-query. |
||
| 633 | * |
||
| 634 | * @param string $table |
||
| 635 | * @param string|array $where |
||
| 636 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 637 | * |
||
| 638 | * @return false|int <p>false on error</p> |
||
| 639 | * |
||
| 640 | * * @throws QueryException |
||
| 641 | 33 | */ |
|
| 642 | View Code Duplication | public function delete($table, $where, $databaseName = null) |
|
| 669 | 33 | ||
| 670 | /** |
||
| 671 | * Ends a transaction and commits if no errors, then ends autocommit. |
||
| 672 | * |
||
| 673 | 24 | * @return bool <p>This will return true or false indicating success of transactions.</p> |
|
| 674 | */ |
||
| 675 | public function endTransaction() |
||
| 689 | |||
| 690 | /** |
||
| 691 | 1 | * Get all errors from "$this->_errors". |
|
| 692 | 1 | * |
|
| 693 | * @return array|false <p>false === on errors</p> |
||
| 694 | 1 | */ |
|
| 695 | 1 | public function errors() |
|
| 701 | 1 | ||
| 702 | 1 | /** |
|
| 703 | * Escape: Use "mysqli_real_escape_string" and clean non UTF-8 chars + some extra optional stuff. |
||
| 704 | 1 | * |
|
| 705 | * @param mixed $var boolean: convert into "integer"<br /> |
||
| 706 | * int: int (don't change it)<br /> |
||
| 707 | 1 | * float: float (don't change it)<br /> |
|
| 708 | * null: null (don't change it)<br /> |
||
| 709 | * array: run escape() for every key => value<br /> |
||
| 710 | * string: run UTF8::cleanup() and mysqli_real_escape_string()<br /> |
||
| 711 | 33 | * @param bool $stripe_non_utf8 |
|
| 712 | * @param bool $html_entity_decode |
||
| 713 | * @param bool|null $convert_array <strong>false</strong> => Keep the array.<br /> |
||
| 714 | 3 | * <strong>true</strong> => Convert to string var1,var2,var3...<br /> |
|
| 715 | 3 | * <strong>null</strong> => Convert the array into null, every time. |
|
| 716 | 3 | * |
|
| 717 | 3 | * @return mixed |
|
| 718 | 33 | */ |
|
| 719 | public function escape($var = '', $stripe_non_utf8 = true, $html_entity_decode = false, $convert_array = false) |
||
| 833 | |||
| 834 | 3 | /** |
|
| 835 | * Execute select/insert/update/delete sql-queries. |
||
| 836 | 3 | * |
|
| 837 | * @param string $query sql-query |
||
| 838 | 3 | * @param bool $useCache use cache? |
|
| 839 | 2 | * @param int $cacheTTL cache-ttl in seconds |
|
| 840 | 2 | * |
|
| 841 | * @return mixed "array" by "<b>SELECT</b>"-queries<br /> |
||
| 842 | 3 | * "int" (insert_id) by "<b>INSERT</b>"-queries<br /> |
|
| 843 | 3 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
|
| 844 | 3 | * "true" by e.g. "DROP"-queries<br /> |
|
| 845 | 3 | * "false" on error |
|
| 846 | * |
||
| 847 | 3 | * @throws QueryException |
|
| 848 | */ |
||
| 849 | public static function execSQL($query, $useCache = false, $cacheTTL = 3600) |
||
| 896 | |||
| 897 | 1 | /** |
|
| 898 | 1 | * Get all table-names via "SHOW TABLES". |
|
| 899 | 1 | * |
|
| 900 | 1 | * @return array |
|
| 901 | 1 | */ |
|
| 902 | public function getAllTables() |
||
| 909 | |||
| 910 | 3 | /** |
|
| 911 | * @return Debug |
||
| 912 | 1 | */ |
|
| 913 | public function getDebugger() |
||
| 917 | 1 | ||
| 918 | /** |
||
| 919 | 1 | * Get errors from "$this->_errors". |
|
| 920 | * |
||
| 921 | 1 | * @return array |
|
| 922 | 1 | */ |
|
| 923 | 1 | public function getErrors() |
|
| 927 | 1 | ||
| 928 | 2 | /** |
|
| 929 | * getInstance() |
||
| 930 | * |
||
| 931 | 3 | * @param string $hostname |
|
| 932 | * @param string $username |
||
| 933 | * @param string $password |
||
| 934 | * @param string $database |
||
| 935 | * @param int|string $port <p>default is (int)3306</p> |
||
| 936 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 937 | * @param bool|string $exit_on_error <p>Use a empty string "" or false to disable it.</p> |
||
| 938 | * @param bool|string $echo_on_error <p>Use a empty string "" or false to disable it.</p> |
||
| 939 | 1 | * @param string $logger_class_name |
|
| 940 | * @param string $logger_level |
||
| 941 | 1 | * @param array $extra_config <p> |
|
| 942 | * 'session_to_db' => false|true<br> |
||
| 943 | * 'socket' => 'string (path)'<br> |
||
| 944 | * 'ssl' => 'bool'<br> |
||
| 945 | * 'clientkey' => 'string (path)'<br> |
||
| 946 | * 'clientcert' => 'string (path)'<br> |
||
| 947 | * 'cacert' => 'string (path)'<br> |
||
| 948 | * </p> |
||
| 949 | * |
||
| 950 | * @return \voku\db\DB |
||
| 951 | */ |
||
| 952 | public static function getInstance($hostname = '', $username = '', $password = '', $database = '', $port = '', $charset = '', $exit_on_error = '', $echo_on_error = '', $logger_class_name = '', $logger_level = '', $extra_config = array()) |
||
| 1008 | 7 | ||
| 1009 | /** |
||
| 1010 | * Get the mysqli-link (link identifier returned by mysqli-connect). |
||
| 1011 | 7 | * |
|
| 1012 | * @return \mysqli |
||
| 1013 | */ |
||
| 1014 | 7 | public function getLink() |
|
| 1018 | |||
| 1019 | /** |
||
| 1020 | * Get the current charset. |
||
| 1021 | * |
||
| 1022 | * @return string |
||
| 1023 | */ |
||
| 1024 | public function get_charset() |
||
| 1028 | 1 | ||
| 1029 | 1 | /** |
|
| 1030 | * Check if we are in a transaction. |
||
| 1031 | * |
||
| 1032 | * @return bool |
||
| 1033 | */ |
||
| 1034 | public function inTransaction() |
||
| 1038 | 1 | ||
| 1039 | 1 | /** |
|
| 1040 | * Execute a "insert"-query. |
||
| 1041 | 1 | * |
|
| 1042 | * @param string $table |
||
| 1043 | * @param array $data |
||
| 1044 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1045 | * |
||
| 1046 | * @return false|int <p>false on error</p> |
||
| 1047 | * |
||
| 1048 | * @throws QueryException |
||
| 1049 | */ |
||
| 1050 | public function insert($table, array $data = array(), $databaseName = null) |
||
| 1077 | |||
| 1078 | 1 | /** |
|
| 1079 | * Returns the auto generated id used in the last query. |
||
| 1080 | 1 | * |
|
| 1081 | 1 | * @return int|string |
|
| 1082 | 1 | */ |
|
| 1083 | 1 | public function insert_id() |
|
| 1087 | 1 | ||
| 1088 | 1 | /** |
|
| 1089 | 1 | * Check if db-connection is ready. |
|
| 1090 | * |
||
| 1091 | * @return boolean |
||
| 1092 | */ |
||
| 1093 | 1 | public function isReady() |
|
| 1097 | |||
| 1098 | /** |
||
| 1099 | * Get the last sql-error. |
||
| 1100 | * |
||
| 1101 | * @return string|false <p>false === there was no error</p> |
||
| 1102 | */ |
||
| 1103 | public function lastError() |
||
| 1109 | 1 | ||
| 1110 | /** |
||
| 1111 | * Execute a sql-multi-query. |
||
| 1112 | 1 | * |
|
| 1113 | 1 | * @param string $sql |
|
| 1114 | * |
||
| 1115 | * @return false|Result[] "Result"-Array by "<b>SELECT</b>"-queries<br /> |
||
| 1116 | * "boolean" by only "<b>INSERT</b>"-queries<br /> |
||
| 1117 | * "boolean" by only (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1118 | * "boolean" by only by e.g. "DROP"-queries<br /> |
||
| 1119 | * |
||
| 1120 | * @throws QueryException |
||
| 1121 | */ |
||
| 1122 | 1 | public function multi_query($sql) |
|
| 1186 | 1 | ||
| 1187 | 1 | /** |
|
| 1188 | * Pings a server connection, or tries to reconnect |
||
| 1189 | * if the connection has gone down. |
||
| 1190 | 2 | * |
|
| 1191 | 2 | * @return boolean |
|
| 1192 | */ |
||
| 1193 | 2 | public function ping() |
|
| 1207 | |||
| 1208 | /** |
||
| 1209 | * Selects a different database than the one specified on construction. |
||
| 1210 | * |
||
| 1211 | 2 | * @param string $database <p>Database name to switch to.</p> |
|
| 1212 | * |
||
| 1213 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 1214 | 2 | */ |
|
| 1215 | public function select_db($database) |
||
| 1223 | |||
| 1224 | /** |
||
| 1225 | * Get a new "Prepare"-Object for your sql-query. |
||
| 1226 | * |
||
| 1227 | * @param string $query |
||
| 1228 | * |
||
| 1229 | * @return Prepare |
||
| 1230 | */ |
||
| 1231 | public function prepare($query) |
||
| 1235 | |||
| 1236 | 21 | /** |
|
| 1237 | * Execute a sql-query and return the result-array for select-statements. |
||
| 1238 | * |
||
| 1239 | 21 | * @param $query |
|
| 1240 | * |
||
| 1241 | 21 | * @return mixed |
|
| 1242 | 2 | * @deprecated |
|
| 1243 | * @throws \Exception |
||
| 1244 | 2 | */ |
|
| 1245 | public static function qry($query) |
||
| 1270 | |||
| 1271 | /** |
||
| 1272 | * Execute a sql-query. |
||
| 1273 | * |
||
| 1274 | 23 | * @param string $sql <p>The sql query-string.</p> |
|
| 1275 | * |
||
| 1276 | * @param array|boolean $params <p> |
||
| 1277 | 23 | * "array" of sql-query-parameters<br/> |
|
| 1278 | * "false" if you don't need any parameter (default)<br/> |
||
| 1279 | * </p> |
||
| 1280 | 23 | * |
|
| 1281 | * @return bool|int|Result <p> |
||
| 1282 | * "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 1283 | * "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 1284 | 23 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
|
| 1285 | 23 | * "true" by e.g. "DROP"-queries<br /> |
|
| 1286 | 23 | * "false" on error |
|
| 1287 | 23 | * </p> |
|
| 1288 | 23 | * |
|
| 1289 | * @throws QueryException |
||
| 1290 | 23 | */ |
|
| 1291 | 2 | public function query($sql = '', $params = false) |
|
| 1364 | |||
| 1365 | 2 | /** |
|
| 1366 | 23 | * Error-handling for the sql-query. |
|
| 1367 | * |
||
| 1368 | * @param string $errorMessage |
||
| 1369 | 23 | * @param int $errorNumber |
|
| 1370 | 23 | * @param string $sql |
|
| 1371 | 23 | * @param array|bool $sqlParams <p>false if there wasn't any parameter</p> |
|
| 1372 | * @param bool $sqlMultiQuery |
||
| 1373 | 23 | * |
|
| 1374 | 23 | * @throws QueryException |
|
| 1375 | 23 | * @throws DBGoneAwayException |
|
| 1376 | 23 | * |
|
| 1377 | * @return bool |
||
| 1378 | 23 | */ |
|
| 1379 | 23 | private function queryErrorHandling($errorMessage, $errorNumber, $sql, $sqlParams = false, $sqlMultiQuery = false) |
|
| 1419 | |||
| 1420 | /** |
||
| 1421 | * Quote && Escape e.g. a table name string. |
||
| 1422 | * |
||
| 1423 | * @param string $str |
||
| 1424 | * |
||
| 1425 | * @return string |
||
| 1426 | */ |
||
| 1427 | public function quote_string($str) |
||
| 1440 | |||
| 1441 | /** |
||
| 1442 | * Reconnect to the MySQL-Server. |
||
| 1443 | * |
||
| 1444 | * @param bool $checkViaPing |
||
| 1445 | * |
||
| 1446 | * @return bool |
||
| 1447 | 1 | */ |
|
| 1448 | public function reconnect($checkViaPing = false) |
||
| 1463 | 1 | ||
| 1464 | /** |
||
| 1465 | * Execute a "replace"-query. |
||
| 1466 | 1 | * |
|
| 1467 | * @param string $table |
||
| 1468 | 1 | * @param array $data |
|
| 1469 | 1 | * @param null|string $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
|
| 1470 | * |
||
| 1471 | 1 | * @return false|int <p>false on error</p> |
|
| 1472 | * |
||
| 1473 | * @throws QueryException |
||
| 1474 | 1 | */ |
|
| 1475 | 1 | public function replace($table, array $data = array(), $databaseName = null) |
|
| 1516 | |||
| 1517 | 6 | /** |
|
| 1518 | * Rollback in a transaction and end the transaction. |
||
| 1519 | * |
||
| 1520 | 6 | * @return bool <p>Boolean true on success, false otherwise.</p> |
|
| 1521 | */ |
||
| 1522 | 6 | public function rollback() |
|
| 1534 | 6 | ||
| 1535 | /** |
||
| 1536 | 6 | * Commits the current transaction and end the transaction. |
|
| 1537 | 2 | * |
|
| 1538 | 6 | * @return bool <p>Boolean true on success, false otherwise.</p> |
|
| 1539 | 4 | */ |
|
| 1540 | 4 | public function commit() |
|
| 1553 | |||
| 1554 | /** |
||
| 1555 | * Execute a callback inside a transaction. |
||
| 1556 | * |
||
| 1557 | * @param callback $callback The callback to run inside the transaction |
||
| 1558 | * |
||
| 1559 | * @return bool Boolean true on success, false otherwise |
||
| 1560 | */ |
||
| 1561 | public function transact($callback) |
||
| 1572 | 1 | ||
| 1573 | /** |
||
| 1574 | * Try to secure a variable, so can you use it in sql-queries. |
||
| 1575 | 2 | * |
|
| 1576 | 1 | * <p> |
|
| 1577 | 2 | * <strong>int:</strong> (also strings that contains only an int-value)<br /> |
|
| 1578 | 2 | * 1. parse into "int" |
|
| 1579 | 2 | * </p><br /> |
|
| 1580 | 1 | * |
|
| 1581 | * <p> |
||
| 1582 | * <strong>float:</strong><br /> |
||
| 1583 | 2 | * 1. return "float" |
|
| 1584 | * </p><br /> |
||
| 1585 | * |
||
| 1586 | * <p> |
||
| 1587 | 2 | * <strong>string:</strong><br /> |
|
| 1588 | * 1. check if the string isn't a default mysql-time-function e.g. 'CURDATE()'<br /> |
||
| 1589 | 2 | * 2. trim whitespace<br /> |
|
| 1590 | * 3. trim '<br /> |
||
| 1591 | * 4. escape the string (and remove non utf-8 chars)<br /> |
||
| 1592 | * 5. trim ' again (because we maybe removed some chars)<br /> |
||
| 1593 | * 6. add ' around the new string<br /> |
||
| 1594 | * </p><br /> |
||
| 1595 | * |
||
| 1596 | * <p> |
||
| 1597 | * <strong>array:</strong><br /> |
||
| 1598 | * 1. return null |
||
| 1599 | * </p><br /> |
||
| 1600 | * |
||
| 1601 | * <p> |
||
| 1602 | * <strong>object:</strong><br /> |
||
| 1603 | 20 | * 1. return false |
|
| 1604 | * </p><br /> |
||
| 1605 | * |
||
| 1606 | 20 | * <p> |
|
| 1607 | * <strong>null:</strong><br /> |
||
| 1608 | 20 | * 1. return null |
|
| 1609 | 1 | * </p> |
|
| 1610 | * |
||
| 1611 | 1 | * @param mixed $var |
|
| 1612 | * |
||
| 1613 | * @return mixed |
||
| 1614 | 20 | */ |
|
| 1615 | 5 | public function secure($var) |
|
| 1641 | |||
| 1642 | /** |
||
| 1643 | * Execute a "select"-query. |
||
| 1644 | * |
||
| 1645 | * @param string $table |
||
| 1646 | 9 | * @param string|array $where |
|
| 1647 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1648 | 9 | * |
|
| 1649 | * @return false|Result <p>false on error</p> |
||
| 1650 | * |
||
| 1651 | * @throws QueryException |
||
| 1652 | */ |
||
| 1653 | View Code Duplication | public function select($table, $where = '1=1', $databaseName = null) |
|
| 1680 | |||
| 1681 | /** |
||
| 1682 | * Set the current charset. |
||
| 1683 | * |
||
| 1684 | * @param string $charset |
||
| 1685 | * |
||
| 1686 | * @return bool |
||
| 1687 | */ |
||
| 1688 | public function set_charset($charset) |
||
| 1710 | |||
| 1711 | /** |
||
| 1712 | * Set the option to convert null to "''" (empty string). |
||
| 1713 | * |
||
| 1714 | * Used in secure() => select(), insert(), update(), delete() |
||
| 1715 | * |
||
| 1716 | * @param $bool |
||
| 1717 | */ |
||
| 1718 | public function set_convert_null_to_empty_string($bool) |
||
| 1722 | |||
| 1723 | /** |
||
| 1724 | * Enables or disables internal report functions |
||
| 1725 | * |
||
| 1726 | * @link http://php.net/manual/en/function.mysqli-report.php |
||
| 1727 | * |
||
| 1728 | * @param int $flags <p> |
||
| 1729 | * <table> |
||
| 1730 | * Supported flags |
||
| 1731 | * <tr valign="top"> |
||
| 1732 | * <td>Name</td> |
||
| 1733 | * <td>Description</td> |
||
| 1734 | * </tr> |
||
| 1735 | * <tr valign="top"> |
||
| 1736 | * <td><b>MYSQLI_REPORT_OFF</b></td> |
||
| 1737 | * <td>Turns reporting off</td> |
||
| 1738 | * </tr> |
||
| 1739 | * <tr valign="top"> |
||
| 1740 | * <td><b>MYSQLI_REPORT_ERROR</b></td> |
||
| 1741 | * <td>Report errors from mysqli function calls</td> |
||
| 1742 | * </tr> |
||
| 1743 | * <tr valign="top"> |
||
| 1744 | * <td><b>MYSQLI_REPORT_STRICT</b></td> |
||
| 1745 | * <td> |
||
| 1746 | * Throw <b>mysqli_sql_exception</b> for errors |
||
| 1747 | * instead of warnings |
||
| 1748 | * </td> |
||
| 1749 | * </tr> |
||
| 1750 | * <tr valign="top"> |
||
| 1751 | * <td><b>MYSQLI_REPORT_INDEX</b></td> |
||
| 1752 | * <td>Report if no index or bad index was used in a query</td> |
||
| 1753 | * </tr> |
||
| 1754 | * <tr valign="top"> |
||
| 1755 | * <td><b>MYSQLI_REPORT_ALL</b></td> |
||
| 1756 | * <td>Set all options (report all)</td> |
||
| 1757 | * </tr> |
||
| 1758 | * </table> |
||
| 1759 | * </p> |
||
| 1760 | * |
||
| 1761 | * @return bool |
||
| 1762 | */ |
||
| 1763 | public function set_mysqli_report($flags) |
||
| 1767 | |||
| 1768 | /** |
||
| 1769 | * Show config errors by throw exceptions. |
||
| 1770 | * |
||
| 1771 | * @return bool |
||
| 1772 | * |
||
| 1773 | * @throws \InvalidArgumentException |
||
| 1774 | */ |
||
| 1775 | public function showConfigError() |
||
| 1803 | |||
| 1804 | /** |
||
| 1805 | * alias: "beginTransaction()" |
||
| 1806 | */ |
||
| 1807 | public function startTransaction() |
||
| 1811 | |||
| 1812 | /** |
||
| 1813 | * Execute a "update"-query. |
||
| 1814 | * |
||
| 1815 | * @param string $table |
||
| 1816 | * @param array $data |
||
| 1817 | * @param array|string $where |
||
| 1818 | * @param null|string $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1819 | * |
||
| 1820 | * @return false|int <p>false on error</p> |
||
| 1821 | * |
||
| 1822 | * @throws QueryException |
||
| 1823 | */ |
||
| 1824 | public function update($table, array $data = array(), $where = '1=1', $databaseName = null) |
||
| 1859 | |||
| 1860 | /** |
||
| 1861 | * @param null|string $sql |
||
| 1862 | * @param array $bindings |
||
| 1863 | * |
||
| 1864 | * @return bool|int|Result|DB <p> |
||
| 1865 | * "DB" by "$sql" === null<br /> |
||
| 1866 | * "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 1867 | * "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 1868 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1869 | * "true" by e.g. "DROP"-queries<br /> |
||
| 1870 | * "false" on error |
||
| 1871 | * </p> |
||
| 1872 | */ |
||
| 1873 | public function __invoke($sql = null, array $bindings = array()) |
||
| 1877 | |||
| 1878 | } |
||
| 1879 |
This check looks for the
elsebranches ofifstatements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
elsebranches can be removed.could be turned into
This is much more concise to read.