Skip to content

Commit

Permalink
*: don't use zero value of mysql.Result
Browse files Browse the repository at this point in the history
Signed-off-by: lance6716 <[email protected]>
  • Loading branch information
lance6716 committed Jan 13, 2025
1 parent 7108b1d commit 7048fbf
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 36 deletions.
1 change: 1 addition & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ func (s *clientTestSuite) TestConn_Insert() {
pkg, err := s.c.Execute(str)
require.NoError(s.T(), err)
require.Equal(s.T(), uint64(1), pkg.AffectedRows)
require.EqualValues(s.T(), 0, pkg.ColumnNumber())
}

func (s *clientTestSuite) TestConn_Insert2() {
Expand Down
8 changes: 4 additions & 4 deletions client/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,10 @@ func (c *Conn) ExecuteMultiple(query string, perResultCallback ExecPerResultCall
// streaming session
// if this would end up in WriteValue, it would just be ignored as all
// responses should have been handled in perResultCallback
return &Result{Resultset: &Resultset{
Streaming: StreamingMultiple,
StreamingDone: true,
}}, nil
rs := NewResultset(0)
rs.Streaming = StreamingMultiple
rs.StreamingDone = true
return NewResult(rs), nil
}

// ExecuteSelectStreaming will call perRowCallback for every row in resultset
Expand Down
6 changes: 2 additions & 4 deletions client/resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (c *Conn) handleOKPacket(data []byte) (*Result, error) {
var n int
var pos = 1

r := new(Result)
r := NewResultReserveResultset(0)

r.AffectedRows, _, n = LengthEncodedInt(data[pos:])
pos += n
Expand Down Expand Up @@ -283,9 +283,7 @@ func (c *Conn) readResultset(data []byte, binary bool) (*Result, error) {
return nil, ErrMalformPacket
}

result := &Result{
Resultset: NewResultset(int(count)),
}
result := NewResultReserveResultset(int(count))

if err := c.readResultColumns(result); err != nil {
return nil, errors.Trace(err)
Expand Down
14 changes: 14 additions & 0 deletions mysql/result.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package mysql

// Result should be created by NewResultWithoutRows or NewResult. The zero value
// of Result is invalid.
type Result struct {
Status uint16
Warnings uint16
Expand All @@ -10,6 +12,18 @@ type Result struct {
*Resultset
}

func NewResult(resultset *Resultset) *Result {
return &Result{
Resultset: resultset,
}
}

func NewResultReserveResultset(fieldCount int) *Result {
return &Result{
Resultset: NewResultset(fieldCount),
}
}

type Executer interface {
Execute(query string, args ...interface{}) (*Result, error)
}
Expand Down
16 changes: 3 additions & 13 deletions mysql/resultset.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const (
StreamingMultiple
)

// Resultset should be created with NewResultset to avoid nil pointer and reduce
// GC pressure.
type Resultset struct {
Fields []*Field
FieldNames map[string]int
Expand Down Expand Up @@ -61,9 +63,7 @@ func (r *Resultset) Reset(fieldsCount int) {
r.RowDatas = r.RowDatas[:0]

if r.FieldNames != nil {
for k := range r.FieldNames {
delete(r.FieldNames, k)
}
clear(r.FieldNames)
} else {
r.FieldNames = make(map[string]int)
}
Expand All @@ -80,22 +80,12 @@ func (r *Resultset) Reset(fieldsCount int) {
}

// RowNumber is returning the number of rows in the [Resultset].
//
// For a nil [Resultset] 0 is returned.
func (r *Resultset) RowNumber() int {
if r == nil {
return 0
}
return len(r.Values)
}

// ColumnNumber is returning the number of fields in the [Resultset].
//
// For a nil [Resultset] 0 is returned.
func (r *Resultset) ColumnNumber() int {
if r == nil {
return 0
}
return len(r.Fields)
}

Expand Down
8 changes: 2 additions & 6 deletions mysql/resultset_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,7 @@ func formatField(field *Field, value interface{}) error {
}

func BuildSimpleTextResultset(names []string, values [][]interface{}) (*Resultset, error) {
r := new(Resultset)

r.Fields = make([]*Field, len(names))
r := NewResultset(len(names))

var b []byte

Expand Down Expand Up @@ -234,9 +232,7 @@ func BuildSimpleTextResultset(names []string, values [][]interface{}) (*Resultse
}

func BuildSimpleBinaryResultset(names []string, values [][]interface{}) (*Resultset, error) {
r := new(Resultset)

r.Fields = make([]*Field, len(names))
r := NewResultset(len(names))

var b []byte

Expand Down
8 changes: 1 addition & 7 deletions server/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,7 @@ func (h EmptyHandler) HandleQuery(query string) (*Result, error) {
if err != nil {
return nil, err
}
return &mysql.Result{
Status: 0,
Warnings: 0,
InsertId: 0,
AffectedRows: 0,
Resultset: r,
}, nil
return mysql.NewResult(r), nil
}

return nil, fmt.Errorf("not supported now")
Expand Down
2 changes: 1 addition & 1 deletion server/resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func (c *Conn) writeOK(r *Result) error {
if r == nil {
r = &Result{}
r = NewResultReserveResultset(0)
}

r.Status |= c.status
Expand Down
2 changes: 1 addition & 1 deletion server/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func (c *Conn) handleStmtReset(data []byte) (*Result, error) {

s.ResetParams()

return &Result{}, nil
return NewResultReserveResultset(0), nil
}

// stmt close command has no response
Expand Down

0 comments on commit 7048fbf

Please sign in to comment.