代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。