Skip to content

Commit

Permalink
Refactor GetXXX methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Giorgi committed Sep 19, 2023
1 parent 3d8400f commit 07169a6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
3 changes: 2 additions & 1 deletion DuckDB.NET.Bindings/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static string ToManagedString(this IntPtr unmanagedString, bool freeWhenC
{
string result;
#if NET6_0_OR_GREATER
result = length.HasValue? Marshal.PtrToStringUTF8(unmanagedString, length.Value) : Marshal.PtrToStringUTF8(unmanagedString);
result = length.HasValue ? Marshal.PtrToStringUTF8(unmanagedString, length.Value) : Marshal.PtrToStringUTF8(unmanagedString);
#else
if (unmanagedString == IntPtr.Zero)
{
Expand Down Expand Up @@ -88,6 +88,7 @@ internal static int GetMicrosecond(this TimeSpan timeSpan)
var ticks = timeSpan.Ticks - GetTicks(timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);
return (int)(ticks / 10);
}

#if NET6_0_OR_GREATER
internal static int GetMicrosecond(this TimeOnly timeOnly)
{
Expand Down
20 changes: 6 additions & 14 deletions DuckDB.NET.Data/DuckDBDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
using System.Data.Common;
using System.IO;
using System.Numerics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;

namespace DuckDB.NET.Data;
Expand Down Expand Up @@ -126,9 +124,7 @@ public override string GetDataTypeName(int ordinal)
public override unsafe DateTime GetDateTime(int ordinal)
{
var data = (DuckDBTimestampStruct*)vectors[ordinal] + rowsReadFromCurrentChunk - 1;
var timestampStruct = *data;

return timestampStruct.ToDateTime();
return data->ToDateTime();
}

private unsafe DuckDBDateOnly GetDateOnly(int ordinal)
Expand Down Expand Up @@ -266,7 +262,7 @@ private unsafe ulong GetUInt64(int ordinal)
private unsafe BigInteger GetBigInteger(int ordinal)
{
var data = (DuckDBHugeInt*)vectors[ordinal] + rowsReadFromCurrentChunk - 1;
return (*data).ToBigInteger();
return data->ToBigInteger();
}

public override string GetName(int ordinal)
Expand All @@ -292,13 +288,12 @@ public override int GetOrdinal(string name)
public override unsafe string GetString(int ordinal)
{
var data = (DuckDBString*)vectors[ordinal] + rowsReadFromCurrentChunk - 1;
var stringData = *data;

var length = *(int*)data;

var pointer = length <= InlineStringMaxLength
? stringData.value.inlined.inlined
: stringData.value.pointer.ptr;
? data->value.inlined.inlined
: data->value.pointer.ptr;

return new string(pointer, 0, length, Encoding.UTF8);
}
Expand Down Expand Up @@ -355,19 +350,16 @@ public override int GetValues(object[] values)
public override unsafe Stream GetStream(int ordinal)
{
var data = (DuckDBString*)vectors[ordinal] + rowsReadFromCurrentChunk - 1;
var stringData = *data;

var length = *(int*)data;

if (length <= InlineStringMaxLength)
{
var value = new string(stringData.value.inlined.inlined, 0, length, Encoding.UTF8);
var value = new string(data->value.inlined.inlined, 0, length, Encoding.UTF8);
return new MemoryStream(Encoding.UTF8.GetBytes(value), false);
}

return new UnmanagedMemoryStream((byte*)stringData.value.pointer.ptr, length, length, FileAccess.Read);

// return new DuckDBStream(stringData.value.pointer.ptr, length);
return new UnmanagedMemoryStream((byte*)data->value.pointer.ptr, length, length, FileAccess.Read);
}

public override unsafe bool IsDBNull(int ordinal)
Expand Down

0 comments on commit 07169a6

Please sign in to comment.