bug修复

This commit is contained in:
MasonLiu committed 2026-05-30 15:06:24 +08:00
1 parent 914208aa70
commit 88f7fc7141
8 files changed
+949 -456

No files matched your search

+11 -43
View File
@@ -1,21 +1,13 @@
<?php
/**
* VPS Hub 数据库辅助类
* 提供SQLite数据库的统一访问接口
*/
require_once __DIR__ . '/logger.php';
class DBHelper {
private $db;
private $dbPath;
/**
* 构造函数
* @param string $dbPath 数据库文件路径
*/
public function __construct($dbPath) {
$this->dbPath = $dbPath;
// 确保目录存在
$dir = dirname($dbPath);
if (!is_dir($dir)) {
mkdir($dir, 0755, true);
@@ -32,9 +24,6 @@ class DBHelper {
/**
* 执行查询并返回所有结果
* @param string $sql SQL语句
* @param array $params 参数数组
* @return array 结果数组
*/
public function query($sql, $params = []) {
try {
@@ -42,16 +31,13 @@ class DBHelper {
$stmt->execute($params);
return $stmt->fetchAll();
} catch (PDOException $e) {
error_log("数据库查询错误: " . $e->getMessage() . " | SQL: " . $sql);
Logger::error("数据库查询错误: " . $e->getMessage() . " | SQL: " . $sql, 'DBHelper::query');
return [];
}
}
/**
* 执行查询并返回单条结果
* @param string $sql SQL语句
* @param array $params 参数数组
* @return array|false 结果数组或false
*/
public function queryOne($sql, $params = []) {
try {
@@ -59,32 +45,26 @@ class DBHelper {
$stmt->execute($params);
return $stmt->fetch();
} catch (PDOException $e) {
error_log("数据库查询错误: " . $e->getMessage() . " | SQL: " . $sql);
Logger::error("数据库查询错误: " . $e->getMessage() . " | SQL: " . $sql, 'DBHelper::queryOne');
return false;
}
}
/**
* 执行插入、更新、删除操作
* @param string $sql SQL语句
* @param array $params 参数数组
* @return bool 是否成功
*/
public function execute($sql, $params = []) {
try {
$stmt = $this->db->prepare($sql);
return $stmt->execute($params);
} catch (PDOException $e) {
error_log("数据库执行错误: " . $e->getMessage() . " | SQL: " . $sql);
Logger::error("数据库执行错误: " . $e->getMessage() . " | SQL: " . $sql, 'DBHelper::execute');
return false;
}
}
/**
* 插入数据并返回最后插入的ID
* @param string $sql SQL语句
* @param array $params 参数数组
* @return int|false 最后插入的ID或false
*/
public function insert($sql, $params = []) {
try {
@@ -92,36 +72,23 @@ class DBHelper {
$stmt->execute($params);
return $this->db->lastInsertId();
} catch (PDOException $e) {
error_log("数据库插入错误: " . $e->getMessage() . " | SQL: " . $sql);
Logger::error("数据库插入错误: " . $e->getMessage() . " | SQL: " . $sql, 'DBHelper::insert');
return false;
}
}
/**
* 开始事务
*/
public function beginTransaction() {
return $this->db->beginTransaction();
}
/**
* 提交事务
*/
public function commit() {
return $this->db->commit();
}
/**
* 回滚事务
*/
public function rollBack() {
return $this->db->rollBack();
}
/**
* 获取数据库连接对象(用于高级操作)
* @return PDO
*/
public function getConnection() {
return $this->db;
}
@@ -133,7 +100,6 @@ function getVpsDB() {
if ($db === null) {
$dbPath = __DIR__ . '/db/vps.db';
$db = new DBHelper($dbPath);
// 初始化表结构
initVpsTables($db);
}
return $db;
@@ -156,7 +122,6 @@ function getStatusDB() {
if ($db === null) {
$dbPath = __DIR__ . '/db/status.db';
$db = new DBHelper($dbPath);
// 初始化表结构
initStatusTables($db);
}
return $db;
@@ -176,7 +141,8 @@ function initVpsTables($db) {
api_key TEXT NOT NULL,
auto_monitor BOOLEAN DEFAULT 1,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE(site_url, account)
)
");
}
@@ -199,15 +165,17 @@ function initVpsListTables($db) {
bandwidth TEXT,
os_type TEXT,
status TEXT,
amount TEXT,
nextduedate INTEGER,
section BOOLEAN DEFAULT 0,
last_check TIMESTAMP,
FOREIGN KEY (config_id) REFERENCES configs(id),
UNIQUE(config_id, vps_id)
FOREIGN KEY (config_id) REFERENCES configs(id)
)
");
$db->execute('CREATE INDEX IF NOT EXISTS idx_vps_config ON vps_list(config_id)');
$db->execute('CREATE INDEX IF NOT EXISTS idx_vps_vps_id ON vps_list(vps_id)');
$db->execute('CREATE INDEX IF NOT EXISTS idx_vps_unique ON vps_list(vps_id, ip_address)');
}
/**