database

package
v1.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

README

Database Package

基于 GORM 的数据库封装,提供简洁、可扩展、易用的数据库操作接口。

✨ 特性

  • 多数据库支持: MySQL、PostgreSQL、SQLite
  • 配置验证: 完整的配置校验机制
  • 连接池管理: 自动配置连接池参数
  • 连接重试: 智能重试机制,支持指数退避和抖动
  • 日志自定义: 支持文件日志输出
  • 读写分离预留: 为未来扩展预留接口
  • 线程安全: 支持并发访问
  • 错误处理: 友好的错误信息
  • GORM Gen 兼容: 直接将 *gorm.DB 注入生成代码,事务/上下文保持一致

🚀 快速开始

基础使用 - 简化配置
package main

import (
    "go-kit/pkg/database"
    "gorm.io/gorm"
)

func main() {
    // 1. 使用Builder模式创建配置
    config := database.NewConfigBuilder().
        SQLite(":memory:").
        Build()

    // 2. 创建数据库连接
    db, err := database.New(config)
    if err != nil {
        panic(err)
    }
    defer db.Close()

    // 3. 获取GORM实例
    gormDB := db.GetDB()
    
    // 4. 使用GORM进行数据库操作
    // ... 你的业务逻辑
}
与 gorm/gen 集成
// 假设生成的 query 包路径为 "yourapp/internal/query"
import (
    "context"
    "database/sql"

    "github.com/tsopia/go-kit/database"
    "gorm.io/gorm"
    "yourapp/internal/model" // gorm/gen 生成的 model 包路径
    "yourapp/internal/query" // gorm/gen 生成的 query 包路径
)

func main() {
    cfg := database.NewConfigBuilder().
        MySQL("127.0.0.1", "root", "password", "app").
        Build()

    db, err := database.New(cfg)
    if err != nil {
        panic(err)
    }
    defer db.Close()

    // 将封装后的 *gorm.DB 传给 gorm/gen 生成的 query 层
    q := query.Use(db.GetDB())

    ctx := context.Background()

    // 在同一个事务中执行多张表的操作(gorm/gen 默认 Transaction 基于 *gorm.DB)
    err = db.TransactionWithContext(ctx, func(tx *gorm.DB) error {
        // 将事务会话透传给生成的 query(Use 会克隆 tx,WithContext 传递 ctx)
        qtx := query.Use(tx).WithContext(ctx)

        if _, err := qtx.User.Create(&model.User{Name: "foo"}); err != nil {
            return err
        }

        _, err := qtx.Account.Create(&model.Account{UserID: 1, Balance: 100})
        return err
    })
    if err != nil {
        panic(err)
    }
}

如果需要指定隔离级别/读写模式,可使用 BeginTx 显式开启事务并传入 sql.TxOptions

tx, err := db.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelSerializable, ReadOnly: false})
if err != nil {
    panic(err)
}
qtx := query.Use(tx).WithContext(ctx)
// ...业务 SQL...
if err := tx.Commit().Error; err != nil {
    _ = tx.Rollback()
    panic(err)
}
封装接口与受控逃逸

Database 同时实现了 database.DB 接口,优先使用封装好的方法,必要时再通过受控逃逸访问底层 *gorm.DB

cfg := database.NewConfigBuilder().MySQL("127.0.0.1", "root", "pwd", "app").Build()

db, err := database.NewWithOptions(
    cfg,
    database.WithLogger(myLogger),
    database.WithHooks(database.Hooks{BeforeProbe: func(ctx context.Context) error {
        // 探针前的自定义检查,例如 feature flag
        return nil
    }}),
)
if err != nil {
    panic(err)
}
defer db.Close()

// 优先使用封装接口
if err := db.Exec(ctx, "UPDATE users SET last_seen = NOW() WHERE id = ?", userID); err != nil {
    panic(err)
}
var u userModel
if err := db.Query(ctx, &u, "SELECT * FROM users WHERE id = ?", userID); err != nil {
    panic(err)
}

// 需要 gorm 高级能力时,再受控获取
gormDB := db.Raw()

// 需要专用连接或 driver 原生能力(如 PostgreSQL LISTEN/NOTIFY、pgvector 扩展)
sqlDB, err := db.SQLDB()
if err != nil {
    panic(err)
}
defer sqlDB.Close()
组件化职责(Phase 1)

