Skip to content

Commit 3129812

Browse files
committed
Fix HasRows returning false. Fixes #327
1 parent 1953152 commit 3129812

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/MySqlConnector/MySqlClient/Results/ResultSet.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Globalization;
44
using System.IO;
@@ -29,6 +29,7 @@ public async Task<ResultSet> ReadResultSetHeaderAsync(IOBehavior ioBehavior)
2929
m_readBuffer.Clear();
3030
m_row = null;
3131
m_rowBuffered = null;
32+
m_hasRows = false;
3233

3334
try
3435
{
@@ -226,6 +227,7 @@ Row ScanRowAsyncRemainder(PayloadData payload)
226227
row = new Row(this);
227228
row.SetData(m_dataLengths, m_dataOffsets, payload.ArraySegment);
228229
m_rowBuffered = row;
230+
m_hasRows = true;
229231
return row;
230232
}
231233
}
@@ -395,7 +397,7 @@ public bool HasRows
395397
{
396398
if (BufferState == ResultSetState.ReadResultSetHeader)
397399
return BufferReadAsync(IOBehavior.Synchronous, CancellationToken.None).GetAwaiter().GetResult() != null;
398-
return BufferState == ResultSetState.ReadingRows;
400+
return m_hasRows;
399401
}
400402
}
401403

@@ -423,5 +425,6 @@ public Row GetCurrentRow()
423425
readonly Queue<Row> m_readBuffer = new Queue<Row>();
424426
Row m_row;
425427
Row m_rowBuffered;
428+
bool m_hasRows;
426429
}
427430
}

tests/SideBySide/QueryTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,41 @@ public void ReturnDerivedTypes()
795795
}
796796
}
797797

798+
[Theory]
799+
[InlineData(new[] { 1 }, new[] { true })]
800+
[InlineData(new[] { 4 }, new[] { false })]
801+
[InlineData(new[] { 1, 2 }, new[] { true, true })]
802+
[InlineData(new[] { 1, 4 }, new[] { true, false })]
803+
[InlineData(new[] { 4, 1 }, new[] { false, true })]
804+
[InlineData(new[] { 4, 5 }, new[] { false, false })]
805+
public void HasRows(int[] values, bool[] expecteds)
806+
{
807+
m_database.Connection.Execute(@"drop table if exists has_rows;
808+
create table has_rows(value int not null);
809+
insert into has_rows(value) values(1),(2),(3);");
810+
811+
var sql = "";
812+
foreach (var value in values)
813+
sql += $"select * from has_rows where value = {value};";
814+
815+
using (var cmd = m_database.Connection.CreateCommand())
816+
{
817+
cmd.CommandText = sql;
818+
using (var reader = cmd.ExecuteReader())
819+
{
820+
for (int i = 0; i < expecteds.Length; i++)
821+
{
822+
Assert.Equal(expecteds[i], reader.HasRows);
823+
Assert.Equal(expecteds[i], reader.Read());
824+
Assert.False(reader.Read());
825+
Assert.Equal(expecteds[i], reader.HasRows);
826+
827+
Assert.Equal(i != expecteds.Length - 1, reader.NextResult());
828+
}
829+
}
830+
}
831+
}
832+
798833
class BoolTest
799834
{
800835
public int Id { get; set; }

0 commit comments

Comments
 (0)