From e2bbe7d71d0ed9de63addba2703b198178fb6a0b Mon Sep 17 00:00:00 2001 From: Frank Wagner Date: Sat, 24 Apr 2021 21:15:09 +0200 Subject: [PATCH] fix row iterator (#37) * check for null pointer handles * don't move row in iterator if MoveToFirstTableRow fails --- src/YaNco.Core/Internal/Api.cs | 34 ++++++++++++++++------------ src/YaNco.Core/RfcRuntime.cs | 10 +++++++- src/YaNco.Core/TableRowEnumerator.cs | 9 +++++--- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/YaNco.Core/Internal/Api.cs b/src/YaNco.Core/Internal/Api.cs index 4bb2d9d6..1c98991b 100644 --- a/src/YaNco.Core/Internal/Api.cs +++ b/src/YaNco.Core/Internal/Api.cs @@ -6,28 +6,30 @@ namespace Dbosoft.YaNco.Internal { public static class Api { - + public static ConnectionHandle OpenConnection(IDictionary connectionParams, - out RfcErrorInfo errorInfo) + out RfcErrorInfo errorInfo) { var rfcOptions = connectionParams.Select(x => new Interopt.RfcConnectionParameter { Name = x.Key, Value = x.Value }) .ToArray(); - return new ConnectionHandle(Interopt.RfcOpenConnection(rfcOptions, (uint)rfcOptions.Length, out errorInfo)); + var ptr = Interopt.RfcOpenConnection(rfcOptions, (uint)rfcOptions.Length, out errorInfo); + return ptr == IntPtr.Zero ? null : new ConnectionHandle(ptr); } public static FunctionDescriptionHandle GetFunctionDescription(FunctionHandle functionHandle, out RfcErrorInfo errorInfo) { - return new FunctionDescriptionHandle(Interopt.RfcDescribeFunction(functionHandle.Ptr, out errorInfo)); + var ptr = Interopt.RfcDescribeFunction(functionHandle.Ptr, out errorInfo); + return ptr == IntPtr.Zero ? null : new FunctionDescriptionHandle(ptr); } public static FunctionDescriptionHandle GetFunctionDescription(ConnectionHandle connectionHandle, string functionName, out RfcErrorInfo errorInfo) { - return new FunctionDescriptionHandle(Interopt.RfcGetFunctionDesc(connectionHandle.Ptr, functionName, out errorInfo)); - + var ptr = Interopt.RfcGetFunctionDesc(connectionHandle.Ptr, functionName, out errorInfo); + return ptr == IntPtr.Zero ? null : new FunctionDescriptionHandle(ptr); } public static RfcRc GetFunctionName(FunctionDescriptionHandle descriptionHandle, out string functionName, @@ -40,7 +42,8 @@ public static RfcRc GetFunctionName(FunctionDescriptionHandle descriptionHandle, public static TypeDescriptionHandle GetTypeDescription(IDataContainerHandle dataContainer, out RfcErrorInfo errorInfo) { - return new TypeDescriptionHandle(Interopt.RfcDescribeType(dataContainer.Ptr, out errorInfo)); + var ptr = Interopt.RfcDescribeType(dataContainer.Ptr, out errorInfo); + return ptr == IntPtr.Zero ? null : new TypeDescriptionHandle(ptr); } @@ -72,7 +75,8 @@ public static RfcRc GetTypeFieldDescription(TypeDescriptionHandle descriptionHan public static FunctionHandle CreateFunction(FunctionDescriptionHandle descriptionHandle, out RfcErrorInfo errorInfo) { - return new FunctionHandle(Interopt.RfcCreateFunction(descriptionHandle.Ptr, out errorInfo)); + var ptr = Interopt.RfcCreateFunction(descriptionHandle.Ptr, out errorInfo); + return ptr == IntPtr.Zero ? null : new FunctionHandle(ptr); } @@ -116,7 +120,7 @@ public static RfcRc GetStructure(IDataContainerHandle dataContainer, string name out StructureHandle structure, out RfcErrorInfo errorInfo) { var rc = Interopt.RfcGetStructure(dataContainer.Ptr, name, out var structPtr, out errorInfo); - structure = new StructureHandle(structPtr); + structure = structPtr == IntPtr.Zero ? null : new StructureHandle(structPtr); return rc; } @@ -125,15 +129,15 @@ public static RfcRc GetTable(IDataContainerHandle dataContainer, string name, ou out RfcErrorInfo errorInfo) { var rc = Interopt.RfcGetTable(dataContainer.Ptr, name, out var tablePtr, out errorInfo); - table = new TableHandle(tablePtr); + table = tablePtr == IntPtr.Zero ? null : new TableHandle(tablePtr); return rc; } public static TableHandle CloneTable(TableHandle tableHandle, out RfcErrorInfo errorInfo) { - return new TableHandle(Interopt.RfcCloneTable(tableHandle.Ptr, out errorInfo)); - + var ptr = Interopt.RfcCloneTable(tableHandle.Ptr, out errorInfo); + return ptr == IntPtr.Zero ? null : new TableHandle(ptr); } public static void AllowStartOfPrograms(ConnectionHandle connectionHandle, StartProgramDelegate callback, out @@ -190,13 +194,15 @@ public static RfcRc GetTableRowCount(TableHandle table, out int count, out RfcEr public static StructureHandle GetCurrentTableRow(TableHandle table, out RfcErrorInfo errorInfo) { - return new StructureHandle(Interopt.RfcGetCurrentRow(table.Ptr, out errorInfo)); + var ptr = Interopt.RfcGetCurrentRow(table.Ptr, out errorInfo); + return ptr == IntPtr.Zero ? null : new StructureHandle(ptr); } public static StructureHandle AppendTableRow(TableHandle table, out RfcErrorInfo errorInfo) { - return new StructureHandle(Interopt.RfcAppendNewRow(table.Ptr, out errorInfo)); + var ptr = Interopt.RfcAppendNewRow(table.Ptr, out errorInfo); + return ptr == IntPtr.Zero ? null : new StructureHandle(ptr); } diff --git a/src/YaNco.Core/RfcRuntime.cs b/src/YaNco.Core/RfcRuntime.cs index d0e616e6..da186157 100644 --- a/src/YaNco.Core/RfcRuntime.cs +++ b/src/YaNco.Core/RfcRuntime.cs @@ -18,7 +18,15 @@ private Either ResultOrError(TResult result, Rfc { Logger.IfSome(l => { - if(logAsError) + if (errorInfo.Code == RfcRc.RFC_OK) + { + if (logAsError) + l.LogError("received null result from api call."); + else + l.LogDebug("received null result from api call."); + } + + if (logAsError) l.LogError("received error from rfc call", errorInfo); else l.LogDebug("received error from rfc call", errorInfo); diff --git a/src/YaNco.Core/TableRowEnumerator.cs b/src/YaNco.Core/TableRowEnumerator.cs index 0a1506ce..70a41add 100644 --- a/src/YaNco.Core/TableRowEnumerator.cs +++ b/src/YaNco.Core/TableRowEnumerator.cs @@ -53,11 +53,14 @@ public Unit ReadCurrentRow() public void Reset() { - _handle.IfSome(h => _rfcRuntime.MoveToFirstTableRow(h)); _first = true; - ReadCurrentRow(); + + _handle.IfSome(h => + { + _rfcRuntime.MoveToFirstTableRow(h).Map(u => ReadCurrentRow()); + }); } - + public Structure Current { get { return _currentRow.MatchUnsafe(s => s, () => null); }