forked from syahidfrd/go-boilerplate
-
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.
feat(syahidfrd#8): transaction on usecase layer
- Loading branch information
Showing
9 changed files
with
215 additions
and
139 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,21 @@ | ||
package domain | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
) | ||
|
||
// Transaction interface (wraps DBTX) | ||
type Transaction interface { | ||
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) | ||
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row | ||
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) | ||
Commit() error | ||
Rollback() error | ||
} | ||
|
||
// Database interface | ||
type Database interface { | ||
BeginTx(ctx context.Context) (Transaction, error) | ||
Close() error | ||
} |
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
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 |
---|---|---|
@@ -1,28 +1,73 @@ | ||
package datastore | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"net/url" | ||
"time" | ||
|
||
_ "github.com/lib/pq" | ||
"github.com/syahidfrd/go-boilerplate/domain" | ||
) | ||
|
||
type Database struct { | ||
db *sql.DB | ||
} | ||
|
||
// NewDatabase will create new database instance | ||
func NewDatabase(databaseURL string) (db *sql.DB, err error) { | ||
func NewDatabase(databaseURL string) (domain.Database, error) { | ||
parseDBUrl, _ := url.Parse(databaseURL) | ||
db, err = sql.Open(parseDBUrl.Scheme, databaseURL) | ||
db, err := sql.Open(parseDBUrl.Scheme, databaseURL) | ||
if err != nil { | ||
return | ||
return nil, err | ||
} | ||
|
||
if err = db.Ping(); err != nil { | ||
return | ||
return nil, err | ||
} | ||
|
||
db.SetConnMaxLifetime(time.Minute * 5) | ||
db.SetMaxIdleConns(0) | ||
db.SetMaxOpenConns(5) | ||
|
||
return | ||
return &Database{db: db}, nil | ||
} | ||
|
||
func (p *Database) BeginTx(ctx context.Context) (domain.Transaction, error) { | ||
tx, err := p.db.BeginTx(ctx, &sql.TxOptions{ | ||
Isolation: sql.LevelReadCommitted, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &Transaction{tx: tx}, nil | ||
} | ||
|
||
func (p *Database) Close() error { | ||
return p.db.Close() | ||
} | ||
|
||
// Transaction implements Transaction interface | ||
type Transaction struct { | ||
tx *sql.Tx | ||
} | ||
|
||
func (t *Transaction) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) { | ||
return t.tx.ExecContext(ctx, query, args...) | ||
} | ||
|
||
func (t *Transaction) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row { | ||
return t.tx.QueryRowContext(ctx, query, args...) | ||
} | ||
|
||
func (t *Transaction) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) { | ||
return t.tx.QueryContext(ctx, query, args...) | ||
} | ||
|
||
func (t *Transaction) Commit() error { | ||
return t.tx.Commit() | ||
} | ||
|
||
func (t *Transaction) Rollback() error { | ||
return t.tx.Rollback() | ||
} |
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
Oops, something went wrong.