This is an automated email from the ASF dual-hosted git repository.

luky116 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-seata-go.git


The following commit(s) were added to refs/heads/master by this push:
     new 9b5b080e fix:sql statement was not closed (#736)
9b5b080e is described below

commit 9b5b080e27a5a52ce4223aa99dfb319aea239bc9
Author: xinfan.wu <[email protected]>
AuthorDate: Sat Dec 14 16:24:29 2024 +0800

    fix:sql statement was not closed (#736)
    
    * feat:add more linter
    
    * feat:change golangclilint version to 1.57.x to support more linter
    
    * feat:adjust lint conf and adjust the code to pass the check
    
    * style: format some code; fix: some sql statement or rows was not been 
closed
    
    ---------
    
    Co-authored-by: JayLiu <[email protected]>
---
 .golangci.yml                                                  | 7 ++-----
 pkg/datasource/sql/datasource/mysql/trigger.go                 | 3 ++-
 pkg/datasource/sql/exec/at/escape.go                           | 2 +-
 pkg/datasource/sql/tx.go                                       | 2 +-
 pkg/datasource/sql/types/image.go                              | 4 ++--
 pkg/datasource/sql/undo/base/undo.go                           | 7 +++++--
 pkg/datasource/sql/undo/executor/executor.go                   | 2 +-
 pkg/datasource/sql/undo/executor/mysql_undo_delete_executor.go | 4 ++--
 pkg/datasource/sql/undo/executor/mysql_undo_insert_executor.go | 4 ++--
 pkg/datasource/sql/undo/executor/mysql_undo_update_executor.go | 5 +++--
 pkg/datasource/sql/undo/executor/sql.go                        | 2 +-
 pkg/remoting/getty/readwriter.go                               | 6 +++---
 pkg/remoting/loadbalance/loadbalance.go                        | 2 +-
 13 files changed, 26 insertions(+), 24 deletions(-)

diff --git a/.golangci.yml b/.golangci.yml
index bb7d6eda..e558827c 100644
--- a/.golangci.yml
+++ b/.golangci.yml
@@ -56,15 +56,12 @@ linters:
     - staticcheck
     - ineffassign
     - misspell
-   # - errcheck
     - asciicheck
     - bodyclose
     - rowserrcheck
-    #- makezero
+    - gofmt
     - durationcheck
-    # - prealloc
-    # - predeclared
-
+    - sqlclosecheck
 
 run:
 
diff --git a/pkg/datasource/sql/datasource/mysql/trigger.go 
b/pkg/datasource/sql/datasource/mysql/trigger.go
index d9339a6d..689ad2c7 100644
--- a/pkg/datasource/sql/datasource/mysql/trigger.go
+++ b/pkg/datasource/sql/datasource/mysql/trigger.go
@@ -93,6 +93,7 @@ func (m *mysqlTrigger) getColumnMetas(ctx context.Context, 
dbName string, table
        if err != nil {
                return nil, err
        }
+       defer stmt.Close()
 
        rows, err := stmt.Query(dbName, table)
        if err != nil {
@@ -164,7 +165,7 @@ func (m *mysqlTrigger) getIndexes(ctx context.Context, 
dbName string, tableName
        if err != nil {
                return nil, err
        }
-
+       defer stmt.Close()
        rows, err := stmt.Query(dbName, tableName)
        if err != nil {
                return nil, err
diff --git a/pkg/datasource/sql/exec/at/escape.go 
b/pkg/datasource/sql/exec/at/escape.go
index 3133b40d..bd8d1c6a 100644
--- a/pkg/datasource/sql/exec/at/escape.go
+++ b/pkg/datasource/sql/exec/at/escape.go
@@ -142,7 +142,7 @@ func addEscape(colName string, dbType types.DBType, escape 
string) string {
        buf := make([]byte, len(colName)+2)
        buf[0], buf[len(buf)-1] = escape[0], escape[0]
 
-       for key, _ := range colName {
+       for key := range colName {
                buf[key+1] = colName[key]
        }
 
diff --git a/pkg/datasource/sql/tx.go b/pkg/datasource/sql/tx.go
index a6c5f70f..db55e97d 100644
--- a/pkg/datasource/sql/tx.go
+++ b/pkg/datasource/sql/tx.go
@@ -166,7 +166,7 @@ func (tx *Tx) register(ctx *types.TransactionContext) error 
{
                if !ctx.HasUndoLog() || !ctx.HasLockKey() {
                        return nil
                }
-               for k, _ := range ctx.LockKeys {
+               for k := range ctx.LockKeys {
                        lockKey += k + ";"
                }
                request.LockKeys = lockKey
diff --git a/pkg/datasource/sql/types/image.go 
b/pkg/datasource/sql/types/image.go
index f755d9c9..0290b366 100644
--- a/pkg/datasource/sql/types/image.go
+++ b/pkg/datasource/sql/types/image.go
@@ -146,7 +146,7 @@ func (r *RowImage) GetColumnMap() map[string]*ColumnImage {
 // PrimaryKeys Primary keys list.
 func (r *RowImage) PrimaryKeys(cols []ColumnImage) []ColumnImage {
        var pkFields []ColumnImage
-       for key, _ := range cols {
+       for key := range cols {
                if cols[key].KeyType == PrimaryKey.Number() {
                        pkFields = append(pkFields, cols[key])
                }
@@ -158,7 +158,7 @@ func (r *RowImage) PrimaryKeys(cols []ColumnImage) 
[]ColumnImage {
 // NonPrimaryKeys get non-primary keys
 func (r *RowImage) NonPrimaryKeys(cols []ColumnImage) []ColumnImage {
        var nonPkFields []ColumnImage
-       for key, _ := range cols {
+       for key := range cols {
                if cols[key].KeyType != PrimaryKey.Number() {
                        nonPkFields = append(nonPkFields, cols[key])
                }
diff --git a/pkg/datasource/sql/undo/base/undo.go 
b/pkg/datasource/sql/undo/base/undo.go
index 394cacbd..752597fb 100644
--- a/pkg/datasource/sql/undo/base/undo.go
+++ b/pkg/datasource/sql/undo/base/undo.go
@@ -106,6 +106,8 @@ func (m *BaseUndoLogManager) InsertUndoLogWithSqlConn(ctx 
context.Context, recor
        if err != nil {
                return err
        }
+       defer stmt.Close()
+
        _, err = stmt.Exec(record.BranchID, record.XID, record.Context, 
record.RollbackInfo, int64(record.LogStatus))
        if err != nil {
                return err
@@ -120,7 +122,7 @@ func (m *BaseUndoLogManager) DeleteUndoLog(ctx 
context.Context, xid string, bran
                log.Errorf("[DeleteUndoLog] prepare sql fail, err: %v", err)
                return err
        }
-
+       defer stmt.Close()
        if _, err = stmt.Exec(branchID, xid); err != nil {
                log.Errorf("[DeleteUndoLog] exec delete undo log fail, err: 
%v", err)
                return err
@@ -146,6 +148,7 @@ func (m *BaseUndoLogManager) BatchDeleteUndoLog(xid 
[]string, branchID []int64,
                log.Errorf("prepare sql fail, err: %v", err)
                return err
        }
+       defer stmt.Close()
 
        branchIDStr, err := Int64Slice2Str(branchID, ",")
        if err != nil {
@@ -413,7 +416,7 @@ func (m *BaseUndoLogManager) DBType() types.DBType {
 
 // HasUndoLogTable check undo log table if exist
 func (m *BaseUndoLogManager) HasUndoLogTable(ctx context.Context, conn 
*sql.Conn) (res bool, err error) {
-       if _, err = conn.QueryContext(ctx, getCheckUndoLogTableExistSql()); err 
!= nil { //nolint:rowserrcheck
+       if _, err = conn.QueryContext(ctx, getCheckUndoLogTableExistSql()); err 
!= nil { //nolint:rowserrcheck,sqlclosecheck
                // 1146 mysql table not exist fault code
                if e, ok := err.(*mysql.SQLError); ok && e.Code == 
mysql.ErrNoSuchTable {
                        return false, nil
diff --git a/pkg/datasource/sql/undo/executor/executor.go 
b/pkg/datasource/sql/undo/executor/executor.go
index f4c9aed4..f5b573a6 100644
--- a/pkg/datasource/sql/undo/executor/executor.go
+++ b/pkg/datasource/sql/undo/executor/executor.go
@@ -124,7 +124,7 @@ func (b *BaseExecutor) queryCurrentRecords(ctx 
context.Context, conn *sql.Conn)
        if err != nil {
                return nil, err
        }
-
+       defer rows.Close()
        image := types.RecordImage{
                TableName: b.undoImage.TableName,
                TableMeta: tableMeta,
diff --git a/pkg/datasource/sql/undo/executor/mysql_undo_delete_executor.go 
b/pkg/datasource/sql/undo/executor/mysql_undo_delete_executor.go
index 7a392fc9..02045c3d 100644
--- a/pkg/datasource/sql/undo/executor/mysql_undo_delete_executor.go
+++ b/pkg/datasource/sql/undo/executor/mysql_undo_delete_executor.go
@@ -48,7 +48,7 @@ func (m *mySQLUndoDeleteExecutor) ExecuteOn(ctx 
context.Context, dbType types.DB
        if err != nil {
                return err
        }
-
+       defer stmt.Close()
        beforeImage := m.sqlUndoLog.BeforeImage
 
        for _, row := range beforeImage.Rows {
@@ -97,7 +97,7 @@ func (m *mySQLUndoDeleteExecutor) buildUndoSQL(dbType 
types.DBType) (string, err
                insertColumnSlice, insertValueSlice []string
        )
 
-       for key, _ := range fields {
+       for key := range fields {
                insertColumnSlice = append(insertColumnSlice, 
AddEscape(fields[key].ColumnName, dbType))
                insertValueSlice = append(insertValueSlice, "?")
        }
diff --git a/pkg/datasource/sql/undo/executor/mysql_undo_insert_executor.go 
b/pkg/datasource/sql/undo/executor/mysql_undo_insert_executor.go
index 108a62e6..bd80804d 100644
--- a/pkg/datasource/sql/undo/executor/mysql_undo_insert_executor.go
+++ b/pkg/datasource/sql/undo/executor/mysql_undo_insert_executor.go
@@ -50,7 +50,7 @@ func (m *mySQLUndoInsertExecutor) ExecuteOn(ctx 
context.Context, dbType types.DB
        if err != nil {
                return err
        }
-
+       defer stmt.Close()
        afterImage := m.sqlUndoLog.AfterImage
        for _, row := range afterImage.Rows {
                pkValueList := make([]interface{}, 0)
@@ -96,7 +96,7 @@ func (m *mySQLUndoInsertExecutor) generateDeleteSql(
        }
 
        var pkList []string
-       for key, _ := range colImages {
+       for key := range colImages {
                pkList = append(pkList, colImages[key].ColumnName)
        }
 
diff --git a/pkg/datasource/sql/undo/executor/mysql_undo_update_executor.go 
b/pkg/datasource/sql/undo/executor/mysql_undo_update_executor.go
index d3561c44..65e13218 100644
--- a/pkg/datasource/sql/undo/executor/mysql_undo_update_executor.go
+++ b/pkg/datasource/sql/undo/executor/mysql_undo_update_executor.go
@@ -54,6 +54,7 @@ func (m *mySQLUndoUpdateExecutor) ExecuteOn(ctx 
context.Context, dbType types.DB
        if err != nil {
                return err
        }
+       defer stmt.Close()
 
        beforeImage := m.sqlUndoLog.BeforeImage
        for _, row := range beforeImage.Rows {
@@ -93,7 +94,7 @@ func (m *mySQLUndoUpdateExecutor) buildUndoSQL(dbType 
types.DBType) (string, err
        )
 
        nonPkFields := row.NonPrimaryKeys(row.Columns)
-       for key, _ := range nonPkFields {
+       for key := range nonPkFields {
                updateColumnSlice = append(updateColumnSlice, 
AddEscape(nonPkFields[key].ColumnName, dbType)+" = ? ")
        }
 
@@ -103,7 +104,7 @@ func (m *mySQLUndoUpdateExecutor) buildUndoSQL(dbType 
types.DBType) (string, err
                return "", err
        }
 
-       for key, _ := range pkList {
+       for key := range pkList {
                pkNameList = append(pkNameList, pkList[key].ColumnName)
        }
 
diff --git a/pkg/datasource/sql/undo/executor/sql.go 
b/pkg/datasource/sql/undo/executor/sql.go
index 104baced..5594a443 100644
--- a/pkg/datasource/sql/undo/executor/sql.go
+++ b/pkg/datasource/sql/undo/executor/sql.go
@@ -142,7 +142,7 @@ func addEscape(colName string, dbType types.DBType, escape 
string) string {
        buf := make([]byte, len(colName)+2)
        buf[0], buf[len(buf)-1] = escape[0], escape[0]
 
-       for key, _ := range colName {
+       for key := range colName {
                buf[key+1] = colName[key]
        }
 
diff --git a/pkg/remoting/getty/readwriter.go b/pkg/remoting/getty/readwriter.go
index 5232315f..32164896 100644
--- a/pkg/remoting/getty/readwriter.go
+++ b/pkg/remoting/getty/readwriter.go
@@ -58,9 +58,9 @@ var (
 
 var (
        ErrNotEnoughStream = errors.New("packet stream is not enough")
-       ErrTooLargePackage = errors.New("package length is exceed the getty 
package's legal maximum length.")
+       ErrTooLargePackage = errors.New("package length is exceed the getty 
package's legal maximum length")
        ErrInvalidPackage  = errors.New("invalid rpc package")
-       ErrIllegalMagic    = errors.New("package magic is not right.")
+       ErrIllegalMagic    = errors.New("package magic is not right")
 )
 
 type RpcPackageHandler struct{}
@@ -141,7 +141,7 @@ func (p *RpcPackageHandler) Write(ss getty.Session, pkg 
interface{}) ([]byte, er
        headLength := message.V1HeadLength
 
        var headMapBytes []byte
-       if msg.HeadMap != nil && len(msg.HeadMap) > 0 {
+       if len(msg.HeadMap) > 0 {
                hb, headMapLength := encodeHeapMap(msg.HeadMap)
                headMapBytes = hb
                headLength += headMapLength
diff --git a/pkg/remoting/loadbalance/loadbalance.go 
b/pkg/remoting/loadbalance/loadbalance.go
index 5704eb39..abfb6b6f 100644
--- a/pkg/remoting/loadbalance/loadbalance.go
+++ b/pkg/remoting/loadbalance/loadbalance.go
@@ -40,7 +40,7 @@ func Select(loadBalanceType string, sessions *sync.Map, xid 
string) getty.Sessio
        case consistentHashLoadBalance:
                return ConsistentHashLoadBalance(sessions, xid)
        case leastActiveLoadBalance:
-               return LeastActiveLoadBalance(sessions, xid)    
+               return LeastActiveLoadBalance(sessions, xid)
        case roundRobinLoadBalance:
                return RoundRobinLoadBalance(sessions, xid)
        default:


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to