Skip to content
This repository has been archived by the owner on Jan 28, 2021. It is now read-only.

Clear warnings just after successful query. #608

Merged
merged 1 commit into from
Jan 30, 2019
Merged
Show file tree
Hide file tree
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
29 changes: 25 additions & 4 deletions engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1055,24 +1055,45 @@ func TestClearWarnings(t *testing.T) {
require := require.New(t)
e := newEngine(t)
ctx := newCtx()
ctx.Session.Warn(&sql.Warning{Code: 1})
ctx.Session.Warn(&sql.Warning{Code: 2})
ctx.Session.Warn(&sql.Warning{Code: 3})

_, iter, err := e.Query(ctx, "SHOW WARNINGS")
_, iter, err := e.Query(ctx, "-- some empty query as a comment")
require.NoError(err)
err = iter.Close()
require.NoError(err)

_, iter, err = e.Query(ctx, "-- some empty query as a comment")
require.NoError(err)
err = iter.Close()
require.NoError(err)

_, iter, err = e.Query(ctx, "-- some empty query as a comment")
require.NoError(err)
err = iter.Close()
require.NoError(err)

_, iter, err = e.Query(ctx, "SHOW WARNINGS")
require.NoError(err)
rows, err := sql.RowIterToRows(iter)
require.NoError(err)
err = iter.Close()
require.NoError(err)
require.Equal(3, len(rows))

_, iter, err = e.Query(ctx, "SHOW WARNINGS LIMIT 1")
require.NoError(err)
rows, err = sql.RowIterToRows(iter)
require.NoError(err)
err = iter.Close()
require.NoError(err)
require.Equal(1, len(rows))

_, _, err = e.Query(ctx, "SELECT * FROM mytable LIMIT 1")
require.NoError(err)
_, err = sql.RowIterToRows(iter)
require.NoError(err)
err = iter.Close()
require.NoError(err)

require.Equal(0, len(ctx.Session.Warnings()))
}

Expand Down
14 changes: 11 additions & 3 deletions sql/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ type BaseSession struct {
mu sync.RWMutex
config map[string]TypedValue
warnings []*Warning
warncnt uint16
}

// Address returns the server address.
func (s *BaseSession) Address() string { return s.addr }

// User returns session's client information.
// Client returns session's client information.
func (s *BaseSession) Client() Client { return s.client }

// Set implements the Session interface.
Expand Down Expand Up @@ -127,8 +128,15 @@ func (s *BaseSession) Warnings() []*Warning {
func (s *BaseSession) ClearWarnings() {
s.mu.Lock()
defer s.mu.Unlock()
if s.warnings != nil {
s.warnings = s.warnings[:0]

cnt := uint16(len(s.warnings))
if s.warncnt == cnt {
if s.warnings != nil {
s.warnings = s.warnings[:0]
}
s.warncnt = 0
} else {
s.warncnt = cnt
}
}

Expand Down