File tree Expand file tree Collapse file tree 6 files changed +113
-2
lines changed Expand file tree Collapse file tree 6 files changed +113
-2
lines changed Original file line number Diff line number Diff line change @@ -28,6 +28,7 @@ public async Task<ResultSet> ReadResultSetHeaderAsync(IOBehavior ioBehavior)
28
28
ColumnTypes = null ;
29
29
LastInsertId = 0 ;
30
30
RecordsAffected = null ;
31
+ WarningCount = 0 ;
31
32
State = ResultSetState . None ;
32
33
m_columnDefinitionPayloadUsedBytes = 0 ;
33
34
m_readBuffer . Clear ( ) ;
@@ -47,6 +48,7 @@ public async Task<ResultSet> ReadResultSetHeaderAsync(IOBehavior ioBehavior)
47
48
var ok = OkPayload . Create ( payload . AsSpan ( ) ) ;
48
49
RecordsAffected = ( RecordsAffected ?? 0 ) + ok . AffectedRowCount ;
49
50
LastInsertId = unchecked ( ( long ) ok . LastInsertId ) ;
51
+ WarningCount = ok . WarningCount ;
50
52
if ( ok . NewSchema != null )
51
53
Connection . Session . DatabaseOverride = ok . NewSchema ;
52
54
ColumnDefinitions = null ;
@@ -128,6 +130,7 @@ int ReadColumnCount(ReadOnlySpan<byte> span)
128
130
}
129
131
130
132
LastInsertId = - 1 ;
133
+ WarningCount = 0 ;
131
134
State = ResultSetState . ReadResultSetHeader ;
132
135
break ;
133
136
}
@@ -329,6 +332,7 @@ public Row GetCurrentRow()
329
332
public MySqlDbType [ ] ColumnTypes { get ; private set ; }
330
333
public long LastInsertId { get ; private set ; }
331
334
public int ? RecordsAffected { get ; private set ; }
335
+ public int WarningCount { get ; private set ; }
332
336
public ResultSetState State { get ; private set ; }
333
337
334
338
ResizableArray < byte > m_columnDefinitionPayloads ;
Original file line number Diff line number Diff line change @@ -309,6 +309,8 @@ private SchemaProvider GetSchemaProvider()
309
309
310
310
public override int ConnectionTimeout => m_connectionSettings . ConnectionTimeout ;
311
311
312
+ public event MySqlInfoMessageEventHandler InfoMessage ;
313
+
312
314
protected override void Dispose ( bool disposing )
313
315
{
314
316
try
@@ -444,10 +446,23 @@ internal void SetActiveReader(MySqlDataReader dataReader)
444
446
m_activeReader = dataReader ;
445
447
}
446
448
447
- internal void FinishQuerying ( )
449
+ internal void FinishQuerying ( bool hasWarnings )
448
450
{
449
451
m_session . FinishQuerying ( ) ;
450
452
m_activeReader = null ;
453
+
454
+ if ( hasWarnings && InfoMessage != null )
455
+ {
456
+ var errors = new List < MySqlError > ( ) ;
457
+ using ( var command = new MySqlCommand ( "SHOW WARNINGS;" , this ) )
458
+ using ( var reader = command . ExecuteReader ( ) )
459
+ {
460
+ while ( reader . Read ( ) )
461
+ errors . Add ( new MySqlError ( reader . GetString ( 0 ) , reader . GetInt32 ( 1 ) , reader . GetString ( 2 ) ) ) ;
462
+ }
463
+
464
+ InfoMessage ( this , new MySqlInfoMessageEventArgs ( errors . ToArray ( ) ) ) ;
465
+ }
451
466
}
452
467
453
468
private async ValueTask < ServerSession > CreateSessionAsync ( IOBehavior ? ioBehavior , CancellationToken cancellationToken )
Original file line number Diff line number Diff line change @@ -94,6 +94,7 @@ private void ActivateResultSet(ResultSet resultSet)
94
94
95
95
Command . LastInsertedId = resultSet . LastInsertId ;
96
96
m_recordsAffected = m_recordsAffected == null ? resultSet . RecordsAffected : m_recordsAffected . Value + ( resultSet . RecordsAffected ?? 0 ) ;
97
+ m_hasWarnings = resultSet . WarningCount != 0 ;
97
98
}
98
99
99
100
private ValueTask < ResultSet > ScanResultSetAsync ( IOBehavior ioBehavior , ResultSet resultSet , CancellationToken cancellationToken )
@@ -431,7 +432,7 @@ private void DoClose()
431
432
m_resultSetBuffered = null ;
432
433
433
434
var connection = Command . Connection ;
434
- connection . FinishQuerying ( ) ;
435
+ connection . FinishQuerying ( m_hasWarnings ) ;
435
436
436
437
Command . ReaderClosed ( ) ;
437
438
if ( ( m_behavior & CommandBehavior . CloseConnection ) != 0 )
@@ -458,6 +459,7 @@ private ResultSet GetResultSet()
458
459
readonly CommandBehavior m_behavior ;
459
460
bool m_closed ;
460
461
int ? m_recordsAffected ;
462
+ bool m_hasWarnings ;
461
463
ResultSet m_resultSet ;
462
464
ResultSet m_resultSetBuffered ;
463
465
#if ! NETSTANDARD1_3
Original file line number Diff line number Diff line change
1
+ namespace MySql . Data . MySqlClient
2
+ {
3
+ public sealed class MySqlError
4
+ {
5
+ internal MySqlError ( string level , int code , string message )
6
+ {
7
+ Level = level ;
8
+ Code = code ;
9
+ Message = message ;
10
+ }
11
+
12
+ public string Level { get ; }
13
+ public int Code { get ; }
14
+ public string Message { get ; }
15
+ } ;
16
+ }
Original file line number Diff line number Diff line change
1
+ using System ;
2
+ using System . Collections . Generic ;
3
+
4
+ namespace MySql . Data . MySqlClient
5
+ {
6
+ public sealed class MySqlInfoMessageEventArgs : EventArgs
7
+ {
8
+ internal MySqlInfoMessageEventArgs ( MySqlError [ ] errors ) => this . errors = errors ;
9
+
10
+ public MySqlError [ ] errors { get ; }
11
+
12
+ public IReadOnlyList < MySqlError > Errors => errors ;
13
+ }
14
+
15
+ public delegate void MySqlInfoMessageEventHandler ( object sender , MySqlInfoMessageEventArgs args ) ;
16
+ }
Original file line number Diff line number Diff line change
1
+ using Dapper ;
2
+ using MySql . Data . MySqlClient ;
3
+ using Xunit ;
4
+
5
+ namespace SideBySide
6
+ {
7
+ public class ConnectionTests : IClassFixture < DatabaseFixture >
8
+ {
9
+ public ConnectionTests ( DatabaseFixture database )
10
+ {
11
+ }
12
+
13
+ [ Fact ]
14
+ public void GotInfoMessageForNonExistentTable ( )
15
+ {
16
+ using ( var connection = new MySqlConnection ( AppConfig . ConnectionString ) )
17
+ {
18
+ connection . Open ( ) ;
19
+
20
+ var gotEvent = false ;
21
+ connection . InfoMessage += ( s , a ) =>
22
+ {
23
+ gotEvent = true ;
24
+ Assert . Single ( a . errors ) ;
25
+ Assert . Equal ( ( int ) MySqlErrorCode . BadTable , a . errors [ 0 ] . Code ) ;
26
+ } ;
27
+
28
+ connection . Execute ( @"drop table if exists table_does_not_exist;" ) ;
29
+ Assert . True ( gotEvent ) ;
30
+ }
31
+ }
32
+
33
+ [ Fact ]
34
+ public void NoInfoMessageWhenNotLastStatementInBatch ( )
35
+ {
36
+ using ( var connection = new MySqlConnection ( AppConfig . ConnectionString ) )
37
+ {
38
+ connection . Open ( ) ;
39
+
40
+ var gotEvent = false ;
41
+ connection . InfoMessage += ( s , a ) =>
42
+ {
43
+ gotEvent = true ;
44
+
45
+ // seeming bug in Connector/NET raises an event with no errors
46
+ Assert . Empty ( a . errors ) ;
47
+ } ;
48
+
49
+ connection . Execute ( @"drop table if exists table_does_not_exist; select 1;" ) ;
50
+ #if BASELINE
51
+ Assert . True ( gotEvent ) ;
52
+ #else
53
+ Assert . False ( gotEvent ) ;
54
+ #endif
55
+ }
56
+ }
57
+ }
58
+ }
You can’t perform that action at this time.
0 commit comments