Skip to content

Commit

Permalink
Migrate to Data Chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
Giorgi committed Sep 14, 2023
1 parent 05f211e commit ef3896d
Show file tree
Hide file tree
Showing 28 changed files with 952 additions and 573 deletions.
26 changes: 24 additions & 2 deletions DuckDB.NET.Bindings/DuckDBNativeObjects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public enum DuckDBType
}

[StructLayout(LayoutKind.Sequential)]
public class DuckDBResult : IDisposable
public struct DuckDBResult : IDisposable
{
[Obsolete]
private long ColumnCount;
Expand All @@ -76,7 +76,7 @@ public class DuckDBResult : IDisposable

public void Dispose()
{
NativeMethods.Query.DuckDBDestroyResult(this);
NativeMethods.Query.DuckDBDestroyResult(ref this);
}
}

Expand Down Expand Up @@ -232,4 +232,26 @@ private static DuckDBInterval FromTimeSpan(TimeSpan timeSpan)
, Convert.ToUInt64(timeSpan.Ticks / 10 - new TimeSpan(timeSpan.Days, 0, 0, 0).Ticks / 10)
);
}

[StructLayout(LayoutKind.Explicit)]
public struct DuckDBString
{
[FieldOffset(0)] public DuckDBStringPointer Pointer;
[FieldOffset(0)] public DuckDBStringInlined Inlined;
}

[StructLayout(LayoutKind.Explicit)]
public struct DuckDBStringPointer
{
[FieldOffset(0)] public uint length;
[FieldOffset(4)] public IntPtr prefix;
[FieldOffset(8)] public IntPtr ptr;
}

[StructLayout(LayoutKind.Explicit)]
public struct DuckDBStringInlined
{
[FieldOffset(0)] public uint length;
[FieldOffset(4)] public IntPtr inlined;
}
}
31 changes: 27 additions & 4 deletions DuckDB.NET.Bindings/DuckDBWrapperObjects.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;

using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using Microsoft.Win32.SafeHandles;

namespace DuckDB.NET
{
Expand Down Expand Up @@ -83,4 +80,30 @@ protected override bool ReleaseHandle()
return true;
}
}

public class DuckDBLogicalType : SafeHandleZeroOrMinusOneIsInvalid
{
public DuckDBLogicalType() : base(true)
{
}

protected override bool ReleaseHandle()
{
NativeMethods.LogicalType.DuckDBDestroyLogicalType(out handle);
return true;
}
}

public class DuckDBDataChunk : SafeHandleZeroOrMinusOneIsInvalid
{
public DuckDBDataChunk() : base(true)
{
}

protected override bool ReleaseHandle()
{
NativeMethods.DataChunks.DuckDBDestroyDataChunk(out handle);
return true;
}
}
}
393 changes: 0 additions & 393 deletions DuckDB.NET.Bindings/NativeMethods.cs

This file was deleted.

8 changes: 8 additions & 0 deletions DuckDB.NET.Bindings/NativeMethods/NativeMethods.All.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace DuckDB.NET;

public static partial class NativeMethods
{
private const string DuckDbLibrary = "duckdb";

//Grouped according to https://duckdb.org/docs/archive/0.8.1/api/c/api
}
136 changes: 136 additions & 0 deletions DuckDB.NET.Bindings/NativeMethods/NativeMethods.Appender.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
using System;
using System.Runtime.InteropServices;

namespace DuckDB.NET;

