Skip to content

Commit 0f19ccf

Browse files
committed
added extra testing and modified code to more closely follow original drivers behaviour.
1 parent 4dacfbe commit 0f19ccf

File tree

6 files changed

+339
-60
lines changed

6 files changed

+339
-60
lines changed

src/AdoNetCore.AseClient/AseDataReader.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public sealed class AseDataReader : DbDataReader
1919
private readonly CommandBehavior _behavior;
2020
private readonly IInfoMessageEventNotifier _eventNotifier;
2121
private bool _hasFirst;
22+
private int _totalResults;
2223

2324
#if ENABLE_SYSTEM_DATA_COMMON_EXTENSIONS
2425
private readonly AseCommand _command;
@@ -44,6 +45,8 @@ internal AseDataReader(CommandBehavior behavior, IInfoMessageEventNotifier event
4445
internal void AddResult(TableResult result)
4546
{
4647
_results.Add(result);
48+
_totalResults++;
49+
result.RecordsAffected = _finalRecordsAffected;
4750

4851
// If this is the first data result back, then we should automatically point at it.
4952
if (!_hasFirst)
@@ -632,16 +635,20 @@ public override bool Read()
632635

633636
public override int Depth => 0;
634637
public override bool IsClosed => _currentTable == null;
638+
private int _finalRecordsAffected = -1;
635639
public override int RecordsAffected
636640
{
637641
get
638642
{
639-
if (_currentTable == null)
640-
return 0;
641-
return _currentTable.Rows.Count > 0 ? -1 : _currentTable.RecordsAffected;
643+
return _currentTable != null && _currentResult < _totalResults ? _currentTable.RecordsAffected : _finalRecordsAffected;
642644
}
643645
}
644646

647+
public void SetRecordsAffected(int value)
648+
{
649+
_finalRecordsAffected = value;
650+
}
651+
645652
public IList GetList()
646653
{
647654
return null; //todo: implement -- populate a DataView with rows from the current record set and return it.

src/AdoNetCore.AseClient/Internal/Handler/DoneTokenHandler.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,24 @@ public void Handle(IToken token)
4646
break;
4747
case DoneProcToken t:
4848
Logger.Instance?.WriteLine($"{t.Type}: {t.Status}");
49-
if (t.Status.HasFlag(DoneProcCommonToken.DoneProcStatus.TDS_DONE_INXACT))
49+
if (t.Status.HasFlag(DoneToken.DoneStatus.TDS_DONE_INXACT))
5050
{
5151
Logger.Instance?.WriteLine($" {t.TransactionState}");
5252
TransactionState = t.TransactionState;
5353
}
54-
if (t.Status.HasFlag(DoneProcCommonToken.DoneProcStatus.TDS_DONE_COUNT))
54+
if (t.Status.HasFlag(DoneToken.DoneStatus.TDS_DONE_COUNT))
5555
{
5656
RowsAffected += t.Count;
5757
}
5858
break;
5959
case DoneInProcToken t:
6060
Logger.Instance?.WriteLine($"{t.Type}: {t.Status}");
61-
if (t.Status.HasFlag(DoneProcCommonToken.DoneProcStatus.TDS_DONE_INXACT))
61+
if (t.Status.HasFlag(DoneToken.DoneStatus.TDS_DONE_INXACT))
6262
{
6363
Logger.Instance?.WriteLine($" {t.TransactionState}");
6464
TransactionState = t.TransactionState;
6565
}
66-
if (t.Status.HasFlag(DoneProcCommonToken.DoneProcStatus.TDS_DONE_COUNT))
66+
if (t.Status.HasFlag(DoneToken.DoneStatus.TDS_DONE_COUNT))
6767
{
6868
RowsAffected += t.Count;
6969
}

src/AdoNetCore.AseClient/Internal/Handler/StreamingDataReaderTokenHandler.cs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ internal class StreamingDataReaderTokenHandler : ITokenHandler
3030
private TableResult _current;
3131
private bool _hasFirstResultSet;
3232
private bool _hasSentCurrent;
33+
private bool _hasFormatted;
34+
private int _runningTotalRecordsAffected;
3335

3436
public StreamingDataReaderTokenHandler(TaskCompletionSource<DbDataReader> readerSource, AseDataReader dataReader, IInfoMessageEventNotifier eventNotifier)
3537
{
@@ -49,13 +51,15 @@ public void Handle(IToken token)
4951
switch (token)
5052
{
5153
case IFormatToken format:
54+
5255
ReturnCurrent();
5356
_current = new TableResult
5457
{
5558
Formats = format.Formats,
5659
RecordsAffected = -1
5760
};
5861
_hasSentCurrent = false;
62+
_hasFormatted = true;
5963
break;
6064
case RowToken row:
6165
_current?.Rows.Add(new RowResult
@@ -64,15 +68,27 @@ public void Handle(IToken token)
6468
});
6569
break;
6670
case DoneToken t:
67-
EnsureResultExists(t.Count);
68-
ReturnCurrent();
69-
break;
70-
case DoneInProcToken t:
71-
EnsureResultExists(t.Count);
72-
ReturnCurrent();
71+
if (_hasFormatted)
72+
{
73+
ReturnCurrent();
74+
_hasFormatted = false;
75+
}
76+
else if ((t.Status & DoneToken.DoneStatus.TDS_DONE_COUNT) == DoneToken.DoneStatus.TDS_DONE_COUNT)
77+
{
78+
if (_current != null)
79+
{
80+
81+
}
82+
_runningTotalRecordsAffected += t.Count;
83+
_dataReader.SetRecordsAffected(_runningTotalRecordsAffected);
84+
}
85+
if ((t.Status & DoneToken.DoneStatus.TDS_DONE_MORE) == DoneToken.DoneStatus.TDS_DONE_FINAL)
86+
{
87+
ReturnCurrent();
88+
}
7389
break;
74-
case DoneProcToken t:
75-
EnsureResultExists(t.Count);
90+
case DoneInProcToken _:
91+
case DoneProcToken _:
7692
ReturnCurrent();
7793
break;
7894
case EedToken t:
@@ -134,17 +150,6 @@ public void Handle(IToken token)
134150
}
135151
}
136152

137-
private void EnsureResultExists(int affectedCount)
138-
{
139-
if (_current == null)
140-
{
141-
_current = new TableResult()
142-
{
143-
RecordsAffected = affectedCount
144-
};
145-
}
146-
}
147-
148153
private void ReturnCurrent()
149154
{
150155
if (_current != null && !_hasSentCurrent)

src/AdoNetCore.AseClient/Internal/TableResult.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ internal sealed class TableResult
88
public List<RowResult> Rows { get; } = new List<RowResult>();
99

1010
public List<MessageResult> Messages { get; } = new List<MessageResult>();
11+
1112
public int RecordsAffected { get; set; } = 0;
1213
}
1314
}
Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.IO;
33
using AdoNetCore.AseClient.Enum;
44
using AdoNetCore.AseClient.Interface;
@@ -8,37 +8,7 @@ namespace AdoNetCore.AseClient.Token
88
{
99
internal abstract class DoneProcCommonToken
1010
{
11-
[Flags]
12-
public enum DoneProcStatus : ushort
13-
{
14-
/// <summary>
15-
/// This is the final result for the last command. It indicates that the command has completed successfully.
16-
/// </summary>
17-
// ReSharper disable once InconsistentNaming
18-
TDS_DONE_FINAL = 0x0000,
19-
/// <summary>
20-
/// This Status indicates that there are more results to follow for the current command.
21-
/// </summary>
22-
// ReSharper disable once InconsistentNaming
23-
TDS_DONE_MORE = 0x0001,
24-
/// <summary>
25-
/// This indicates that an error occurred on the current command.
26-
/// </summary>
27-
// ReSharper disable once InconsistentNaming
28-
TDS_DONE_ERROR = 0x0002,
29-
/// <summary>
30-
/// There is a transaction in progress for the current request.
31-
/// </summary>
32-
// ReSharper disable once InconsistentNaming
33-
TDS_DONE_INXACT = 0x0004,
34-
/// <summary>
35-
/// This Status indicates that the count argument is valid. This bit is used to distinguish between an empty count field and a count field with a value of 0.
36-
/// </summary>
37-
// ReSharper disable once InconsistentNaming
38-
TDS_DONE_COUNT = 0x0010,
39-
}
40-
41-
public DoneProcStatus Status { get; set; }
11+
public DoneToken.DoneStatus Status { get; set; }
4212
public TranState TransactionState { get; set; }
4313
public int Count { get; set; }
4414

@@ -49,10 +19,10 @@ public void Write(Stream stream, DbEnvironment env)
4919

5020
public void Read(Stream stream, DbEnvironment env, IFormatToken previous)
5121
{
52-
Status = (DoneProcStatus)stream.ReadUShort();
22+
Status = (DoneToken.DoneStatus)stream.ReadUShort();
5323
TransactionState = (TranState)stream.ReadUShort();
5424
Count = stream.ReadInt();
5525
Logger.Instance?.WriteLine($"<- {Status}, {TransactionState}, {Count}");
5626
}
5727
}
58-
}
28+
}

0 commit comments

Comments
 (0)