修复图表时间和查询区间不匹配问题
This commit is contained in:
39
backend/internal/db/db.go
Normal file
39
backend/internal/db/db.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
// DB 定义数据库连接接口
|
||||
type DB interface {
|
||||
// 获取原始数据库连接
|
||||
GetDB() *sql.DB
|
||||
|
||||
// 执行查询,返回多行结果
|
||||
Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
|
||||
|
||||
// 执行查询,返回单行结果
|
||||
QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row
|
||||
|
||||
// 执行更新操作(INSERT、UPDATE、DELETE)
|
||||
Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
|
||||
|
||||
// 开始事务
|
||||
BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
|
||||
|
||||
// 关闭数据库连接
|
||||
Close() error
|
||||
}
|
||||
|
||||
// Config 数据库配置结构体
|
||||
type Config struct {
|
||||
Type string
|
||||
Host string
|
||||
Port int
|
||||
Username string
|
||||
Password string
|
||||
Database string
|
||||
SSLMode string
|
||||
Charset string
|
||||
}
|
||||
49
backend/internal/db/factory.go
Normal file
49
backend/internal/db/factory.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"log"
|
||||
)
|
||||
|
||||
// NewDB 根据配置创建数据库连接实例
|
||||
func NewDB(cfg Config) (DB, error) {
|
||||
// 只支持MySQL数据库
|
||||
return NewMySQLDB(cfg)
|
||||
}
|
||||
|
||||
// 基础数据库连接结构体
|
||||
type baseDB struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
// GetDB 获取原始数据库连接
|
||||
func (b *baseDB) GetDB() *sql.DB {
|
||||
return b.db
|
||||
}
|
||||
|
||||
// Query 执行查询,返回多行结果
|
||||
func (b *baseDB) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
|
||||
return b.db.QueryContext(ctx, query, args...)
|
||||
}
|
||||
|
||||
// QueryRow 执行查询,返回单行结果
|
||||
func (b *baseDB) QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row {
|
||||
return b.db.QueryRowContext(ctx, query, args...)
|
||||
}
|
||||
|
||||
// Exec 执行更新操作(INSERT、UPDATE、DELETE)
|
||||
func (b *baseDB) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
|
||||
return b.db.ExecContext(ctx, query, args...)
|
||||
}
|
||||
|
||||
// BeginTx 开始事务
|
||||
func (b *baseDB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error) {
|
||||
return b.db.BeginTx(ctx, opts)
|
||||
}
|
||||
|
||||
// Close 关闭数据库连接
|
||||
func (b *baseDB) Close() error {
|
||||
log.Println("Closing database connection")
|
||||
return b.db.Close()
|
||||
}
|
||||
51
backend/internal/db/mysql.go
Normal file
51
backend/internal/db/mysql.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
// 导入MySQL驱动
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
// MySQLDB MySQL数据库连接结构体
|
||||
type MySQLDB struct {
|
||||
baseDB
|
||||
}
|
||||
|
||||
// NewMySQLDB 创建MySQL数据库连接实例
|
||||
func NewMySQLDB(cfg Config) (DB, error) {
|
||||
// 构建MySQL连接字符串
|
||||
// 格式: username:password@tcp(host:port)/database?charset=utf8mb4&parseTime=True&loc=Local
|
||||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=Local",
|
||||
cfg.Username, cfg.Password, cfg.Host, cfg.Port, cfg.Database, cfg.Charset)
|
||||
|
||||
// 打开数据库连接
|
||||
db, err := sql.Open("mysql", dsn)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open mysql connection: %w", err)
|
||||
}
|
||||
|
||||
// 配置连接池
|
||||
db.SetMaxOpenConns(20) // 最大打开连接数
|
||||
db.SetMaxIdleConns(10) // 最大空闲连接数
|
||||
db.SetConnMaxLifetime(30 * time.Minute) // 连接最大生命周期
|
||||
db.SetConnMaxIdleTime(10 * time.Minute) // 连接最大空闲时间
|
||||
|
||||
// 测试连接
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if err := db.PingContext(ctx); err != nil {
|
||||
return nil, fmt.Errorf("failed to ping mysql: %w", err)
|
||||
}
|
||||
|
||||
log.Println("MySQL connection established successfully")
|
||||
|
||||
return &MySQLDB{
|
||||
baseDB: baseDB{db: db},
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user