Skip to content

Commit

Permalink
Merge 9b924f6 into c6cc733
Browse files Browse the repository at this point in the history
  • Loading branch information
Cricle committed Jun 14, 2024
2 parents c6cc733 + 9b924f6 commit 39906ca
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
24 changes: 22 additions & 2 deletions DuckDB.NET.Bindings/NativeMethods/NativeMethods.Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,30 @@ public partial class NativeMethods
public static class Startup
{
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_open")]
public static extern DuckDBState DuckDBOpen(string? path, out DuckDBDatabase database);
public static extern DuckDBState DuckDBOpenNative(SafeUnmanagedMemoryHandle path, out DuckDBDatabase database);

public static DuckDBState DuckDBOpen(string? path, out DuckDBDatabase database)
{
if (path == null)
{
return DuckDBOpenNative(new SafeUnmanagedMemoryHandle(IntPtr.Zero), out database);
}
using var handle = path.ToUnmanagedString();
return DuckDBOpenNative(handle, out database);
}

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_open_ext")]
public static extern DuckDBState DuckDBOpen(string? path, out DuckDBDatabase database, DuckDBConfig config, out IntPtr error);
public static extern DuckDBState DuckDBOpenNative(SafeUnmanagedMemoryHandle path, out DuckDBDatabase database, DuckDBConfig config, out IntPtr error);

public static DuckDBState DuckDBOpen(string? path, out DuckDBDatabase database, DuckDBConfig config, out IntPtr error)
{
if (path == null)
{
return DuckDBOpenNative(new SafeUnmanagedMemoryHandle(IntPtr.Zero), out database, config, out error);
}
using var handle = path.ToUnmanagedString();
return DuckDBOpenNative(handle, out database, config, out error);
}

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_close")]
public static extern void DuckDBClose(out IntPtr database);
Expand Down
2 changes: 1 addition & 1 deletion DuckDB.NET.Samples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private static void AdoNetSamples()

private static void LowLevelBindingsSample()
{
var result = Startup.DuckDBOpen(null, out var database);
var result = Startup.DuckDBOpenNative(null, out var database);

using (database)
{
Expand Down
34 changes: 34 additions & 0 deletions DuckDB.NET.Test/OpenTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using DuckDB.NET.Data;
using System.IO;
using Xunit;

namespace DuckDB.NET.Test;

public class OpenTests
{
[Theory]
[InlineData("中文", "中文.db")]
[InlineData("english", "english.db")]
[InlineData("한국어", "한국어.db")]
public void OpenWithFile(string path,string fileName)
{
var dir = Path.Combine(path);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
var file = Path.Combine(dir, fileName);
using (var conn=new DuckDBConnection($"Data Source={file}"))
{
conn.Open();
}
try
{
File.Delete(file);
Directory.Delete(dir);
}
catch (System.Exception)
{
}
}
}

0 comments on commit 39906ca

Please sign in to comment.