eMysql

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2022 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const NotLimit = -1

Variables

View Source
var DBRecordNoFound = &bizerr.BizErr{Code: 404, Msg: "record not found in database"}

First 查询

View Source
var ErrNoInit = errors.New("eMysql: please initialize with eMysql.Init()")

Functions

func GetConnect

func GetConnect(name string) *gorm.DB

func GetConnectByConf

func GetConnectByConf(conf MultipleConf) (*gorm.DB, error)

GetConnectByConf 获取gorm连接

func Init

func Init(c map[string]MultipleConf, logOptions ...GormConfigOption)

Types

type Conf

type Conf struct {
	Host     string
	Port     int
	Database string
	User     string
	Password string
	Params   string
}

func (*Conf) DSN

func (c *Conf) DSN() string

type EGorm

type EGorm struct {
	ConfName string
}

EGorm 在结构体引入组合并赋值ConfName,即可通过GDB()获取gorm连接 Example

type User struct {
	 EGorm
}

var user = User{EGorm{ConfName: "localhost"}}

func (u *User) GetUser(id int64) error {
	 u.GDB().Where("id = ?", id).First()
}

func (*EGorm) Create added in v0.0.7

func (ctl *EGorm) Create(c context.Context, values interface{}) error

Create 创建

func (*EGorm) First added in v0.0.7

func (ctl *EGorm) First(c context.Context, where, noWhere map[string]interface{}, result interface{}) error

func (*EGorm) GDB

func (ctl *EGorm) GDB() *gorm.DB

GDB 获取DB连接

func (*EGorm) Save added in v0.0.7

func (ctl *EGorm) Save(c context.Context, values interface{}) error

Save 更新结构体指定id的所有字段

func (*EGorm) Updates added in v0.0.7

func (ctl *EGorm) Updates(c context.Context, table string, where, noWhere, updates map[string]interface{}, limit int) (int64, error)

Updates 更新指定字段

type GormConfigOption

type GormConfigOption func(conf *logConf)

func WithIgnoreRecordNotFound

func WithIgnoreRecordNotFound(isIgnore bool) GormConfigOption

WithIgnoreRecordNotFound 是否忽略notFound错误,默认为 false

func WithName

func WithName(name string) GormConfigOption

WithName 慢日志名,默认为 mysql

func WithSlowThreshold

func WithSlowThreshold(duration time.Duration) GormConfigOption

WithSlowThreshold 慢日志时间阈值,默认为 100毫秒

type MultipleConf

type MultipleConf struct {
	Sources  []Conf // 写库
	Replicas []Conf // 读库
}

MultipleConf 读写分离配置

func (*MultipleConf) ConfToGormDialector

func (m *MultipleConf) ConfToGormDialector() (sourcesDialector, replicasDialector []gorm.Dialector)

ConfToGormDialector 处理配置为gorm配置 @param conf 配置列表 @return sourcesDSN 写库dsn列表 @return replicasDSN 读库dsn列表

Jump to

Keyboard shortcuts

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