Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions lib/xdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@ package lib
import (
"context"
"database/sql"
"fmt"
"strings"
)

type DBContexter interface {
context.Context
Conn() *sql.DB
Conn() CommonConn
}

type CommonConn interface {
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
}

type DBContext struct {
Expand All @@ -39,7 +47,11 @@ func NewDBContext(ctx context.Context, conn *sql.DB) *DBContext {
}
}

func (ctx *DBContext) Conn() *sql.DB {
func (ctx *DBContext) Conn() CommonConn {
// use tx first
if ctx.tx != nil {
return ctx.tx
}
return ctx.conn
}

Expand Down Expand Up @@ -101,6 +113,15 @@ func RDBTxnExecute(dc *DBContext, handler func(context.Context) error) error {
}
}

defer func() {
if err1 := recover(); err1 != nil {
dc.tx.Rollback()

err = fmt.Errorf("%v", err1)
return
}
}()

err = handler(dc)
return commitOrRollback(dc.tx, err)
}
Expand Down