2026-02-14 05:36:00 +08:00
|
|
|
|
package db
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2026-02-18 17:09:52 +08:00
|
|
|
|
"bufio"
|
2026-02-14 05:36:00 +08:00
|
|
|
|
"database/sql"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"os"
|
|
|
|
|
|
"path/filepath"
|
2026-02-18 17:09:52 +08:00
|
|
|
|
"strings"
|
2026-02-14 05:36:00 +08:00
|
|
|
|
"sync"
|
|
|
|
|
|
|
2026-02-15 07:18:14 +08:00
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
2026-02-14 05:36:00 +08:00
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
|
|
|
2026-02-15 07:18:14 +08:00
|
|
|
|
"sunhpc/internal/config"
|
|
|
|
|
|
"sunhpc/internal/log"
|
2026-02-14 05:36:00 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2026-02-15 07:18:14 +08:00
|
|
|
|
// DB wraps the sql.DB connection pool.
|
2026-02-14 05:36:00 +08:00
|
|
|
|
type DB struct {
|
2026-02-15 07:18:14 +08:00
|
|
|
|
engine *sql.DB
|
2026-02-18 17:09:52 +08:00
|
|
|
|
config *config.DBConfig // 保存配置
|
2026-02-14 05:36:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-18 17:09:52 +08:00
|
|
|
|
/*
|
2026-02-15 07:18:14 +08:00
|
|
|
|
// Engine returns the underlying *sql.DB.
|
|
|
|
|
|
func (d *DB) Engine() *sql.DB {
|
|
|
|
|
|
return d.engine
|
2026-02-14 05:36:00 +08:00
|
|
|
|
}
|
2026-02-18 17:09:52 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
func ConfirmWithRetry(prompt string, maxAttempts int) bool {
|
|
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
|
|
|
|
|
|
|
|
for attempt := 1; attempt <= maxAttempts; attempt++ {
|
|
|
|
|
|
log.Infof("%s [y/n]", prompt)
|
|
|
|
|
|
|
|
|
|
|
|
response, err := reader.ReadString('\n')
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
response = strings.ToLower(strings.TrimSpace(response))
|
|
|
|
|
|
|
|
|
|
|
|
switch response {
|
|
|
|
|
|
case "y", "yes":
|
|
|
|
|
|
return true
|
|
|
|
|
|
case "n", "no", "":
|
|
|
|
|
|
return false
|
|
|
|
|
|
default:
|
|
|
|
|
|
if attempt < maxAttempts {
|
|
|
|
|
|
log.Warnf(
|
|
|
|
|
|
"⚠️ 无效输入、请输入 'y' 或 'n'(剩余尝试次数: %d)",
|
|
|
|
|
|
maxAttempts-attempt)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
log.Warn("⚠️ 警告:尝试次数过多、操作已取消")
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
2026-02-14 05:36:00 +08:00
|
|
|
|
|
2026-02-15 07:18:14 +08:00
|
|
|
|
// InitSchema initializes the database schema.
|
|
|
|
|
|
// If force is true, drops existing tables before recreating them.
|
|
|
|
|
|
func (d *DB) InitSchema(force bool) error {
|
2026-02-18 17:09:52 +08:00
|
|
|
|
fullPath := filepath.Join(d.config.Path, d.config.Name)
|
|
|
|
|
|
|
|
|
|
|
|
// 检查文件是否存在
|
|
|
|
|
|
_, err := os.Stat(fullPath)
|
|
|
|
|
|
fileExists := err == nil
|
|
|
|
|
|
|
|
|
|
|
|
// 处理不同的场景
|
|
|
|
|
|
switch {
|
|
|
|
|
|
case !fileExists:
|
|
|
|
|
|
// 场景1:文件不存在,连接并创建(allowCreate = true).
|
|
|
|
|
|
log.Infof("数据库文件不存在,将创建: %s", fullPath)
|
|
|
|
|
|
if err := d.Connect(true); err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
return createTables(d.engine)
|
|
|
|
|
|
|
|
|
|
|
|
case fileExists && !force:
|
|
|
|
|
|
// 场景2:文件存在、无 force 参数、提示友好退出.
|
|
|
|
|
|
log.Warnf("数据库文件已存在: %s", fullPath)
|
|
|
|
|
|
log.Warn("如果需要强制重新初始化,请添加 --force 参数")
|
|
|
|
|
|
log.Warn("数据库已存在、退出初始化操作.")
|
|
|
|
|
|
os.Exit(1)
|
2026-02-14 05:36:00 +08:00
|
|
|
|
|
2026-02-18 17:09:52 +08:00
|
|
|
|
case fileExists && force:
|
|
|
|
|
|
// 场景3:文件存在、force 参数 -> 需要用户确认并重建.
|
|
|
|
|
|
log.Warn("警告:强制重新初始化将清空数据库中的所有数据!")
|
|
|
|
|
|
if !ConfirmWithRetry("是否继续?", 3) {
|
|
|
|
|
|
return fmt.Errorf("用户取消操作")
|
2026-02-14 05:36:00 +08:00
|
|
|
|
}
|
2026-02-18 17:09:52 +08:00
|
|
|
|
|
|
|
|
|
|
// 连接现有数据库(allowCreate = true, 因为文件已经存在)
|
|
|
|
|
|
if err := d.Connect(true); err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 清空现有数据.
|
|
|
|
|
|
if err := dropTables(d.engine); err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 清空表
|
|
|
|
|
|
if err := dropTriggers(d.engine); err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
log.Info("已清空现有数据库触发器")
|
|
|
|
|
|
return createTables(d.engine)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
log.Info("数据库创建成功")
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 辅助函数:检查文件是否存在
|
|
|
|
|
|
func fileExists(path string) bool {
|
|
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
|
|
return false
|
2026-02-14 05:36:00 +08:00
|
|
|
|
}
|
2026-02-18 17:09:52 +08:00
|
|
|
|
// 其他问题(如权限问题)也视为文件不存在,但应该记录日志
|
|
|
|
|
|
log.Debugf("检查数据库文件状态失败: %v", err)
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
2026-02-14 05:36:00 +08:00
|
|
|
|
|
2026-02-18 17:09:52 +08:00
|
|
|
|
// 辅助函数: 创建数据库表
|
|
|
|
|
|
func createTables(db *sql.DB) error {
|
2026-02-15 07:18:14 +08:00
|
|
|
|
// ✅ 调用 schema.go 中的函数
|
|
|
|
|
|
for _, ddl := range CreateTableStatements() {
|
2026-02-18 17:09:52 +08:00
|
|
|
|
log.Debugf("执行: %s", ddl)
|
2026-02-15 07:18:14 +08:00
|
|
|
|
if _, err := db.Exec(ddl); err != nil {
|
2026-02-18 17:09:52 +08:00
|
|
|
|
return fmt.Errorf("数据表创建失败: %w", err)
|
2026-02-14 05:36:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-02-18 17:09:52 +08:00
|
|
|
|
log.Info("数据库表创建成功")
|
2026-02-15 07:18:14 +08:00
|
|
|
|
return nil
|
2026-02-14 05:36:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-15 07:18:14 +08:00
|
|
|
|
func dropTables(db *sql.DB) error {
|
|
|
|
|
|
// ✅ 调用 schema.go 中的函数
|
|
|
|
|
|
for _, table := range DropTableOrder() {
|
|
|
|
|
|
if _, err := db.Exec(fmt.Sprintf("DROP TABLE IF EXISTS `%s`", table)); err != nil {
|
2026-02-14 05:36:00 +08:00
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-18 17:09:52 +08:00
|
|
|
|
func dropTriggers(db *sql.DB) error {
|
|
|
|
|
|
// ✅ 调用 schema.go 中的函数
|
|
|
|
|
|
for _, trigger := range DropTriggerStatements() {
|
|
|
|
|
|
if _, err := db.Exec(fmt.Sprintf("DROP TRIGGER IF EXISTS `%s`", trigger)); err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-15 07:18:14 +08:00
|
|
|
|
// --- Singleton DB Instance ---
|
|
|
|
|
|
var (
|
|
|
|
|
|
globalDB *DB
|
|
|
|
|
|
initOnce sync.Once
|
|
|
|
|
|
initErr error
|
|
|
|
|
|
)
|
2026-02-14 05:36:00 +08:00
|
|
|
|
|
2026-02-15 07:18:14 +08:00
|
|
|
|
func GetDB() (*DB, error) {
|
|
|
|
|
|
initOnce.Do(func() {
|
|
|
|
|
|
cfg, err := config.LoadConfig()
|
2026-02-14 05:36:00 +08:00
|
|
|
|
if err != nil {
|
2026-02-15 07:18:14 +08:00
|
|
|
|
initErr = fmt.Errorf("数据库配置文件加载失败: %w", err)
|
|
|
|
|
|
return
|
2026-02-14 05:36:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-18 17:09:52 +08:00
|
|
|
|
globalDB = &DB{
|
|
|
|
|
|
config: &cfg.DB,
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
return globalDB, initErr
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (d *DB) Connect(allowCreate bool) error {
|
|
|
|
|
|
// 如果已经连接,直接返回
|
|
|
|
|
|
if d.engine != nil {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch d.config.Type {
|
|
|
|
|
|
case "sqlite":
|
|
|
|
|
|
fullPath := filepath.Join(d.config.Path, d.config.Name)
|
|
|
|
|
|
|
|
|
|
|
|
// 检查文件是否存在
|
|
|
|
|
|
_, err := os.Stat(fullPath)
|
|
|
|
|
|
fileExists := err == nil
|
|
|
|
|
|
|
|
|
|
|
|
// 如果文件不存在且不允许创建,返回错误
|
|
|
|
|
|
if !fileExists && !allowCreate {
|
|
|
|
|
|
return fmt.Errorf("数据库文件不存在: %s, 请先初始化.", fullPath)
|
2026-02-14 05:36:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-18 17:09:52 +08:00
|
|
|
|
// 确保目录存在
|
|
|
|
|
|
if err := os.MkdirAll(d.config.Path, 0755); err != nil {
|
|
|
|
|
|
return fmt.Errorf("创建数据库目录失败: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 连接参数
|
|
|
|
|
|
dsn := fmt.Sprintf("%s?_foreign_keys=on&_journal_mode=WAL&_timeout=5000",
|
|
|
|
|
|
fullPath)
|
|
|
|
|
|
|
|
|
|
|
|
engine, err := sql.Open("sqlite3", dsn)
|
2026-02-15 07:18:14 +08:00
|
|
|
|
if err != nil {
|
2026-02-18 17:09:52 +08:00
|
|
|
|
return fmt.Errorf("数据库打开失败: %w", err)
|
2026-02-14 05:36:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-15 07:18:14 +08:00
|
|
|
|
if err := engine.Ping(); err != nil {
|
|
|
|
|
|
engine.Close()
|
2026-02-18 17:09:52 +08:00
|
|
|
|
return fmt.Errorf("数据库连接失败: %w", err)
|
2026-02-14 05:36:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-18 17:09:52 +08:00
|
|
|
|
d.engine = engine
|
|
|
|
|
|
case "mysql":
|
|
|
|
|
|
// TODO: 实现 MySQL 连接逻辑
|
|
|
|
|
|
return fmt.Errorf("mysql 数据库连接未实现")
|
|
|
|
|
|
}
|
2026-02-14 05:36:00 +08:00
|
|
|
|
|
2026-02-18 17:09:52 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Close 关闭数据库连接
|
|
|
|
|
|
func (d *DB) Close() error {
|
|
|
|
|
|
if d.engine != nil {
|
|
|
|
|
|
return d.engine.Close()
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetEngine 获取数据库引擎(自动连接)
|
|
|
|
|
|
func (d *DB) GetEngine() (*sql.DB, error) {
|
|
|
|
|
|
// 如果还没有连接,自动连接(但不创建新文件)
|
|
|
|
|
|
if d.engine == nil {
|
|
|
|
|
|
if err := d.Connect(false); err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return d.engine, nil
|
2026-02-14 05:36:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-15 07:18:14 +08:00
|
|
|
|
// MustGetDB is a helper that panics on error (use in main/init only).
|
|
|
|
|
|
func MustGetDB() *DB {
|
|
|
|
|
|
db, err := GetDB()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
log.Fatalf("数据库初始化失败: %v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
return db
|
2026-02-14 05:36:00 +08:00
|
|
|
|
}
|
2026-02-18 17:09:52 +08:00
|
|
|
|
|
|
|
|
|
|
func GetDBConfig() (*config.DBConfig, error) {
|
|
|
|
|
|
cfg, err := config.LoadConfig()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, fmt.Errorf("数据库配置文件加载失败: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
return &cfg.DB, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func CheckDB() (*config.Config, error) {
|
|
|
|
|
|
cfg, err := config.LoadConfig()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
log.Warnf("加载配置失败: %v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 统一转为小写,避免用户输入错误
|
|
|
|
|
|
dbType := strings.ToLower(cfg.DB.Type)
|
|
|
|
|
|
|
|
|
|
|
|
// 打印配置(调试用)
|
|
|
|
|
|
log.Debugf("数据库类型: %s", dbType)
|
|
|
|
|
|
log.Debugf("数据库名称: %s", cfg.DB.Name)
|
|
|
|
|
|
log.Debugf("数据库路径: %s", cfg.DB.Path)
|
|
|
|
|
|
log.Debugf("数据库用户: %s", cfg.DB.User)
|
|
|
|
|
|
log.Debugf("数据库主机: %s", cfg.DB.Host)
|
|
|
|
|
|
log.Debugf("数据库套接字: %s", cfg.DB.Socket)
|
|
|
|
|
|
log.Debugf("数据库详细日志: %v", cfg.DB.Verbose)
|
|
|
|
|
|
|
|
|
|
|
|
// 支持 sqlite,mysql的常见别名
|
|
|
|
|
|
isSQLite := dbType == "sqlite" || dbType == "sqlite3"
|
|
|
|
|
|
isMySQL := dbType == "mysql"
|
|
|
|
|
|
|
|
|
|
|
|
// 检查数据库类型,只允许 sqlite 和 mysql
|
|
|
|
|
|
if !isSQLite && !isMySQL {
|
|
|
|
|
|
log.Fatalf("不支持的数据库类型: %s(仅支持 sqlite、sqlite3、mysql)", dbType)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 检查数据库路径是否存在
|
|
|
|
|
|
if isSQLite {
|
|
|
|
|
|
if _, err := os.Stat(cfg.DB.Path); os.IsNotExist(err) {
|
|
|
|
|
|
log.Warnf("SQLite 数据库路径 %s 不存在", cfg.DB.Path)
|
|
|
|
|
|
log.Warn("必须先执行 'sunhpc init database' 初始化数据库")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return cfg, nil
|
|
|
|
|
|
}
|