| Total Complexity | 108 |
| Total Lines | 649 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 1 |
Complex classes like mysql 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 mysql, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class mysql { |
||
| 23 | private static mysqli $connection; |
||
| 24 | |||
| 25 | private static bool $auto_process = true; |
||
| 26 | |||
| 27 | private static string $db_name = ''; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * If you want to use it in standalone mode , you MUST set `auto_process` to `false` |
||
| 31 | */ |
||
| 32 | public static function init (string $host = 'localhost', string $username = 'root', string $password = '', string $dbname = '', bool $auto_process = null, int $port = 3306): void { |
||
| 33 | $host = settings::$db['host'] ?? $host; |
||
| 34 | $port = settings::$db['port'] ?? $port; |
||
| 35 | $user = settings::$db['user'] ?? settings::$db['username'] ?? $username; |
||
| 36 | $pass = settings::$db['pass'] ?? settings::$db['password'] ?? $password; |
||
| 37 | self::$auto_process = $auto_process ?? (!isset(settings::$db['auto_process']) || (isset(settings::$db['auto_process']) && settings::$db['auto_process'] == true)); |
||
| 38 | $dbname = settings::$db['dbname'] ?? $dbname; |
||
| 39 | self::$db_name = $dbname; |
||
| 40 | self::$connection = new mysqli($host, $user, $pass, $dbname, $port); |
||
| 41 | if (self::$connection->connect_errno) { |
||
| 42 | logger::write('SQL connection has problem : ' . self::$connection->connect_error, loggerTypes::ERROR); |
||
| 43 | throw new bptException('SQL_CONNECTION_PROBLEM'); |
||
| 44 | } |
||
| 45 | if (self::$auto_process && !lock::exist('BPT-MYSQL')) { |
||
| 46 | self::install(); |
||
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | private static function install (): void { |
||
| 51 | self::pureQuery(" |
||
| 52 | CREATE TABLE `users` |
||
| 53 | ( |
||
| 54 | `id` BIGINT(20) NOT NULL, |
||
| 55 | `username` VARCHAR(32) NULL DEFAULT NULL, |
||
| 56 | `lang_code` VARCHAR(3) NULL DEFAULT NULL, |
||
| 57 | `first_active` INT(11) NOT NULL DEFAULT '0', |
||
| 58 | `last_active` INT(11) NOT NULL DEFAULT '0', |
||
| 59 | `referral` BIGINT(20) NULL DEFAULT NULL, |
||
| 60 | `blocked` BOOLEAN NOT NULL DEFAULT FALSE, |
||
| 61 | `step` VARCHAR(64) NOT NULL DEFAULT 'main', |
||
| 62 | `value` TEXT NULL DEFAULT NULL, |
||
| 63 | PRIMARY KEY (`id`) |
||
| 64 | ) ENGINE = InnoDB;"); |
||
| 65 | lock::set('BPT-MYSQL'); |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @internal Only for BPT self usage , Don't use it in your source! |
||
| 70 | */ |
||
| 71 | public static function process (): void { |
||
| 72 | if (self::$auto_process) { |
||
| 73 | if (isset(BPT::$update->message)) { |
||
| 74 | self::processMessage(BPT::$update->message); |
||
|
|
|||
| 75 | } |
||
| 76 | elseif (isset(BPT::$update->edited_message)) { |
||
| 77 | self::processMessage(BPT::$update->edited_message); |
||
| 78 | } |
||
| 79 | elseif (isset(BPT::$update->callback_query)) { |
||
| 80 | self::processCallbackQuery(BPT::$update->callback_query); |
||
| 81 | } |
||
| 82 | elseif (isset(BPT::$update->inline_query)) { |
||
| 83 | self::processInlineQuery(BPT::$update->inline_query); |
||
| 84 | } |
||
| 85 | elseif (isset(BPT::$update->my_chat_member)) { |
||
| 86 | self::processMyChatMember(BPT::$update->my_chat_member); |
||
| 87 | } |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | private static function processMessage (message $update): void { |
||
| 92 | $type = $update->chat->type; |
||
| 93 | if ($type === chatType::PRIVATE) { |
||
| 94 | $user_id = $update->from->id; |
||
| 95 | $first_active = $last_active = time(); |
||
| 96 | $referral = null; |
||
| 97 | $username = $update->from->username; |
||
| 98 | $lang_code = $update->from->language_code; |
||
| 99 | if (isset($update->command) && isset($update->command_payload) && $update->command === 'start' && str_starts_with($update->command_payload, 'ref_')) { |
||
| 100 | if (tools::isShorted(substr($update->command_payload, 4))) { |
||
| 101 | $referral = tools::shortDecode(substr($update->command_payload, 4)); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | self::query("INSERT INTO `users`(`id`, `username`, `lang_code`, `first_active`, `last_active`, `referral`) VALUES (?,?,?,?,?,?) on duplicate key update `last_active` = ?, `username` = ?", [ |
||
| 105 | $user_id, |
||
| 106 | $username, |
||
| 107 | $lang_code, |
||
| 108 | $first_active, |
||
| 109 | $last_active, |
||
| 110 | $referral, |
||
| 111 | $last_active, |
||
| 112 | $username |
||
| 113 | ]); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | private static function processCallbackQuery (callbackQuery $update): void { |
||
| 118 | $type = $update->message->chat->type; |
||
| 119 | if ($type === chatType::PRIVATE) { |
||
| 120 | $user_id = $update->from->id; |
||
| 121 | $last_active = time(); |
||
| 122 | $username = $update->from->username; |
||
| 123 | self::update('users', ['last_active' => $last_active, 'username' => $username], ['id' => $user_id], 1); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | private static function processInlineQuery (inlineQuery $update): void { |
||
| 128 | $type = $update->chat_type; |
||
| 129 | if ($type === chatType::PRIVATE || $type === chatType::SENDER) { |
||
| 130 | $user_id = $update->from->id; |
||
| 131 | $last_active = time(); |
||
| 132 | $username = $update->from->username; |
||
| 133 | self::update('users', ['last_active' => $last_active, 'username' => $username], ['id' => $user_id], 1); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | private static function processMyChatMember (chatMemberUpdated $update): void { |
||
| 138 | $type = $update->chat->type; |
||
| 139 | if ($type === chatType::PRIVATE) { |
||
| 140 | if ($update->new_chat_member->status === chatMemberStatus::MEMBER) { |
||
| 141 | self::update('users', ['blocked' => false], ['id' => $update->from->id], 1); |
||
| 142 | } |
||
| 143 | else { |
||
| 144 | self::update('users', ['blocked' => true], ['id' => $update->from->id], 1); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Get real mysqli connections |
||
| 151 | * |
||
| 152 | * @return mysqli |
||
| 153 | */ |
||
| 154 | public static function getMysqli (): mysqli { |
||
| 155 | return self::$connection; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Get affected rows |
||
| 160 | * |
||
| 161 | * same as affectedRows |
||
| 162 | * |
||
| 163 | * @return int|string |
||
| 164 | */ |
||
| 165 | public static function affected_rows (): int|string { |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Get affected rows |
||
| 171 | * |
||
| 172 | * same as affected_rows |
||
| 173 | * |
||
| 174 | * @return int|string |
||
| 175 | */ |
||
| 176 | public static function affectedRows (): int|string { |
||
| 177 | return self::$connection->affected_rows; |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Get inserted id |
||
| 182 | * |
||
| 183 | * same as insertId |
||
| 184 | * |
||
| 185 | * @return int|string |
||
| 186 | */ |
||
| 187 | public static function insert_id (): int|string { |
||
| 188 | return self::$connection->insert_id; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get inserted id |
||
| 193 | * |
||
| 194 | * same as insert_id |
||
| 195 | * |
||
| 196 | * @return int|string |
||
| 197 | */ |
||
| 198 | public static function insertId (): int|string { |
||
| 199 | return self::$connection->insert_id; |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Escape string with real_escape_string of mysqli class |
||
| 204 | * |
||
| 205 | * @param string $text |
||
| 206 | * |
||
| 207 | * @return string |
||
| 208 | */ |
||
| 209 | public static function escapeString (string $text): string { |
||
| 210 | return self::$connection->real_escape_string($text); |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Get last error |
||
| 215 | * |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | public static function error (): string { |
||
| 219 | return self::$connection->error; |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Get last error code |
||
| 224 | * |
||
| 225 | * @return int |
||
| 226 | */ |
||
| 227 | public static function errno (): int { |
||
| 228 | return self::$connection->errno; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * set database charset |
||
| 233 | * |
||
| 234 | * @param string $charset |
||
| 235 | * |
||
| 236 | * @return bool |
||
| 237 | */ |
||
| 238 | public static function setCharset (string $charset): bool { |
||
| 239 | return self::$connection->set_charset($charset); |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Run query as what is it |
||
| 244 | * |
||
| 245 | * The library doesn't do anything on it |
||
| 246 | * |
||
| 247 | * It's like calling mysqli->query(); |
||
| 248 | * |
||
| 249 | * @param string $query |
||
| 250 | * |
||
| 251 | * @return mysqli_result|bool |
||
| 252 | */ |
||
| 253 | public static function pureQuery (string $query): mysqli_result|bool { |
||
| 254 | return self::$connection->query($query); |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Run query with safe execution |
||
| 259 | * |
||
| 260 | * Replace inputs with `?` in query to be replaced safely with $vars in order |
||
| 261 | * |
||
| 262 | * it will use `pureQuery` if `$vars` be empty |
||
| 263 | * |
||
| 264 | * e.g. : mysql::query('select * from `users` where `id` = ? limit 1',[123456789]); |
||
| 265 | * |
||
| 266 | * e.g. : mysql::query('update `users` set `step` = ? where `id` = ? limit 1',['main',123456789]); |
||
| 267 | * |
||
| 268 | * @param string $query |
||
| 269 | * @param array $vars default [] or empty |
||
| 270 | * @param bool $need_result set if you want result be returned, default : true |
||
| 271 | * |
||
| 272 | * @return mysqli_result|bool |
||
| 273 | */ |
||
| 274 | public static function query (string $query, array $vars = [], bool $need_result = true): mysqli_result|bool { |
||
| 275 | if (empty($vars)) { |
||
| 276 | return self::pureQuery($query); |
||
| 277 | } |
||
| 278 | $prepare = self::$connection->prepare($query); |
||
| 279 | $types = ''; |
||
| 280 | foreach ($vars as $var) { |
||
| 281 | if (is_int($var)) { |
||
| 282 | $types .= 'i'; |
||
| 283 | } |
||
| 284 | elseif (is_double($var)) { |
||
| 285 | $types .= 'd'; |
||
| 286 | } |
||
| 287 | else { |
||
| 288 | $types .= 's'; |
||
| 289 | } |
||
| 290 | } |
||
| 291 | $prepare->bind_param($types,...$vars); |
||
| 292 | if (!$prepare->execute()) { |
||
| 293 | logger::write(loggerTypes::WARNING, $prepare->error); |
||
| 294 | return false; |
||
| 295 | } |
||
| 296 | return $need_result ? $prepare->get_result() : true; |
||
| 297 | } |
||
| 298 | |||
| 299 | private static function whereBuilder(string &$query, array $where = null): array { |
||
| 300 | if (empty($where)) { |
||
| 301 | return []; |
||
| 302 | } |
||
| 303 | |||
| 304 | $query .= " WHERE"; |
||
| 305 | $first = true; |
||
| 306 | $values = []; |
||
| 307 | |||
| 308 | foreach ($where as $name => $value) { |
||
| 309 | if ($first) { |
||
| 310 | $first = false; |
||
| 311 | } |
||
| 312 | else { |
||
| 313 | $query .= ' AND'; |
||
| 314 | } |
||
| 315 | |||
| 316 | $operator = substr($value,0,2); |
||
| 317 | $operator_value = substr($value,2); |
||
| 318 | switch ($operator) { |
||
| 319 | case '>=': |
||
| 320 | $query .= " `$name` >= ?"; |
||
| 321 | $value = $operator_value; |
||
| 322 | break; |
||
| 323 | case '<=': |
||
| 324 | $query .= " `$name` <= ?"; |
||
| 325 | $value = $operator_value; |
||
| 326 | break; |
||
| 327 | case '> ': |
||
| 328 | $query .= " `$name` > ?"; |
||
| 329 | $value = $operator_value; |
||
| 330 | break; |
||
| 331 | case '< ': |
||
| 332 | $query .= " `$name` < ?"; |
||
| 333 | $value = $operator_value; |
||
| 334 | break; |
||
| 335 | case '% ': |
||
| 336 | $query .= " `$name` like ?"; |
||
| 337 | $value = $operator_value; |
||
| 338 | break; |
||
| 339 | case '!=': |
||
| 340 | $query .= " `$name` != ?"; |
||
| 341 | $value = $operator_value; |
||
| 342 | break; |
||
| 343 | default: |
||
| 344 | $query .= " `$name` = ?"; |
||
| 345 | break; |
||
| 346 | } |
||
| 347 | |||
| 348 | $values[] = $value; |
||
| 349 | } |
||
| 350 | |||
| 351 | return $values; |
||
| 352 | } |
||
| 353 | |||
| 354 | private static function groupByBuilder(string &$query, string|array $group_by = []): void { |
||
| 355 | if (empty($group_by)) { |
||
| 356 | return; |
||
| 357 | } |
||
| 358 | if (is_string($group_by)) { |
||
| 359 | $group_by = [$group_by]; |
||
| 360 | } |
||
| 361 | $query .= ' GROUP BY `' . implode('`, `',$group_by) . '`'; |
||
| 362 | } |
||
| 363 | |||
| 364 | private static function orderByBuilder(string &$query, string|array $order_by = []): void { |
||
| 365 | if (empty($order_by)) { |
||
| 366 | return; |
||
| 367 | } |
||
| 368 | if (is_string($order_by)) { |
||
| 369 | $order_by = [$order_by => 'ASC']; |
||
| 370 | } |
||
| 371 | |||
| 372 | $query .= ' ORDER BY `'; |
||
| 373 | |||
| 374 | $first = true; |
||
| 375 | foreach ($order_by as $key => $mode) { |
||
| 376 | if ($first) { |
||
| 377 | $first = false; |
||
| 378 | } |
||
| 379 | else { |
||
| 380 | $query .= ', '; |
||
| 381 | } |
||
| 382 | if (is_numeric($key)) { |
||
| 383 | $key = $mode; |
||
| 384 | $mode = 'ASC'; |
||
| 385 | } |
||
| 386 | $query .= "$key` $mode"; |
||
| 387 | } |
||
| 388 | } |
||
| 389 | |||
| 390 | private static function countBuilder(string &$query, int $count = null, int $offset = null): void { |
||
| 391 | if (!empty($count)) { |
||
| 392 | $query .= !empty($offset) ? " LIMIT $offset,$count" : " LIMIT $count"; |
||
| 393 | } |
||
| 394 | elseif (!empty($offset)) { |
||
| 395 | $query .= " OFFSET $offset"; |
||
| 396 | } |
||
| 397 | } |
||
| 398 | |||
| 399 | private static function updateBuilder(string &$query, array $modify): array { |
||
| 400 | $first = true; |
||
| 401 | $values = []; |
||
| 402 | |||
| 403 | foreach ($modify as $name => $value) { |
||
| 404 | if ($first) { |
||
| 405 | $first = false; |
||
| 406 | } |
||
| 407 | else { |
||
| 408 | $query .= ' ,'; |
||
| 409 | } |
||
| 410 | |||
| 411 | $operator = substr($value,0,2); |
||
| 412 | $operator_value = substr($value,2); |
||
| 413 | switch ($operator) { |
||
| 414 | case '+=': |
||
| 415 | $query .= " `$name` = `$name` + ?"; |
||
| 416 | $value = $operator_value; |
||
| 417 | break; |
||
| 418 | case '-=': |
||
| 419 | $query .= " `$name` = `$name` - ?"; |
||
| 420 | $value = $operator_value; |
||
| 421 | break; |
||
| 422 | case '*=': |
||
| 423 | $query .= " `$name` = `$name` * ?"; |
||
| 424 | $value = $operator_value; |
||
| 425 | break; |
||
| 426 | case '/=': |
||
| 427 | $query .= " `$name` = `$name` / ?"; |
||
| 428 | $value = $operator_value; |
||
| 429 | break; |
||
| 430 | case '%=': |
||
| 431 | $query .= " `$name` = `$name` % ?"; |
||
| 432 | $value = $operator_value; |
||
| 433 | break; |
||
| 434 | default: |
||
| 435 | $query .= " `$name` = ?"; |
||
| 436 | break; |
||
| 437 | } |
||
| 438 | |||
| 439 | $values[] = $value; |
||
| 440 | } |
||
| 441 | |||
| 442 | return $values; |
||
| 443 | } |
||
| 444 | |||
| 445 | private static function insertBuilder(string &$query, string|array $columns, array|string $values): array { |
||
| 446 | $query .= '(`' . (is_string($columns) ? $columns : implode('`,`', $columns)) . '`) VALUES ('; |
||
| 447 | if (is_string($values)) $values = [$values]; |
||
| 448 | $query .= '?' . str_repeat(',?', count($values) - 1) . ')'; |
||
| 449 | return $values; |
||
| 450 | } |
||
| 451 | |||
| 452 | private static function selectBuilder(string &$query, string|array $columns): void { |
||
| 453 | if ($columns == '*') { |
||
| 454 | $query .= " * "; |
||
| 455 | } |
||
| 456 | else { |
||
| 457 | $query .= ' `' . (is_string($columns) ? $columns : implode('`,`', $columns)) . '` '; |
||
| 458 | } |
||
| 459 | } |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Run delete query |
||
| 463 | * |
||
| 464 | * e.g. : `mysql::delete('users',['id'=>123456789],1);` |
||
| 465 | * |
||
| 466 | * @param string $table table name |
||
| 467 | * @param array|null $where Set your ifs default : null |
||
| 468 | * @param int|null $count Set if you want to delete specific amount of row default : null |
||
| 469 | * @param int|null $offset Set if you want to delete rows after specific row default : null |
||
| 470 | * |
||
| 471 | * @return mysqli_result|bool |
||
| 472 | */ |
||
| 473 | public static function delete (string $table, array $where = null, int $count = null, int $offset = null): bool { |
||
| 477 | } |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Run update query |
||
| 481 | * |
||
| 482 | * e.g. : mysql::update('users',['step'=>'panel'],['id'=>123456789],1); |
||
| 483 | * |
||
| 484 | * @param string $table table name |
||
| 485 | * @param array $modify Set the data's you want to modify |
||
| 486 | * @param array|null $where Set your ifs default : null |
||
| 487 | * @param int|null $count Set if you want to update specific amount of row default : null |
||
| 488 | * @param int|null $offset Set if you want to update rows after specific row default : null |
||
| 489 | * |
||
| 490 | * @return mysqli_result|bool |
||
| 491 | */ |
||
| 492 | public static function update (string $table, array $modify, array $where = null, int $count = null, int $offset = null): bool { |
||
| 493 | $query = "UPDATE `$table` SET"; |
||
| 494 | $modify_vars = self::updateBuilder($query, $modify); |
||
| 495 | $where_vars = self::whereBuilder($query, $where); |
||
| 496 | self::countBuilder($query, $count, $offset); |
||
| 497 | return self::query($query, array_merge($modify_vars, $where_vars), false); |
||
| 498 | } |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Run insert query |
||
| 502 | * |
||
| 503 | * e.g. : `mysql::insert('users',['id','column1','column2','column3'],[123456789,'value1','value2','value3']);` |
||
| 504 | * |
||
| 505 | * @param string $table table name |
||
| 506 | * @param string|array $columns sets columns that you want to fill |
||
| 507 | * @param array|string $values sets value that you want to set |
||
| 508 | * |
||
| 509 | * @return mysqli_result|bool |
||
| 510 | */ |
||
| 511 | public static function insert (string $table, string|array $columns, array|string $values): bool { |
||
| 512 | $query = "INSERT INTO `$table`"; |
||
| 513 | $values = self::insertBuilder($query, $columns, $values); |
||
| 514 | return self::query($query, $values, false); |
||
| 515 | } |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Run select query |
||
| 519 | * |
||
| 520 | * e.g. : mysql::select('users','*',['id'=>123456789],1); |
||
| 521 | * |
||
| 522 | * e.g. : mysql::select('users',['step','referrals'],['id'=>123456789],1); |
||
| 523 | * |
||
| 524 | * @param string $table table name |
||
| 525 | * @param array|string $columns sets column that you want to retrieve , set '*' to retrieve all , default : '*' |
||
| 526 | * @param array|null $where Set your ifs default : null |
||
| 527 | * @param int|null $count Set if you want to select specific amount of row default : null |
||
| 528 | * @param int|null $offset Set if you want to select rows after specific row default : null |
||
| 529 | * @param array|string $group_by group result based on these columns |
||
| 530 | * @param array|string $order_by order result based on these columns |
||
| 531 | * |
||
| 532 | * @return mysqli_result|bool |
||
| 533 | */ |
||
| 534 | public static function select (string $table, array|string $columns = '*', array $where = null, int $count = null, int $offset = null, array|string $group_by = [], array|string $order_by = []): mysqli_result|bool { |
||
| 535 | $query = "SELECT"; |
||
| 536 | self::selectBuilder($query, $columns); |
||
| 537 | $query .= "FROM `$table`"; |
||
| 538 | $var = self::whereBuilder($query,$where); |
||
| 539 | self::groupByBuilder($query, $group_by); |
||
| 540 | self::orderByBuilder($query, $order_by); |
||
| 541 | self::countBuilder($query,$count,$offset); |
||
| 542 | return self::query($query, $var); |
||
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Same as mysql::select but return first result as array |
||
| 547 | * |
||
| 548 | * mysql::selectArray('users','*',['id'=>123456789]); |
||
| 549 | * |
||
| 550 | * @param string $table table name |
||
| 551 | * @param array|string $columns sets column that you want to retrieve , set '*' to retrieve all , default : '*' |
||
| 552 | * @param array|null $where Set your ifs default : null |
||
| 553 | * @param array|string $group_by group result based on these columns |
||
| 554 | * @param array|string $order_by order result based on these columns |
||
| 555 | * |
||
| 556 | * @return null|bool|array |
||
| 557 | */ |
||
| 558 | public static function selectArray (string $table, array|string $columns = '*', array $where = null, array|string $group_by = [], array|string $order_by = []): bool|array|null { |
||
| 559 | $res = self::select($table, $columns, $where, 1, $group_by, $order_by); |
||
| 560 | if ($res) { |
||
| 561 | return $res->fetch_assoc(); |
||
| 562 | } |
||
| 563 | return $res; |
||
| 564 | } |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Same as mysql::select but return first result as object(stdClass) |
||
| 568 | * |
||
| 569 | * mysql::selectObject('users','*',['id'=>123456789]); |
||
| 570 | * |
||
| 571 | * @param string $table table name |
||
| 572 | * @param array|string $columns sets column that you want to retrieve , set '*' to retrieve all , default : '*' |
||
| 573 | * @param array|null $where Set your ifs default : null |
||
| 574 | * @param array|string $group_by group result based on these columns |
||
| 575 | * @param array|string $order_by order result based on these columns |
||
| 576 | */ |
||
| 577 | public static function selectObject (string $table, array|string $columns = '*', array $where = null, array|string $group_by = [], array|string $order_by = []) { |
||
| 578 | $res = self::select($table, $columns, $where, 1, $group_by, $order_by); |
||
| 579 | if ($res) { |
||
| 580 | return $res->fetch_object(); |
||
| 581 | } |
||
| 582 | return $res; |
||
| 583 | } |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Same as mysql::select but return each row as generator |
||
| 587 | * |
||
| 588 | * e.g. : mysql::selectEach('users','*',['id'=>123456789],1); |
||
| 589 | * e.g. : mysql::selectEach('users',['id']); |
||
| 590 | * |
||
| 591 | * @param string $table table name |
||
| 592 | * @param array|string $columns sets column that you want to retrieve , set '*' to retrieve all , default : '*' |
||
| 593 | * @param array|null $where Set your ifs default : null |
||
| 594 | * @param int|null $count Set if you want to select specific amount of row default : null |
||
| 595 | * @param int|null $offset Set if you want to select rows after specific row default : null |
||
| 596 | * @param array|string $group_by group result based on these columns |
||
| 597 | * @param array|string $order_by order result based on these columns |
||
| 598 | * |
||
| 599 | * @return bool|Generator |
||
| 600 | */ |
||
| 601 | public static function selectEach (string $table, array|string $columns = '*', array $where = null, int $count = null, int $offset = null, array|string $group_by = [], array|string $order_by = []): bool|Generator { |
||
| 602 | $res = self::select($table, $columns, $where, $count, $offset, $group_by, $order_by); |
||
| 603 | if ($res) { |
||
| 604 | while ($row = $res->fetch_assoc()) yield $row; |
||
| 605 | } |
||
| 606 | else return $res; |
||
| 607 | } |
||
| 608 | |||
| 609 | /** |
||
| 610 | * get backup from database, you can get full backup or specific table backup |
||
| 611 | * |
||
| 612 | * @param array|null $wanted_tables set if you want specific table which exist |
||
| 613 | * @param bool $table_data set false if you only want the creation queries(no data) |
||
| 614 | * @param bool $save set false if you want to receive sql as string |
||
| 615 | * @param string $file_name file name for saving |
||
| 616 | * |
||
| 617 | * @return string if save is true , return file name otherwise return sql data |
||
| 618 | */ |
||
| 619 | public static function backup (array $wanted_tables = null, bool $table_data = true, bool $save = true, string $file_name = ''): string { |
||
| 671 | } |
||
| 672 | } |