db.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php /** @noinspection PhpUnused */
  2. class DB {
  3. public static $lastQuery = "";
  4. public static $queries = [];
  5. public static $queryCount = 0;
  6. public static $DB;
  7. public static function affectedRows(mysqli $link = null): int {
  8. if($link === null) $link = self::$DB;
  9. return mysqli_affected_rows($link);
  10. }
  11. /**
  12. * @param string $host
  13. * @param string $username
  14. * @param string $password
  15. * @param string $database
  16. * @param int $port
  17. * @return mysqli
  18. * @throws Exception
  19. */
  20. public static function connect(string $host, string $username, string $password, string $database = "", int $port = 3306): mysqli {
  21. $link = @new mysqli($host, $username, $password, $database, $port);
  22. self::$DB = $link;
  23. if(!is_null($link->connect_error)) {
  24. throw new Exception("Connect Error: " . $link->connect_error);
  25. }
  26. self::query("SET NAMES utf8mb4");
  27. return $link;
  28. }
  29. /**
  30. * @param mysqli_result $result
  31. * @param int $row_number
  32. * @return bool
  33. */
  34. public static function dataSeek(mysqli_result $result, int $row_number): bool {
  35. return mysqli_data_seek($result, $row_number);
  36. }
  37. /**
  38. * @param $value
  39. * @return string
  40. */
  41. public static function escape($value): string {
  42. return mysqli_real_escape_string(self::$DB, $value);
  43. }
  44. public static function escapeNvp(array $nvp): array {
  45. foreach($nvp as $name => $value) {
  46. $nvp[$name] = self::escape($value);
  47. }
  48. return $nvp;
  49. }
  50. /**
  51. * @return string
  52. */
  53. public static function error(): string {
  54. return mysqli_error(self::$DB);
  55. }
  56. /**
  57. * @param mysqli_result $result
  58. * @return array|null
  59. */
  60. public static function fetchArray(mysqli_result $result): ?array {
  61. return mysqli_fetch_array($result);
  62. }
  63. /**
  64. * @param mysqli_result $result
  65. * @return array|null
  66. */
  67. public static function fetchAssoc(mysqli_result $result): ?array {
  68. return mysqli_fetch_assoc($result);
  69. }
  70. /**
  71. * @param mysqli_result $result
  72. * @return object|null
  73. */
  74. public static function fetchObject(mysqli_result $result): ?object {
  75. return mysqli_fetch_object($result);
  76. }
  77. /**
  78. * @param mysqli_result $result
  79. * @return array|null
  80. */
  81. public static function fetchRow(mysqli_result $result): ?array {
  82. return mysqli_fetch_row($result);
  83. }
  84. public static function freeResult(mysqli_result $result): void {
  85. mysqli_free_result($result);
  86. }
  87. /**
  88. * @param array $nvp
  89. * @param bool $escape
  90. * @return string
  91. */
  92. public static function getValueString(array $nvp, bool $escape = false): string {
  93. if($escape) {
  94. $nvp = self::escapeNvp($nvp);
  95. }
  96. $string_array = [];
  97. foreach($nvp as $field => $value) {
  98. if(is_null($value)) {
  99. $string_array[] = "`$field`=NULL";
  100. }
  101. else if(stripos($field, "binary")!==false && stripos($value, "x'")===0) {
  102. $string_array[] = "`$field`=$value";
  103. }
  104. else {
  105. $string_array[] = "`$field`='$value'";
  106. }
  107. }
  108. return implode(", ", $string_array);
  109. }
  110. /**
  111. * @param array $nvp
  112. * @param array|bool $options
  113. * @return string
  114. */
  115. public static function getWhereString(array $nvp, $options = []): string {
  116. $escape = false;
  117. $fuzzy = false;
  118. if(is_bool($options)) { // Legacy escape
  119. $escape = $options;
  120. }
  121. if(is_array($options)) {
  122. $escape = $options["escape"] ?? false;
  123. $fuzzy = $options["fuzzy"] ?? false;
  124. }
  125. if($escape) {
  126. $nvp = self::escapeNvp($nvp);
  127. }
  128. $string_array = [];
  129. foreach($nvp as $field => $value) {
  130. if($value === null) {
  131. $string_array[] = "`$field` IS NULL";
  132. }
  133. else if(is_array($value)) {
  134. if(empty($value)) $string_array[] = "0"; // Empty array
  135. else $string_array[] = "`$field` IN (" . implode(", ", self::quoteValues($value)) . ")";
  136. }
  137. else if($value instanceof Constraint) {
  138. $string_array[] = "`$field` " . $value->getConstraintString();
  139. }
  140. else {
  141. $string_array[] = $fuzzy ? "`$field` LIKE '%$value%'" : "`$field`='$value'";
  142. }
  143. }
  144. return implode(" AND ", $string_array);
  145. }
  146. /**
  147. * @param string $table
  148. * @param string $index_name
  149. * @return bool
  150. * @throws Exception
  151. */
  152. public static function hasIndex(string $table, string $index_name): bool {
  153. $sql = "SHOW INDEX FROM `$table` WHERE `Key_name`='$index_name'";
  154. $result = self::query($sql);
  155. return self::numRows($result) === 1;
  156. }
  157. /**
  158. * @return int
  159. */
  160. public static function insertId(): int {
  161. return mysqli_insert_id(self::$DB);
  162. }
  163. /**
  164. * @param mysqli_result $result
  165. * @return int
  166. */
  167. public static function numRows(mysqli_result $result): int {
  168. return mysqli_num_rows($result);
  169. }
  170. /**
  171. * @param mysqli_result $result
  172. * @return object|null
  173. */
  174. public static function objectOrNull(mysqli_result $result): ?object {
  175. return mysqli_num_rows($result)>0 ? self::fetchObject($result) : null;
  176. }
  177. /**
  178. * @param string $table
  179. * @param string $field
  180. * @param int $id
  181. * @return object|null
  182. * @throws Exception
  183. */
  184. public static function objectWithId(string $table, string $field, int $id): ?object {
  185. $sql = "SELECT * FROM `$table` WHERE `$field`='$id'";
  186. $result = self::query($sql);
  187. return self::objectOrNull($result);
  188. }
  189. /**
  190. * @return bool
  191. */
  192. public static function ping(): bool {
  193. return mysqli_ping(self::$DB);
  194. }
  195. /**
  196. * @param string $sql
  197. * @return bool|mysqli_result
  198. * @throws Exception
  199. * @noinspection ForgottenDebugOutputInspection
  200. */
  201. public static function query(string $sql) {
  202. if(!is_string($sql)) return false;
  203. self::$lastQuery = $sql;
  204. $result = mysqli_query(self::$DB, $sql);
  205. if(!$result) {
  206. $error = mysqli_error(self::$DB);
  207. error_log("Query failed: $sql\n");
  208. error_log("Error: $error\n");
  209. if(!PRODUCTION) {
  210. throw new Exception("Query failed: $sql");
  211. }
  212. else {
  213. throw new Exception("A database error has occurred. Please try again later.");
  214. }
  215. }
  216. self::$queries[] = $sql;
  217. self::$queryCount++;
  218. return $result;
  219. }
  220. /**
  221. * @param string $name
  222. * @param mysqli|null $link_identifier
  223. * @return bool
  224. */
  225. public static function selectDb(string $name, mysqli $link_identifier = null): bool {
  226. if($link_identifier === null) {
  227. return mysqli_select_db(self::$DB, $name);
  228. }
  229. return mysqli_select_db($link_identifier, $name);
  230. }
  231. /**
  232. * @return int
  233. */
  234. public static function threadId(): int {
  235. return mysqli_thread_id(self::$DB);
  236. }
  237. public static function dts(): string {
  238. return gmdate("Y-m-d H:i:s");
  239. }
  240. public static function quoteValue($value): string {
  241. return "'{$value}'";
  242. }
  243. public static function quoteValues(array $values): array {
  244. return array_map("self::quoteValue", $values);
  245. }
  246. }