github.com/sttk/sabi@v0.5.0/txn.go (about) 1 // Copyright (C) 2023 Takayuki Sato. All Rights Reserved. 2 // This program is free software under MIT License. 3 // See the file LICENSE in this distribution for more details. 4 5 package sabi 6 7 // RunTxn is a function which runs logic functions specified as arguments in a 8 // transaction. 9 func RunTxn[D any](base DaxBase, logics ...func(dax D) Err) Err { 10 base.begin() 11 12 dax := base.(D) 13 err := Ok() 14 15 for _, logic := range logics { 16 err = logic(dax) 17 if !err.IsOk() { 18 break 19 } 20 } 21 22 if err.IsOk() { 23 err = base.commit() 24 } 25 26 if !err.IsOk() { 27 base.rollback() 28 } 29 30 base.end() 31 32 return err 33 } 34 35 // Txn is a function which creates a transaction having specified logic 36 // functions. 37 func Txn[D any](base DaxBase, logics ...func(dax D) Err) func() Err { 38 return func() Err { 39 return RunTxn[D](base, logics...) 40 } 41 }