150 lines
3.4 KiB
PHP
150 lines
3.4 KiB
PHP
<?php
|
|
namespace App\Core;
|
|
|
|
use PDO;
|
|
use PDOException;
|
|
|
|
/**
|
|
* 数据库操作类 (PDO Singleton)
|
|
*/
|
|
class Database
|
|
{
|
|
private static ?Database $instance = null;
|
|
private PDO $pdo;
|
|
private array $config;
|
|
|
|
private function __construct()
|
|
{
|
|
$this->config = require CONFIG_PATH . '/database.php';
|
|
$this->connect();
|
|
}
|
|
|
|
private function connect(): void
|
|
{
|
|
$dsn = sprintf(
|
|
'%s:host=%s;port=%s;dbname=%s;charset=%s',
|
|
$this->config['driver'],
|
|
$this->config['host'],
|
|
$this->config['port'],
|
|
$this->config['database'],
|
|
$this->config['charset']
|
|
);
|
|
|
|
$options = [
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
|
PDO::ATTR_EMULATE_PREPARES => false,
|
|
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES {$this->config['charset']} COLLATE {$this->config['collation']}",
|
|
];
|
|
|
|
try {
|
|
$this->pdo = new PDO($dsn, $this->config['username'], $this->config['password'], $options);
|
|
} catch (PDOException $e) {
|
|
throw new \RuntimeException('数据库连接失败: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public static function getInstance(): self
|
|
{
|
|
if (self::$instance === null) {
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
public function getPdo(): PDO
|
|
{
|
|
return $this->pdo;
|
|
}
|
|
|
|
public function getPrefix(): string
|
|
{
|
|
return $this->config['prefix'];
|
|
}
|
|
|
|
public function table(string $name): string
|
|
{
|
|
return $this->config['prefix'] . $name;
|
|
}
|
|
|
|
/**
|
|
* 执行查询并返回所有结果
|
|
*/
|
|
public function fetchAll(string $sql, array $params = []): array
|
|
{
|
|
$stmt = $this->pdo->prepare($sql);
|
|
$stmt->execute($params);
|
|
return $stmt->fetchAll();
|
|
}
|
|
|
|
/**
|
|
* 执行查询并返回单行结果
|
|
*/
|
|
public function fetch(string $sql, array $params = []): ?array
|
|
{
|
|
$stmt = $this->pdo->prepare($sql);
|
|
$stmt->execute($params);
|
|
$result = $stmt->fetch();
|
|
return $result ?: null;
|
|
}
|
|
|
|
/**
|
|
* 执行插入并返回最后插入ID
|
|
*/
|
|
public function insert(string $sql, array $params = []): int
|
|
{
|
|
$stmt = $this->pdo->prepare($sql);
|
|
$stmt->execute($params);
|
|
return (int) $this->pdo->lastInsertId();
|
|
}
|
|
|
|
/**
|
|
* 执行更新/删除
|
|
*/
|
|
public function execute(string $sql, array $params = []): int
|
|
{
|
|
$stmt = $this->pdo->prepare($sql);
|
|
$stmt->execute($params);
|
|
return $stmt->rowCount();
|
|
}
|
|
|
|
/**
|
|
* 开启事务
|
|
*/
|
|
public function beginTransaction(): void
|
|
{
|
|
$this->pdo->beginTransaction();
|
|
}
|
|
|
|
/**
|
|
* 提交事务
|
|
*/
|
|
public function commit(): void
|
|
{
|
|
$this->pdo->commit();
|
|
}
|
|
|
|
/**
|
|
* 回滚事务
|
|
*/
|
|
public function rollback(): void
|
|
{
|
|
$this->pdo->rollBack();
|
|
}
|
|
|
|
/**
|
|
* 获取单列值
|
|
*/
|
|
public function fetchColumn(string $sql, array $params = [], int $column = 0): mixed
|
|
{
|
|
$stmt = $this->pdo->prepare($sql);
|
|
$stmt->execute($params);
|
|
return $stmt->fetchColumn($column);
|
|
}
|
|
|
|
private function __clone() {}
|
|
public function __wakeup()
|
|
{
|
|
throw new \RuntimeException('Cannot unserialize singleton');
|
|
}
|
|
} |