Skip to content

Commit

Permalink
Make the DuckDBConnection support unicode file path (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cricle authored Jun 14, 2024
1 parent c6cc733 commit 87187a2
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 4 deletions.
6 changes: 6 additions & 0 deletions DuckDB.NET.Bindings/NativeMethods/NativeMethods.Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ public partial class NativeMethods
//https://duckdb.org/docs/api/c/api#openconnect
public static class Startup
{
[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_open")]
public static extern DuckDBState DuckDBOpen(SafeUnmanagedMemoryHandle path, out DuckDBDatabase database);

[DllImport(DuckDbLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "duckdb_open")]
public static extern DuckDBState DuckDBOpen(string? path, out DuckDBDatabase database);

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

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

Expand Down
4 changes: 3 additions & 1 deletion DuckDB.NET.Data/Internal/ConnectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ internal ConnectionReference GetConnectionReference(DuckDBConnectionString conne
}
}

var resultOpen = NativeMethods.Startup.DuckDBOpen(path, out var db, config, out var error);
using var pathUnmanaged = path.ToUnmanagedString();

var resultOpen = NativeMethods.Startup.DuckDBOpen(pathUnmanaged, out var db, config, out var error);

if (!resultOpen.IsSuccess())
{
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.DuckDBOpen(new SafeUnmanagedMemoryHandle(IntPtr.Zero), out var database);

using (database)
{
Expand Down
2 changes: 1 addition & 1 deletion DuckDB.NET.Test/AppenderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class DuckDBAppenderTests
[Fact]
public void AppenderTests()
{
var result = NativeMethods.Startup.DuckDBOpen(null, out var database);
var result = NativeMethods.Startup.DuckDBOpen((string)null, out var database);
result.Should().Be(DuckDBState.Success);

result = NativeMethods.Startup.DuckDBConnect(database, out var connection);
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)
{
}
}
}
2 changes: 1 addition & 1 deletion DuckDB.NET.Test/QueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class QueryTests(DuckDBDatabaseFixture db) : DuckDBTestBase(db)
[Fact]
public void QueryTest()
{
var result = NativeMethods.Startup.DuckDBOpen(null, out var database);
var result = NativeMethods.Startup.DuckDBOpen((string)null, out var database);
result.Should().Be(DuckDBState.Success);

result = NativeMethods.Startup.DuckDBConnect(database, out var connection);
Expand Down

0 comments on commit 87187a2

Please sign in to comment.