| Total Complexity | 89 |
| Total Lines | 554 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 3 | 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 |
||
| 27 | abstract class XoopsMySQLDatabase extends XoopsDatabase |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * Strict guard is active in dev or when XOOPS debug mode is on. |
||
| 31 | */ |
||
| 32 | private function isStrict(): bool |
||
| 33 | { |
||
| 34 | // Respect environment switch and also auto-enable when XOOPS debug is on |
||
| 35 | $envStrict = (defined('XOOPS_DB_STRICT') && XOOPS_DB_STRICT); |
||
|
|
|||
| 36 | $xoopsDebug = !empty($GLOBALS['xoopsConfig']['debug_mode']); |
||
| 37 | return $envStrict || $xoopsDebug; |
||
| 38 | } |
||
| 39 | /** |
||
| 40 | * Database connection |
||
| 41 | * |
||
| 42 | * @var XoopsDatabase|mysqli |
||
| 43 | */ |
||
| 44 | public $conn; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * connect to the database |
||
| 48 | * |
||
| 49 | * @param bool $selectdb select the database now? |
||
| 50 | * @return bool successful? |
||
| 51 | */ |
||
| 52 | public function connect($selectdb = true) |
||
| 53 | { |
||
| 54 | if (!extension_loaded('mysqli')) { |
||
| 55 | throw new \Exception('notrace:mysqli extension not loaded'); |
||
| 56 | |||
| 57 | return false; |
||
| 58 | } |
||
| 59 | |||
| 60 | $this->allowWebChanges = ($_SERVER['REQUEST_METHOD'] !== 'GET'); |
||
| 61 | |||
| 62 | if ($selectdb) { |
||
| 63 | $dbname = constant('XOOPS_DB_NAME'); |
||
| 64 | } else { |
||
| 65 | $dbname = ''; |
||
| 66 | } |
||
| 67 | mysqli_report(MYSQLI_REPORT_OFF); |
||
| 68 | if (XOOPS_DB_PCONNECT == 1) { |
||
| 69 | $this->conn = new mysqli('p:' . XOOPS_DB_HOST, XOOPS_DB_USER, XOOPS_DB_PASS, $dbname); |
||
| 70 | } else { |
||
| 71 | $this->conn = new mysqli(XOOPS_DB_HOST, XOOPS_DB_USER, XOOPS_DB_PASS, $dbname); |
||
| 72 | } |
||
| 73 | |||
| 74 | // errno is 0 if connect was successful |
||
| 75 | if (0 !== $this->conn->connect_errno) { |
||
| 76 | return false; |
||
| 77 | } |
||
| 78 | |||
| 79 | if (defined('XOOPS_DB_CHARSET') && ('' !== XOOPS_DB_CHARSET)) { |
||
| 80 | // $this->queryF("SET NAMES '" . XOOPS_DB_CHARSET . "'"); |
||
| 81 | $this->conn->set_charset(XOOPS_DB_CHARSET); |
||
| 82 | } |
||
| 83 | $this->queryF('SET SQL_BIG_SELECTS = 1'); |
||
| 84 | |||
| 85 | return true; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * generate an ID for a new row |
||
| 90 | * |
||
| 91 | * This is for compatibility only. Will always return 0, because MySQL supports |
||
| 92 | * autoincrement for primary keys. |
||
| 93 | * |
||
| 94 | * @param string $sequence name of the sequence from which to get the next ID |
||
| 95 | * @return int always 0, because mysql has support for autoincrement |
||
| 96 | */ |
||
| 97 | public function genId($sequence) |
||
| 98 | { |
||
| 99 | return 0; // will use auto_increment |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Get a result row as an enumerated array |
||
| 104 | * |
||
| 105 | * @param \mysqli_result $result |
||
| 106 | * |
||
| 107 | * @return array|false false on end of data |
||
| 108 | */ |
||
| 109 | public function fetchRow($result) |
||
| 110 | { |
||
| 111 | $row = @mysqli_fetch_row($result); |
||
| 112 | return $row ?? false; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Fetch a result row as an associative array |
||
| 117 | * |
||
| 118 | * @param \mysqli_result $result |
||
| 119 | * |
||
| 120 | * @return array|false false on end of data |
||
| 121 | */ |
||
| 122 | public function fetchArray($result) |
||
| 123 | { |
||
| 124 | $row = @mysqli_fetch_assoc($result); |
||
| 125 | return $row ?? false; |
||
| 126 | |||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Fetch a result row as an associative array |
||
| 131 | * |
||
| 132 | * @param \mysqli_result $result |
||
| 133 | * |
||
| 134 | * @return array|false false on end of data |
||
| 135 | */ |
||
| 136 | public function fetchBoth($result) |
||
| 137 | { |
||
| 138 | $row = @mysqli_fetch_array($result, MYSQLI_BOTH); |
||
| 139 | return $row ?? false; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * XoopsMySQLDatabase::fetchObject() |
||
| 144 | * |
||
| 145 | * @param \mysqli_result $result |
||
| 146 | * @return stdClass|false false on end of data |
||
| 147 | */ |
||
| 148 | public function fetchObject($result) |
||
| 149 | { |
||
| 150 | $row = @mysqli_fetch_object($result); |
||
| 151 | return $row ?? false; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Get the ID generated from the previous INSERT operation |
||
| 156 | * |
||
| 157 | * @return int|string |
||
| 158 | */ |
||
| 159 | public function getInsertId() |
||
| 160 | { |
||
| 161 | return mysqli_insert_id($this->conn); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Get number of rows in result |
||
| 166 | * |
||
| 167 | * @param \mysqli_result $result |
||
| 168 | * |
||
| 169 | * @return int |
||
| 170 | */ |
||
| 171 | public function getRowsNum($result) |
||
| 172 | { |
||
| 173 | return (int)@mysqli_num_rows($result); |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Get number of affected rows |
||
| 178 | * |
||
| 179 | * @return int |
||
| 180 | */ |
||
| 181 | public function getAffectedRows() |
||
| 182 | { |
||
| 183 | return (int)mysqli_affected_rows($this->conn); |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Close MySQL connection |
||
| 188 | * |
||
| 189 | * @return void |
||
| 190 | */ |
||
| 191 | public function close() |
||
| 192 | { |
||
| 193 | mysqli_close($this->conn); |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * will free all memory associated with the result identifier result. |
||
| 198 | * |
||
| 199 | * @param \mysqli_result $result result |
||
| 200 | * |
||
| 201 | * @return void |
||
| 202 | */ |
||
| 203 | public function freeRecordSet($result) |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Returns the text of the error message from previous MySQL operation |
||
| 210 | * |
||
| 211 | * @return string Returns the error text from the last MySQL function, or '' (the empty string) if no error occurred. |
||
| 212 | */ |
||
| 213 | public function error() |
||
| 214 | { |
||
| 215 | return @mysqli_error($this->conn); |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Returns the numerical value of the error message from previous MySQL operation |
||
| 220 | * |
||
| 221 | * @return int Returns the error number from the last MySQL function, or 0 (zero) if no error occurred. |
||
| 222 | */ |
||
| 223 | public function errno() |
||
| 224 | { |
||
| 225 | return @mysqli_errno($this->conn); |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Returns escaped string text with single quotes around it to be safely stored in database |
||
| 230 | * |
||
| 231 | * @param string $str unescaped string text |
||
| 232 | * @return string escaped string text with single quotes around |
||
| 233 | * @deprecated : delegate to exec(). |
||
| 234 | */ |
||
| 235 | public function quoteString($str) |
||
| 236 | { |
||
| 237 | |||
| 238 | if (is_object($this->logger)) { |
||
| 239 | $this->logger->addDeprecated(__METHOD__ . " is deprecated since XOOPS 2.5.12, please use 'quote()' instead."); |
||
| 240 | } |
||
| 241 | |||
| 242 | return $this->quote($str); |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Quotes a string for use in a query. |
||
| 247 | * |
||
| 248 | * @param string $string string to quote/escape for use in query |
||
| 249 | * |
||
| 250 | * @return string |
||
| 251 | */ |
||
| 252 | public function quote($string) |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Escapes a string for use in a query. Does not add surrounding quotes. |
||
| 260 | * |
||
| 261 | * @param string $string string to escape |
||
| 262 | * |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | public function escape($string) |
||
| 266 | { |
||
| 267 | return mysqli_real_escape_string($this->conn, (string) $string); |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * perform a query on the database |
||
| 272 | * |
||
| 273 | * @param string $sql a valid MySQL query |
||
| 274 | * @param int $limit number of records to return |
||
| 275 | * @param int $start offset of first record to return |
||
| 276 | * @return mysqli_result|bool query result or FALSE if successful |
||
| 277 | * or TRUE if successful and no result |
||
| 278 | */ |
||
| 279 | public function queryF($sql, $limit = 0, $start = 0) |
||
| 280 | { |
||
| 281 | if (!empty($limit)) { |
||
| 282 | if (empty($start)) { |
||
| 283 | $start = 0; |
||
| 284 | } |
||
| 285 | $sql .= ' LIMIT ' . (int)$start . ', ' . (int)$limit; |
||
| 286 | } |
||
| 287 | $this->logger->startTime('query_time'); |
||
| 288 | $result = mysqli_query($this->conn, $sql); |
||
| 289 | $this->logger->stopTime('query_time'); |
||
| 290 | $t = $this->logger->dumpTime('query_time', true); |
||
| 291 | |||
| 292 | if ($result) { |
||
| 293 | $this->logger->addQuery($sql, null, null, $t); |
||
| 294 | return $result; // mysqli_result for SELECT, true for writes |
||
| 295 | } else { |
||
| 296 | $this->logger->addQuery($sql, $this->error(), $this->errno(), $t); |
||
| 297 | return false; |
||
| 298 | } |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * perform a query |
||
| 303 | * |
||
| 304 | * This method is empty and does nothing! It should therefore only be |
||
| 305 | * used if nothing is exactly what you want done! ;-) |
||
| 306 | * |
||
| 307 | * @param string $sql a valid MySQL query |
||
| 308 | * @param int|null $limit number of records to return |
||
| 309 | * @param int|null $start offset of first record to return |
||
| 310 | * |
||
| 311 | * @return \mysqli_result|bool false on failure; true only if a write slipped through (BC) |
||
| 312 | */ |
||
| 313 | public function query(string $sql, ?int $limit = null, ?int $start = null) |
||
| 362 | } |
||
| 363 | |||
| 364 | |||
| 365 | /** |
||
| 366 | * perform queries from SQL dump file in a batch |
||
| 367 | * |
||
| 368 | * @param string $file file path to an SQL dump file |
||
| 369 | * @return bool FALSE if failed reading SQL file or TRUE if the file has been read and queries executed |
||
| 370 | */ |
||
| 371 | public function queryFromFile($file) |
||
| 372 | { |
||
| 373 | if (false !== ($fp = fopen($file, 'r'))) { |
||
| 374 | include_once XOOPS_ROOT_PATH . '/class/database/sqlutility.php'; |
||
| 375 | $sql_queries = trim(fread($fp, filesize($file))); |
||
| 376 | SqlUtility::splitMySqlFile($pieces, $sql_queries); |
||
| 377 | foreach ($pieces as $query) { |
||
| 378 | // [0] contains the prefixed query |
||
| 379 | // [4] contains unprefixed table name |
||
| 380 | $prefixed_query = SqlUtility::prefixQuery(trim($query), $this->prefix()); |
||
| 381 | if ($prefixed_query != false) { |
||
| 382 | $this->query($prefixed_query[0]); |
||
| 383 | } |
||
| 384 | } |
||
| 385 | |||
| 386 | return true; |
||
| 387 | } |
||
| 388 | |||
| 389 | return false; |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Get field name |
||
| 394 | * |
||
| 395 | * @param \mysqli_result $result query result |
||
| 396 | * @param int $offset numerical field index |
||
| 397 | * |
||
| 398 | * @return string |
||
| 399 | */ |
||
| 400 | public function getFieldName($result, $offset) |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Get field type |
||
| 407 | * |
||
| 408 | * @param \mysqli_result $result query result |
||
| 409 | * @param int $offset numerical field index |
||
| 410 | * |
||
| 411 | * @return string |
||
| 412 | */ |
||
| 413 | public function getFieldType($result, $offset) |
||
| 501 | } |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Get number of fields in result |
||
| 505 | * |
||
| 506 | * @param \mysqli_result $result query result |
||
| 507 | * |
||
| 508 | * @return int |
||
| 509 | */ |
||
| 510 | public function getFieldsNum($result) |
||
| 511 | { |
||
| 512 | return mysqli_num_fields($result); |
||
| 513 | } |
||
| 514 | |||
| 515 | /** |
||
| 516 | * getServerVersion get version of the mysql server |
||
| 517 | * |
||
| 518 | * @return string |
||
| 519 | */ |
||
| 520 | public function getServerVersion() |
||
| 521 | { |
||
| 522 | return mysqli_get_server_info($this->conn); |
||
| 523 | } |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Test the passed result to determine if it is a valid result set |
||
| 527 | * |
||
| 528 | * @param mixed $result value to test |
||
| 529 | * |
||
| 530 | * @return bool true if $result is a database result set, otherwise false |
||
| 531 | */ |
||
| 532 | public function isResultSet($result) |
||
| 533 | { |
||
| 534 | return is_a($result, 'mysqli_result'); |
||
| 535 | } |
||
| 536 | |||
| 537 | public function exec(string $sql): bool |
||
| 581 | } |
||
| 582 | } |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Safe Connection to a MySQL database. |
||
| 586 | * |
||
| 587 | * Delegates to parent; signature matches parent for LSP. |
||
| 588 | */ |
||
| 589 | class XoopsMySQLDatabaseSafe extends XoopsMySQLDatabase |
||
| 590 | { |
||
| 591 | /** |
||
| 592 | * perform a query on the database |
||
| 648 |