| Total Complexity | 67 |
| Total Lines | 444 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like XoopsMySQLDatabase 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.
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 XoopsMySQLDatabase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | abstract class XoopsMySQLDatabase extends XoopsDatabase |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * Database connection |
||
| 37 | * |
||
| 38 | * @var XoopsDatabase|mysqli |
||
| 39 | */ |
||
| 40 | public $conn; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * connect to the database |
||
| 44 | * |
||
| 45 | * @param bool $selectdb select the database now? |
||
| 46 | * @return bool successful? |
||
| 47 | */ |
||
| 48 | public function connect($selectdb = true) |
||
| 49 | { |
||
| 50 | if (!extension_loaded('mysqli')) { |
||
| 51 | trigger_error('notrace:mysqli extension not loaded', E_USER_ERROR); |
||
| 52 | |||
| 53 | return false; |
||
| 54 | } |
||
| 55 | |||
| 56 | $this->allowWebChanges = ($_SERVER['REQUEST_METHOD'] !== 'GET'); |
||
| 57 | |||
| 58 | if ($selectdb) { |
||
| 59 | $dbname = constant('XOOPS_DB_NAME'); |
||
| 60 | } else { |
||
| 61 | $dbname = ''; |
||
| 62 | } |
||
| 63 | mysqli_report(MYSQLI_REPORT_OFF); |
||
| 64 | if (XOOPS_DB_PCONNECT == 1) { |
||
|
|
|||
| 65 | $this->conn = new mysqli('p:' . XOOPS_DB_HOST, XOOPS_DB_USER, XOOPS_DB_PASS, $dbname); |
||
| 66 | } else { |
||
| 67 | $this->conn = new mysqli(XOOPS_DB_HOST, XOOPS_DB_USER, XOOPS_DB_PASS, $dbname); |
||
| 68 | } |
||
| 69 | |||
| 70 | // errno is 0 if connect was successful |
||
| 71 | if (0 !== $this->conn->connect_errno) { |
||
| 72 | return false; |
||
| 73 | } |
||
| 74 | |||
| 75 | if (defined('XOOPS_DB_CHARSET') && ('' !== XOOPS_DB_CHARSET)) { |
||
| 76 | // $this->queryF("SET NAMES '" . XOOPS_DB_CHARSET . "'"); |
||
| 77 | $this->conn->set_charset(XOOPS_DB_CHARSET); |
||
| 78 | } |
||
| 79 | $this->queryF('SET SQL_BIG_SELECTS = 1'); |
||
| 80 | |||
| 81 | return true; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * generate an ID for a new row |
||
| 86 | * |
||
| 87 | * This is for compatibility only. Will always return 0, because MySQL supports |
||
| 88 | * autoincrement for primary keys. |
||
| 89 | * |
||
| 90 | * @param string $sequence name of the sequence from which to get the next ID |
||
| 91 | * @return int always 0, because mysql has support for autoincrement |
||
| 92 | */ |
||
| 93 | public function genId($sequence) |
||
| 94 | { |
||
| 95 | return 0; // will use auto_increment |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Get a result row as an enumerated array |
||
| 100 | * |
||
| 101 | * @param mysqli_result $result |
||
| 102 | * |
||
| 103 | * @return array|false false on end of data |
||
| 104 | */ |
||
| 105 | public function fetchRow($result) |
||
| 106 | { |
||
| 107 | $row = @mysqli_fetch_row($result); |
||
| 108 | return (null === $row) ? false : $row; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Fetch a result row as an associative array |
||
| 113 | * |
||
| 114 | * @param mysqli_result $result |
||
| 115 | * |
||
| 116 | * @return array|false false on end of data |
||
| 117 | */ |
||
| 118 | public function fetchArray($result) |
||
| 119 | { |
||
| 120 | $row = @mysqli_fetch_assoc($result); |
||
| 121 | return (null === $row) ? false : $row; |
||
| 122 | |||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Fetch a result row as an associative array |
||
| 127 | * |
||
| 128 | * @param mysqli_result $result |
||
| 129 | * |
||
| 130 | * @return array|false false on end of data |
||
| 131 | */ |
||
| 132 | public function fetchBoth($result) |
||
| 133 | { |
||
| 134 | $row = @mysqli_fetch_array($result, MYSQLI_BOTH); |
||
| 135 | return (null === $row) ? false : $row; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * XoopsMySQLiDatabase::fetchObjected() |
||
| 140 | * |
||
| 141 | * @param mixed $result |
||
| 142 | * @return stdClass|false false on end of data |
||
| 143 | */ |
||
| 144 | public function fetchObject($result) |
||
| 145 | { |
||
| 146 | $row = @mysqli_fetch_object($result); |
||
| 147 | return (null === $row) ? false : $row; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Get the ID generated from the previous INSERT operation |
||
| 152 | * |
||
| 153 | * @return int|string |
||
| 154 | */ |
||
| 155 | public function getInsertId() |
||
| 156 | { |
||
| 157 | return mysqli_insert_id($this->conn); |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get number of rows in result |
||
| 162 | * |
||
| 163 | * @param mysqli_result $result |
||
| 164 | * |
||
| 165 | * @return int |
||
| 166 | */ |
||
| 167 | public function getRowsNum($result) |
||
| 168 | { |
||
| 169 | return @mysqli_num_rows($result); |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Get number of affected rows |
||
| 174 | * |
||
| 175 | * @return int |
||
| 176 | */ |
||
| 177 | public function getAffectedRows() |
||
| 178 | { |
||
| 179 | return mysqli_affected_rows($this->conn); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Close MySQL connection |
||
| 184 | * |
||
| 185 | * @return void |
||
| 186 | */ |
||
| 187 | public function close() |
||
| 188 | { |
||
| 189 | mysqli_close($this->conn); |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * will free all memory associated with the result identifier result. |
||
| 194 | * |
||
| 195 | * @param mysqli_result $result result |
||
| 196 | * |
||
| 197 | * @return void |
||
| 198 | */ |
||
| 199 | public function freeRecordSet($result) |
||
| 200 | { |
||
| 201 | mysqli_free_result($result); |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Returns the text of the error message from previous MySQL operation |
||
| 206 | * |
||
| 207 | * @return string Returns the error text from the last MySQL function, or '' (the empty string) if no error occurred. |
||
| 208 | */ |
||
| 209 | public function error() |
||
| 210 | { |
||
| 211 | return @mysqli_error($this->conn); |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Returns the numerical value of the error message from previous MySQL operation |
||
| 216 | * |
||
| 217 | * @return int Returns the error number from the last MySQL function, or 0 (zero) if no error occurred. |
||
| 218 | */ |
||
| 219 | public function errno() |
||
| 220 | { |
||
| 221 | return @mysqli_errno($this->conn); |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Returns escaped string text with single quotes around it to be safely stored in database |
||
| 226 | * |
||
| 227 | * @param string $str unescaped string text |
||
| 228 | * @return string escaped string text with single quotes around |
||
| 229 | */ |
||
| 230 | public function quoteString($str) |
||
| 231 | { |
||
| 232 | return $this->quote($str); |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Quotes a string for use in a query. |
||
| 237 | * |
||
| 238 | * @param string $string string to quote/escape for use in query |
||
| 239 | * |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | public function quote($string) |
||
| 243 | { |
||
| 244 | $quoted = $this->escape($string); |
||
| 245 | return "'{$quoted}'"; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Escapes a string for use in a query. Does not add surrounding quotes. |
||
| 250 | * |
||
| 251 | * @param string $string string to escape |
||
| 252 | * |
||
| 253 | * @return string |
||
| 254 | */ |
||
| 255 | public function escape($string) |
||
| 256 | { |
||
| 257 | return mysqli_real_escape_string($this->conn, (string) $string); |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * perform a query on the database |
||
| 262 | * |
||
| 263 | * @param string $sql a valid MySQL query |
||
| 264 | * @param int $limit number of records to return |
||
| 265 | * @param int $start offset of first record to return |
||
| 266 | * @return mysqli_result|bool query result or FALSE if successful |
||
| 267 | * or TRUE if successful and no result |
||
| 268 | */ |
||
| 269 | public function queryF($sql, $limit = 0, $start = 0) |
||
| 270 | { |
||
| 271 | if (!empty($limit)) { |
||
| 272 | if (empty($start)) { |
||
| 273 | $start = 0; |
||
| 274 | } |
||
| 275 | $sql = $sql . ' LIMIT ' . (int)$start . ', ' . (int)$limit; |
||
| 276 | } |
||
| 277 | $this->logger->startTime('query_time'); |
||
| 278 | $result = mysqli_query($this->conn, $sql); |
||
| 279 | $this->logger->stopTime('query_time'); |
||
| 280 | $query_time = $this->logger->dumpTime('query_time', true); |
||
| 281 | if ($result) { |
||
| 282 | $this->logger->addQuery($sql, null, null, $query_time); |
||
| 283 | |||
| 284 | return $result; |
||
| 285 | } else { |
||
| 286 | $this->logger->addQuery($sql, $this->error(), $this->errno(), $query_time); |
||
| 287 | |||
| 288 | return false; |
||
| 289 | } |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * perform a query |
||
| 294 | * |
||
| 295 | * This method is empty and does nothing! It should therefore only be |
||
| 296 | * used if nothing is exactly what you want done! ;-) |
||
| 297 | * |
||
| 298 | * @param string $sql a valid MySQL query |
||
| 299 | * @param int $limit number of records to return |
||
| 300 | * @param int $start offset of first record to return |
||
| 301 | * |
||
| 302 | * @return mysqli_result|bool query result or FALSE if successful |
||
| 303 | * or TRUE if successful and no result |
||
| 304 | */ |
||
| 305 | abstract public function query($sql, $limit = 0, $start = 0); |
||
| 306 | |||
| 307 | /** |
||
| 308 | * perform queries from SQL dump file in a batch |
||
| 309 | * |
||
| 310 | * @param string $file file path to an SQL dump file |
||
| 311 | * @return bool FALSE if failed reading SQL file or TRUE if the file has been read and queries executed |
||
| 312 | */ |
||
| 313 | public function queryFromFile($file) |
||
| 314 | { |
||
| 315 | if (false !== ($fp = fopen($file, 'r'))) { |
||
| 316 | include_once XOOPS_ROOT_PATH . '/class/database/sqlutility.php'; |
||
| 317 | $sql_queries = trim(fread($fp, filesize($file))); |
||
| 318 | SqlUtility::splitMySqlFile($pieces, $sql_queries); |
||
| 319 | foreach ($pieces as $query) { |
||
| 320 | // [0] contains the prefixed query |
||
| 321 | // [4] contains unprefixed table name |
||
| 322 | $prefixed_query = SqlUtility::prefixQuery(trim($query), $this->prefix()); |
||
| 323 | if ($prefixed_query != false) { |
||
| 324 | $this->query($prefixed_query[0]); |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | return true; |
||
| 329 | } |
||
| 330 | |||
| 331 | return false; |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Get field name |
||
| 336 | * |
||
| 337 | * @param mysqli_result $result query result |
||
| 338 | * @param int $offset numerical field index |
||
| 339 | * |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | public function getFieldName($result, $offset) |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Get field type |
||
| 349 | * |
||
| 350 | * @param mysqli_result $result query result |
||
| 351 | * @param int $offset numerical field index |
||
| 352 | * |
||
| 353 | * @return string |
||
| 354 | */ |
||
| 355 | public function getFieldType($result, $offset) |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Get number of fields in result |
||
| 447 | * |
||
| 448 | * @param mysqli_result $result query result |
||
| 449 | * |
||
| 450 | * @return int |
||
| 451 | */ |
||
| 452 | public function getFieldsNum($result) |
||
| 455 | } |
||
| 456 | |||
| 457 | /** |
||
| 458 | * getServerVersion get version of the mysql server |
||
| 459 | * |
||
| 460 | * @return string |
||
| 461 | */ |
||
| 462 | public function getServerVersion() |
||
| 465 | } |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Test the passed result to determine if it is a valid result set |
||
| 469 | * |
||
| 470 | * @param mixed $result value to test |
||
| 471 | * |
||
| 472 | * @return bool true if $result is a database result set, otherwise false |
||
| 473 | */ |
||
| 474 | public function isResultSet($result) |
||
| 475 | { |
||
| 476 | return is_a($result, 'mysqli_result'); |
||
| 477 | } |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Safe Connection to a MySQL database. |
||
| 482 | * |
||
| 483 | * @author Kazumi Ono <[email protected]> |
||
| 484 | * @copyright (c) 2000-2019 XOOPS Project (https://xoops.org) |
||
| 542 |