Skip to content

Commit c1edd0f

Browse files
committed
Implement SingleRow for multiple result sets. Fixes #681
1 parent 266fe62 commit c1edd0f

File tree

2 files changed

+54
-24
lines changed

2 files changed

+54
-24
lines changed

src/MySqlConnector/MySql.Data.MySqlClient/MySqlDataReader.cs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,39 +55,43 @@ internal async Task<bool> NextResultAsync(IOBehavior ioBehavior, CancellationTok
5555
VerifyNotDisposed();
5656
try
5757
{
58-
while (true)
58+
do
5959
{
60-
await m_resultSet!.ReadEntireAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
61-
await ScanResultSetAsync(ioBehavior, m_resultSet, cancellationToken).ConfigureAwait(false);
62-
if (m_hasMoreResults && m_resultSet.ContainsCommandParameters)
63-
await ReadOutParametersAsync(Command!, m_resultSet, ioBehavior, cancellationToken).ConfigureAwait(false);
64-
else
65-
break;
66-
}
60+
while (true)
61+
{
62+
await m_resultSet!.ReadEntireAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
63+
await ScanResultSetAsync(ioBehavior, m_resultSet, cancellationToken).ConfigureAwait(false);
64+
if (m_hasMoreResults && m_resultSet.ContainsCommandParameters)
65+
await ReadOutParametersAsync(Command!, m_resultSet, ioBehavior, cancellationToken).ConfigureAwait(false);
66+
else
67+
break;
68+
}
6769

68-
if (!m_hasMoreResults)
69-
{
70-
if (m_commandListPosition.CommandIndex < m_commandListPosition.Commands.Count)
70+
if (!m_hasMoreResults)
7171
{
72-
Command = m_commandListPosition.Commands[m_commandListPosition.CommandIndex];
73-
using (Command.CancellableCommand.RegisterCancel(cancellationToken))
72+
if (m_commandListPosition.CommandIndex < m_commandListPosition.Commands.Count)
7473
{
75-
var writer = new ByteBufferWriter();
76-
if (!Command.Connection!.Session.IsCancelingQuery && m_payloadCreator.WriteQueryCommand(ref m_commandListPosition, m_cachedProcedures!, writer))
74+
Command = m_commandListPosition.Commands[m_commandListPosition.CommandIndex];
75+
using (Command.CancellableCommand.RegisterCancel(cancellationToken))
7776
{
78-
using var payload = writer.ToPayloadData();
79-
await Command.Connection.Session.SendAsync(payload, ioBehavior, cancellationToken).ConfigureAwait(false);
80-
await m_resultSet.ReadResultSetHeaderAsync(ioBehavior).ConfigureAwait(false);
81-
ActivateResultSet();
82-
m_hasMoreResults = true;
77+
var writer = new ByteBufferWriter();
78+
if (!Command.Connection!.Session.IsCancelingQuery && m_payloadCreator.WriteQueryCommand(ref m_commandListPosition, m_cachedProcedures!, writer))
79+
{
80+
using var payload = writer.ToPayloadData();
81+
await Command.Connection.Session.SendAsync(payload, ioBehavior, cancellationToken).ConfigureAwait(false);
82+
await m_resultSet.ReadResultSetHeaderAsync(ioBehavior).ConfigureAwait(false);
83+
ActivateResultSet();
84+
m_hasMoreResults = true;
85+
}
8386
}
8487
}
8588
}
89+
else
90+
{
91+
ActivateResultSet();
92+
}
8693
}
87-
else
88-
{
89-
ActivateResultSet();
90-
}
94+
while (m_hasMoreResults && (Command!.CommandBehavior & CommandBehavior.SingleRow) != 0);
9195

9296
if (!m_hasMoreResults)
9397
m_resultSet.Reset();

tests/SideBySide/QueryTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,32 @@ public void CommandBehaviorSingleRow()
10711071
Assert.False(reader.Read());
10721072
}
10731073

1074+
[Fact]
1075+
public async Task CommandBehaviorSingleRowMultipleResultSets()
1076+
{
1077+
using var connection = new MySqlConnection(AppConfig.ConnectionString);
1078+
connection.Open();
1079+
connection.Execute(@"drop table if exists command_behavior_single_row;
1080+
create table command_behavior_single_row(id integer not null primary key);");
1081+
1082+
using (var cmd = new MySqlCommand("SELECT 1; insert into command_behavior_single_row(id) values(1); SELECT 2;", connection))
1083+
{
1084+
using var reader = await cmd.ExecuteReaderAsync(CommandBehavior.SingleRow);
1085+
Assert.True(await reader.ReadAsync());
1086+
Assert.Equal(1, reader.GetInt32(0));
1087+
Assert.False(await reader.ReadAsync());
1088+
#if !BASELINE
1089+
Assert.False(await reader.NextResultAsync());
1090+
#endif
1091+
}
1092+
1093+
// subsequent commands were still executed (and had effects) even though they didn't return results
1094+
using (var cmd = new MySqlCommand("select count(*) from command_behavior_single_row;", connection))
1095+
{
1096+
Assert.Equal(1L, await cmd.ExecuteScalarAsync());
1097+
}
1098+
}
1099+
10741100
#if !BASELINE
10751101
[Fact]
10761102
public void NoBackslashEscapes()

0 commit comments

Comments
 (0)