数据库封装已拆分为可替换的职责组件,便于按需扩展或接入自定义实现:

  • Connector:负责配置验证、连接建立与重试、命名策略以及连接池参数配置。
  • Executor:承载 Exec / Query / Tx / BeginTx 等常规执行能力。
  • HealthChecker:实现 Ping / HealthCheck / HealthCheckWithContext,串联探针钩子。

默认实现随 database.New/NewWithOptions 一起注入;若需要接入自定义拨号器或观测增强,可以通过 WithConnectorWithExecutorWithHealthChecker 选项替换对应组件,无需修改业务代码。

命名与兼容约定
  • GetDB()Raw() 属于公开 API,保持向后兼容。
  • 内部私有辅助方法可重构或去重,但不应改变导出方法行为与线程安全语义。
PostgreSQL 高级能力示例
LISTEN/NOTIFY(类消息队列)

GORM 不直接暴露异步通知 API,但可以通过 SQLDB() 拿到底层连接,并结合 pgx stdlib 驱动访问原生 *pgconn.PgConn

import (
    "context"
    "log"

    "github.com/jackc/pgx/v5/pgconn"
    "github.com/jackc/pgx/v5/stdlib"
    "github.com/tsopia/go-kit/database"
)

db, _ := database.New(database.NewConfigBuilder().
    PostgreSQL("127.0.0.1", "postgres", "pwd", "app").Build())
ctx := context.Background()

sqlDB, _ := db.SQLDB()
conn, _ := sqlDB.Conn(ctx)
defer conn.Close()

var pgConn *pgconn.PgConn
_ = conn.Raw(func(dc interface{}) error {
    stdConn, ok := dc.(*stdlib.Conn)
    if !ok {
        return fmt.Errorf("unexpected driver conn %T", dc)
    }
    pgConn = stdConn.Conn()
    return nil
})

if _, err := pgConn.Exec(ctx, "LISTEN events").ReadAll(); err != nil {
    log.Fatal(err)
}
for {
    notif, err := pgConn.WaitForNotification(ctx)
    if err != nil {
        log.Fatal(err)
    }
    log.Printf("channel=%s payload=%s", notif.Channel, notif.Payload)
}
pgvector 嵌入向量

pgvector 需通过扩展启用,创建列时可直接使用 Raw()/GORM 声明字段类型:

import "github.com/pgvector/pgvector-go"

// 启用扩展
_ = db.Raw().Exec("CREATE EXTENSION IF NOT EXISTS vector").Error

// 定义模型
type Item struct {
    ID        int64
    Embedding pgvector.Vector `gorm:"type:vector(3)"`
}

// 相似度查询
var items []Item
err := db.Raw().
    Where("embedding <-> ? < 0.3", pgvector.NewVector([]float32{0.1, 0.2, 0.3})).
    Find(&items).Error
高级配置 - 自定义重试策略
// 自定义重试配置
config := &database.Config{
    Driver:   "mysql",
    Host:     "localhost",
    Port:     3306,
    Username: "root",
    Password: "password",
    Database: "test",
    
    // 自定义重试策略
    RetryEnabled:       true,
    RetryMaxAttempts:   5,                    // 最大重试5次
    RetryInitialDelay:  500 * time.Millisecond, // 初始延迟0.5秒
    RetryMaxDelay:      10 * time.Second,      // 最大延迟10秒
    RetryBackoffFactor: 1.5,                   // 退避因子1.5
    RetryJitterEnabled: true,                  // 启用抖动
}

db, err := database.New(config)
if err != nil {
    log.Fatal(err)
}
defer db.Close()
禁用重试
// 禁用重试机制
config := &database.Config{
    Driver:   "sqlite",
    Database: "test.db",
    
    // 禁用重试
    RetryEnabled:     false,
    RetryMaxAttempts: 1, // 或者设置为1
}
不同数据库的简化配置
// MySQL - 最简单
config := database.NewConfigBuilder().
    MySQL("localhost", "root", "password", "test_db").
    Build()

