|
|
@@ -2,135 +2,128 @@
|
|
|
|
|
|
class db
|
|
|
{
|
|
|
- private static $dbh;
|
|
|
- private static $error;
|
|
|
- private static $stmt;
|
|
|
+ private static PDO $dbh;
|
|
|
+ private static string $error = '';
|
|
|
+ private static PDOStatement $stmt;
|
|
|
|
|
|
- private static function connexion()
|
|
|
+ private static function connect(): void
|
|
|
{
|
|
|
- if (self::$dbh) {
|
|
|
+ if (isset(self::$dbh)) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- $dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME;
|
|
|
- $options = array(
|
|
|
+ $dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=utf8mb4";
|
|
|
+ $options = [
|
|
|
PDO::ATTR_PERSISTENT => true,
|
|
|
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
|
|
|
- );
|
|
|
+ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
|
|
|
+ ];
|
|
|
|
|
|
try {
|
|
|
self::$dbh = new PDO($dsn, DB_USER, DB_PASS, $options);
|
|
|
} catch (PDOException $e) {
|
|
|
self::$error = $e->getMessage();
|
|
|
- throw new Exception(self::$error);
|
|
|
+ throw new Exception("Database connection error: " . self::$error);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static function version()
|
|
|
+ public static function version(): string
|
|
|
{
|
|
|
- self::connexion();
|
|
|
+ self::connect();
|
|
|
return self::$dbh->getAttribute(PDO::ATTR_SERVER_VERSION);
|
|
|
}
|
|
|
|
|
|
- public static function query(string $query)
|
|
|
+ public static function query(string $query): void
|
|
|
{
|
|
|
- self::connexion();
|
|
|
+ self::connect();
|
|
|
self::$stmt = self::$dbh->prepare($query);
|
|
|
}
|
|
|
|
|
|
- public static function bind(string $param, $value = null, $type = null)
|
|
|
+ public static function bind(string $param, mixed $value = null, ?int $type = null): void
|
|
|
{
|
|
|
if (is_null($type)) {
|
|
|
- switch (true) {
|
|
|
- case is_int($value):
|
|
|
- $type = PDO::PARAM_INT;
|
|
|
- break;
|
|
|
- case is_bool($value):
|
|
|
- $type = PDO::PARAM_BOOL;
|
|
|
- break;
|
|
|
- case is_null($value):
|
|
|
- $type = PDO::PARAM_NULL;
|
|
|
- break;
|
|
|
- default:
|
|
|
- $type = PDO::PARAM_STR;
|
|
|
- }
|
|
|
+ $type = match (true) {
|
|
|
+ is_int($value) => PDO::PARAM_INT,
|
|
|
+ is_bool($value) => PDO::PARAM_BOOL,
|
|
|
+ is_null($value) => PDO::PARAM_NULL,
|
|
|
+ default => PDO::PARAM_STR,
|
|
|
+ };
|
|
|
}
|
|
|
self::$stmt->bindValue($param, $value, $type);
|
|
|
}
|
|
|
|
|
|
- public static function execute()
|
|
|
+ public static function execute(): bool
|
|
|
{
|
|
|
- self::connexion();
|
|
|
- if (debug::isFile("sql")) {
|
|
|
+ self::connect();
|
|
|
+ if (class_exists('debug') && method_exists('debug', 'isFile') && debug::isFile("sql")) {
|
|
|
ob_start();
|
|
|
- $return = self::$stmt->execute();
|
|
|
+ $result = self::$stmt->execute();
|
|
|
self::$stmt->debugDumpParams();
|
|
|
$debugOutput = ob_get_clean();
|
|
|
debug::logSession($debugOutput);
|
|
|
- return $return;
|
|
|
- } else {
|
|
|
- return self::$stmt->execute();
|
|
|
+ return $result;
|
|
|
}
|
|
|
+ return self::$stmt->execute();
|
|
|
}
|
|
|
|
|
|
- public static function resultset()
|
|
|
+ public static function resultset(): array
|
|
|
{
|
|
|
self::execute();
|
|
|
return self::$stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
}
|
|
|
|
|
|
- public static function single()
|
|
|
+ public static function single(): array|false
|
|
|
{
|
|
|
self::execute();
|
|
|
return self::$stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
}
|
|
|
|
|
|
- public static function rowCount()
|
|
|
+ public static function rowCount(): int
|
|
|
{
|
|
|
return self::$stmt->rowCount();
|
|
|
}
|
|
|
|
|
|
- public static function lastInsertId()
|
|
|
+ public static function lastInsertId(): string
|
|
|
{
|
|
|
- self::connexion();
|
|
|
+ self::connect();
|
|
|
return self::$dbh->lastInsertId();
|
|
|
}
|
|
|
|
|
|
- public static function beginTransaction()
|
|
|
+ public static function beginTransaction(): bool
|
|
|
{
|
|
|
- self::connexion();
|
|
|
+ self::connect();
|
|
|
return self::$dbh->beginTransaction();
|
|
|
}
|
|
|
|
|
|
- public static function endTransaction()
|
|
|
+ public static function endTransaction(): bool
|
|
|
{
|
|
|
- self::connexion();
|
|
|
+ self::connect();
|
|
|
return self::$dbh->commit();
|
|
|
}
|
|
|
|
|
|
- public static function cancelTransaction()
|
|
|
+ public static function cancelTransaction(): bool
|
|
|
{
|
|
|
- self::connexion();
|
|
|
+ self::connect();
|
|
|
return self::$dbh->rollBack();
|
|
|
}
|
|
|
|
|
|
- public static function debugDumpParams()
|
|
|
+ public static function debugDumpParams(): void
|
|
|
{
|
|
|
- self::connexion();
|
|
|
- return self::$stmt->debugDumpParams();
|
|
|
+ self::connect();
|
|
|
+ self::$stmt->debugDumpParams();
|
|
|
}
|
|
|
|
|
|
- public static function queryError()
|
|
|
+ public static function queryError(): void
|
|
|
{
|
|
|
- self::connexion();
|
|
|
+ self::connect();
|
|
|
$qError = self::$dbh->errorInfo();
|
|
|
- if (!is_null($qError[2])) {
|
|
|
- echo $qError[2];
|
|
|
+ if (!empty($qError[2])) {
|
|
|
+ echo "Query Error: " . $qError[2];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static function getError()
|
|
|
+ public static function getError(): string
|
|
|
{
|
|
|
return self::$error;
|
|
|
}
|
|
|
}
|
|
|
+
|