Ai
2 Star 1 Fork 2

go-mao/mao

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
task_line.go 2.46 KB
一键复制 编辑 原始数据 按行查看 历史
haitgo 提交于 2023-02-23 18:07 +08:00 . 保存
package frame
import (
"sync"
"sync/atomic"
"gitee.com/go-mao/mao/libs/try"
"xorm.io/xorm"
)
type Taskline struct {
server *Server
lock *sync.Mutex
xormSession *xorm.Session
startTrans bool //开启了事务
index int64
sessionId string
}
func newTaskline(server *Server) *Taskline {
atomic.AddInt64(&server.taskIndex, 1)
object := &Taskline{index: server.taskIndex, server: server}
return object
}
// 获取当前taskline的id
func (this *Taskline) GetTaskid() int64 {
return this.index
}
// 当前请求的互斥锁
func (this *Taskline) Mutex() *sync.Mutex {
if this.lock == nil {
this.lock = &sync.Mutex{}
}
return this.lock
}
// 放置钩子
func (this *Taskline) PutHook(h HookInterface, args ...any) {
this.server.hook.trigger(this, h, args...)
}
// 实例化一个对象【泛型】
func (this *Taskline) OrmModel(model ModelInterface) ModelInterface {
model.init(this.OrmSession(), model)
return model
}
// 初始化数据库模型
func (this *Taskline) OrmTable(bean any) *xorm.Session {
return this.OrmSession().Table(bean)
}
// ORM对象
func (this *Taskline) OrmSession() *xorm.Session {
if this.xormSession == nil {
this.xormSession = this.server.ormEngine.NewSession()
}
return this.xormSession
}
// 新建一个任务线
func (this *Taskline) NewTaskLine() *Taskline {
return newTaskline(this.server)
}
// 开启事务,只执行最上面层的事务
func (this *Taskline) Transaction(fn func()) {
if this.startTrans {
fn()
return
}
this.startTrans = true
defer func() {
this.startTrans = false
}()
sess := this.OrmSession()
try.Do(func() {
sess.Begin()
fn()
sess.Commit()
}, func(e try.Exception) {
sess.Rollback()
try.Throw(e.ErrCode(), e.ErrMsg())
})
}
// 分页查询
// listPtr = 查询列表指针
// page = 页码
// limit = 每页查询数量
// 返回查询总数
func (this *Taskline) FindPage(db *xorm.Session, listPtr interface{}, page, limit int) int64 {
if page <= 0 {
page = 1
}
if limit == 0 {
limit = 20
}
if limit > 1000 {
limit = 1000
}
start := (page - 1) * limit
total, err := db.Limit(limit, start).FindAndCount(listPtr)
CheckSqlError(err)
return total
}
// 获取配置对象
func (this *Taskline) MaoConfig() MaoConfigInterface {
return this.server.config
}
// 初始化配置
func (this *Taskline) RenderConfig(conf ConfigInterface) ConfigInterface {
return this.MaoConfig().Get(conf)
}
// 日志
func (this *Taskline) Log() MaoLogInterface {
return this.server.logger
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/go-mao/mao.git
git@gitee.com:go-mao/mao.git
go-mao
mao
mao
v1.0.9

搜索帮助