// PostgreSQL - 带高级选项
config := database.NewConfigBuilder().
    PostgreSQL("localhost", "postgres", "password", "test_db").
    WithPort(5432).
    WithSSLMode("disable").
    WithLogFile("/tmp/pg.log").
    Build()

// SQLite - 内存数据库
config := database.NewConfigBuilder().
    SQLite(":memory:").
    Build()
高级配置
config := &database.Config{
    Driver:          "mysql",
    Host:            "localhost",
    Port:            3306,
    Username:        "root",
    Password:        "password",
    Database:        "test_db",
    Charset:         "utf8mb4",
    Timezone:        "Local",
    
    // 连接池配置
    MaxIdleConns:    10,
    MaxOpenConns:    100,
    ConnMaxLifetime: time.Hour,
    ConnMaxIdleTime: 10 * time.Minute,
    
    // 日志配置
    LogLevel:        "info",
    SlowThreshold:   200 * time.Millisecond,
    Colorful:        true,
    LogOutput:       "file:///var/log/db.log", // 文件日志
    
    // 命名策略
    TablePrefix:     "app_",
    SingularTable:   true,
    
    // 其他配置
    DisableForeignKey: false,
    PrepareStmt:       true,
    DryRun:            false,
}

📋 配置说明

基础配置
字段 类型 说明 必填
Driver string 数据库驱动 (mysql/postgres/sqlite)
Host string 数据库主机 MySQL/PostgreSQL必填
Port int 数据库端口 MySQL/PostgreSQL必填
Username string 数据库用户名 MySQL/PostgreSQL必填
Password string 数据库密码
Database string 数据库名/文件路径
连接池配置
字段 类型 默认值 说明
MaxIdleConns int 10 最大空闲连接数
MaxOpenConns int 100 最大打开连接数
ConnMaxLifetime time.Duration 1小时 连接最大生命周期
ConnMaxIdleTime time.Duration 10分钟 空闲连接最大时间
日志配置
字段 类型 默认值 说明
LogLevel string "info" 日志级别 (silent/error/warn/info)
SlowThreshold time.Duration 200ms 慢查询阈值
Colorful bool false 是否彩色输出
LogOutput string "" 日志输出路径 (file:///path/to/log)
重试配置
字段 类型 默认值 说明
RetryEnabled bool true 是否启用重试 (当MaxAttempts>1时自动启用)
RetryMaxAttempts int 3 最大重试次数
RetryInitialDelay time.Duration 1s 初始重试延迟
RetryMaxDelay time.Duration 30s 最大重试延迟
RetryBackoffFactor float64 2.0 退避因子 (指数退避)
RetryJitterEnabled bool true 是否启用抖动 (避免雷群效应)

🔧 API 参考

Config
type Config struct {
    // 基础配置
    Driver   string
    Host     string
    Port     int
    Username string
    Password string
    Database string
    Charset  string
    SSLMode  string
    Timezone string
    
    // 连接池配置
    MaxIdleConns    int
    MaxOpenConns    int
    ConnMaxLifetime time.Duration
    ConnMaxIdleTime time.Duration
    
    // 日志配置
    LogLevel      string
    SlowThreshold time.Duration
    Colorful      bool
    LogOutput     string
    
    // 重试配置
    RetryEnabled       bool
    RetryMaxAttempts   int
    RetryInitialDelay  time.Duration
    RetryMaxDelay      time.Duration
    RetryBackoffFactor float64
    RetryJitterEnabled bool
    
    // 读写分离配置
    ReadReplicas  []ReplicaConfig
    WriteReplicas []ReplicaConfig
    
    // 其他配置
    TablePrefix       string
    SingularTable     bool
    DisableForeignKey bool
    PrepareStmt       bool
    DryRun            bool
    Plugins           []string
    Hooks             map[string]string
}
Database
type Database struct {
    config *Config
    db     *gorm.DB
    mu     sync.RWMutex

    connector     Connector
    executor      Executor
    healthChecker HealthChecker
}
主要方法
方法 说明
New(config *Config) (*Database, error) 创建数据库连接
GetDB() *gorm.DB 获取GORM实例
Raw() *gorm.DB 获取底层 GORM 实例(需自行控制高级能力)
SQLDB() (*sql.DB, error) 获取底层 *sql.DB,适合 LISTEN/NOTIFY、pgvector 等原生能力
Close() error 关闭数据库连接
Ping() error 测试数据库连接
Stats() PoolStats 获取连接池统计
AutoMigrate(dst ...interface{}) error 自动迁移表结构

高级定制:需要替换拨号/执行/探针逻辑时,可通过 WithConnectorWithExecutorWithHealthChecker 选项注入自定义实现,无需改动业务调用方。

🧪 测试

运行测试:

go test ./pkg/database -v

测试覆盖:

  • ✅ 配置验证
  • ✅ 数据库连接
  • ✅ CRUD操作
  • ✅ 事务处理
  • ✅ 错误处理
  • ✅ 并发访问

📝 示例

简化配置示例

完整示例请参考:examples/database-simple/main.go

传统配置示例

完整示例请参考:examples/database-optimized/main.go

🎯 配置简化对比

传统方式 vs Builder模式

传统方式 (20+ 行):

config := &database.Config{
    Driver:          "mysql",
    Host:            "localhost",
    Port:            3306,
    Username:        "root",
    Password:        "password",
    Database:        "test_db",
    Charset:         "utf8mb4",
    Timezone:        "Local",
    MaxIdleConns:    10,
    MaxOpenConns:    100,
    ConnMaxLifetime: time.Hour,
    ConnMaxIdleTime: 10 * time.Minute,
    LogLevel:        "info",
    SlowThreshold:   200 * time.Millisecond,
    Colorful:        true,
    TablePrefix:     "app_",
    SingularTable:   true,
    PrepareStmt:     true,
    DryRun:          false,
}

Builder模式 (5-10 行):

config := database.NewConfigBuilder().
    MySQL("localhost", "root", "password", "test_db").
    WithConnectionPool(10, 100, time.Hour, 10*time.Minute).
    WithLogging("info", 200*time.Millisecond, true).
    WithTablePrefix("app_").
    Build()
优势总结
维度 传统方式 Builder模式 改进
代码行数 20+ 行 5-10 行 -70%
可读性 一般 优秀 +50%
学习成本 -60%
错误率 -80%
维护性 一般 优秀 +40%

🔄 优化历史

v1.2.0 (当前版本) - 配置简化

重大改进:

  • 配置简化: 引入Builder模式,配置代码减少70%
  • 学习成本降低: 从20+字段简化为链式调用
  • 错误率降低: 类型安全的Builder API
  • 可读性提升: 配置意图一目了然

新增功能:

  • NewConfigBuilder() - 配置构建器
  • MySQL()/PostgreSQL()/SQLite() - 数据库类型方法
  • WithXXX() - 链式配置方法
  • 合理的默认值,无需记忆所有参数
v1.1.0 (历史版本)

优化内容:

  • ✅ 清理未使用的 ConnectionPool 接口
  • ✅ 改进错误信息友好性
  • ✅ 添加副本配置验证
  • ✅ 改进SQLite路径处理
  • ✅ 添加日志自定义支持
  • ✅ 完善测试用例

新增功能:

  • 支持文件日志输出 (LogOutput: "file:///path/to/log")
  • SQLite路径验证和目录检查
  • 更详细的错误信息提示
  • 完整的配置验证机制

🚨 注意事项

  1. 资源管理: 使用完毕后务必调用 Close() 方法
  2. 并发安全: 支持并发访问,但建议在应用层做适当控制
  3. 配置验证: 建议在创建连接前调用 config.Validate() 进行预校验
  4. 日志输出: 文件日志路径需要确保目录存在且有写权限

🤝 贡献

欢迎提交 Issue 和 Pull Request!

�� 许可证

MIT License

Documentation

Index

Constants

View Source
const (
	DefaultMaxIdleConns     = 10
	DefaultMaxOpenConns     = 100
	DefaultConnMaxLifetime  = time.Hour
	DefaultConnMaxIdleTime  = 10 * time.Minute
	DefaultSlowThreshold    = time.Second
	DefaultLogLevel         = "silent"
	DefaultCharset          = "utf8mb4"
	DefaultTimezone         = "Local"
	DefaultPostgresSSLMode  = "disable"
	DefaultPostgresTimezone = "UTC"

	// 重试配置默认值
	DefaultRetryMaxAttempts   = 3
	DefaultRetryInitialDelay  = 1 * time.Second
	DefaultRetryMaxDelay      = 30 * time.Second
	DefaultRetryBackoffFactor = 2.0
	DefaultRetryJitterEnabled = true

	MaxRetryAttempts = 100
)

默认配置常量

Variables

View Source
var (
	ErrMissingDriver     = errors.New("数据库驱动不能为空")
	ErrUnsupportedDriver = errors.New("不支持的数据库驱动")
	ErrMissingHost       = errors.New("数据库主机不能为空")
	ErrInvalidPort       = errors.New("数据库端口无效")
	ErrMissingUsername   = errors.New("数据库用户名不能为空")
	ErrMissingDatabase   = errors.New("数据库名不能为空")
	ErrMissingDBPath     = errors.New("SQLite数据库路径不能为空")
	ErrInvalidLogLevel   = errors.New("无效的日志级别")
	ErrInvalidCharset    = errors.New("无效的字符集")
	ErrInvalidSSLMode    = errors.New("无效的SSL模式")
	ErrInvalidConnPool   = errors.New("连接池配置无效")
	ErrInvalidTimeout    = errors.New("超时配置无效")
	ErrConnectionFailed  = errors.New("数据库连接失败")
	ErrTransactionFailed = errors.New("事务执行失败")
	ErrQueryFailed       = errors.New("查询执行失败")
	ErrMigrationFailed   = errors.New("数据库迁移失败")
)

预定义错误

Functions

func IsConnectionError

func IsConnectionError(err error) bool

IsConnectionError 检查是否为连接错误

func IsValidLogLevel

func IsValidLogLevel(level string) bool

IsValidLogLevel 验证日志级别是否有效

func IsValidationError

func IsValidationError(err error) bool

IsValidationError 检查是否为验证错误

func NewGormLogger

func NewGormLogger(l SimpleLogger, logLevel string) logger.Interface

NewGormLogger 构造函数

  • l: 你的 zap / logrus / zerolog ... 实例
  • level: GORM 日志级别,不想打印 SQL 就传 logger.Silent

Types

type Config

type Config struct {
	// 基础连接配置
	Driver   string `mapstructure:"driver" json:"driver" yaml:"driver"`
	Host     string `mapstructure:"host" json:"host" yaml:"host"`
	Port     int    `mapstructure:"port" json:"port" yaml:"port"`
	Username string `mapstructure:"username" json:"username" yaml:"username"`
	Password string `mapstructure:"password" json:"password" yaml:"password"`
	Database string `mapstructure:"database" json:"database" yaml:"database"`
	Charset  string `mapstructure:"charset" json:"charset" yaml:"charset"`
	SSLMode  string `mapstructure:"ssl_mode" json:"ssl_mode" yaml:"ssl_mode"`
	Timezone string `mapstructure:"timezone" json:"timezone" yaml:"timezone"`

	// 连接池配置
	MaxIdleConns    int           `mapstructure:"max_idle_conns" json:"max_idle_conns" yaml:"max_idle_conns"`
	MaxOpenConns    int           `mapstructure:"max_open_conns" json:"max_open_conns" yaml:"max_open_conns"`
	ConnMaxLifetime time.Duration `mapstructure:"conn_max_lifetime" json:"conn_max_lifetime" yaml:"conn_max_lifetime"`
	ConnMaxIdleTime time.Duration `mapstructure:"conn_max_idle_time" json:"conn_max_idle_time" yaml:"conn_max_idle_time"`

	// GORM日志配置
	CustomLogger              logger.Interface `mapstructure:"-" json:"-" yaml:"-"`
	LogLevel                  string           `mapstructure:"log_level" json:"log_level" yaml:"log_level"`
	SlowThreshold             time.Duration    `mapstructure:"slow_threshold" json:"slow_threshold" yaml:"slow_threshold"`
	IgnoreRecordNotFoundError bool             `mapstructure:"ignore_record_not_found_error" json:"ignore_record_not_found_error" yaml:"ignore_record_not_found_error"`
	ParameterizedQueries      bool             `mapstructure:"parameterized_queries" json:"parameterized_queries" yaml:"parameterized_queries"`
	Colorful                  bool             `mapstructure:"colorful" json:"colorful" yaml:"colorful"`

	// 连接重试配置
	RetryMaxAttempts   int           `mapstructure:"retry_max_attempts" json:"retry_max_attempts" yaml:"retry_max_attempts"`
	RetryInitialDelay  time.Duration `mapstructure:"retry_initial_delay" json:"retry_initial_delay" yaml:"retry_initial_delay"`
	RetryMaxDelay      time.Duration `mapstructure:"retry_max_delay" json:"retry_max_delay" yaml:"retry_max_delay"`
	RetryBackoffFactor float64       `mapstructure:"retry_backoff_factor" json:"retry_backoff_factor" yaml:"retry_backoff_factor"`
	RetryJitterEnabled bool          `mapstructure:"retry_jitter_enabled" json:"retry_jitter_enabled" yaml:"retry_jitter_enabled"`
	RetryEnabled       bool          `mapstructure:"retry_enabled" json:"retry_enabled" yaml:"retry_enabled"`

	// 其他配置
	TablePrefix       string `mapstructure:"table_prefix" json:"table_prefix" yaml:"table_prefix"`
	SingularTable     bool   `mapstructure:"singular_table" json:"singular_table" yaml:"singular_table"`
	DisableForeignKey bool   `mapstructure:"disable_foreign_key" json:"disable_foreign_key" yaml:"disable_foreign_key"`
	PrepareStmt       bool   `mapstructure:"prepare_stmt" json:"prepare_stmt" yaml:"prepare_stmt"`
	DryRun            bool   `mapstructure:"dry_run" json:"dry_run" yaml:"dry_run"`
}

Config 数据库配置

func (*Config) SafeString

func (c *Config) SafeString() string

SafeString 返回安全的配置字符串(密码已脱敏)

func (*Config) SetCustomLogger

func (c *Config) SetCustomLogger(l SimpleLogger, level string)

SetCustomLogger 允许在 Config 层设置 GORM 日志实现,便于与外部 logger 对齐。

func (*Config) SetDefaults

func (c *Config) SetDefaults()

SetDefaults 设置默认值

func (*Config) Validate

func (c *Config) Validate() error

Validate 验证配置

type Connector added in v1.1.0

type Connector interface {
	Connect(ctx context.Context) (*gorm.DB, error)
}

Connector 负责建立数据库连接并完成基础配置(重试、命名策略、连接池)。

type ContextualLogger

type ContextualLogger interface {
	SimpleLogger
	InfoWithContext(ctx context.Context, msg string, fields ...interface{})
	WarnWithContext(ctx context.Context, msg string, fields ...interface{})
	ErrorWithContext(ctx context.Context, msg string, fields ...interface{})
}

ContextualLogger 定义支持Context的日志接口

type DB added in v1.1.0

type DB interface {
	// Exec 执行写操作,适用于 INSERT/UPDATE/DELETE 等语句。
	Exec(ctx context.Context, query string, args ...interface{}) error

	// Query 执行查询并将结果扫描到 dest。
	Query(ctx context.Context, dest interface{}, query string, args ...interface{}) error

	// Tx 启动事务并在回调中执行业务逻辑,支持传入可选的 sql.TxOptions。
	Tx(ctx context.Context, fn func(tx *gorm.DB) error, opts ...*sql.TxOptions) error

	// Raw 返回底层 *gorm.DB 以支持少数无法封装的高级用法(请谨慎使用)。
	Raw() *gorm.DB

	// SQLDB 返回底层 *sql.DB,便于执行需要专用连接的场景(如 PostgreSQL LISTEN/NOTIFY、
	// 使用特定扩展或 Driver 原生能力)。调用方负责确保合理的生命周期管理。
	SQLDB() (*sql.DB, error)
}

DB 定义对外暴露的最小能力集合,便于业务代码依赖接口而非具体实现。

绝大多数场景建议通过这些方法完成常规操作;若确需底层能力,可使用 Raw() 受控地获取底层 *gorm.DB。

type Database

type Database struct {
	// contains filtered or unexported fields
}

Database 数据库管理器,组合 Connector、Executor、HealthChecker 等职责。

func New

func New(config *Config) (*Database, error)

New 创建新的数据库管理器

func NewWithOptions added in v1.1.0

func NewWithOptions(config *Config, opts ...Option) (*Database, error)

NewWithOptions 支持通过可选参数注入日志、钩子或自定义组件。

func (*Database) AutoMigrate

func (d *Database) AutoMigrate(dst ...interface{}) error

AutoMigrate 自动迁移数据库表

func (*Database) BeginTx added in v1.1.0

func (d *Database) BeginTx(ctx context.Context, opts ...*sql.TxOptions) (*gorm.DB, error)

BeginTx 开启事务

func (*Database) Close

func (d *Database) Close() error

Close 关闭数据库连接

func (*Database) CloseWithContext added in v1.1.0

func (d *Database) CloseWithContext(ctx context.Context) error

CloseWithContext 允许调用方传入上下文,配合生命周期钩子完成优雅关闭。

func (*Database) Exec added in v1.1.0

func (d *Database) Exec(ctx context.Context, query string, args ...interface{}) error

Exec 执行写操作

func (*Database) GetConfig

func (d *Database) GetConfig() Config

GetConfig 获取数据库配置(返回副本,防止外部修改)

func (*Database) GetDB

func (d *Database) GetDB() *gorm.DB

GetDB 获取GORM数据库实例

func (*Database) GetDriver

func (d *Database) GetDriver() string

GetDriver 获取数据库驱动类型

func (*Database) HealthCheck

func (d *Database) HealthCheck() error

HealthCheck 健康检查

func (*Database) HealthCheckWithContext

func (d *Database) HealthCheckWithContext(ctx context.Context) *HealthStatus

HealthCheckWithContext 带Context的健康检查

func (*Database) IsConnected

func (d *Database) IsConnected() bool

IsConnected 检查数据库连接状态

func (*Database) IsReadOnly

func (d *Database) IsReadOnly() bool

IsReadOnly 检查是否为只读模式(DryRun模式)

func (*Database) Ping

func (d *Database) Ping() error

Ping 测试数据库连接

func (*Database) Query added in v1.1.0

func (d *Database) Query(ctx context.Context, dest interface{}, query string, args ...interface{}) error

Query 执行查询

func (*Database) Raw added in v1.1.0

func (d *Database) Raw() *gorm.DB

Raw 提供受控的底层 *gorm.DB 访问。

func (*Database) SQLDB added in v1.1.0

func (d *Database) SQLDB() (*sql.DB, error)

SQLDB 返回底层 *sql.DB。

func (*Database) Stats

func (d *Database) Stats() PoolStats

Stats 获取连接池统计信息

func (*Database) Transaction

func (d *Database) Transaction(fn func(*gorm.DB) error) error

Transaction 事务便利方法,自动处理提交和回滚

func (*Database) TransactionWithContext

func (d *Database) TransactionWithContext(ctx context.Context, fn func(*gorm.DB) error) error

TransactionWithContext 带Context的事务便利方法

func (*Database) Tx added in v1.1.0

func (d *Database) Tx(ctx context.Context, fn func(tx *gorm.DB) error, opts ...*sql.TxOptions) error

Tx 启动事务

func (*Database) WithContext

func (d *Database) WithContext(ctx context.Context) *gorm.DB

WithContext 返回带有Context的GORM实例

type DatabaseError

type DatabaseError struct {
	Type      ErrorType
	Operation string
	Err       error
	Context   map[string]interface{}
}

DatabaseError 数据库错误结构

func NewDatabaseError

func NewDatabaseError(errorType ErrorType, operation string, err error) *DatabaseError

NewDatabaseError 创建数据库错误

func (*DatabaseError) Error

func (e *DatabaseError) Error() string

Error 实现error接口

func (*DatabaseError) Is

func (e *DatabaseError) Is(target error) bool

Is 支持errors.Is

func (*DatabaseError) Unwrap

func (e *DatabaseError) Unwrap() error

Unwrap 支持errors.Unwrap

func (*DatabaseError) WithContext

func (e *DatabaseError) WithContext(key string, value interface{}) *DatabaseError

WithContext 添加错误上下文

type ErrorType

type ErrorType int

ErrorType 错误类型

const (
	ErrorTypeConnection ErrorType = iota
	ErrorTypeValidation
	ErrorTypeQuery
	ErrorTypeTransaction
	ErrorTypeMigration
)

type Executor added in v1.1.0

type Executor interface {
	Exec(ctx context.Context, query string, args ...interface{}) error
	Query(ctx context.Context, dest interface{}, query string, args ...interface{}) error
	Tx(ctx context.Context, fn func(tx *gorm.DB) error, opts ...*sql.TxOptions) error
	BeginTx(ctx context.Context, opts ...*sql.TxOptions) (*gorm.DB, error)
}

Executor 承载常规执行能力,便于与连接/健康检查解耦。

type GORMLogger

type GORMLogger struct {
	// contains filtered or unexported fields
}

GORMLogger 把任意 SimpleLogger 转成 gorm.logger.Interface

func (*GORMLogger) Error

func (g *GORMLogger) Error(ctx context.Context, msg string, data ...interface{})

Error 实现logger.Interface

func (*GORMLogger) Info

func (g *GORMLogger) Info(ctx context.Context, msg string, data ...interface{})

Info 实现logger.Interface

func (*GORMLogger) LogMode

func (g *GORMLogger) LogMode(l logger.LogLevel) logger.Interface

LogMode 实现接口

func (*GORMLogger) Trace

func (g *GORMLogger) Trace(ctx context.Context, begin time.Time,
	fc func() (sql string, rowsAffected int64), err error)

Trace 打印 SQL

func (*GORMLogger) Warn

func (g *GORMLogger) Warn(ctx context.Context, msg string, data ...interface{})

Warn 实现logger.Interface

type HealthChecker added in v1.1.0

type HealthChecker interface {
	Ping() error
	HealthCheck() error
	HealthCheckWithContext(ctx context.Context) *HealthStatus
}

HealthChecker 定义健康探测接口,支持 context 取消。

type HealthStatus

type HealthStatus struct {
	Healthy   bool      `json:"healthy"`
	Timestamp time.Time `json:"timestamp"`
	Driver    string    `json:"driver"`
	Errors    []string  `json:"errors,omitempty"`
	Warnings  []string  `json:"warnings,omitempty"`
	Stats     PoolStats `json:"stats"`
}

HealthStatus 健康检查状态

type Hooks added in v1.1.0

type Hooks struct {
	BeforeConnect func(cfg *Config)
	AfterConnect  func(db *gorm.DB)

	BeforeClose func(ctx context.Context, db *gorm.DB) error
	AfterClose  func(ctx context.Context, closeErr error)

	BeforeProbe func(ctx context.Context) error
	AfterProbe  func(ctx context.Context, probeErr error)
}

Hooks 定义生命周期扩展点,便于调用方在关键节点插入自定义逻辑。

type Option added in v1.1.0

type Option func(*Database)

Option 允许在构建 Database 时注入可选配置,例如 Logger、Hooks 等。

func WithConnector added in v1.1.0

func WithConnector(connector Connector) Option

Option helpers for custom components

func WithExecutor added in v1.1.0

func WithExecutor(executor Executor) Option

func WithHealthChecker added in v1.1.0

func WithHealthChecker(healthChecker HealthChecker) Option

func WithHooks added in v1.1.0

func WithHooks(h Hooks) Option

WithHooks 设置生命周期钩子。

func WithLogger added in v1.1.0

func WithLogger(l SimpleLogger) Option

WithLogger 设置用于启动、重试和健康检查日志的 SimpleLogger。

type PoolConfigurator added in v1.1.0

type PoolConfigurator interface {
	Configure(db *gorm.DB) error
}

PoolConfigurator 允许对底层 *sql.DB 进行池参数配置。

type PoolStats

type PoolStats struct {
	OpenConnections   int
	IdleConnections   int
	WaitCount         int64
	WaitDuration      time.Duration
	MaxIdleClosed     int64
	MaxLifetimeClosed int64
}

PoolStats 连接池统计信息

type SimpleLogger

type SimpleLogger interface {
	Info(msg string, fields ...interface{})
	Warn(msg string, fields ...interface{})
	Error(msg string, fields ...interface{})
}

SimpleLogger 定义基础日志接口

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL