Skip to content

Commit

Permalink
fix:sql statement was not closed (#736)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
No-SilverBullet and luky116 authored Dec 14, 2024
1 parent 3188ecf commit 9b5b080
Show file tree
Hide file tree
Showing 13 changed files with 26 additions and 24 deletions.
7 changes: 2 additions & 5 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,12 @@ linters:
- staticcheck
- ineffassign
- misspell
# - errcheck
- asciicheck
- bodyclose
- rowserrcheck
#- makezero
- gofmt
- durationcheck
# - prealloc
# - predeclared

- sqlclosecheck

run:

Expand Down
3 changes: 2 additions & 1 deletion pkg/datasource/sql/datasource/mysql/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/datasource/sql/exec/at/escape.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/datasource/sql/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/datasource/sql/types/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
Expand All @@ -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])
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/datasource/sql/undo/base/undo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/datasource/sql/undo/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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, "?")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)+" = ? ")
}

Expand All @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/datasource/sql/undo/executor/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/remoting/getty/readwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/remoting/loadbalance/loadbalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 9b5b080

Please sign in to comment.