Skip to content

Commit

Permalink
fix row iterator (#37)
Browse files Browse the repository at this point in the history
* check for null pointer handles

* don't move row in iterator if MoveToFirstTableRow fails
  • Loading branch information
fw2568 authored Apr 24, 2021
1 parent 444cc42 commit e2bbe7d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
34 changes: 20 additions & 14 deletions src/YaNco.Core/Internal/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,30 @@ namespace Dbosoft.YaNco.Internal
{
public static class Api
{

public static ConnectionHandle OpenConnection(IDictionary<string, string> 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,
Expand All @@ -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);

}

Expand Down Expand Up @@ -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);

}

Expand Down Expand Up @@ -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;

}
Expand All @@ -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
Expand Down Expand Up @@ -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);

}

Expand Down
10 changes: 9 additions & 1 deletion src/YaNco.Core/RfcRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ private Either<RfcErrorInfo, TResult> ResultOrError<TResult>(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);
Expand Down
9 changes: 6 additions & 3 deletions src/YaNco.Core/TableRowEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
Expand Down

0 comments on commit e2bbe7d

Please sign in to comment.