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 | * @param null|string $sql |
|
| 217 | * @param array $bindings |
||
| 218 | 10 | * |
|
| 219 | * @return bool|int|Result|DB <p> |
||
| 220 | 10 | * "DB" by "$sql" === null<br /> |
|
| 221 | * "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 222 | * "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 223 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 224 | * "true" by e.g. "DROP"-queries<br /> |
||
| 225 | * "false" on error |
||
| 226 | * </p> |
||
| 227 | */ |
||
| 228 | public function __invoke($sql = null, array $bindings = array()) |
||
| 232 | |||
| 233 | /** |
||
| 234 | 10 | * __wakeup |
|
| 235 | 10 | * |
|
| 236 | 9 | * @return void |
|
| 237 | 9 | */ |
|
| 238 | 8 | public function __wakeup() |
|
| 242 | 1 | ||
| 243 | /** |
||
| 244 | * Load the config from the constructor. |
||
| 245 | 2 | * |
|
| 246 | 1 | * @param string $hostname |
|
| 247 | * @param string $username |
||
| 248 | * @param string $password |
||
| 249 | 1 | * @param string $database |
|
| 250 | 1 | * @param int|string $port <p>default is (int)3306</p> |
|
| 251 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 252 | * @param boolean|string $exit_on_error <p>Use a empty string "" or false to disable it.</p> |
||
| 253 | * @param boolean|string $echo_on_error <p>Use a empty string "" or false to disable it.</p> |
||
| 254 | * @param string $logger_class_name |
||
| 255 | * @param string $logger_level |
||
| 256 | 7 | * @param array $extra_config <p> |
|
| 257 | * 'session_to_db' => false|true<br> |
||
| 258 | * 'socket' => 'string (path)'<br> |
||
| 259 | * 'ssl' => 'bool'<br> |
||
| 260 | * 'clientkey' => 'string (path)'<br> |
||
| 261 | * 'clientcert' => 'string (path)'<br> |
||
| 262 | * 'cacert' => 'string (path)'<br> |
||
| 263 | * </p> |
||
| 264 | * |
||
| 265 | * @return bool |
||
| 266 | 9 | */ |
|
| 267 | private function _loadConfig($hostname, $username, $password, $database, $port, $charset, $exit_on_error, $echo_on_error, $logger_class_name, $logger_level, $extra_config) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Parses arrays with value pairs and generates SQL to use in queries. |
||
| 343 | * |
||
| 344 | * @param array $arrayPair |
||
| 345 | * @param string $glue <p>This is the separator.</p> |
||
| 346 | * |
||
| 347 | * @return string |
||
| 348 | * |
||
| 349 | * @internal |
||
| 350 | */ |
||
| 351 | public function _parseArrayPair($arrayPair, $glue = ',') |
||
| 497 | |||
| 498 | 23 | /** |
|
| 499 | 22 | * _parseQueryParams |
|
| 500 | 22 | * |
|
| 501 | * @param string $sql |
||
| 502 | 22 | * @param array $params |
|
| 503 | * |
||
| 504 | * @return array <p>with the keys -> 'sql', 'params'</p> |
||
| 505 | */ |
||
| 506 | 8 | private function _parseQueryParams($sql, array $params) |
|
| 537 | |||
| 538 | /** |
||
| 539 | * Gets the number of affected rows in a previous MySQL operation. |
||
| 540 | 3 | * |
|
| 541 | 3 | * @return int |
|
| 542 | 3 | */ |
|
| 543 | public function affected_rows() |
||
| 547 | 3 | ||
| 548 | 3 | /** |
|
| 549 | 3 | * Begins a transaction, by turning off auto commit. |
|
| 550 | 3 | * |
|
| 551 | * @return bool <p>This will return true or false indicating success of transaction</p> |
||
| 552 | 3 | */ |
|
| 553 | View Code Duplication | public function beginTransaction() |
|
| 570 | |||
| 571 | /** |
||
| 572 | * Clear the errors in "_debug->_errors". |
||
| 573 | * |
||
| 574 | * @return bool |
||
| 575 | */ |
||
| 576 | public function clearErrors() |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Closes a previously opened database connection. |
||
| 583 | */ |
||
| 584 | public function close() |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Commits the current transaction and end the transaction. |
||
| 595 | * |
||
| 596 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 597 | 26 | */ |
|
| 598 | View Code Duplication | public function commit() |
|
| 612 | 22 | ||
| 613 | 22 | /** |
|
| 614 | * Open a new connection to the MySQL server. |
||
| 615 | 26 | * |
|
| 616 | * @return bool |
||
| 617 | 26 | * |
|
| 618 | 22 | * @throws DBConnectException |
|
| 619 | 22 | */ |
|
| 620 | public function connect() |
||
| 695 | 1 | ||
| 696 | /** |
||
| 697 | * Execute a "delete"-query. |
||
| 698 | 1 | * |
|
| 699 | 1 | * @param string $table |
|
| 700 | * @param string|array $where |
||
| 701 | 1 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
|
| 702 | 1 | * |
|
| 703 | * @return false|int <p>false on error</p> |
||
| 704 | 1 | * |
|
| 705 | * * @throws QueryException |
||
| 706 | */ |
||
| 707 | 1 | View Code Duplication | public function delete($table, $where, $databaseName = null) |
| 734 | |||
| 735 | 33 | /** |
|
| 736 | * Ends a transaction and commits if no errors, then ends autocommit. |
||
| 737 | 33 | * |
|
| 738 | * @return bool <p>This will return true or false indicating success of transactions.</p> |
||
| 739 | */ |
||
| 740 | View Code Duplication | public function endTransaction() |
|
| 760 | |||
| 761 | 35 | /** |
|
| 762 | * Get all errors from "$this->_errors". |
||
| 763 | 35 | * |
|
| 764 | * @return array|false <p>false === on errors</p> |
||
| 765 | */ |
||
| 766 | public function errors() |
||
| 772 | |||
| 773 | 22 | /** |
|
| 774 | * Returns the SQL by replacing :placeholders with SQL-escaped values. |
||
| 775 | * |
||
| 776 | * @param mixed $sql <p>The SQL string.</p> |
||
| 777 | * @param array $params <p>An array of key-value bindings.</p> |
||
| 778 | * |
||
| 779 | * @return array <p>with the keys -> 'sql', 'params'</p> |
||
| 780 | */ |
||
| 781 | 8 | public function _parseQueryParamsByName($sql, array $params = array()) |
|
| 818 | |||
| 819 | 8 | /** |
|
| 820 | * Escape: Use "mysqli_real_escape_string" and clean non UTF-8 chars + some extra optional stuff. |
||
| 821 | * |
||
| 822 | 8 | * @param mixed $var boolean: convert into "integer"<br /> |
|
| 823 | * int: int (don't change it)<br /> |
||
| 824 | 8 | * float: float (don't change it)<br /> |
|
| 825 | * null: null (don't change it)<br /> |
||
| 826 | * array: run escape() for every key => value<br /> |
||
| 827 | * string: run UTF8::cleanup() and mysqli_real_escape_string()<br /> |
||
| 828 | * @param bool $stripe_non_utf8 |
||
| 829 | * @param bool $html_entity_decode |
||
| 830 | * @param bool|null $convert_array <strong>false</strong> => Keep the array.<br /> |
||
| 831 | * <strong>true</strong> => Convert to string var1,var2,var3...<br /> |
||
| 832 | * <strong>null</strong> => Convert the array into null, every time. |
||
| 833 | * |
||
| 834 | 3 | * @return mixed |
|
| 835 | */ |
||
| 836 | 3 | public function escape($var = '', $stripe_non_utf8 = true, $html_entity_decode = false, $convert_array = false) |
|
| 950 | |||
| 951 | /** |
||
| 952 | * Execute select/insert/update/delete sql-queries. |
||
| 953 | * |
||
| 954 | * @param string $query <p>sql-query</p> |
||
| 955 | * @param bool $useCache <p>use cache?</p> |
||
| 956 | * @param int $cacheTTL <p>cache-ttl in seconds</p> |
||
| 957 | * @param DB $db optional <p>the database connection</p> |
||
| 958 | * |
||
| 959 | * @return mixed "array" by "<b>SELECT</b>"-queries<br /> |
||
| 960 | * "int" (insert_id) by "<b>INSERT</b>"-queries<br /> |
||
| 961 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 962 | * "true" by e.g. "DROP"-queries<br /> |
||
| 963 | * "false" on error |
||
| 964 | * |
||
| 965 | * @throws QueryException |
||
| 966 | */ |
||
| 967 | public static function execSQL($query, $useCache = false, $cacheTTL = 3600, DB $db = null) |
||
| 1016 | 7 | ||
| 1017 | /** |
||
| 1018 | * Get all table-names via "SHOW TABLES". |
||
| 1019 | * |
||
| 1020 | * @return array |
||
| 1021 | */ |
||
| 1022 | public function getAllTables() |
||
| 1029 | 1 | ||
| 1030 | /** |
||
| 1031 | * @return Debug |
||
| 1032 | */ |
||
| 1033 | public function getDebugger() |
||
| 1037 | |||
| 1038 | 1 | /** |
|
| 1039 | 1 | * Get errors from "$this->_errors". |
|
| 1040 | * |
||
| 1041 | 1 | * @return array |
|
| 1042 | */ |
||
| 1043 | public function getErrors() |
||
| 1047 | |||
| 1048 | /** |
||
| 1049 | * getInstance() |
||
| 1050 | * |
||
| 1051 | * @param string $hostname |
||
| 1052 | * @param string $username |
||
| 1053 | * @param string $password |
||
| 1054 | * @param string $database |
||
| 1055 | * @param int|string $port <p>default is (int)3306</p> |
||
| 1056 | 1 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
|
| 1057 | * @param bool|string $exit_on_error <p>Use a empty string "" or false to disable it.</p> |
||
| 1058 | 1 | * @param bool|string $echo_on_error <p>Use a empty string "" or false to disable it.</p> |
|
| 1059 | * @param string $logger_class_name |
||
| 1060 | * @param string $logger_level |
||
| 1061 | * @param array $extra_config <p> |
||
| 1062 | 1 | * 'session_to_db' => false|true<br> |
|
| 1063 | 1 | * 'socket' => 'string (path)'<br> |
|
| 1064 | * 'ssl' => 'bool'<br> |
||
| 1065 | 1 | * 'clientkey' => 'string (path)'<br> |
|
| 1066 | * 'clientcert' => 'string (path)'<br> |
||
| 1067 | * 'cacert' => 'string (path)'<br> |
||
| 1068 | 1 | * </p> |
|
| 1069 | 1 | * |
|
| 1070 | 1 | * @return \voku\db\DB |
|
| 1071 | */ |
||
| 1072 | 1 | public static function getInstance($hostname = '', $username = '', $password = '', $database = '', $port = '', $charset = '', $exit_on_error = '', $echo_on_error = '', $logger_class_name = '', $logger_level = '', $extra_config = array()) |
|
| 1128 | |||
| 1129 | /** |
||
| 1130 | * Get the mysqli-link (link identifier returned by mysqli-connect). |
||
| 1131 | * |
||
| 1132 | 4 | * @return \mysqli |
|
| 1133 | */ |
||
| 1134 | 4 | public function getLink() |
|
| 1138 | |||
| 1139 | 1 | /** |
|
| 1140 | * Get the current charset. |
||
| 1141 | * |
||
| 1142 | 4 | * @return string |
|
| 1143 | */ |
||
| 1144 | public function get_charset() |
||
| 1148 | 4 | ||
| 1149 | 4 | /** |
|
| 1150 | * Check if we are in a transaction. |
||
| 1151 | 4 | * |
|
| 1152 | * @return bool |
||
| 1153 | */ |
||
| 1154 | public function inTransaction() |
||
| 1158 | |||
| 1159 | 4 | /** |
|
| 1160 | * Execute a "insert"-query. |
||
| 1161 | 4 | * |
|
| 1162 | * @param string $table |
||
| 1163 | * @param array $data |
||
| 1164 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1165 | * |
||
| 1166 | * @return false|int <p>false on error</p> |
||
| 1167 | * |
||
| 1168 | * @throws QueryException |
||
| 1169 | 4 | */ |
|
| 1170 | public function insert($table, array $data = array(), $databaseName = null) |
||
| 1197 | |||
| 1198 | /** |
||
| 1199 | * Returns the auto generated id used in the last query. |
||
| 1200 | * |
||
| 1201 | 2 | * @return int|string |
|
| 1202 | */ |
||
| 1203 | 2 | public function insert_id() |
|
| 1207 | |||
| 1208 | /** |
||
| 1209 | * Check if db-connection is ready. |
||
| 1210 | * |
||
| 1211 | 2 | * @return boolean |
|
| 1212 | */ |
||
| 1213 | public function isReady() |
||
| 1217 | 2 | ||
| 1218 | 2 | /** |
|
| 1219 | 2 | * Get the last sql-error. |
|
| 1220 | 2 | * |
|
| 1221 | * @return string|false <p>false === there was no error</p> |
||
| 1222 | 2 | */ |
|
| 1223 | public function lastError() |
||
| 1229 | |||
| 1230 | /** |
||
| 1231 | * Execute a sql-multi-query. |
||
| 1232 | * |
||
| 1233 | * @param string $sql |
||
| 1234 | * |
||
| 1235 | * @return false|Result[] "Result"-Array by "<b>SELECT</b>"-queries<br /> |
||
| 1236 | 21 | * "boolean" by only "<b>INSERT</b>"-queries<br /> |
|
| 1237 | * "boolean" by only (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1238 | * "boolean" by only by e.g. "DROP"-queries<br /> |
||
| 1239 | 21 | * |
|
| 1240 | * @throws QueryException |
||
| 1241 | 21 | */ |
|
| 1242 | 2 | public function multi_query($sql) |
|
| 1309 | |||
| 1310 | 23 | /** |
|
| 1311 | 1 | * Pings a server connection, or tries to reconnect |
|
| 1312 | 1 | * if the connection has gone down. |
|
| 1313 | * |
||
| 1314 | 23 | * @return boolean |
|
| 1315 | 1 | */ |
|
| 1316 | 1 | public function ping() |
|
| 1330 | 23 | ||
| 1331 | 1 | /** |
|
| 1332 | 1 | * Get a new "Prepare"-Object for your sql-query. |
|
| 1333 | * |
||
| 1334 | 23 | * @param string $query |
|
| 1335 | 4 | * |
|
| 1336 | 4 | * @return Prepare |
|
| 1337 | */ |
||
| 1338 | 23 | public function prepare($query) |
|
| 1342 | 23 | ||
| 1343 | 1 | /** |
|
| 1344 | 1 | * Execute a sql-query and return the result-array for select-statements. |
|
| 1345 | * |
||
| 1346 | 23 | * @param string $query |
|
| 1347 | 2 | * |
|
| 1348 | 2 | * @return mixed |
|
| 1349 | * @deprecated |
||
| 1350 | 23 | * @throws \Exception |
|
| 1351 | 1 | */ |
|
| 1352 | 1 | public static function qry($query) |
|
| 1377 | |||
| 1378 | 23 | /** |
|
| 1379 | 23 | * Execute a sql-query. |
|
| 1380 | 23 | * |
|
| 1381 | * @param string $sql <p>The sql query-string.</p> |
||
| 1382 | 23 | * |
|
| 1383 | 23 | * @param array|boolean $params <p> |
|
| 1384 | 23 | * "array" of sql-query-parameters<br/> |
|
| 1385 | * "false" if you don't need any parameter (default)<br/> |
||
| 1386 | 23 | * </p> |
|
| 1387 | 23 | * |
|
| 1388 | 23 | * @return bool|int|Result <p> |
|
| 1389 | * "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 1390 | 23 | * "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
|
| 1391 | 23 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
|
| 1392 | * "true" by e.g. "DROP"-queries<br /> |
||
| 1393 | 23 | * "false" on error |
|
| 1394 | * </p> |
||
| 1395 | 23 | * |
|
| 1396 | * @throws QueryException |
||
| 1397 | 23 | */ |
|
| 1398 | 1 | public function query($sql = '', $params = false) |
|
| 1473 | |||
| 1474 | 1 | /** |
|
| 1475 | 1 | * Error-handling for the sql-query. |
|
| 1476 | * |
||
| 1477 | 1 | * @param string $errorMessage |
|
| 1478 | * @param int $errorNumber |
||
| 1479 | * @param string $sql |
||
| 1480 | * @param array|bool $sqlParams <p>false if there wasn't any parameter</p> |
||
| 1481 | 1 | * @param bool $sqlMultiQuery |
|
| 1482 | 1 | * |
|
| 1483 | * @throws QueryException |
||
| 1484 | 1 | * @throws DBGoneAwayException |
|
| 1485 | 1 | * |
|
| 1486 | * @return bool |
||
| 1487 | 1 | */ |
|
| 1488 | private function queryErrorHandling($errorMessage, $errorNumber, $sql, $sqlParams = false, $sqlMultiQuery = false) |
||
| 1528 | 6 | ||
| 1529 | 2 | /** |
|
| 1530 | * Quote && Escape e.g. a table name string. |
||
| 1531 | 2 | * |
|
| 1532 | * @param string $str |
||
| 1533 | * |
||
| 1534 | 6 | * @return string |
|
| 1535 | */ |
||
| 1536 | 6 | public function quote_string($str) |
|
| 1549 | |||
| 1550 | 6 | /** |
|
| 1551 | * Reconnect to the MySQL-Server. |
||
| 1552 | * |
||
| 1553 | * @param bool $checkViaPing |
||
| 1554 | * |
||
| 1555 | * @return bool |
||
| 1556 | */ |
||
| 1557 | public function reconnect($checkViaPing = false) |
||
| 1572 | 1 | ||
| 1573 | /** |
||
| 1574 | * Execute a "replace"-query. |
||
| 1575 | 2 | * |
|
| 1576 | 1 | * @param string $table |
|
| 1577 | 2 | * @param array $data |
|
| 1578 | 2 | * @param null|string $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
|
| 1579 | 2 | * |
|
| 1580 | 1 | * @return false|int <p>false on error</p> |
|
| 1581 | * |
||
| 1582 | * @throws QueryException |
||
| 1583 | 2 | */ |
|
| 1584 | public function replace($table, array $data = array(), $databaseName = null) |
||
| 1625 | |||
| 1626 | 20 | /** |
|
| 1627 | * Rollback in a transaction and end the transaction. |
||
| 1628 | 20 | * |
|
| 1629 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 1630 | */ |
||
| 1631 | View Code Duplication | public function rollback() |
|
| 1645 | |||
| 1646 | 9 | /** |
|
| 1647 | * Try to secure a variable, so can you use it in sql-queries. |
||
| 1648 | 9 | * |
|
| 1649 | * <p> |
||
| 1650 | * <strong>int:</strong> (also strings that contains only an int-value)<br /> |
||
| 1651 | * 1. parse into "int" |
||
| 1652 | * </p><br /> |
||
| 1653 | * |
||
| 1654 | * <p> |
||
| 1655 | * <strong>float:</strong><br /> |
||
| 1656 | * 1. return "float" |
||
| 1657 | * </p><br /> |
||
| 1658 | * |
||
| 1659 | * <p> |
||
| 1660 | * <strong>string:</strong><br /> |
||
| 1661 | * 1. check if the string isn't a default mysql-time-function e.g. 'CURDATE()'<br /> |
||
| 1662 | * 2. trim whitespace<br /> |
||
| 1663 | * 3. trim '<br /> |
||
| 1664 | * 4. escape the string (and remove non utf-8 chars)<br /> |
||
| 1665 | * 5. trim ' again (because we maybe removed some chars)<br /> |
||
| 1666 | 2 | * 6. add ' around the new string<br /> |
|
| 1667 | * </p><br /> |
||
| 1668 | 2 | * |
|
| 1669 | * <p> |
||
| 1670 | 2 | * <strong>array:</strong><br /> |
|
| 1671 | 2 | * 1. return null |
|
| 1672 | 2 | * </p><br /> |
|
| 1673 | 2 | * |
|
| 1674 | * <p> |
||
| 1675 | * <strong>object:</strong><br /> |
||
| 1676 | * 1. return false |
||
| 1677 | * </p><br /> |
||
| 1678 | * |
||
| 1679 | * <p> |
||
| 1680 | * <strong>null:</strong><br /> |
||
| 1681 | * 1. return null |
||
| 1682 | * </p> |
||
| 1683 | * |
||
| 1684 | * @param mixed $var |
||
| 1685 | * |
||
| 1686 | * @return mixed |
||
| 1687 | */ |
||
| 1688 | public function secure($var) |
||
| 1714 | |||
| 1715 | /** |
||
| 1716 | * Execute a "select"-query. |
||
| 1717 | * |
||
| 1718 | * @param string $table |
||
| 1719 | * @param string|array $where |
||
| 1720 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1721 | * |
||
| 1722 | * @return false|Result <p>false on error</p> |
||
| 1723 | * |
||
| 1724 | * @throws QueryException |
||
| 1725 | */ |
||
| 1726 | View Code Duplication | public function select($table, $where = '1=1', $databaseName = null) |
|
| 1753 | |||
| 1754 | /** |
||
| 1755 | * Selects a different database than the one specified on construction. |
||
| 1756 | * |
||
| 1757 | * @param string $database <p>Database name to switch to.</p> |
||
| 1758 | * |
||
| 1759 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 1760 | */ |
||
| 1761 | public function select_db($database) |
||
| 1769 | |||
| 1770 | /** |
||
| 1771 | * Set the current charset. |
||
| 1772 | * |
||
| 1773 | * @param string $charset |
||
| 1774 | * |
||
| 1775 | * @return bool |
||
| 1776 | */ |
||
| 1777 | public function set_charset($charset) |
||
| 1799 | |||
| 1800 | /** |
||
| 1801 | * Set the option to convert null to "''" (empty string). |
||
| 1802 | * |
||
| 1803 | * Used in secure() => select(), insert(), update(), delete() |
||
| 1804 | * |
||
| 1805 | * @param $bool |
||
| 1806 | */ |
||
| 1807 | public function set_convert_null_to_empty_string($bool) |
||
| 1811 | |||
| 1812 | /** |
||
| 1813 | * Enables or disables internal report functions |
||
| 1814 | * |
||
| 1815 | * @link http://php.net/manual/en/function.mysqli-report.php |
||
| 1816 | * |
||
| 1817 | * @param int $flags <p> |
||
| 1818 | * <table> |
||
| 1819 | * Supported flags |
||
| 1820 | * <tr valign="top"> |
||
| 1821 | * <td>Name</td> |
||
| 1822 | * <td>Description</td> |
||
| 1823 | * </tr> |
||
| 1824 | * <tr valign="top"> |
||
| 1825 | * <td><b>MYSQLI_REPORT_OFF</b></td> |
||
| 1826 | * <td>Turns reporting off</td> |
||
| 1827 | * </tr> |
||
| 1828 | * <tr valign="top"> |
||
| 1829 | * <td><b>MYSQLI_REPORT_ERROR</b></td> |
||
| 1830 | * <td>Report errors from mysqli function calls</td> |
||
| 1831 | * </tr> |
||
| 1832 | * <tr valign="top"> |
||
| 1833 | * <td><b>MYSQLI_REPORT_STRICT</b></td> |
||
| 1834 | * <td> |
||
| 1835 | * Throw <b>mysqli_sql_exception</b> for errors |
||
| 1836 | * instead of warnings |
||
| 1837 | * </td> |
||
| 1838 | * </tr> |
||
| 1839 | * <tr valign="top"> |
||
| 1840 | * <td><b>MYSQLI_REPORT_INDEX</b></td> |
||
| 1841 | * <td>Report if no index or bad index was used in a query</td> |
||
| 1842 | * </tr> |
||
| 1843 | * <tr valign="top"> |
||
| 1844 | * <td><b>MYSQLI_REPORT_ALL</b></td> |
||
| 1845 | * <td>Set all options (report all)</td> |
||
| 1846 | * </tr> |
||
| 1847 | * </table> |
||
| 1848 | * </p> |
||
| 1849 | * |
||
| 1850 | * @return bool |
||
| 1851 | */ |
||
| 1852 | public function set_mysqli_report($flags) |
||
| 1856 | |||
| 1857 | /** |
||
| 1858 | * Show config errors by throw exceptions. |
||
| 1859 | * |
||
| 1860 | * @return bool |
||
| 1861 | * |
||
| 1862 | * @throws \InvalidArgumentException |
||
| 1863 | */ |
||
| 1864 | public function showConfigError() |
||
| 1892 | |||
| 1893 | /** |
||
| 1894 | * alias: "beginTransaction()" |
||
| 1895 | */ |
||
| 1896 | public function startTransaction() |
||
| 1900 | |||
| 1901 | /** |
||
| 1902 | * Execute a callback inside a transaction. |
||
| 1903 | * |
||
| 1904 | * @param callback $callback <p>The callback to run inside the transaction.</p> |
||
| 1905 | * |
||
| 1906 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 1907 | */ |
||
| 1908 | public function transact($callback) |
||
| 1935 | |||
| 1936 | /** |
||
| 1937 | * Execute a "update"-query. |
||
| 1938 | * |
||
| 1939 | * @param string $table |
||
| 1940 | * @param array $data |
||
| 1941 | * @param array|string $where |
||
| 1942 | * @param null|string $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1943 | * |
||
| 1944 | * @return false|int <p>false on error</p> |
||
| 1945 | * |
||
| 1946 | * @throws QueryException |
||
| 1947 | */ |
||
| 1948 | public function update($table, array $data = array(), $where = '1=1', $databaseName = null) |
||
| 1983 | |||
| 1984 | } |
||
| 1985 |
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.