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 | * The path name to the key file |
||
| 95 | * |
||
| 96 | * @var string |
||
| 97 | */ |
||
| 98 | private $_clientkey; |
||
| 99 | /** |
||
| 100 | * The path name to the certificate file |
||
| 101 | * |
||
| 102 | * @var string |
||
| 103 | */ |
||
| 104 | private $_clientcert; |
||
| 105 | /** |
||
| 106 | * The path name to the certificate authority file |
||
| 107 | * |
||
| 108 | * @var string |
||
| 109 | 10 | */ |
|
| 110 | private $_cacert; |
||
| 111 | 10 | ||
| 112 | /** |
||
| 113 | 10 | * @var Debug |
|
| 114 | */ |
||
| 115 | 10 | private $_debug; |
|
| 116 | 10 | ||
| 117 | 10 | /** |
|
| 118 | 10 | * __construct() |
|
| 119 | 10 | * |
|
| 120 | 10 | * @param string $hostname |
|
| 121 | 10 | * @param string $username |
|
| 122 | 10 | * @param string $password |
|
| 123 | 10 | * @param string $database |
|
| 124 | 10 | * @param int $port |
|
| 125 | 10 | * @param string $charset |
|
| 126 | * @param boolean|string $exit_on_error <p>Use a empty string "" or false to disable it.</p> |
||
| 127 | 10 | * @param boolean|string $echo_on_error <p>Use a empty string "" or false to disable it.</p> |
|
| 128 | * @param string $logger_class_name |
||
| 129 | 7 | * @param string $logger_level <p>'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'</p> |
|
| 130 | * @param array $extra_config <p> |
||
| 131 | 4 | * 'session_to_db' => false|true<br> |
|
| 132 | * 'socket' => 'string (path)'<br> |
||
| 133 | 4 | * 'ssl' => 'bool'<br> |
|
| 134 | * 'clientkey' => 'string (path)'<br> |
||
| 135 | 4 | * 'clientcert' => 'string (path)'<br> |
|
| 136 | * 'cacert' => 'string (path)'<br> |
||
| 137 | 4 | * </p> |
|
| 138 | */ |
||
| 139 | 4 | protected function __construct($hostname, $username, $password, $database, $port, $charset, $exit_on_error, $echo_on_error, $logger_class_name, $logger_level, $extra_config = array()) |
|
| 190 | 4 | ||
| 191 | 4 | /** |
|
| 192 | * Prevent the instance from being cloned. |
||
| 193 | * |
||
| 194 | 7 | * @return void |
|
| 195 | */ |
||
| 196 | private function __clone() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * __destruct |
||
| 202 | 10 | * |
|
| 203 | */ |
||
| 204 | 10 | public function __destruct() |
|
| 211 | 10 | ||
| 212 | 10 | /** |
|
| 213 | 10 | * __wakeup |
|
| 214 | * |
||
| 215 | 10 | * @return void |
|
| 216 | 10 | */ |
|
| 217 | public function __wakeup() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Load the config from the constructor. |
||
| 224 | * |
||
| 225 | * @param string $hostname |
||
| 226 | * @param string $username |
||
| 227 | * @param string $password |
||
| 228 | * @param string $database |
||
| 229 | * @param int|string $port <p>default is (int)3306</p> |
||
| 230 | 10 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
|
| 231 | * @param boolean|string $exit_on_error <p>Use a empty string "" or false to disable it.</p> |
||
| 232 | * @param boolean|string $echo_on_error <p>Use a empty string "" or false to disable it.</p> |
||
| 233 | * @param string $logger_class_name |
||
| 234 | 10 | * @param string $logger_level |
|
| 235 | 10 | * @param array $extra_config <p> |
|
| 236 | 9 | * 'session_to_db' => false|true<br> |
|
| 237 | 9 | * 'socket' => 'string (path)'<br> |
|
| 238 | 8 | * 'ssl' => 'bool'<br> |
|
| 239 | 10 | * 'clientkey' => 'string (path)'<br> |
|
| 240 | * 'clientcert' => 'string (path)'<br> |
||
| 241 | 3 | * 'cacert' => 'string (path)'<br> |
|
| 242 | 1 | * </p> |
|
| 243 | * |
||
| 244 | * @return bool |
||
| 245 | 2 | */ |
|
| 246 | 1 | private function _loadConfig($hostname, $username, $password, $database, $port, $charset, $exit_on_error, $echo_on_error, $logger_class_name, $logger_level, $extra_config) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Parses arrays with value pairs and generates SQL to use in queries. |
||
| 322 | * |
||
| 323 | * @param array $arrayPair |
||
| 324 | * @param string $glue <p>This is the separator.</p> |
||
| 325 | * |
||
| 326 | 2 | * @return string |
|
| 327 | * |
||
| 328 | 2 | * @internal |
|
| 329 | */ |
||
| 330 | public function _parseArrayPair($arrayPair, $glue = ',') |
||
| 476 | 33 | ||
| 477 | /** |
||
| 478 | 33 | * _parseQueryParams |
|
| 479 | 33 | * |
|
| 480 | 28 | * @param string $sql |
|
| 481 | 28 | * @param array $params |
|
| 482 | 24 | * |
|
| 483 | * @return string |
||
| 484 | */ |
||
| 485 | 33 | private function _parseQueryParams($sql, array $params) |
|
| 506 | 8 | ||
| 507 | 8 | /** |
|
| 508 | 8 | * Gets the number of affected rows in a previous MySQL operation. |
|
| 509 | * |
||
| 510 | 8 | * @return int |
|
| 511 | */ |
||
| 512 | public function affected_rows() |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Begins a transaction, by turning off auto commit. |
||
| 519 | * |
||
| 520 | 8 | * @return bool <p>This will return true or false indicating success of transaction</p> |
|
| 521 | */ |
||
| 522 | 8 | View Code Duplication | public function beginTransaction() |
| 538 | |||
| 539 | |||
| 540 | 3 | /** |
|
| 541 | 3 | * Clear the errors in "_debug->_errors". |
|
| 542 | 3 | * |
|
| 543 | * @return bool |
||
| 544 | 3 | */ |
|
| 545 | 3 | public function clearErrors() |
|
| 549 | 3 | ||
| 550 | 3 | /** |
|
| 551 | * Closes a previously opened database connection. |
||
| 552 | 3 | */ |
|
| 553 | public function close() |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Open a new connection to the MySQL server. |
||
| 565 | * |
||
| 566 | * @return bool |
||
| 567 | * |
||
| 568 | * @throws DBConnectException |
||
| 569 | */ |
||
| 570 | public function connect() |
||
| 641 | 33 | ||
| 642 | /** |
||
| 643 | 33 | * Execute a "delete"-query. |
|
| 644 | 2 | * |
|
| 645 | * @param string $table |
||
| 646 | * @param string|array $where |
||
| 647 | 33 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
|
| 648 | 2 | * |
|
| 649 | * @return false|int <p>false on error</p> |
||
| 650 | * |
||
| 651 | * * @throws QueryException |
||
| 652 | 33 | */ |
|
| 653 | 33 | View Code Duplication | public function delete($table, $where, $databaseName = null) |
| 680 | 5 | ||
| 681 | /** |
||
| 682 | * Ends a transaction and commits if no errors, then ends autocommit. |
||
| 683 | 33 | * |
|
| 684 | * @return bool <p>This will return true or false indicating success of transactions.</p> |
||
| 685 | */ |
||
| 686 | View Code Duplication | public function endTransaction() |
|
| 705 | |||
| 706 | /** |
||
| 707 | 1 | * Get all errors from "$this->_errors". |
|
| 708 | * |
||
| 709 | * @return array|false <p>false === on errors</p> |
||
| 710 | */ |
||
| 711 | 33 | public function errors() |
|
| 717 | 3 | ||
| 718 | 33 | /** |
|
| 719 | * Escape: Use "mysqli_real_escape_string" and clean non UTF-8 chars + some extra optional stuff. |
||
| 720 | * |
||
| 721 | * @param mixed $var boolean: convert into "integer"<br /> |
||
| 722 | 33 | * int: int (don't change it)<br /> |
|
| 723 | * float: float (don't change it)<br /> |
||
| 724 | 33 | * null: null (don't change it)<br /> |
|
| 725 | 9 | * array: run escape() for every key => value<br /> |
|
| 726 | 9 | * string: run UTF8::cleanup() and mysqli_real_escape_string()<br /> |
|
| 727 | * @param bool $stripe_non_utf8 |
||
| 728 | 33 | * @param bool $html_entity_decode |
|
| 729 | * @param bool|null $convert_array <strong>false</strong> => Keep the array.<br /> |
||
| 730 | 1 | * <strong>true</strong> => Convert to string var1,var2,var3...<br /> |
|
| 731 | 1 | * <strong>null</strong> => Convert the array into null, every time. |
|
| 732 | * |
||
| 733 | 33 | * @return mixed |
|
| 734 | */ |
||
| 735 | 33 | public function escape($var = '', $stripe_non_utf8 = true, $html_entity_decode = false, $convert_array = false) |
|
| 849 | |||
| 850 | /** |
||
| 851 | * Execute select/insert/update/delete sql-queries. |
||
| 852 | * |
||
| 853 | * @param string $query sql-query |
||
| 854 | * @param bool $useCache use cache? |
||
| 855 | * @param int $cacheTTL cache-ttl in seconds |
||
| 856 | 3 | * @param DB $db |
|
| 857 | * |
||
| 858 | * @return mixed "array" by "<b>SELECT</b>"-queries<br /> |
||
| 859 | 3 | * "int" (insert_id) by "<b>INSERT</b>"-queries<br /> |
|
| 860 | 3 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
|
| 861 | 3 | * "true" by e.g. "DROP"-queries<br /> |
|
| 862 | 3 | * "false" on error |
|
| 863 | * |
||
| 864 | * @throws QueryException |
||
| 865 | 3 | */ |
|
| 866 | public static function execSQL($query, $useCache = false, $cacheTTL = 3600, DB $db = null) |
||
| 915 | |||
| 916 | /** |
||
| 917 | 1 | * Get all table-names via "SHOW TABLES". |
|
| 918 | * |
||
| 919 | 1 | * @return array |
|
| 920 | */ |
||
| 921 | 1 | public function getAllTables() |
|
| 928 | 2 | ||
| 929 | /** |
||
| 930 | * @return Debug |
||
| 931 | 3 | */ |
|
| 932 | public function getDebugger() |
||
| 936 | |||
| 937 | /** |
||
| 938 | * Get errors from "$this->_errors". |
||
| 939 | 1 | * |
|
| 940 | * @return array |
||
| 941 | 1 | */ |
|
| 942 | public function getErrors() |
||
| 946 | |||
| 947 | /** |
||
| 948 | * getInstance() |
||
| 949 | * |
||
| 950 | * @param string $hostname |
||
| 951 | * @param string $username |
||
| 952 | * @param string $password |
||
| 953 | * @param string $database |
||
| 954 | * @param int|string $port <p>default is (int)3306</p> |
||
| 955 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 956 | * @param bool|string $exit_on_error <p>Use a empty string "" or false to disable it.</p> |
||
| 957 | * @param bool|string $echo_on_error <p>Use a empty string "" or false to disable it.</p> |
||
| 958 | * @param string $logger_class_name |
||
| 959 | * @param string $logger_level |
||
| 960 | * @param array $extra_config <p> |
||
| 961 | * 'session_to_db' => false|true<br> |
||
| 962 | * 'socket' => 'string (path)'<br> |
||
| 963 | * 'ssl' => 'bool'<br> |
||
| 964 | * 'clientkey' => 'string (path)'<br> |
||
| 965 | * 'clientcert' => 'string (path)'<br> |
||
| 966 | * 'cacert' => 'string (path)'<br> |
||
| 967 | * </p> |
||
| 968 | * |
||
| 969 | * @return \voku\db\DB |
||
| 970 | */ |
||
| 971 | public static function getInstance($hostname = '', $username = '', $password = '', $database = '', $port = '', $charset = '', $exit_on_error = '', $echo_on_error = '', $logger_class_name = '', $logger_level = '', $extra_config = array()) |
||
| 1027 | |||
| 1028 | 1 | /** |
|
| 1029 | 1 | * Get the mysqli-link (link identifier returned by mysqli-connect). |
|
| 1030 | * |
||
| 1031 | * @return \mysqli |
||
| 1032 | */ |
||
| 1033 | public function getLink() |
||
| 1037 | |||
| 1038 | 1 | /** |
|
| 1039 | 1 | * Get the current charset. |
|
| 1040 | * |
||
| 1041 | 1 | * @return string |
|
| 1042 | */ |
||
| 1043 | public function get_charset() |
||
| 1047 | |||
| 1048 | /** |
||
| 1049 | * Check if we are in a transaction. |
||
| 1050 | * |
||
| 1051 | * @return bool |
||
| 1052 | */ |
||
| 1053 | public function inTransaction() |
||
| 1057 | |||
| 1058 | 1 | /** |
|
| 1059 | * Execute a "insert"-query. |
||
| 1060 | * |
||
| 1061 | * @param string $table |
||
| 1062 | 1 | * @param array $data |
|
| 1063 | 1 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
|
| 1064 | * |
||
| 1065 | 1 | * @return false|int <p>false on error</p> |
|
| 1066 | * |
||
| 1067 | * @throws QueryException |
||
| 1068 | 1 | */ |
|
| 1069 | 1 | public function insert($table, array $data = array(), $databaseName = null) |
|
| 1096 | |||
| 1097 | /** |
||
| 1098 | * Returns the auto generated id used in the last query. |
||
| 1099 | * |
||
| 1100 | * @return int|string |
||
| 1101 | */ |
||
| 1102 | public function insert_id() |
||
| 1106 | |||
| 1107 | /** |
||
| 1108 | 1 | * Check if db-connection is ready. |
|
| 1109 | 1 | * |
|
| 1110 | * @return boolean |
||
| 1111 | */ |
||
| 1112 | 1 | public function isReady() |
|
| 1116 | |||
| 1117 | /** |
||
| 1118 | * Get the last sql-error. |
||
| 1119 | * |
||
| 1120 | * @return string|false <p>false === there was no error</p> |
||
| 1121 | */ |
||
| 1122 | 1 | public function lastError() |
|
| 1128 | |||
| 1129 | /** |
||
| 1130 | * Execute a sql-multi-query. |
||
| 1131 | * |
||
| 1132 | 4 | * @param string $sql |
|
| 1133 | * |
||
| 1134 | 4 | * @return false|Result[] "Result"-Array by "<b>SELECT</b>"-queries<br /> |
|
| 1135 | * "boolean" by only "<b>INSERT</b>"-queries<br /> |
||
| 1136 | 4 | * "boolean" by only (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
|
| 1137 | 1 | * "boolean" by only by e.g. "DROP"-queries<br /> |
|
| 1138 | * |
||
| 1139 | 1 | * @throws QueryException |
|
| 1140 | */ |
||
| 1141 | public function multi_query($sql) |
||
| 1198 | |||
| 1199 | /** |
||
| 1200 | * Pings a server connection, or tries to reconnect |
||
| 1201 | 2 | * if the connection has gone down. |
|
| 1202 | * |
||
| 1203 | 2 | * @return boolean |
|
| 1204 | */ |
||
| 1205 | 2 | public function ping() |
|
| 1219 | 2 | ||
| 1220 | 2 | /** |
|
| 1221 | * Selects a different database than the one specified on construction. |
||
| 1222 | 2 | * |
|
| 1223 | * @param string $database <p>Database name to switch to.</p> |
||
| 1224 | * |
||
| 1225 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 1226 | */ |
||
| 1227 | public function select_db($database) |
||
| 1235 | |||
| 1236 | 21 | /** |
|
| 1237 | * Get a new "Prepare"-Object for your sql-query. |
||
| 1238 | * |
||
| 1239 | 21 | * @param string $query |
|
| 1240 | * |
||
| 1241 | 21 | * @return Prepare |
|
| 1242 | 2 | */ |
|
| 1243 | public function prepare($query) |
||
| 1247 | 20 | ||
| 1248 | 3 | /** |
|
| 1249 | * Execute a sql-query and return the result-array for select-statements. |
||
| 1250 | 3 | * |
|
| 1251 | * @param string $query |
||
| 1252 | * @param DB $db |
||
| 1253 | 18 | * |
|
| 1254 | * @return mixed |
||
| 1255 | 18 | * @deprecated |
|
| 1256 | * @throws \Exception |
||
| 1257 | */ |
||
| 1258 | public static function qry($query, DB $db = null) |
||
| 1285 | 23 | ||
| 1286 | 23 | /** |
|
| 1287 | 23 | * Execute a sql-query. |
|
| 1288 | 23 | * |
|
| 1289 | * @param string $sql <p>The sql query-string.</p> |
||
| 1290 | 23 | * |
|
| 1291 | 2 | * @param array|boolean $params <p> |
|
| 1292 | 2 | * "array" of sql-query-parameters<br/> |
|
| 1293 | * "false" if you don't need any parameter (default)<br/> |
||
| 1294 | 23 | * </p> |
|
| 1295 | 1 | * |
|
| 1296 | 1 | * @return bool|int|Result <p> |
|
| 1297 | * "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 1298 | 23 | * "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
|
| 1299 | 1 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
|
| 1300 | 1 | * "true" by e.g. "DROP"-queries<br /> |
|
| 1301 | * "false" on error |
||
| 1302 | 23 | * </p> |
|
| 1303 | 1 | * |
|
| 1304 | 1 | * @throws QueryException |
|
| 1305 | */ |
||
| 1306 | 23 | public function query($sql = '', $params = false) |
|
| 1373 | 23 | ||
| 1374 | 23 | /** |
|
| 1375 | 23 | * Error-handling for the sql-query. |
|
| 1376 | 23 | * |
|
| 1377 | * @param string $errorMessage |
||
| 1378 | 23 | * @param int $errorNumber |
|
| 1379 | 23 | * @param string $sql |
|
| 1380 | 23 | * @param array|bool $sqlParams <p>false if there wasn't any parameter</p> |
|
| 1381 | * @param bool $sqlMultiQuery |
||
| 1382 | 23 | * |
|
| 1383 | 23 | * @throws QueryException |
|
| 1384 | 23 | * @throws DBGoneAwayException |
|
| 1385 | * |
||
| 1386 | 23 | * @return bool |
|
| 1387 | 23 | */ |
|
| 1388 | 23 | private function queryErrorHandling($errorMessage, $errorNumber, $sql, $sqlParams = false, $sqlMultiQuery = false) |
|
| 1427 | |||
| 1428 | 26 | /** |
|
| 1429 | * Quote && Escape e.g. a table name string. |
||
| 1430 | 26 | * |
|
| 1431 | 26 | * @param string $str |
|
| 1432 | 26 | * |
|
| 1433 | 26 | * @return string |
|
| 1434 | 26 | */ |
|
| 1435 | public function quote_string($str) |
||
| 1448 | |||
| 1449 | 1 | /** |
|
| 1450 | * Reconnect to the MySQL-Server. |
||
| 1451 | * |
||
| 1452 | * @param bool $checkViaPing |
||
| 1453 | * |
||
| 1454 | * @return bool |
||
| 1455 | */ |
||
| 1456 | public function reconnect($checkViaPing = false) |
||
| 1471 | 1 | ||
| 1472 | /** |
||
| 1473 | * Execute a "replace"-query. |
||
| 1474 | 1 | * |
|
| 1475 | 1 | * @param string $table |
|
| 1476 | * @param array $data |
||
| 1477 | 1 | * @param null|string $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
|
| 1478 | * |
||
| 1479 | * @return false|int <p>false on error</p> |
||
| 1480 | * |
||
| 1481 | 1 | * @throws QueryException |
|
| 1482 | 1 | */ |
|
| 1483 | public function replace($table, array $data = array(), $databaseName = null) |
||
| 1524 | |||
| 1525 | 1 | /** |
|
| 1526 | * Rollback in a transaction and end the transaction. |
||
| 1527 | * |
||
| 1528 | 6 | * @return bool <p>Boolean true on success, false otherwise.</p> |
|
| 1529 | 2 | */ |
|
| 1530 | View Code Duplication | public function rollback() |
|
| 1543 | |||
| 1544 | 6 | ||
| 1545 | /** |
||
| 1546 | * Commits the current transaction and end the transaction. |
||
| 1547 | * |
||
| 1548 | 6 | * @return bool <p>Boolean true on success, false otherwise.</p> |
|
| 1549 | */ |
||
| 1550 | 6 | View Code Duplication | public function commit() |
| 1563 | |||
| 1564 | 2 | /** |
|
| 1565 | * Execute a callback inside a transaction. |
||
| 1566 | * |
||
| 1567 | 2 | * @param callback $callback The callback to run inside the transaction |
|
| 1568 | * |
||
| 1569 | 2 | * @return bool Boolean true on success, false otherwise |
|
| 1570 | 1 | */ |
|
| 1571 | public function transact($callback) |
||
| 1582 | |||
| 1583 | 2 | /** |
|
| 1584 | * Try to secure a variable, so can you use it in sql-queries. |
||
| 1585 | * |
||
| 1586 | * <p> |
||
| 1587 | 2 | * <strong>int:</strong> (also strings that contains only an int-value)<br /> |
|
| 1588 | * 1. parse into "int" |
||
| 1589 | 2 | * </p><br /> |
|
| 1590 | * |
||
| 1591 | * <p> |
||
| 1592 | * <strong>float:</strong><br /> |
||
| 1593 | * 1. return "float" |
||
| 1594 | * </p><br /> |
||
| 1595 | * |
||
| 1596 | * <p> |
||
| 1597 | * <strong>string:</strong><br /> |
||
| 1598 | * 1. check if the string isn't a default mysql-time-function e.g. 'CURDATE()'<br /> |
||
| 1599 | * 2. trim whitespace<br /> |
||
| 1600 | * 3. trim '<br /> |
||
| 1601 | * 4. escape the string (and remove non utf-8 chars)<br /> |
||
| 1602 | * 5. trim ' again (because we maybe removed some chars)<br /> |
||
| 1603 | 20 | * 6. add ' around the new string<br /> |
|
| 1604 | * </p><br /> |
||
| 1605 | * |
||
| 1606 | 20 | * <p> |
|
| 1607 | * <strong>array:</strong><br /> |
||
| 1608 | 20 | * 1. return null |
|
| 1609 | 1 | * </p><br /> |
|
| 1610 | * |
||
| 1611 | 1 | * <p> |
|
| 1612 | * <strong>object:</strong><br /> |
||
| 1613 | * 1. return false |
||
| 1614 | 20 | * </p><br /> |
|
| 1615 | 5 | * |
|
| 1616 | 20 | * <p> |
|
| 1617 | 16 | * <strong>null:</strong><br /> |
|
| 1618 | 16 | * 1. return null |
|
| 1619 | 1 | * </p> |
|
| 1620 | * |
||
| 1621 | * @param mixed $var |
||
| 1622 | 20 | * |
|
| 1623 | * @return mixed |
||
| 1624 | */ |
||
| 1625 | public function secure($var) |
||
| 1651 | |||
| 1652 | /** |
||
| 1653 | * Execute a "select"-query. |
||
| 1654 | * |
||
| 1655 | * @param string $table |
||
| 1656 | * @param string|array $where |
||
| 1657 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1658 | * |
||
| 1659 | * @return false|Result <p>false on error</p> |
||
| 1660 | * |
||
| 1661 | * @throws QueryException |
||
| 1662 | */ |
||
| 1663 | View Code Duplication | public function select($table, $where = '1=1', $databaseName = null) |
|
| 1690 | |||
| 1691 | 2 | /** |
|
| 1692 | 2 | * Set the current charset. |
|
| 1693 | * |
||
| 1694 | * @param string $charset |
||
| 1695 | * |
||
| 1696 | * @return bool |
||
| 1697 | */ |
||
| 1698 | public function set_charset($charset) |
||
| 1720 | |||
| 1721 | /** |
||
| 1722 | * Set the option to convert null to "''" (empty string). |
||
| 1723 | * |
||
| 1724 | * Used in secure() => select(), insert(), update(), delete() |
||
| 1725 | * |
||
| 1726 | * @param $bool |
||
| 1727 | */ |
||
| 1728 | public function set_convert_null_to_empty_string($bool) |
||
| 1732 | |||
| 1733 | /** |
||
| 1734 | * Enables or disables internal report functions |
||
| 1735 | * |
||
| 1736 | * @link http://php.net/manual/en/function.mysqli-report.php |
||
| 1737 | * |
||
| 1738 | * @param int $flags <p> |
||
| 1739 | * <table> |
||
| 1740 | * Supported flags |
||
| 1741 | * <tr valign="top"> |
||
| 1742 | * <td>Name</td> |
||
| 1743 | * <td>Description</td> |
||
| 1744 | * </tr> |
||
| 1745 | * <tr valign="top"> |
||
| 1746 | * <td><b>MYSQLI_REPORT_OFF</b></td> |
||
| 1747 | * <td>Turns reporting off</td> |
||
| 1748 | * </tr> |
||
| 1749 | * <tr valign="top"> |
||
| 1750 | * <td><b>MYSQLI_REPORT_ERROR</b></td> |
||
| 1751 | * <td>Report errors from mysqli function calls</td> |
||
| 1752 | * </tr> |
||
| 1753 | * <tr valign="top"> |
||
| 1754 | * <td><b>MYSQLI_REPORT_STRICT</b></td> |
||
| 1755 | * <td> |
||
| 1756 | * Throw <b>mysqli_sql_exception</b> for errors |
||
| 1757 | * instead of warnings |
||
| 1758 | * </td> |
||
| 1759 | * </tr> |
||
| 1760 | * <tr valign="top"> |
||
| 1761 | * <td><b>MYSQLI_REPORT_INDEX</b></td> |
||
| 1762 | * <td>Report if no index or bad index was used in a query</td> |
||
| 1763 | * </tr> |
||
| 1764 | * <tr valign="top"> |
||
| 1765 | * <td><b>MYSQLI_REPORT_ALL</b></td> |
||
| 1766 | * <td>Set all options (report all)</td> |
||
| 1767 | * </tr> |
||
| 1768 | * </table> |
||
| 1769 | * </p> |
||
| 1770 | * |
||
| 1771 | * @return bool |
||
| 1772 | */ |
||
| 1773 | public function set_mysqli_report($flags) |
||
| 1777 | |||
| 1778 | /** |
||
| 1779 | * Show config errors by throw exceptions. |
||
| 1780 | * |
||
| 1781 | * @return bool |
||
| 1782 | * |
||
| 1783 | * @throws \InvalidArgumentException |
||
| 1784 | */ |
||
| 1785 | public function showConfigError() |
||
| 1813 | |||
| 1814 | /** |
||
| 1815 | * alias: "beginTransaction()" |
||
| 1816 | */ |
||
| 1817 | public function startTransaction() |
||
| 1821 | |||
| 1822 | /** |
||
| 1823 | * Execute a "update"-query. |
||
| 1824 | * |
||
| 1825 | * @param string $table |
||
| 1826 | * @param array $data |
||
| 1827 | * @param array|string $where |
||
| 1828 | * @param null|string $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1829 | * |
||
| 1830 | * @return false|int <p>false on error</p> |
||
| 1831 | * |
||
| 1832 | * @throws QueryException |
||
| 1833 | */ |
||
| 1834 | public function update($table, array $data = array(), $where = '1=1', $databaseName = null) |
||
| 1869 | |||
| 1870 | } |
||
| 1871 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.