db

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB struct {
	*DBResourceManager
}

Db

func NewDb

func NewDb(conf *config.Config) (*DB, error)

NewDb 从配置初始化数据库 配置了的数据库连接失败会返回错误(fail-fast);完全没配置数据库时返回可用的空实例

func (*DB) BeginTx added in v0.3.0

func (s *DB) BeginTx(dbname string) (*xorm.Session, error)

BeginTx 启动事务(支持跨表操作) 使用示例:

sess, err := igo.App.DB.BeginTx("test")
if err != nil {
    return err
}
defer sess.Close()

sess.Table("users").Insert(&user)
sess.Table("orders").Insert(&order)
err = sess.Commit()

func (*DB) Close added in v0.2.0

func (s *DB) Close() error

Close 关闭所有数据库连接

func (*DB) NewDBTable

func (s *DB) NewDBTable(dbname string, tableName string) *Repo

NewDBTable 获取绑定到指定库和表的操作对象 dbname 必须是配置文件中 [mysql.xxx]/[sqlite.xxx] 里的配置名,不存在时 panic 并给出明确提示 (数据库连接在 NewApp 阶段已保证可用,走到这里失败只可能是配置名写错,尽早暴露)

func (*DB) NewSession added in v0.3.0

func (s *DB) NewSession(dbname string) *xorm.Session

NewSession 获取指定数据库的原始Session(未绑定表,可用于跨表操作) 使用示例:

sess := igo.App.DB.NewSession("test")
defer sess.Close()
sess.Table("users").Insert(&user)
sess.Table("orders").Insert(&order)

func (*DB) Transaction added in v0.4.0

func (s *DB) Transaction(dbname string, fn func(sess *xorm.Session) error) error

Transaction 闭包式事务:fn 返回 error 或 panic 时自动回滚,正常返回时自动提交。 相比 BeginTx 免去手动管理 Commit/Rollback/Close,推荐优先使用。 使用示例:

err := igo.App.DB.Transaction("test", func(sess *xorm.Session) error {
    if _, err := sess.Table("users").Insert(&user); err != nil {
        return err // 自动回滚
    }
    _, err := sess.Table("orders").Insert(&order)
    return err // nil 则自动提交
})

type DBConfig

type DBConfig struct {
	MaxIdle     int    `json:"max_idle" toml:"max_idle" yaml:"max_idle" mapstructure:"max_idle"`
	MaxOpen     int    `json:"max_open" toml:"max_open" yaml:"max_open" mapstructure:"max_open"`
	MaxIdleLife int    `json:"max_idle_life" toml:"max_idle_life" yaml:"max_idle_life" mapstructure:"max_idle_life"`
	IsDebug     bool   `json:"is_debug" toml:"is_debug" yaml:"is_debug" mapstructure:"is_debug"`
	Datasource  string `json:"data_source" toml:"data_source" yaml:"data_source" mapstructure:"data_source"`
	DbType      string `json:"-" toml:"-" yaml:"-" mapstructure:"-"`
}

type DBResourceManager

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

func New

func New(conf *viper.Viper) (*DBResourceManager, error)

New 从配置初始化所有数据库连接 配置了的数据库必须初始化并 Ping 成功,否则返回错误(fail-fast); 完全没有 [mysql]/[sqlite] 配置时返回空 manager,不报错。

func (*DBResourceManager) Close added in v0.2.0

func (db *DBResourceManager) Close() error

Close 关闭所有数据库连接

func (*DBResourceManager) Get

func (db *DBResourceManager) Get(name string) *DatabaseManager

func (*DBResourceManager) Names added in v0.3.0

func (db *DBResourceManager) Names() []string

Names 返回所有已初始化的数据库配置名

func (*DBResourceManager) PingAll added in v0.3.0

func (db *DBResourceManager) PingAll() map[string]error

PingAll 对所有数据库执行 Ping,返回每个库的结果(nil 表示正常)

type DatabaseManager

type DatabaseManager struct {
	WriteDB *xorm.Engine
	// contains filtered or unexported fields
}

DatabaseManager

func (*DatabaseManager) Ping

func (db *DatabaseManager) Ping() error

Ping Database

type Repo

type Repo struct {
	Engine *xorm.Engine
	// contains filtered or unexported fields
}

Repo Reference xorm

func (*Repo) Exec

func (repo *Repo) Exec(sql string, args ...any) (sql.Result, error)

func (*Repo) Find

func (repo *Repo) Find(bean any, condiBeans ...any) error

func (*Repo) Get

func (repo *Repo) Get(bean any) (bool, error)

func (*Repo) In

func (repo *Repo) In(column string, args ...any) *xorm.Session

func (*Repo) Insert

func (repo *Repo) Insert(beans ...any) (int64, error)

func (*Repo) InsertOne

func (repo *Repo) InsertOne(beans any) (int64, error)

func (*Repo) NewSession

func (repo *Repo) NewSession() *xorm.Session

func (*Repo) Query

func (repo *Repo) Query(sql string, paramStr ...any) (resultsSlice []map[string][]byte, err error)

func (*Repo) Select

func (repo *Repo) Select(query string) *xorm.Session

func (*Repo) SetTableName

func (repo *Repo) SetTableName(name string)

SetTableName 修改 Repo 绑定的表名

func (*Repo) ShowSQL

func (repo *Repo) ShowSQL(b bool)

func (*Repo) Where

func (repo *Repo) Where(query any, args ...any) *xorm.Session

func (*Repo) WithCtx added in v0.4.0

func (repo *Repo) WithCtx(ctx context.Context) *xorm.Session

WithCtx 返回绑定了 context 的查询 session:请求取消/超时后查询会被中断, 慢查询不会在客户端断开后继续占用数据库。igo 的 context.IContext 可直接传入。 使用示例:

err := repo.WithCtx(ctx).Where("uid = ?", uid).Find(&rows)

Jump to

Keyboard shortcuts

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