Documentation
¶
Index ¶
- type DB
- type DBConfig
- type DBResourceManager
- type DatabaseManager
- type Repo
- func (repo *Repo) Exec(sql string, args ...any) (sql.Result, error)
- func (repo *Repo) Find(bean any, condiBeans ...any) error
- func (repo *Repo) Get(bean any) (bool, error)
- func (repo *Repo) In(column string, args ...any) *xorm.Session
- func (repo *Repo) Insert(beans ...any) (int64, error)
- func (repo *Repo) InsertOne(beans any) (int64, error)
- func (repo *Repo) NewSession() *xorm.Session
- func (repo *Repo) Query(sql string, paramStr ...any) (resultsSlice []map[string][]byte, err error)
- func (repo *Repo) Select(query string) *xorm.Session
- func (repo *Repo) SetTableName(name string)
- func (repo *Repo) ShowSQL(b bool)
- func (repo *Repo) Where(query any, args ...any) *xorm.Session
- func (repo *Repo) WithCtx(ctx context.Context) *xorm.Session
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DB ¶
type DB struct {
*DBResourceManager
}
Db
func (*DB) BeginTx ¶ added in v0.3.0
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) NewDBTable ¶
NewDBTable 获取绑定到指定库和表的操作对象 dbname 必须是配置文件中 [mysql.xxx]/[sqlite.xxx] 里的配置名,不存在时 panic 并给出明确提示 (数据库连接在 NewApp 阶段已保证可用,走到这里失败只可能是配置名写错,尽早暴露)
func (*DB) NewSession ¶ added in v0.3.0
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
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 ¶
DatabaseManager
Click to show internal directories.
Click to hide internal directories.