Documentation ¶
Index ¶
- Constants
- Variables
- func GetConnect(name string) *gorm.DB
- func GetConnectByConf(conf MultipleConf) (*gorm.DB, error)
- func Init(c map[string]MultipleConf, logOptions []LogConfigOption)
- type Conf
- type EGorm
- func (ctl *EGorm) Create(c context.Context, values interface{}) error
- func (ctl *EGorm) First(c context.Context, where, noWhere map[string]interface{}, result interface{}) error
- func (ctl *EGorm) GDB() *gorm.DB
- func (ctl *EGorm) SaveById(c context.Context, records interface{}, updateColumns []string) error
- func (ctl *EGorm) Updates(c context.Context, table string, ...) (int64, error)
- type LogConfigOption
- type MultipleConf
Constants ¶
const NotLimit = -1
Variables ¶
var ErrNoInit = errors.New("eMysql: please initialize with eMysql.Init()")
Functions ¶
func GetConnect ¶
func GetConnectByConf ¶
func GetConnectByConf(conf MultipleConf) (*gorm.DB, error)
GetConnectByConf 获取gorm连接
func Init ¶
func Init(c map[string]MultipleConf, logOptions []LogConfigOption)
Types ¶
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) First ¶ added in v0.0.7
func (ctl *EGorm) First(c context.Context, where, noWhere map[string]interface{}, result interface{}) error
First 查询第一条记录 @param c @param where where条件 @param noWhere no where条件 @param result 查询结果数据 @return error
func (*EGorm) SaveById ¶ added in v0.0.16
Save 更新结构体指定id的所有字段 @param c @param records 记录数据 @param updateColumns 当id为0时,需要更新的字段 @return error
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 更新指定字段 @param c @param table 更新表名 @param where where条件 @param noWhere no where条件 @param updates 更新数据 @param limit 更新限制条数,不限制则使用NotLimit @return int64 更新有效结果数量 @return error
type LogConfigOption ¶ added in v0.0.16
type LogConfigOption func(conf *logConf)
func WithIgnoreRecordNotFound ¶
func WithIgnoreRecordNotFound(isIgnore bool) LogConfigOption
WithIgnoreRecordNotFound 是否忽略notFound错误,默认为 false
func WithLogLevel ¶ added in v0.0.16
func WithLogLevel(level logger.LogLevel) LogConfigOption
WithLogLevel 是否忽略notFound错误
func WithSlowThreshold ¶
func WithSlowThreshold(duration time.Duration) LogConfigOption
WithSlowThreshold 慢日志时间阈值,默认为 100毫秒
type MultipleConf ¶
MultipleConf 读写分离配置
func (*MultipleConf) ConfToGormDialector ¶
func (m *MultipleConf) ConfToGormDialector() (sourcesDialector, replicasDialector []gorm.Dialector)
ConfToGormDialector 处理配置为gorm配置 @param conf 配置列表 @return sourcesDSN 写库dsn列表 @return replicasDSN 读库dsn列表