-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Masudur Rahman <masudjuly02@gmail.com>
- Loading branch information
1 parent
7a8a9db
commit 248372e
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package database | ||
|
||
import ( | ||
"github.com/masudur-rahman/database/nosql" | ||
"github.com/masudur-rahman/database/sql" | ||
) | ||
|
||
// UnitOfWork represents the unit of work for coordinating transactions | ||
type UnitOfWork struct { | ||
SQL sql.Database | ||
NoSQL nosql.Database | ||
} | ||
|
||
// 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 | ||
} |