| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- <?php /** @noinspection PhpUnused */
- class DB {
- public static $lastQuery = "";
- public static $queries = [];
- public static $queryCount = 0;
- public static $DB;
- public static function affectedRows(mysqli $link = null): int {
- if($link === null) $link = self::$DB;
- return mysqli_affected_rows($link);
- }
- /**
- * @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 {
- $link = @new mysqli($host, $username, $password, $database, $port);
- 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);
- }
- }
|