public partial class NativeMethods
{
public static class Appender
{
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_appender_create")]
public static extern DuckDBState DuckDBAppenderCreate(DuckDBNativeConnection connection, string schema, string table, out DuckDBAppender appender);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_appender_error")]
public static extern IntPtr DuckDBAppenderError(DuckDBAppender appender);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_appender_flush")]
public static extern DuckDBState DuckDBAppenderFlush(DuckDBAppender appender);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_appender_end_row")]
public static extern DuckDBState DuckDBAppenderEndRow(DuckDBAppender appender);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_appender_close")]
public static extern DuckDBState DuckDBAppenderClose(DuckDBAppender appender);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_appender_destroy")]
public static extern DuckDBState DuckDBDestroyAppender(out IntPtr appender);

#if NET5_0_OR_GREATER
[SuppressGCTransition]
#endif
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_append_bool")]
public static extern DuckDBState DuckDBAppendBool(DuckDBAppender appender, bool val);

#if NET5_0_OR_GREATER
[SuppressGCTransition]
#endif
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_append_int8")]
public static extern DuckDBState DuckDBAppendInt8(DuckDBAppender appender, sbyte val);

#if NET5_0_OR_GREATER
[SuppressGCTransition]
#endif
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_append_int16")]
public static extern DuckDBState DuckDBAppendInt16(DuckDBAppender appender, short val);

#if NET5_0_OR_GREATER
[SuppressGCTransition]
#endif
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_append_int32")]
public static extern DuckDBState DuckDBAppendInt32(DuckDBAppender appender, int val);

#if NET5_0_OR_GREATER
[SuppressGCTransition]
#endif
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_append_int64")]
public static extern DuckDBState DuckDBAppendInt64(DuckDBAppender appender, long val);

#if NET5_0_OR_GREATER
[SuppressGCTransition]
#endif
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_append_hugeint")]
public static extern DuckDBState DuckDBAppendHugeInt(DuckDBAppender appender, DuckDBHugeInt val);

#if NET5_0_OR_GREATER
[SuppressGCTransition]
#endif
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_append_uint8")]
public static extern DuckDBState DuckDBAppendUInt8(DuckDBAppender appender, byte val);

#if NET5_0_OR_GREATER
[SuppressGCTransition]
#endif
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_append_uint16")]
public static extern DuckDBState DuckDBAppendUInt16(DuckDBAppender appender, ushort val);

#if NET5_0_OR_GREATER
[SuppressGCTransition]
#endif
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_append_uint32")]
public static extern DuckDBState DuckDBAppendUInt32(DuckDBAppender appender, uint val);

#if NET5_0_OR_GREATER
[SuppressGCTransition]
#endif
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_append_uint64")]
public static extern DuckDBState DuckDBAppendUInt64(DuckDBAppender appender, ulong val);

#if NET5_0_OR_GREATER
[SuppressGCTransition]
#endif
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_append_float")]
public static extern DuckDBState DuckDBAppendFloat(DuckDBAppender appender, float val);

#if NET5_0_OR_GREATER
[SuppressGCTransition]
#endif
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_append_double")]
public static extern DuckDBState DuckDBAppendDouble(DuckDBAppender appender, double val);

#if NET5_0_OR_GREATER
[SuppressGCTransition]
#endif
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_append_date")]
public static extern DuckDBState DuckDBAppendDate(DuckDBAppender appender, DuckDBDate val);

#if NET5_0_OR_GREATER
[SuppressGCTransition]
#endif
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_append_time")]
public static extern DuckDBState DuckDBAppendTime(DuckDBAppender appender, DuckDBTime val);

#if NET5_0_OR_GREATER
[SuppressGCTransition]
#endif
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_append_timestamp")]
public static extern DuckDBState DuckDBAppendTimestamp(DuckDBAppender appender, DuckDBTimestampStruct val);

#if NET5_0_OR_GREATER
[SuppressGCTransition]
#endif
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_append_interval")]
public static extern DuckDBState DuckDBAppendInterval(DuckDBAppender appender, DuckDBInterval val);

#if NET5_0_OR_GREATER
[SuppressGCTransition]
#endif
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_append_varchar")]
public static extern DuckDBState DuckDBAppendVarchar(DuckDBAppender appender, SafeUnmanagedMemoryHandle val);

#if NET5_0_OR_GREATER
[SuppressGCTransition]
#endif
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_append_null")]
public static extern DuckDBState DuckDBAppendNull(DuckDBAppender appender);
}
}
25 changes: 25 additions & 0 deletions DuckDB.NET.Bindings/NativeMethods/NativeMethods.Configure.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Runtime.InteropServices;

namespace DuckDB.NET;

public partial class NativeMethods
{
public static class Configure
{
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_create_config")]
public static extern DuckDBState DuckDBCreateConfig(out DuckDBConfig config);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_config_count")]
public static extern int DuckDBConfigCount();

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_get_config_flag")]
public static extern DuckDBState DuckDBGetConfigFlag(int index, out IntPtr name, out IntPtr description);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_set_config")]
public static extern DuckDBState DuckDBSetConfig(DuckDBConfig config, string name, string option);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_destroy_config")]
public static extern void DuckDBDestroyConfig(out IntPtr config);
}
}
40 changes: 40 additions & 0 deletions DuckDB.NET.Bindings/NativeMethods/NativeMethods.DataChunks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Runtime.InteropServices;

