-
Notifications
You must be signed in to change notification settings - Fork 0
/
uow.go
51 lines (46 loc) · 1.07 KB
/
uow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package styx
import (
"github.com/masudur-rahman/styx/nosql"
"github.com/masudur-rahman/styx/sql"
)
// UnitOfWork represents the unit of work for coordinating transactions
type UnitOfWork struct {
SQL sql.Engine
NoSQL nosql.Engine
}
// Begin starts a new transaction
func (uow UnitOfWork) Begin() (UnitOfWork, error) {
cp := UnitOfWork{
SQL: uow.SQL,
NoSQL: uow.NoSQL,
}
if uow.SQL != nil {
sqlTx, err := uow.SQL.BeginTx()
if err != nil {
return UnitOfWork{}, err
}
cp.SQL = sqlTx
}
// For NoSQL databases, no action needed for beginning a transaction
return cp, nil
}
// Commit commits the transaction
func (uow UnitOfWork) Commit() error {
if uow.SQL != nil {
if err := uow.SQL.Commit(); err != nil {
return err
}
}
// For NoSQL databases, no action needed for committing a transaction
return nil
}
// Rollback rolls back the transaction
func (uow UnitOfWork) Rollback() error {
if uow.SQL != nil {
if err := uow.SQL.Rollback(); err != nil {
return err
}
}
// For NoSQL databases, no action needed for rolling back a transaction
return nil
}