1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- package utils
-
- import (
- "github.com/go-xorm/xorm"
- )
-
-
-
- type Context struct {
- DB *xorm.Session
- box map[string]interface{}
- }
-
-
- func NewContext(engine *xorm.Engine, opt map[string]interface{}) *Context {
- return &Context{
- DB: engine.NewSession(),
- box: opt,
- }
- }
-
-
- func (c *Context) Get(key string, def ...interface{}) interface{} {
- if c.box != nil {
- if opt, has := c.box[key]; has {
- return opt
- }
- }
-
- if len(def) > 0 {
- return def[0]
- }
-
- return nil
- }
-
-
- func (c *Context) Set(key string, val interface{}) {
- if c.box == nil {
- c.box = make(map[string]interface{})
- }
-
- c.box[key] = val
- }
-
-
- func (c *Context) Ready() error {
-
- if err := c.DB.Begin(); err != nil {
- return err
- }
-
- return nil
- }
-
-
- func (c *Context) Destroy() {
- c.DB.Close()
- }
|