2018-06-07 14:54:36 -05:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"reflect"
|
|
|
|
|
2020-04-01 08:57:21 -05:00
|
|
|
"xorm.io/xorm"
|
2018-06-07 14:54:36 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type DBSession struct {
|
|
|
|
*xorm.Session
|
|
|
|
events []interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type dbTransactionFunc func(sess *DBSession) error
|
|
|
|
|
|
|
|
func (sess *DBSession) publishAfterCommit(msg interface{}) {
|
|
|
|
sess.events = append(sess.events, msg)
|
|
|
|
}
|
|
|
|
|
2019-05-16 05:39:59 -05:00
|
|
|
// NewSession returns a new DBSession
|
2021-05-27 06:55:33 -05:00
|
|
|
func (ss *SQLStore) NewSession(ctx context.Context) *DBSession {
|
|
|
|
sess := &DBSession{Session: ss.engine.NewSession()}
|
|
|
|
sess.Session = sess.Session.Context(ctx)
|
|
|
|
return sess
|
2019-05-16 05:39:59 -05:00
|
|
|
}
|
|
|
|
|
2021-05-27 06:55:33 -05:00
|
|
|
func newSession(ctx context.Context) *DBSession {
|
|
|
|
sess := &DBSession{Session: x.NewSession()}
|
|
|
|
sess.Session = sess.Session.Context(ctx)
|
|
|
|
|
|
|
|
return sess
|
2018-06-07 14:54:36 -05:00
|
|
|
}
|
|
|
|
|
2018-06-15 13:49:14 -05:00
|
|
|
func startSession(ctx context.Context, engine *xorm.Engine, beginTran bool) (*DBSession, error) {
|
2020-03-23 07:37:53 -05:00
|
|
|
value := ctx.Value(ContextSessionKey{})
|
2018-06-07 14:54:36 -05:00
|
|
|
var sess *DBSession
|
|
|
|
sess, ok := value.(*DBSession)
|
|
|
|
|
2018-06-18 07:50:36 -05:00
|
|
|
if ok {
|
2021-05-27 06:55:33 -05:00
|
|
|
sess.Session = sess.Session.Context(ctx)
|
2018-06-18 07:50:36 -05:00
|
|
|
return sess, nil
|
2018-06-07 14:54:36 -05:00
|
|
|
}
|
|
|
|
|
2018-06-18 07:50:36 -05:00
|
|
|
newSess := &DBSession{Session: engine.NewSession()}
|
|
|
|
if beginTran {
|
|
|
|
err := newSess.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2021-05-27 06:55:33 -05:00
|
|
|
|
|
|
|
newSess.Session = newSess.Session.Context(ctx)
|
2018-06-18 07:50:36 -05:00
|
|
|
return newSess, nil
|
2018-06-07 14:54:36 -05:00
|
|
|
}
|
|
|
|
|
2021-03-18 08:27:59 -05:00
|
|
|
// WithDbSession calls the callback with a session.
|
2020-11-10 23:21:08 -06:00
|
|
|
func (ss *SQLStore) WithDbSession(ctx context.Context, callback dbTransactionFunc) error {
|
2021-03-18 08:27:59 -05:00
|
|
|
return withDbSession(ctx, ss.engine, callback)
|
2019-05-16 05:39:59 -05:00
|
|
|
}
|
|
|
|
|
2021-03-18 08:27:59 -05:00
|
|
|
func withDbSession(ctx context.Context, engine *xorm.Engine, callback dbTransactionFunc) error {
|
|
|
|
sess := &DBSession{Session: engine.NewSession()}
|
2021-05-27 06:55:33 -05:00
|
|
|
sess.Session = sess.Session.Context(ctx)
|
2021-03-18 08:27:59 -05:00
|
|
|
defer sess.Close()
|
2018-06-07 14:54:36 -05:00
|
|
|
|
|
|
|
return callback(sess)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sess *DBSession) InsertId(bean interface{}) (int64, error) {
|
|
|
|
table := sess.DB().Mapper.Obj2Table(getTypeName(bean))
|
|
|
|
|
2019-10-22 07:08:18 -05:00
|
|
|
if err := dialect.PreInsertId(table, sess.Session); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2018-06-07 14:54:36 -05:00
|
|
|
id, err := sess.Session.InsertOne(bean)
|
2019-10-22 07:08:18 -05:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if err := dialect.PostInsertId(table, sess.Session); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2018-06-07 14:54:36 -05:00
|
|
|
|
2019-10-22 07:08:18 -05:00
|
|
|
return id, nil
|
2018-06-07 14:54:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func getTypeName(bean interface{}) (res string) {
|
|
|
|
t := reflect.TypeOf(bean)
|
|
|
|
for t.Kind() == reflect.Ptr {
|
|
|
|
t = t.Elem()
|
|
|
|
}
|
|
|
|
return t.Name()
|
|
|
|
}
|