options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, 1); self::$DB = $link; if(!is_null($link->connect_error)) { throw new Exception("Connect Error: " . $link->connect_error); } self::query("SET NAMES utf8mb4"); return $link; } /** * @param mysqli_result $result * @param int $row_number * @return bool */ public static function dataSeek(mysqli_result $result, int $row_number): bool { return mysqli_data_seek($result, $row_number); } /** * @param $value * @return string */ public static function escape($value): string { return mysqli_real_escape_string(self::$DB, $value); } public static function escapeNvp(array $nvp): array { foreach($nvp as $name => $value) { $nvp[$name] = self::escape($value); } return $nvp; } /** * @return string */ public static function error(): string { return mysqli_error(self::$DB); } /** * @param mysqli_result $result * @return array|null */ public static function fetchArray(mysqli_result $result): ?array { return mysqli_fetch_array($result); } /** * @param mysqli_result $result * @return array|null */ public static function fetchAssoc(mysqli_result $result): ?array { return mysqli_fetch_assoc($result); } /** * @param mysqli_result $result * @return object|null */ public static function fetchObject(mysqli_result $result): ?object { return mysqli_fetch_object($result); } /** * @param mysqli_result $result * @return array|null */ public static function fetchRow(mysqli_result $result): ?array { return mysqli_fetch_row($result); } public static function freeResult(mysqli_result $result): void { mysqli_free_result($result); } /** * @param array $nvp * @param bool $escape * @return string */ public static function getValueString(array $nvp, bool $escape = false): string { if($escape) { $nvp = self::escapeNvp($nvp); } $string_array = []; foreach($nvp as $field => $value) { if(is_null($value)) { $string_array[] = "`$field`=NULL"; } else if(stripos($field, "binary")!==false && stripos($value, "x'")===0) { $string_array[] = "`$field`=$value"; } else { $string_array[] = "`$field`='$value'"; } } return implode(", ", $string_array); } /** * @param array $nvp * @param array|bool $options * @return string */ public static function getWhereString(array $nvp, $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) { $nvp = self::escapeNvp($nvp); } $string_array = []; foreach($nvp as $field => $value) { if($value === null) { $string_array[] = "`$field` IS NULL"; } else if(is_array($value)) { if(empty($value)) $string_array[] = "0"; // Empty array else $string_array[] = "`$field` IN (" . implode(", ", self::quoteValues($value)) . ")"; } else if($value instanceof Constraint) { $string_array[] = "`$field` " . $value->getConstraintString(); } else { $string_array[] = $fuzzy ? "`$field` LIKE '%$value%'" : "`$field`='$value'"; } } return implode(" AND ", $string_array); } /** * @param string $table * @param string $index_name * @return bool * @throws Exception */ public static function hasIndex(string $table, string $index_name): bool { $sql = "SHOW INDEX FROM `$table` WHERE `Key_name`='$index_name'"; $result = self::query($sql); return self::numRows($result) === 1; } /** * @return int */ public static function insertId(): int { return mysqli_insert_id(self::$DB); } /** * @param mysqli_result $result * @return int */ public static function numRows(mysqli_result $result): int { return mysqli_num_rows($result); } /** * @param mysqli_result $result * @return object|null */ public static function objectOrNull(mysqli_result $result): ?object { return mysqli_num_rows($result)>0 ? self::fetchObject($result) : null; } /** * @param string $table * @param string $field * @param int $id * @return object|null * @throws Exception */ public static function objectWithId(string $table, string $field, int $id): ?object { $sql = "SELECT * FROM `$table` WHERE `$field`='$id'"; $result = self::query($sql); return self::objectOrNull($result); } /** * @return bool */ public static function ping(): bool { return mysqli_ping(self::$DB); } /** * @param string $sql * @return bool|mysqli_result * @throws Exception * @noinspection ForgottenDebugOutputInspection */ public static function query(string $sql) { if(!is_string($sql)) return false; self::$lastQuery = $sql; $result = mysqli_query(self::$DB, $sql); if(!$result) { $error = mysqli_error(self::$DB); error_log("Query failed: $sql\n"); error_log("Error: $error\n"); if(!PRODUCTION) { throw new Exception("Query failed: $sql"); } else { throw new Exception("A database error has occurred. Please try again later."); } } self::$queries[] = $sql; self::$queryCount++; return $result; } /** * @param string $name * @param mysqli|null $link_identifier * @return bool */ public static function selectDb(string $name, mysqli $link_identifier = null): bool { if($link_identifier === null) { return mysqli_select_db(self::$DB, $name); } return mysqli_select_db($link_identifier, $name); } /** * @return int */ public static function threadId(): int { return mysqli_thread_id(self::$DB); } public static function dts(): string { return gmdate("Y-m-d H:i:s"); } public static function quoteValue($value): string { return "'{$value}'"; } public static function quoteValues(array $values): array { return array_map("self::quoteValue", $values); } }