db.class.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. class db
  3. {
  4. private static PDO $dbh;
  5. private static string $error = '';
  6. private static PDOStatement $stmt;
  7. private static function connect(): void
  8. {
  9. if (isset(self::$dbh)) {
  10. return;
  11. }
  12. $dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=utf8mb4";
  13. $options = [
  14. PDO::ATTR_PERSISTENT => true,
  15. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  16. ];
  17. try {
  18. self::$dbh = new PDO($dsn, DB_USER, DB_PASS, $options);
  19. } catch (PDOException $e) {
  20. self::$error = $e->getMessage();
  21. throw new Exception("Database connection error: " . self::$error);
  22. }
  23. }
  24. public static function version(): string
  25. {
  26. self::connect();
  27. return self::$dbh->getAttribute(PDO::ATTR_SERVER_VERSION);
  28. }
  29. public static function query(string $query): void
  30. {
  31. self::connect();
  32. self::$stmt = self::$dbh->prepare($query);
  33. }
  34. public static function bind(string $param, mixed $value = null, ?int $type = null): void
  35. {
  36. if (is_null($type)) {
  37. $type = match (true) {
  38. is_int($value) => PDO::PARAM_INT,
  39. is_bool($value) => PDO::PARAM_BOOL,
  40. is_null($value) => PDO::PARAM_NULL,
  41. default => PDO::PARAM_STR,
  42. };
  43. }
  44. self::$stmt->bindValue($param, $value, $type);
  45. }
  46. public static function execute(): bool
  47. {
  48. self::connect();
  49. if (class_exists('debug') && method_exists('debug', 'isFile') && debug::isFile("sql")) {
  50. ob_start();
  51. $result = self::$stmt->execute();
  52. self::$stmt->debugDumpParams();
  53. $debugOutput = ob_get_clean();
  54. debug::logSession($debugOutput);
  55. return $result;
  56. }
  57. return self::$stmt->execute();
  58. }
  59. public static function resultset(): array
  60. {
  61. self::execute();
  62. return self::$stmt->fetchAll(PDO::FETCH_ASSOC);
  63. }
  64. public static function single(): array|false
  65. {
  66. self::execute();
  67. return self::$stmt->fetch(PDO::FETCH_ASSOC);
  68. }
  69. public static function rowCount(): int
  70. {
  71. return self::$stmt->rowCount();
  72. }
  73. public static function lastInsertId(): string
  74. {
  75. self::connect();
  76. return self::$dbh->lastInsertId();
  77. }
  78. public static function beginTransaction(): bool
  79. {
  80. self::connect();
  81. return self::$dbh->beginTransaction();
  82. }
  83. public static function endTransaction(): bool
  84. {
  85. self::connect();
  86. return self::$dbh->commit();
  87. }
  88. public static function cancelTransaction(): bool
  89. {
  90. self::connect();
  91. return self::$dbh->rollBack();
  92. }
  93. public static function debugDumpParams(): void
  94. {
  95. self::connect();
  96. self::$stmt->debugDumpParams();
  97. }
  98. public static function queryError(): void
  99. {
  100. self::connect();
  101. $qError = self::$dbh->errorInfo();
  102. if (!empty($qError[2])) {
  103. echo "Query Error: " . $qError[2];
  104. }
  105. }
  106. public static function getError(): string
  107. {
  108. return self::$error;
  109. }
  110. }