vikunja-api/vendor/xorm.io/xorm/session_tx.go

128 lines
3.1 KiB
Go
Raw Permalink Normal View History

2018-06-10 11:11:41 +02:00
// Copyright 2016 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package xorm
import (
"time"
"xorm.io/xorm/log"
)
2018-06-10 11:11:41 +02:00
// Begin a transaction
func (session *Session) Begin() error {
if session.isAutoCommit {
tx, err := session.DB().BeginTx(session.ctx, nil)
2018-06-10 11:11:41 +02:00
if err != nil {
return err
}
session.isAutoCommit = false
session.isCommitedOrRollbacked = false
session.tx = tx
2018-06-10 11:11:41 +02:00
session.saveLastSQL("BEGIN TRANSACTION")
}
return nil
}
// Rollback When using transaction, you can rollback if any error
func (session *Session) Rollback() error {
if !session.isAutoCommit && !session.isCommitedOrRollbacked {
session.saveLastSQL("ROLL BACK")
2018-06-10 11:11:41 +02:00
session.isCommitedOrRollbacked = true
2019-03-29 18:54:35 +01:00
session.isAutoCommit = true
start := time.Now()
needSQL := session.DB().NeedLogSQL(session.ctx)
if needSQL {
session.engine.logger.BeforeSQL(log.LogContext{
Ctx: session.ctx,
SQL: "ROLL BACK",
})
}
err := session.tx.Rollback()
if needSQL {
session.engine.logger.AfterSQL(log.LogContext{
Ctx: session.ctx,
SQL: "ROLL BACK",
ExecuteTime: time.Now().Sub(start),
Err: err,
})
}
return err
2018-06-10 11:11:41 +02:00
}
return nil
}
// Commit When using transaction, Commit will commit all operations.
func (session *Session) Commit() error {
if !session.isAutoCommit && !session.isCommitedOrRollbacked {
session.saveLastSQL("COMMIT")
session.isCommitedOrRollbacked = true
2019-03-29 18:54:35 +01:00
session.isAutoCommit = true
start := time.Now()
needSQL := session.DB().NeedLogSQL(session.ctx)
if needSQL {
session.engine.logger.BeforeSQL(log.LogContext{
Ctx: session.ctx,
SQL: "COMMIT",
})
}
err := session.tx.Commit()
if needSQL {
session.engine.logger.AfterSQL(log.LogContext{
Ctx: session.ctx,
SQL: "COMMIT",
ExecuteTime: time.Now().Sub(start),
Err: err,
})
}
if err != nil {
return err
}
// handle processors after tx committed
closureCallFunc := func(closuresPtr *[]func(interface{}), bean interface{}) {
if closuresPtr != nil {
for _, closure := range *closuresPtr {
closure(bean)
2018-06-10 11:11:41 +02:00
}
}
}
2018-06-10 11:11:41 +02:00
for bean, closuresPtr := range session.afterInsertBeans {
closureCallFunc(closuresPtr, bean)
2018-06-10 11:11:41 +02:00
if processor, ok := interface{}(bean).(AfterInsertProcessor); ok {
processor.AfterInsert()
2018-06-10 11:11:41 +02:00
}
}
for bean, closuresPtr := range session.afterUpdateBeans {
closureCallFunc(closuresPtr, bean)
2018-06-10 11:11:41 +02:00
if processor, ok := interface{}(bean).(AfterUpdateProcessor); ok {
processor.AfterUpdate()
2018-06-10 11:11:41 +02:00
}
}
for bean, closuresPtr := range session.afterDeleteBeans {
closureCallFunc(closuresPtr, bean)
2018-06-10 11:11:41 +02:00
if processor, ok := interface{}(bean).(AfterDeleteProcessor); ok {
processor.AfterDelete()
2018-06-10 11:11:41 +02:00
}
}
cleanUpFunc := func(slices *map[interface{}]*[]func(interface{})) {
if len(*slices) > 0 {
*slices = make(map[interface{}]*[]func(interface{}), 0)
2018-06-10 11:11:41 +02:00
}
}
cleanUpFunc(&session.afterInsertBeans)
cleanUpFunc(&session.afterUpdateBeans)
cleanUpFunc(&session.afterDeleteBeans)
2018-06-10 11:11:41 +02:00
}
return nil
}