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 bool|string $exit_on_error <p>Throw a 'Exception' when a query failed, otherwise it will return 'false'. |
|
| 130 | * Use a empty string "" or false to disable it.</p> |
||
| 131 | 4 | * @param bool|string $echo_on_error <p>Echo the error if "checkForDev()" returns true. |
|
| 132 | * Use a empty string "" or false to disable it.</p> |
||
| 133 | 4 | * @param string $logger_class_name |
|
| 134 | * @param string $logger_level <p>'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'</p> |
||
| 135 | 4 | * @param array $extra_config <p> |
|
| 136 | * 'session_to_db' => false|true<br> |
||
| 137 | 4 | * 'socket' => 'string (path)'<br> |
|
| 138 | * 'ssl' => 'bool'<br> |
||
| 139 | 4 | * 'clientkey' => 'string (path)'<br> |
|
| 140 | * 'clientcert' => 'string (path)'<br> |
||
| 141 | 4 | * 'cacert' => 'string (path)'<br> |
|
| 142 | * </p> |
||
| 143 | 4 | */ |
|
| 144 | protected function __construct($hostname, $username, $password, $database, $port, $charset, $exit_on_error, $echo_on_error, $logger_class_name, $logger_level, $extra_config = array()) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Prevent the instance from being cloned. |
||
| 198 | 10 | * |
|
| 199 | * @return void |
||
| 200 | */ |
||
| 201 | private function __clone() |
||
| 204 | 10 | ||
| 205 | 10 | /** |
|
| 206 | * __destruct |
||
| 207 | 10 | * |
|
| 208 | 10 | */ |
|
| 209 | 10 | public function __destruct() |
|
| 216 | 10 | ||
| 217 | /** |
||
| 218 | 10 | * @param null|string $sql |
|
| 219 | * @param array $bindings |
||
| 220 | 10 | * |
|
| 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 | 10 | public function __invoke($sql = null, array $bindings = array()) |
|
| 234 | 10 | ||
| 235 | 10 | /** |
|
| 236 | 9 | * __wakeup |
|
| 237 | 9 | * |
|
| 238 | 8 | * @return void |
|
| 239 | 10 | */ |
|
| 240 | public function __wakeup() |
||
| 244 | |||
| 245 | 2 | /** |
|
| 246 | 1 | * Load the config from the constructor. |
|
| 247 | * |
||
| 248 | * @param string $hostname |
||
| 249 | 1 | * @param string $username |
|
| 250 | 1 | * @param string $password |
|
| 251 | * @param string $database |
||
| 252 | * @param int|string $port <p>default is (int)3306</p> |
||
| 253 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 254 | * @param bool|string $exit_on_error <p>Throw a 'Exception' when a query failed, otherwise it will return 'false'. |
||
| 255 | * Use a empty string "" or false to disable it.</p> |
||
| 256 | 7 | * @param bool|string $echo_on_error <p>Echo the error if "checkForDev()" returns true. |
|
| 257 | * Use a empty string "" or 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 | 9 | * 'cacert' => 'string (path)'<br> |
|
| 267 | * </p> |
||
| 268 | 9 | * |
|
| 269 | 1 | * @return bool |
|
| 270 | */ |
||
| 271 | private function _loadConfig($hostname, $username, $password, $database, $port, $charset, $exit_on_error, $echo_on_error, $logger_class_name, $logger_level, $extra_config) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Parses arrays with value pairs and generates SQL to use in queries. |
||
| 347 | * |
||
| 348 | * @param array $arrayPair |
||
| 349 | * @param string $glue <p>This is the separator.</p> |
||
| 350 | * |
||
| 351 | * @return string |
||
| 352 | * |
||
| 353 | * @internal |
||
| 354 | */ |
||
| 355 | public function _parseArrayPair($arrayPair, $glue = ',') |
||
| 501 | |||
| 502 | 22 | /** |
|
| 503 | * _parseQueryParams |
||
| 504 | * |
||
| 505 | * @param string $sql |
||
| 506 | 8 | * @param array $params |
|
| 507 | 8 | * |
|
| 508 | 8 | * @return array <p>with the keys -> 'sql', 'params'</p> |
|
| 509 | */ |
||
| 510 | 8 | private function _parseQueryParams($sql, array $params) |
|
| 541 | 3 | ||
| 542 | 3 | /** |
|
| 543 | * Gets the number of affected rows in a previous MySQL operation. |
||
| 544 | 3 | * |
|
| 545 | 3 | * @return int |
|
| 546 | 3 | */ |
|
| 547 | 3 | public function affected_rows() |
|
| 551 | |||
| 552 | 3 | /** |
|
| 553 | * Begins a transaction, by turning off auto commit. |
||
| 554 | * |
||
| 555 | * @return bool <p>This will return true or false indicating success of transaction</p> |
||
| 556 | */ |
||
| 557 | View Code Duplication | public function beginTransaction() |
|
| 574 | |||
| 575 | /** |
||
| 576 | * Clear the errors in "_debug->_errors". |
||
| 577 | * |
||
| 578 | * @return bool |
||
| 579 | */ |
||
| 580 | public function clearErrors() |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Closes a previously opened database connection. |
||
| 587 | */ |
||
| 588 | public function close() |
||
| 596 | |||
| 597 | 26 | /** |
|
| 598 | * Commits the current transaction and end the transaction. |
||
| 599 | * |
||
| 600 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 601 | 26 | */ |
|
| 602 | 26 | View Code Duplication | public function commit() |
| 616 | |||
| 617 | 26 | /** |
|
| 618 | 22 | * Open a new connection to the MySQL server. |
|
| 619 | 22 | * |
|
| 620 | * @return bool |
||
| 621 | 26 | * |
|
| 622 | * @throws DBConnectException |
||
| 623 | */ |
||
| 624 | public function connect() |
||
| 699 | 1 | ||
| 700 | /** |
||
| 701 | 1 | * Execute a "delete"-query. |
|
| 702 | 1 | * |
|
| 703 | * @param string $table |
||
| 704 | 1 | * @param string|array $where |
|
| 705 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 706 | * |
||
| 707 | 1 | * @return false|int <p>false on error</p> |
|
| 708 | * |
||
| 709 | * * @throws QueryException |
||
| 710 | */ |
||
| 711 | 33 | View Code Duplication | public function delete($table, $where, $databaseName = null) |
| 738 | |||
| 739 | /** |
||
| 740 | * Ends a transaction and commits if no errors, then ends autocommit. |
||
| 741 | 3 | * |
|
| 742 | * @return bool <p>This will return true or false indicating success of transactions.</p> |
||
| 743 | */ |
||
| 744 | View Code Duplication | public function endTransaction() |
|
| 764 | |||
| 765 | /** |
||
| 766 | * Get all errors from "$this->_errors". |
||
| 767 | * |
||
| 768 | * @return array|false <p>false === on errors</p> |
||
| 769 | */ |
||
| 770 | public function errors() |
||
| 776 | |||
| 777 | /** |
||
| 778 | * Returns the SQL by replacing :placeholders with SQL-escaped values. |
||
| 779 | * |
||
| 780 | * @param mixed $sql <p>The SQL string.</p> |
||
| 781 | 8 | * @param array $params <p>An array of key-value bindings.</p> |
|
| 782 | * |
||
| 783 | 8 | * @return array <p>with the keys -> 'sql', 'params'</p> |
|
| 784 | */ |
||
| 785 | public function _parseQueryParamsByName($sql, array $params = array()) |
||
| 822 | 8 | ||
| 823 | /** |
||
| 824 | 8 | * Escape: Use "mysqli_real_escape_string" and clean non UTF-8 chars + some extra optional stuff. |
|
| 825 | * |
||
| 826 | * @param mixed $var boolean: convert into "integer"<br /> |
||
| 827 | * int: int (don't change it)<br /> |
||
| 828 | * float: float (don't change it)<br /> |
||
| 829 | * null: null (don't change it)<br /> |
||
| 830 | * array: run escape() for every key => value<br /> |
||
| 831 | * string: run UTF8::cleanup() and mysqli_real_escape_string()<br /> |
||
| 832 | * @param bool $stripe_non_utf8 |
||
| 833 | * @param bool $html_entity_decode |
||
| 834 | 3 | * @param bool|null $convert_array <strong>false</strong> => Keep the array.<br /> |
|
| 835 | * <strong>true</strong> => Convert to string var1,var2,var3...<br /> |
||
| 836 | 3 | * <strong>null</strong> => Convert the array into null, every time. |
|
| 837 | * |
||
| 838 | 3 | * @return mixed |
|
| 839 | 2 | */ |
|
| 840 | 2 | public function escape($var = '', $stripe_non_utf8 = true, $html_entity_decode = false, $convert_array = false) |
|
| 954 | |||
| 955 | /** |
||
| 956 | * Execute select/insert/update/delete sql-queries. |
||
| 957 | * |
||
| 958 | * @param string $query <p>sql-query</p> |
||
| 959 | * @param bool $useCache <p>use cache?</p> |
||
| 960 | * @param int $cacheTTL <p>cache-ttl in seconds</p> |
||
| 961 | * @param DB $db optional <p>the database connection</p> |
||
| 962 | * |
||
| 963 | * @return mixed "array" by "<b>SELECT</b>"-queries<br /> |
||
| 964 | * "int" (insert_id) by "<b>INSERT</b>"-queries<br /> |
||
| 965 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 966 | * "true" by e.g. "DROP"-queries<br /> |
||
| 967 | * "false" on error |
||
| 968 | * |
||
| 969 | * @throws QueryException |
||
| 970 | */ |
||
| 971 | public static function execSQL($query, $useCache = false, $cacheTTL = 3600, DB $db = null) |
||
| 1020 | |||
| 1021 | /** |
||
| 1022 | * Get all table-names via "SHOW TABLES". |
||
| 1023 | * |
||
| 1024 | * @return array |
||
| 1025 | */ |
||
| 1026 | 1 | public function getAllTables() |
|
| 1033 | |||
| 1034 | /** |
||
| 1035 | * @return Debug |
||
| 1036 | 1 | */ |
|
| 1037 | public function getDebugger() |
||
| 1041 | 1 | ||
| 1042 | /** |
||
| 1043 | * Get errors from "$this->_errors". |
||
| 1044 | * |
||
| 1045 | * @return array |
||
| 1046 | */ |
||
| 1047 | public function getErrors() |
||
| 1051 | |||
| 1052 | /** |
||
| 1053 | * getInstance() |
||
| 1054 | * |
||
| 1055 | * @param string $hostname |
||
| 1056 | 1 | * @param string $username |
|
| 1057 | * @param string $password |
||
| 1058 | 1 | * @param string $database |
|
| 1059 | * @param int|string $port <p>default is (int)3306</p> |
||
| 1060 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 1061 | * @param bool|string $exit_on_error <p>Throw a 'Exception' when a query failed, otherwise it will return 'false'. |
||
| 1062 | 1 | * Use a empty string "" or false to disable it.</p> |
|
| 1063 | 1 | * @param bool|string $echo_on_error <p>Echo the error if "checkForDev()" returns true. |
|
| 1064 | * Use a empty string "" or false to disable it.</p> |
||
| 1065 | 1 | * @param string $logger_class_name |
|
| 1066 | * @param string $logger_level <p>'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'</p> |
||
| 1067 | * @param array $extra_config <p> |
||
| 1068 | 1 | * 'session_to_db' => false|true<br> |
|
| 1069 | 1 | * 'socket' => 'string (path)'<br> |
|
| 1070 | 1 | * 'ssl' => 'bool'<br> |
|
| 1071 | * 'clientkey' => 'string (path)'<br> |
||
| 1072 | 1 | * 'clientcert' => 'string (path)'<br> |
|
| 1073 | * 'cacert' => 'string (path)'<br> |
||
| 1074 | 1 | * </p> |
|
| 1075 | 1 | * |
|
| 1076 | 1 | * @return \voku\db\DB |
|
| 1077 | */ |
||
| 1078 | 1 | public static function getInstance($hostname = '', $username = '', $password = '', $database = '', $port = '', $charset = '', $exit_on_error = true, $echo_on_error = true, $logger_class_name = '', $logger_level = '', $extra_config = array()) |
|
| 1134 | 4 | ||
| 1135 | /** |
||
| 1136 | 4 | * Get the mysqli-link (link identifier returned by mysqli-connect). |
|
| 1137 | 1 | * |
|
| 1138 | * @return \mysqli |
||
| 1139 | 1 | */ |
|
| 1140 | public function getLink() |
||
| 1144 | |||
| 1145 | /** |
||
| 1146 | * Get the current charset. |
||
| 1147 | * |
||
| 1148 | 4 | * @return string |
|
| 1149 | 4 | */ |
|
| 1150 | public function get_charset() |
||
| 1154 | |||
| 1155 | /** |
||
| 1156 | * Check if we are in a transaction. |
||
| 1157 | * |
||
| 1158 | * @return bool |
||
| 1159 | 4 | */ |
|
| 1160 | public function inTransaction() |
||
| 1164 | |||
| 1165 | /** |
||
| 1166 | * Execute a "insert"-query. |
||
| 1167 | * |
||
| 1168 | * @param string $table |
||
| 1169 | 4 | * @param array $data |
|
| 1170 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1171 | 4 | * |
|
| 1172 | * @return false|int <p>false on error</p> |
||
| 1173 | * |
||
| 1174 | * @throws QueryException |
||
| 1175 | */ |
||
| 1176 | public function insert($table, array $data = array(), $databaseName = null) |
||
| 1203 | 2 | ||
| 1204 | /** |
||
| 1205 | 2 | * Returns the auto generated id used in the last query. |
|
| 1206 | * |
||
| 1207 | * @return int|string |
||
| 1208 | */ |
||
| 1209 | public function insert_id() |
||
| 1213 | |||
| 1214 | 2 | /** |
|
| 1215 | * Check if db-connection is ready. |
||
| 1216 | 2 | * |
|
| 1217 | 2 | * @return boolean |
|
| 1218 | 2 | */ |
|
| 1219 | 2 | public function isReady() |
|
| 1223 | |||
| 1224 | /** |
||
| 1225 | * Get the last sql-error. |
||
| 1226 | * |
||
| 1227 | * @return string|false <p>false === there was no error</p> |
||
| 1228 | */ |
||
| 1229 | public function lastError() |
||
| 1235 | |||
| 1236 | 21 | /** |
|
| 1237 | * Execute a sql-multi-query. |
||
| 1238 | * |
||
| 1239 | 21 | * @param string $sql |
|
| 1240 | * |
||
| 1241 | 21 | * @return false|Result[] "Result"-Array by "<b>SELECT</b>"-queries<br /> |
|
| 1242 | 2 | * "boolean" by only "<b>INSERT</b>"-queries<br /> |
|
| 1243 | * "boolean" by only (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1244 | 2 | * "boolean" by only by e.g. "DROP"-queries<br /> |
|
| 1245 | * |
||
| 1246 | * @throws QueryException |
||
| 1247 | 20 | */ |
|
| 1248 | 3 | public function multi_query($sql) |
|
| 1315 | 1 | ||
| 1316 | 1 | /** |
|
| 1317 | * Pings a server connection, or tries to reconnect |
||
| 1318 | 23 | * if the connection has gone down. |
|
| 1319 | 2 | * |
|
| 1320 | 2 | * @return boolean |
|
| 1321 | */ |
||
| 1322 | 23 | public function ping() |
|
| 1336 | 4 | ||
| 1337 | /** |
||
| 1338 | 23 | * Get a new "Prepare"-Object for your sql-query. |
|
| 1339 | 1 | * |
|
| 1340 | 1 | * @param string $query |
|
| 1341 | * |
||
| 1342 | 23 | * @return Prepare |
|
| 1343 | 1 | */ |
|
| 1344 | 1 | public function prepare($query) |
|
| 1348 | 2 | ||
| 1349 | /** |
||
| 1350 | 23 | * Execute a sql-query and return the result-array for select-statements. |
|
| 1351 | 1 | * |
|
| 1352 | 1 | * @param string $query |
|
| 1353 | * |
||
| 1354 | 23 | * @return mixed |
|
| 1355 | 2 | * @deprecated |
|
| 1356 | 2 | * @throws \Exception |
|
| 1357 | 2 | */ |
|
| 1358 | public static function qry($query) |
||
| 1383 | 23 | ||
| 1384 | 23 | /** |
|
| 1385 | * Execute a sql-query. |
||
| 1386 | 23 | * |
|
| 1387 | 23 | * @param string $sql <p>The sql query-string.</p> |
|
| 1388 | 23 | * |
|
| 1389 | * @param array|boolean $params <p> |
||
| 1390 | 23 | * "array" of sql-query-parameters<br/> |
|
| 1391 | 23 | * "false" if you don't need any parameter (default)<br/> |
|
| 1392 | * </p> |
||
| 1393 | 23 | * |
|
| 1394 | * @return bool|int|Result <p> |
||
| 1395 | 23 | * "Result" by "<b>SELECT</b>"-queries<br /> |
|
| 1396 | * "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 1397 | 23 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
|
| 1398 | 1 | * "true" by e.g. "DROP"-queries<br /> |
|
| 1399 | 23 | * "false" on error |
|
| 1400 | 23 | * </p> |
|
| 1401 | 23 | * |
|
| 1402 | * @throws QueryException |
||
| 1403 | 23 | */ |
|
| 1404 | 1 | public function query($sql = '', $params = false) |
|
| 1479 | |||
| 1480 | /** |
||
| 1481 | 1 | * Error-handling for the sql-query. |
|
| 1482 | 1 | * |
|
| 1483 | * @param string $errorMessage |
||
| 1484 | 1 | * @param int $errorNumber |
|
| 1485 | 1 | * @param string $sql |
|
| 1486 | * @param array|bool $sqlParams <p>false if there wasn't any parameter</p> |
||
| 1487 | 1 | * @param bool $sqlMultiQuery |
|
| 1488 | * |
||
| 1489 | * @throws QueryException |
||
| 1490 | 1 | * @throws DBGoneAwayException |
|
| 1491 | * |
||
| 1492 | 1 | * @return bool |
|
| 1493 | 1 | */ |
|
| 1494 | 1 | private function queryErrorHandling($errorMessage, $errorNumber, $sql, $sqlParams = false, $sqlMultiQuery = false) |
|
| 1539 | 4 | ||
| 1540 | 4 | /** |
|
| 1541 | 1 | * Quote && Escape e.g. a table name string. |
|
| 1542 | * |
||
| 1543 | * @param string $str |
||
| 1544 | 6 | * |
|
| 1545 | * @return string |
||
| 1546 | */ |
||
| 1547 | public function quote_string($str) |
||
| 1560 | |||
| 1561 | /** |
||
| 1562 | * Reconnect to the MySQL-Server. |
||
| 1563 | * |
||
| 1564 | 2 | * @param bool $checkViaPing |
|
| 1565 | * |
||
| 1566 | * @return bool |
||
| 1567 | 2 | */ |
|
| 1568 | public function reconnect($checkViaPing = false) |
||
| 1583 | 2 | ||
| 1584 | /** |
||
| 1585 | * Execute a "replace"-query. |
||
| 1586 | * |
||
| 1587 | 2 | * @param string $table |
|
| 1588 | * @param array $data |
||
| 1589 | 2 | * @param null|string $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
|
| 1590 | * |
||
| 1591 | * @return false|int <p>false on error</p> |
||
| 1592 | * |
||
| 1593 | * @throws QueryException |
||
| 1594 | */ |
||
| 1595 | public function replace($table, array $data = array(), $databaseName = null) |
||
| 1636 | 1 | ||
| 1637 | /** |
||
| 1638 | 1 | * Rollback in a transaction and end the transaction. |
|
| 1639 | * |
||
| 1640 | 1 | * @return bool <p>Boolean true on success, false otherwise.</p> |
|
| 1641 | */ |
||
| 1642 | View Code Duplication | public function rollback() |
|
| 1656 | |||
| 1657 | /** |
||
| 1658 | * Try to secure a variable, so can you use it in sql-queries. |
||
| 1659 | * |
||
| 1660 | * <p> |
||
| 1661 | * <strong>int:</strong> (also strings that contains only an int-value)<br /> |
||
| 1662 | * 1. parse into "int" |
||
| 1663 | * </p><br /> |
||
| 1664 | * |
||
| 1665 | * <p> |
||
| 1666 | 2 | * <strong>float:</strong><br /> |
|
| 1667 | * 1. return "float" |
||
| 1668 | 2 | * </p><br /> |
|
| 1669 | * |
||
| 1670 | 2 | * <p> |
|
| 1671 | 2 | * <strong>string:</strong><br /> |
|
| 1672 | 2 | * 1. check if the string isn't a default mysql-time-function e.g. 'CURDATE()'<br /> |
|
| 1673 | 2 | * 2. trim whitespace<br /> |
|
| 1674 | * 3. trim '<br /> |
||
| 1675 | * 4. escape the string (and remove non utf-8 chars)<br /> |
||
| 1676 | * 5. trim ' again (because we maybe removed some chars)<br /> |
||
| 1677 | * 6. add ' around the new string<br /> |
||
| 1678 | * </p><br /> |
||
| 1679 | * |
||
| 1680 | * <p> |
||
| 1681 | * <strong>array:</strong><br /> |
||
| 1682 | * 1. return null |
||
| 1683 | * </p><br /> |
||
| 1684 | * |
||
| 1685 | * <p> |
||
| 1686 | * <strong>object:</strong><br /> |
||
| 1687 | * 1. return false |
||
| 1688 | * </p><br /> |
||
| 1689 | 2 | * |
|
| 1690 | * <p> |
||
| 1691 | 2 | * <strong>null:</strong><br /> |
|
| 1692 | 2 | * 1. return null |
|
| 1693 | * </p> |
||
| 1694 | * |
||
| 1695 | * @param mixed $var |
||
| 1696 | * |
||
| 1697 | * @return mixed |
||
| 1698 | */ |
||
| 1699 | public function secure($var) |
||
| 1725 | |||
| 1726 | /** |
||
| 1727 | * Execute a "select"-query. |
||
| 1728 | * |
||
| 1729 | * @param string $table |
||
| 1730 | * @param string|array $where |
||
| 1731 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1732 | * |
||
| 1733 | * @return false|Result <p>false on error</p> |
||
| 1734 | * |
||
| 1735 | * @throws QueryException |
||
| 1736 | */ |
||
| 1737 | View Code Duplication | public function select($table, $where = '1=1', $databaseName = null) |
|
| 1764 | |||
| 1765 | /** |
||
| 1766 | * Selects a different database than the one specified on construction. |
||
| 1767 | * |
||
| 1768 | * @param string $database <p>Database name to switch to.</p> |
||
| 1769 | * |
||
| 1770 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 1771 | */ |
||
| 1772 | public function select_db($database) |
||
| 1780 | |||
| 1781 | /** |
||
| 1782 | * Set the current charset. |
||
| 1783 | * |
||
| 1784 | * @param string $charset |
||
| 1785 | * |
||
| 1786 | * @return bool |
||
| 1787 | */ |
||
| 1788 | public function set_charset($charset) |
||
| 1810 | |||
| 1811 | /** |
||
| 1812 | * Set the option to convert null to "''" (empty string). |
||
| 1813 | * |
||
| 1814 | * Used in secure() => select(), insert(), update(), delete() |
||
| 1815 | * |
||
| 1816 | * @param $bool |
||
| 1817 | */ |
||
| 1818 | public function set_convert_null_to_empty_string($bool) |
||
| 1822 | |||
| 1823 | /** |
||
| 1824 | * Enables or disables internal report functions |
||
| 1825 | * |
||
| 1826 | * @link http://php.net/manual/en/function.mysqli-report.php |
||
| 1827 | * |
||
| 1828 | * @param int $flags <p> |
||
| 1829 | * <table> |
||
| 1830 | * Supported flags |
||
| 1831 | * <tr valign="top"> |
||
| 1832 | * <td>Name</td> |
||
| 1833 | * <td>Description</td> |
||
| 1834 | * </tr> |
||
| 1835 | * <tr valign="top"> |
||
| 1836 | * <td><b>MYSQLI_REPORT_OFF</b></td> |
||
| 1837 | * <td>Turns reporting off</td> |
||
| 1838 | * </tr> |
||
| 1839 | * <tr valign="top"> |
||
| 1840 | * <td><b>MYSQLI_REPORT_ERROR</b></td> |
||
| 1841 | * <td>Report errors from mysqli function calls</td> |
||
| 1842 | * </tr> |
||
| 1843 | * <tr valign="top"> |
||
| 1844 | * <td><b>MYSQLI_REPORT_STRICT</b></td> |
||
| 1845 | * <td> |
||
| 1846 | * Throw <b>mysqli_sql_exception</b> for errors |
||
| 1847 | * instead of warnings |
||
| 1848 | * </td> |
||
| 1849 | * </tr> |
||
| 1850 | * <tr valign="top"> |
||
| 1851 | * <td><b>MYSQLI_REPORT_INDEX</b></td> |
||
| 1852 | * <td>Report if no index or bad index was used in a query</td> |
||
| 1853 | * </tr> |
||
| 1854 | * <tr valign="top"> |
||
| 1855 | * <td><b>MYSQLI_REPORT_ALL</b></td> |
||
| 1856 | * <td>Set all options (report all)</td> |
||
| 1857 | * </tr> |
||
| 1858 | * </table> |
||
| 1859 | * </p> |
||
| 1860 | * |
||
| 1861 | * @return bool |
||
| 1862 | */ |
||
| 1863 | public function set_mysqli_report($flags) |
||
| 1867 | |||
| 1868 | /** |
||
| 1869 | * Show config errors by throw exceptions. |
||
| 1870 | * |
||
| 1871 | * @return bool |
||
| 1872 | * |
||
| 1873 | * @throws \InvalidArgumentException |
||
| 1874 | */ |
||
| 1875 | public function showConfigError() |
||
| 1903 | |||
| 1904 | /** |
||
| 1905 | * alias: "beginTransaction()" |
||
| 1906 | */ |
||
| 1907 | public function startTransaction() |
||
| 1911 | |||
| 1912 | /** |
||
| 1913 | * Execute a callback inside a transaction. |
||
| 1914 | * |
||
| 1915 | * @param callback $callback <p>The callback to run inside the transaction, if it's throws an "Exception" or if it's |
||
| 1916 | * returns "false", all SQL-statements in the callback will be rollbacked.</p> |
||
| 1917 | * |
||
| 1918 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 1919 | */ |
||
| 1920 | public function transact($callback) |
||
| 1946 | |||
| 1947 | /** |
||
| 1948 | * Execute a "update"-query. |
||
| 1949 | * |
||
| 1950 | * @param string $table |
||
| 1951 | * @param array $data |
||
| 1952 | * @param array|string $where |
||
| 1953 | * @param null|string $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1954 | * |
||
| 1955 | * @return false|int <p>false on error</p> |
||
| 1956 | * |
||
| 1957 | * @throws QueryException |
||
| 1958 | */ |
||
| 1959 | public function update($table, array $data = array(), $where = '1=1', $databaseName = null) |
||
| 1994 | |||
| 1995 | } |
||
| 1996 |
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.