Skip to content

Commit

Permalink
Clear RFC table rows before applying values
Browse files Browse the repository at this point in the history
As tables may be re-used between calls of the same SapFunction,
there may still be rows left over from previous calls.
Before applying parameter data to tables, clear the content of the table to avoid duplicate data.
  • Loading branch information
oliverkahrmann-basf committed Aug 4, 2023
1 parent d8c56de commit 1ceba8f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/SapNwRfc/Internal/Fields/EnumerableField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public override void Apply(RfcInterop interop, IntPtr dataHandle)

resultCode.ThrowOnError(errorInfo);

// Clear the table content before adding more - these tables are re-used between calls
interop.DeleteAllRows(tableHandle, out errorInfo);

foreach (TItem row in Value)
{
IntPtr lineHandle = interop.AppendNewRow(tableHandle, out errorInfo);
Expand Down
5 changes: 5 additions & 0 deletions src/SapNwRfc/Internal/Fields/TableField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public override void Apply(RfcInterop interop, IntPtr dataHandle)

resultCode.ThrowOnError(errorInfo);

// Clear the table content before adding more - these tables are re-used between calls
interop.DeleteAllRows(tableHandle, out errorInfo);

resultCode.ThrowOnError(errorInfo);

foreach (TItem row in Value)
{
IntPtr lineHandle = interop.AppendNewRow(tableHandle, out errorInfo);
Expand Down
6 changes: 6 additions & 0 deletions src/SapNwRfc/Internal/Interop/RfcInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,12 @@ public virtual RfcResultCode MoveToFirstRow(IntPtr tableHandle, out RfcErrorInfo
public virtual IntPtr AppendNewRow(IntPtr tableHandle, out RfcErrorInfo errorInfo)
=> RfcAppendNewRow(tableHandle, out errorInfo);

[DllImport(SapNwRfcDllName)]
private static extern RfcResultCode RfcDeleteAllRows(IntPtr tableHandle, out RfcErrorInfo errorInfo);

public virtual RfcResultCode DeleteAllRows(IntPtr tableHandle, out RfcErrorInfo errorInfo)
=> RfcDeleteAllRows(tableHandle, out errorInfo);

#endregion

#region Server
Expand Down
2 changes: 2 additions & 0 deletions tests/SapNwRfc.Tests/Internal/InputMapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ public void Apply_Array_ShouldMapRowsAndValues()
InputMapper.Apply(_interopMock.Object, DataHandle, model);

// Assert
_interopMock.Verify(x => x.DeleteAllRows(tableHandle, out errorInfo));
_interopMock.Verify(x => x.AppendNewRow(tableHandle, out errorInfo), Times.Exactly(numberOfRows));
foreach (ArrayElement element in model.SomeArray)
{
Expand Down Expand Up @@ -363,6 +364,7 @@ public void Apply_Enumerable_ShouldMapRowsAndValues()
InputMapper.Apply(_interopMock.Object, DataHandle, model);

// Assert
_interopMock.Verify(x => x.DeleteAllRows(tableHandle, out errorInfo));
_interopMock.Verify(x => x.AppendNewRow(tableHandle, out errorInfo), Times.Exactly(numberOfRows));
foreach (EnumerableElement element in model.SomeEnumerable)
{
Expand Down

0 comments on commit 1ceba8f

Please sign in to comment.