| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- <?php
- namespace App;
- use Exception;
- use mysqli;
- use mysqli_result;
- use RuntimeException;
- use App\Constraint\Constraint;
- class Database {
- /** @var string */
- public static $lastQuery;
- /** @var string[] */
- public static $queries = [];
- /** @var int */
- public static $queryCount = 0;
- /** @var mysqli */
- public static $connection;
- public static function affectedRows(): int {
- return mysqli_affected_rows(self::$connection);
- }
- /**
- * @param string $host
- * @param string $username
- * @param string $password
- * @param string $database
- * @param int $port
- * @return mysqli
- * @throws Exception
- */
- public static function connect(string $host, string $username, string $password, string $database, int $port = 3306): mysqli {
- $connection = @new mysqli($host, $username, $password, $database, $port);
- if($connection->connect_errno !== 0) {
- throw new RuntimeException("Connect Error: " . $connection->connect_error);
- }
- self::$connection = $connection;
- self::query("SET NAMES utf8mb4");
- self::query("SET SESSION sql_mode='ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'");
- return self::$connection;
- }
- public static function dataSeek(mysqli_result $result, int $row_number): bool {
- return mysqli_data_seek($result, $row_number);
- }
- public static function escape(?string $string): string {
- return mysqli_real_escape_string(self::$connection, $string);
- }
- public static function error(): string {
- return mysqli_error(self::$connection);
- }
- public static function fetchArray(mysqli_result $result): ?array {
- return mysqli_fetch_array($result);
- }
- public static function fetchAssoc(mysqli_result $result): ?array {
- return mysqli_fetch_assoc($result);
- }
- public static function fetchObject(mysqli_result $result): ?object {
- return mysqli_fetch_object($result);
- }
- public static function fetchRow(mysqli_result $result): ?array {
- return mysqli_fetch_row($result);
- }
- public static function freeResult($result): void {
- mysqli_free_result($result);
- }
- public static function getValueString(array $pairs, bool $escape = false): string {
- if($escape) {
- $pairs = array_map("DB::escape", $pairs);
- }
- $string_array = [];
- foreach($pairs as $field => $value) {
- if($value === null) {
- $string_array[] = "`$field`=NULL";
- }
- else {
- $string_array[] = "`$field`='$value'";
- }
- }
- return implode(", ", $string_array);
- }
- /**
- * @param array $pairs
- * @param array|bool $options
- * @return string
- */
- public static function getWhereString(array $pairs, $options = []): string {
- $escape = false;
- $fuzzy = false;
- if(is_bool($options)) { // Legacy escape
- $escape = $options;
- }
- if(is_array($options)) {
- $escape = $options["escape"] ?? false;
- $fuzzy = $options["fuzzy"] ?? false;
- }
- if($escape) {
- foreach($pairs as &$value) {
- $value = self::escape($value);
- }
- unset($value);
- }
- $parts = [];
- foreach($pairs as $field => $value) {
- if($value === null) {
- $parts[] = "`$field` IS NULL";
- }
- else if(is_array($value)) {
- $values = array_map(static function(string $string) {
- return "'$string'";
- }, $value);
- if(empty($value)) {
- $parts[] = "0";
- } // Empty array
- else {
- $parts[] = "`$field` IN (" . implode(", ", $values) . ")";
- }
- }
- else if(is_a($value, Constraint::class)) {
- /** @var Constraint $value */
- $parts[] = "`$field` " . $value->getString();
- }
- else {
- $parts[] = $fuzzy ? "`$field` LIKE '%$value%'" : "`$field`='$value'";
- }
- }
- return implode(" AND ", $parts);
- }
- /**
- * @param string $table
- * @param string $name
- * @return bool
- * @throws Exception
- */
- public static function hasIndex(string $table, string $name): bool {
- $sql = "SHOW INDEX FROM `$table` WHERE `Key_name`='$name'";
- $result = self::query($sql);
- return self::numRows($result) > 0;
- }
- public static function insertId() {
- return mysqli_insert_id(self::$connection);
- }
- /**
- * @param mysqli_result|bool $result
- * @return int
- */
- public static function numRows($result): int {
- if(is_bool($result)) {
- return 0;
- }
- return mysqli_num_rows($result);
- }
- public static function objectOrNull(mysqli_result $result): ?object {
- return mysqli_num_rows($result)>0 ? self::fetchObject($result) : null;
- }
- public static function ping(): bool {
- return mysqli_ping(self::$connection);
- }
- /**
- * @param string $sql
- * @return bool|mysqli_result
- * @throws Exception
- */
- public static function query(string $sql) {
- if(self::$connection === null) {
- return false;
- }
- $is_development = ($_ENV["ENV"] === "development");
- self::$queries[] = $sql;
- self::$lastQuery = $sql;
- self::$queryCount++;
- $result = mysqli_query(self::$connection, $sql);
- if($result === false) {
- if($is_development) {
- $error = self::error();
- throw new RuntimeException("Query failed: $error $sql");
- }
- throw new RuntimeException("A database error has occurred. Please try again later.");
- }
- return $result;
- }
- public static function selectDatabase(string $name): void {
- mysqli_select_db(self::$connection, $name);
- }
- public static function dts(): string {
- return gmdate("Y-m-d H:i:s");
- }
- }
|