40 lines
847 B
Go
40 lines
847 B
Go
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
|
||
}
|