Skip to content

Commit 9c5b06f

Browse files
committed
Added bindings for table functions
1 parent 9e14d36 commit 9c5b06f

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System.Runtime.InteropServices;
2+
using System;
3+
4+
namespace DuckDB.NET.Native;
5+
6+
public partial class NativeMethods
7+
{
8+
public static class TableFunction
9+
{
10+
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_create_tableFunction")]
11+
public static extern IntPtr DuckDBCreateTableFunction();
12+
13+
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_destroy_tableFunction")]
14+
public static extern void DuckDBDestroyTableFunction(out IntPtr tableFunction);
15+
16+
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_table_function_set_name")]
17+
public static extern void DuckDBTableFunctionSetName(IntPtr tableFunction, SafeUnmanagedMemoryHandle name);
18+
19+
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_table_function_add_parameter")]
20+
public static extern void DuckDBTableFunctionAddParameter(IntPtr tableFunction, DuckDBLogicalType type);
21+
22+
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_table_function_set_extra_info")]
23+
public static extern unsafe void DuckDBTableFunctionSetExtraInfo(IntPtr tableFunction, IntPtr extraInfo, delegate* unmanaged[Cdecl]<IntPtr, void> destroy);
24+
25+
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_table_function_set_bind")]
26+
public static extern unsafe void DuckDBTableFunctionSetBind(IntPtr tableFunction, delegate* unmanaged[Cdecl]<IntPtr, void> bind);
27+
28+
//[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_table_function_set_init")]
29+
//public static extern unsafe void DuckDBTableFunctionSetInit(IntPtr tableFunction, duckdb_tableFunction_init_t init);
30+
31+
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_table_function_set_function")]
32+
public static extern unsafe void DuckDBTableFunctionSetFunction(IntPtr tableFunction, delegate* unmanaged[Cdecl]<IntPtr, IntPtr, IntPtr, void> callback);
33+
34+
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_register_table_function")]
35+
public static extern DuckDBState DuckDBRegisterTableFunction(DuckDBNativeConnection con, IntPtr tableFunction);
36+
37+
#region TableFunctionBind
38+
39+
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_bind_add_result_column")]
40+
public static extern unsafe void DuckDBBindAddResultColumn(IntPtr info, SafeUnmanagedMemoryHandle name, DuckDBLogicalType type);
41+
42+
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_bind_get_parameter_count")]
43+
public static extern unsafe ulong DuckDBBindGetParameterCount(IntPtr info);
44+
45+
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_bind_get_parameter")]
46+
public static extern unsafe DuckDBValue DuckDBBindGetParameter(IntPtr info, ulong index);
47+
48+
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_bind_set_bind_data")]
49+
public static extern unsafe void DuckDBBindSetBindData(IntPtr info, IntPtr bindData, delegate* unmanaged[Cdecl]<IntPtr, void> destroy);
50+
51+
#endregion
52+
53+
#region TableFunction
54+
55+
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_function_get_bind_data")]
56+
public static extern unsafe void* duckdb_function_get_bind_data(IntPtr info);
57+
58+
#endregion
59+
}
60+
}

DuckDB.NET.Bindings/NativeMethods/NativeMethods.Value.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ public static class Value
7070

7171
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_create_blob")]
7272
public static extern DuckDBValue DuckDBCreateBlob([In] byte[] value, long length);
73+
74+
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_get_int32")]
75+
public static extern int DuckDBGetInt32(DuckDBValue value);
7376

7477
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_create_list_value")]
7578
public static extern DuckDBValue DuckDBCreateListValue(DuckDBLogicalType logicalType, IntPtr[] values, long count);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
using DuckDB.NET.Data.Extensions;
5+
using DuckDB.NET.Data.Internal;
6+
using DuckDB.NET.Native;
7+
8+
namespace DuckDB.NET.Data;
9+
10+
partial class DuckDBConnection
11+
{
12+
public unsafe void RegisterTableFunction<T, TResult>(string name)
13+
{
14+
var function = NativeMethods.TableFunction.DuckDBCreateTableFunction();
15+
NativeMethods.TableFunction.DuckDBTableFunctionSetName(function, name.ToUnmanagedString());
16+
17+
using (var logicalType = DuckDBTypeMap.GetLogicalType<T>())
18+
{
19+
NativeMethods.TableFunction.DuckDBTableFunctionAddParameter(function, logicalType);
20+
}
21+
22+
NativeMethods.TableFunction.DuckDBTableFunctionSetBind(function, &Bind);
23+
}
24+
25+
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
26+
public static unsafe void Bind(IntPtr info)
27+
{
28+
var parameters = new object[NativeMethods.TableFunction.DuckDBBindGetParameterCount(info)];
29+
30+
for (var i = 0; i < parameters.Length; i++)
31+
{
32+
using var value = NativeMethods.TableFunction.DuckDBBindGetParameter(info, (ulong)i);
33+
parameters[i] = NativeMethods.Value.DuckDBGetInt32(value);
34+
}
35+
36+
NativeMethods.TableFunction.DuckDBBindSetBindData(info, parameters.ToHandle(), &DestroyExtraInfo);
37+
}
38+
}

0 commit comments

Comments
 (0)