namespace DuckDB.NET;

public partial class NativeMethods
{
public static class DataChunks
{
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_data_chunk_get_column_count")]
public static extern long DuckDBDataChunkGetColumnCount(IntPtr chunk);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_data_chunk_get_vector")]
public static extern IntPtr DuckDBDataChunkGetVector(DuckDBDataChunk chunk, long columnIndex);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_data_chunk_get_size")]
public static extern long DuckDBDataChunkGetSize(DuckDBDataChunk chunk);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_vector_get_column_type")]
public static extern IntPtr DuckDBVectorGetColumnType(IntPtr vector);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_vector_get_data")]
public static extern IntPtr DuckDBVectorGetData(IntPtr vector);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_vector_get_validity")]
public static extern IntPtr DuckDBVectorGetValidity(IntPtr vector);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_list_vector_get_child")]
public static extern long DuckDBListVectorGetChild(IntPtr vector);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_list_vector_get_size")]
public static extern long DuckDBListVectorGetSize(IntPtr vector);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_struct_vector_get_child")]
public static extern long DuckDBStructVectorGetChild(IntPtr vector, long index);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_destroy_data_chunk")]
public static extern void DuckDBDestroyDataChunk(out IntPtr chunk);
}
}
27 changes: 27 additions & 0 deletions DuckDB.NET.Bindings/NativeMethods/NativeMethods.DateTime.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Runtime.InteropServices;

namespace DuckDB.NET;

public partial class NativeMethods
{
public static class DateTime
{
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_from_date")]
public static extern DuckDBDateOnly DuckDBFromDate(DuckDBDate date);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_to_date")]
public static extern DuckDBDate DuckDBToDate(DuckDBDateOnly dateStruct);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_from_time")]
public static extern DuckDBTimeOnly DuckDBFromTime(DuckDBTime date);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_to_time")]
public static extern DuckDBTime DuckDBToTime(DuckDBTimeOnly dateStruct);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_from_timestamp")]
public static extern DuckDBTimestamp DuckDBFromTimestamp(DuckDBTimestampStruct date);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_to_timestamp")]
public static extern DuckDBTimestampStruct DuckDBToTimestamp(DuckDBTimestamp dateStruct);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Runtime.InteropServices;

namespace DuckDB.NET;

public partial class NativeMethods
{
public static class ExtractStatements
{
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_extract_statements")]
public static extern int DuckDBExtractStatements(DuckDBNativeConnection connection, string query, out DuckDBExtractedStatements extractedStatements);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_prepare_extracted_statement")]
public static extern DuckDBState DuckDBPrepareExtractedStatement(DuckDBNativeConnection connection, DuckDBExtractedStatements extractedStatements, long index, out DuckDBPreparedStatement preparedStatement);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_extract_statements_error")]
public static extern IntPtr DuckDBExtractStatementsError(DuckDBExtractedStatements extractedStatements);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_destroy_extracted")]
public static extern void DuckDBDestroyExtracted(out IntPtr extractedStatements);
}
}
16 changes: 16 additions & 0 deletions DuckDB.NET.Bindings/NativeMethods/NativeMethods.Helpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Runtime.InteropServices;

namespace DuckDB.NET;

public partial class NativeMethods
{
public static class Helpers
{
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_free")]
public static extern void DuckDBFree(IntPtr ptr);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_decimal_to_double")]
public static extern double DuckDBDecimalToDouble(DuckDBDecimal val);
}
}
22 changes: 22 additions & 0 deletions DuckDB.NET.Bindings/NativeMethods/NativeMethods.LogicalType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Runtime.InteropServices;

namespace DuckDB.NET;

public partial class NativeMethods
{
public static class LogicalType
{
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_decimal_width")]
public static extern byte DuckDBDecimalWidth(DuckDBLogicalType type);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_decimal_scale")]
public static extern byte DuckDBDecimalScale(DuckDBLogicalType type);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_decimal_internal_type")]
public static extern DuckDBType DuckDBDecimalInternalType(DuckDBLogicalType type);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_destroy_logical_type")]
public static extern void DuckDBDestroyLogicalType(out IntPtr type);
}
}
Loading

0 comments on commit ef3896d

Please sign in to comment.