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

Commit 5bbb4f0

Browse files
authored
Merge pull request #608 from kuba--/fix-600/warnings
Clear warnings just after successful query.
2 parents de21348 + 4a2cb0d commit 5bbb4f0

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

engine_test.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,24 +1055,45 @@ func TestClearWarnings(t *testing.T) {
10551055
require := require.New(t)
10561056
e := newEngine(t)
10571057
ctx := newCtx()
1058-
ctx.Session.Warn(&sql.Warning{Code: 1})
1059-
ctx.Session.Warn(&sql.Warning{Code: 2})
1060-
ctx.Session.Warn(&sql.Warning{Code: 3})
10611058

1062-
_, iter, err := e.Query(ctx, "SHOW WARNINGS")
1059+
_, iter, err := e.Query(ctx, "-- some empty query as a comment")
1060+
require.NoError(err)
1061+
err = iter.Close()
1062+
require.NoError(err)
1063+
1064+
_, iter, err = e.Query(ctx, "-- some empty query as a comment")
1065+
require.NoError(err)
1066+
err = iter.Close()
1067+
require.NoError(err)
1068+
1069+
_, iter, err = e.Query(ctx, "-- some empty query as a comment")
1070+
require.NoError(err)
1071+
err = iter.Close()
1072+
require.NoError(err)
1073+
1074+
_, iter, err = e.Query(ctx, "SHOW WARNINGS")
10631075
require.NoError(err)
10641076
rows, err := sql.RowIterToRows(iter)
10651077
require.NoError(err)
1078+
err = iter.Close()
1079+
require.NoError(err)
10661080
require.Equal(3, len(rows))
10671081

10681082
_, iter, err = e.Query(ctx, "SHOW WARNINGS LIMIT 1")
10691083
require.NoError(err)
10701084
rows, err = sql.RowIterToRows(iter)
10711085
require.NoError(err)
1086+
err = iter.Close()
1087+
require.NoError(err)
10721088
require.Equal(1, len(rows))
10731089

10741090
_, _, err = e.Query(ctx, "SELECT * FROM mytable LIMIT 1")
10751091
require.NoError(err)
1092+
_, err = sql.RowIterToRows(iter)
1093+
require.NoError(err)
1094+
err = iter.Close()
1095+
require.NoError(err)
1096+
10761097
require.Equal(0, len(ctx.Session.Warnings()))
10771098
}
10781099

sql/session.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,13 @@ type BaseSession struct {
5959
mu sync.RWMutex
6060
config map[string]TypedValue
6161
warnings []*Warning
62+
warncnt uint16
6263
}
6364

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

67-
// User returns session's client information.
68+
// Client returns session's client information.
6869
func (s *BaseSession) Client() Client { return s.client }
6970

7071
// Set implements the Session interface.
@@ -127,8 +128,15 @@ func (s *BaseSession) Warnings() []*Warning {
127128
func (s *BaseSession) ClearWarnings() {
128129
s.mu.Lock()
129130
defer s.mu.Unlock()
130-
if s.warnings != nil {
131-
s.warnings = s.warnings[:0]
131+
132+
cnt := uint16(len(s.warnings))
133+
if s.warncnt == cnt {
134+
if s.warnings != nil {
135+
s.warnings = s.warnings[:0]
136+
}
137+
s.warncnt = 0
138+
} else {
139+
s.warncnt = cnt
132140
}
133141
}
134142

0 commit comments

Comments
 (0)