Skip to content

Commit

Permalink
optimize some format (apache#392)
Browse files Browse the repository at this point in the history
Co-authored-by: haohongfan1 <haohongfan1@jd.com>
  • Loading branch information
georgehao and haohongfan1 authored Dec 8, 2022
1 parent c9fffa5 commit 9f6d875
Show file tree
Hide file tree
Showing 17 changed files with 113 additions and 141 deletions.
3 changes: 1 addition & 2 deletions pkg/datasource/sql/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
// by multiple goroutines.
//
// Conn is assumed to be stateful.

type Conn struct {
res *DBResource
txCtx *types.TransactionContext
Expand Down Expand Up @@ -135,7 +134,7 @@ func (c *Conn) Query(query string, args []driver.Value) (driver.Rows, error) {
return nil, driver.ErrSkip
}

executor, err := exec.BuildExecutor(c.res.dbType, c.txCtx.TxType, query)
executor, err := exec.BuildExecutor(c.res.dbType, c.txCtx.TransactionMode, query)
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/datasource/sql/conn_at.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (c *ATConn) QueryContext(ctx context.Context, query string, args []driver.N
}

ret, err := c.createNewTxOnExecIfNeed(ctx, func() (types.ExecResult, error) {
executor, err := exec.BuildExecutor(c.res.dbType, c.txCtx.TxType, query)
executor, err := exec.BuildExecutor(c.res.dbType, c.txCtx.TransactionMode, query)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -89,7 +89,7 @@ func (c *ATConn) ExecContext(ctx context.Context, query string, args []driver.Na
}

ret, err := c.createNewTxOnExecIfNeed(ctx, func() (types.ExecResult, error) {
executor, err := exec.BuildExecutor(c.res.dbType, c.txCtx.TxType, query)
executor, err := exec.BuildExecutor(c.res.dbType, c.txCtx.TransactionMode, query)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -130,7 +130,7 @@ func (c *ATConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx,

if tm.IsGlobalTx(ctx) {
c.txCtx.XID = tm.GetXID(ctx)
c.txCtx.TxType = types.ATMode
c.txCtx.TransactionMode = types.ATMode
}

tx, err := c.Conn.BeginTx(ctx, opts)
Expand All @@ -149,7 +149,7 @@ func (c *ATConn) createOnceTxContext(ctx context.Context) bool {
c.txCtx.DBType = c.res.dbType
c.txCtx.ResourceID = c.res.resourceID
c.txCtx.XID = tm.GetXID(ctx)
c.txCtx.TxType = types.ATMode
c.txCtx.TransactionMode = types.ATMode
c.txCtx.GlobalLockRequire = true
}

Expand All @@ -162,7 +162,7 @@ func (c *ATConn) createNewTxOnExecIfNeed(ctx context.Context, f func() (types.Ex
err error
)

if c.txCtx.TxType != types.Local && c.autoCommit {
if c.txCtx.TransactionMode != types.Local && c.autoCommit {
tx, err = c.BeginTx(ctx, driver.TxOptions{Isolation: driver.IsolationLevel(gosql.LevelDefault)})
if err != nil {
return nil, err
Expand Down
12 changes: 6 additions & 6 deletions pkg/datasource/sql/conn_at_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ func TestATConn_ExecContext(t *testing.T) {
beforeHook := func(_ context.Context, execCtx *types.ExecContext) {
t.Logf("on exec xid=%s", execCtx.TxCtx.XID)
assert.Equal(t, tm.GetXID(ctx), execCtx.TxCtx.XID)
assert.Equal(t, types.ATMode, execCtx.TxCtx.TxType)
assert.Equal(t, types.ATMode, execCtx.TxCtx.TransactionMode)
}
mi.before = beforeHook

var comitCnt int32
beforeCommit := func(tx *Tx) {
atomic.AddInt32(&comitCnt, 1)
assert.Equal(t, types.ATMode, tx.tranCtx.TxType)
assert.Equal(t, types.ATMode, tx.tranCtx.TransactionMode)
}
ti.beforeCommit = beforeCommit

Expand All @@ -112,7 +112,7 @@ func TestATConn_ExecContext(t *testing.T) {
t.Run("not xid", func(t *testing.T) {
mi.before = func(_ context.Context, execCtx *types.ExecContext) {
assert.Equal(t, "", execCtx.TxCtx.XID)
assert.Equal(t, types.Local, execCtx.TxCtx.TxType)
assert.Equal(t, types.Local, execCtx.TxCtx.TransactionMode)
}

var comitCnt int32
Expand Down Expand Up @@ -149,7 +149,7 @@ func TestATConn_BeginTx(t *testing.T) {

mi.before = func(_ context.Context, execCtx *types.ExecContext) {
assert.Equal(t, "", execCtx.TxCtx.XID)
assert.Equal(t, types.Local, execCtx.TxCtx.TxType)
assert.Equal(t, types.Local, execCtx.TxCtx.TransactionMode)
}

var comitCnt int32
Expand All @@ -175,7 +175,7 @@ func TestATConn_BeginTx(t *testing.T) {

mi.before = func(_ context.Context, execCtx *types.ExecContext) {
assert.Equal(t, "", execCtx.TxCtx.XID)
assert.Equal(t, types.Local, execCtx.TxCtx.TxType)
assert.Equal(t, types.Local, execCtx.TxCtx.TransactionMode)
}

var comitCnt int32
Expand Down Expand Up @@ -203,7 +203,7 @@ func TestATConn_BeginTx(t *testing.T) {

mi.before = func(_ context.Context, execCtx *types.ExecContext) {
assert.Equal(t, tm.GetXID(ctx), execCtx.TxCtx.XID)
assert.Equal(t, types.ATMode, execCtx.TxCtx.TxType)
assert.Equal(t, types.ATMode, execCtx.TxCtx.TransactionMode)
}

var comitCnt int32
Expand Down
4 changes: 2 additions & 2 deletions pkg/datasource/sql/conn_xa.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (c *XAConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx,
c.txCtx.TxOpt = opts

if tm.IsGlobalTx(ctx) {
c.txCtx.TxType = types.XAMode
c.txCtx.TransactionMode = types.XAMode
c.txCtx.XID = tm.GetXID(ctx)
}

Expand All @@ -92,7 +92,7 @@ func (c *XAConn) createOnceTxContext(ctx context.Context) bool {
c.txCtx = types.NewTxCtx()
c.txCtx.DBType = c.res.dbType
c.txCtx.XID = tm.GetXID(ctx)
c.txCtx.TxType = types.XAMode
c.txCtx.TransactionMode = types.XAMode
}

return onceTx
Expand Down
12 changes: 6 additions & 6 deletions pkg/datasource/sql/conn_xa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ func TestXAConn_ExecContext(t *testing.T) {
before := func(_ context.Context, execCtx *types.ExecContext) {
t.Logf("on exec xid=%s", execCtx.TxCtx.XID)
assert.Equal(t, tm.GetXID(ctx), execCtx.TxCtx.XID)
assert.Equal(t, types.XAMode, execCtx.TxCtx.TxType)
assert.Equal(t, types.XAMode, execCtx.TxCtx.TransactionMode)
}
mi.before = before

var comitCnt int32
beforeCommit := func(tx *Tx) {
atomic.AddInt32(&comitCnt, 1)
assert.Equal(t, tx.tranCtx.TxType, types.XAMode)
assert.Equal(t, tx.tranCtx.TransactionMode, types.XAMode)
}
ti.beforeCommit = beforeCommit

Expand All @@ -164,7 +164,7 @@ func TestXAConn_ExecContext(t *testing.T) {
t.Run("not xid", func(t *testing.T) {
before := func(_ context.Context, execCtx *types.ExecContext) {
assert.Equal(t, "", execCtx.TxCtx.XID)
assert.Equal(t, types.Local, execCtx.TxCtx.TxType)
assert.Equal(t, types.Local, execCtx.TxCtx.TransactionMode)
}
mi.before = before

Expand Down Expand Up @@ -203,7 +203,7 @@ func TestXAConn_BeginTx(t *testing.T) {

mi.before = func(_ context.Context, execCtx *types.ExecContext) {
assert.Equal(t, "", execCtx.TxCtx.XID)
assert.Equal(t, types.Local, execCtx.TxCtx.TxType)
assert.Equal(t, types.Local, execCtx.TxCtx.TransactionMode)
}

var comitCnt int32
Expand All @@ -229,7 +229,7 @@ func TestXAConn_BeginTx(t *testing.T) {

mi.before = func(_ context.Context, execCtx *types.ExecContext) {
assert.Equal(t, "", execCtx.TxCtx.XID)
assert.Equal(t, types.Local, execCtx.TxCtx.TxType)
assert.Equal(t, types.Local, execCtx.TxCtx.TransactionMode)
}

var comitCnt int32
Expand Down Expand Up @@ -257,7 +257,7 @@ func TestXAConn_BeginTx(t *testing.T) {

mi.before = func(_ context.Context, execCtx *types.ExecContext) {
assert.Equal(t, tm.GetXID(ctx), execCtx.TxCtx.XID)
assert.Equal(t, types.XAMode, execCtx.TxCtx.TxType)
assert.Equal(t, types.XAMode, execCtx.TxCtx.TransactionMode)
}

var comitCnt int32
Expand Down
6 changes: 3 additions & 3 deletions pkg/datasource/sql/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (

type seataATConnector struct {
*seataConnector
transType types.TransactionType
transType types.TransactionMode
}

func (c *seataATConnector) Connect(ctx context.Context) (driver.Conn, error) {
Expand All @@ -53,7 +53,7 @@ func (c *seataATConnector) Driver() driver.Driver {

type seataXAConnector struct {
*seataConnector
transType types.TransactionType
transType types.TransactionMode
}

func (c *seataXAConnector) Connect(ctx context.Context) (driver.Conn, error) {
Expand Down Expand Up @@ -88,7 +88,7 @@ func (c *seataXAConnector) Driver() driver.Driver {
// If a Connector implements io.Closer, the sql package's DB.Close
// method will call Close and return error (if any).
type seataConnector struct {
transType types.TransactionType
transType types.TransactionMode
conf *seataServerConfig
res *DBResource
once sync.Once
Expand Down
4 changes: 2 additions & 2 deletions pkg/datasource/sql/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func Test_seataATConnector_Connect(t *testing.T) {

atConn, ok := conn.(*ATConn)
assert.True(t, ok, "need return seata at connection")
assert.True(t, atConn.txCtx.TxType == types.Local, "init need local tx")
assert.True(t, atConn.txCtx.TransactionMode == types.Local, "init need local tx")
}

func initMockXaConnector(t *testing.T, ctrl *gomock.Controller, db *sql.DB, f initConnectorFunc) driver.Connector {
Expand Down Expand Up @@ -126,5 +126,5 @@ func Test_seataXAConnector_Connect(t *testing.T) {

xaConn, ok := conn.(*XAConn)
assert.True(t, ok, "need return seata xa connection")
assert.True(t, xaConn.txCtx.TxType == types.Local, "init need local tx")
assert.True(t, xaConn.txCtx.TransactionMode == types.Local, "init need local tx")
}
3 changes: 2 additions & 1 deletion pkg/datasource/sql/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func init() {
target: mysql.MySQLDriver{},
},
})

sql.Register(SeataXAMySQLDriver, &seataXADriver{
seataDriver: &seataDriver{
transType: types.XAMode,
Expand Down Expand Up @@ -96,7 +97,7 @@ func (d *seataXADriver) OpenConnector(name string) (c driver.Connector, err erro
}

type seataDriver struct {
transType types.TransactionType
transType types.TransactionMode
target driver.Driver
}

Expand Down
Loading

0 comments on commit 9f6d875

Please sign in to comment.