Giorgi / DuckDB.NET

Bindings and ADO.NET Provider for DuckDB
https://duckdb.net
MIT License
437 stars 70 forks source link

Unable to debug queries with parameters #229

Closed jhgoodwin closed 3 hours ago

jhgoodwin commented 3 hours ago

When I run this test without debugging, it works fine. When I enable debugging, it gives null reference exceptions trying to read the parameters.

Am I doing something wrong?

using System.Text;
using DuckDB.NET.Data;

namespace MyApp.Data.Tests;
public class Tests
{
    private DuckDBConnection _connection;

    [SetUp]
    public void Setup()
    {
        _connection = new DuckDBConnection(DuckDBConnectionStringBuilder.InMemorySharedConnectionString);
        _connection.Open();
    }

    [TearDown]
    public void TearDown()
    {
        _connection.Close();
    }

    [Test]
    public void ParamsWorkByNameForSelect()
    {
        using var cmd = _connection.CreateCommand();
        cmd.CommandText = "select column_name from information_schema.columns where table_name = $table_name";
        cmd.Parameters.Add(new DuckDBParameter("table_name", "does_not_exist"));
        var results = new StringBuilder();
        var reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            results.Append(reader.GetString(0) + ",");
        }
        Assert.That(results.ToString(), Is.EqualTo(string.Empty));
    }

    [Test]
    public void ParamsWorkByNameForInsert()
    {
        using (var tran = _connection.BeginTransaction())
        {
            using var cmd = _connection.CreateCommand();
            cmd.CommandText = "CREATE TABLE test (id INTEGER, name VARCHAR)";
            cmd.ExecuteNonQuery();
            tran.Commit();
        }

        using (var tran2 = _connection.BeginTransaction())
        {
            using var cmd2 = _connection.CreateCommand();
            cmd2.CommandText = "INSERT INTO test (id, name) VALUES ($id, $name)";
            cmd2.Parameters.Add(new DuckDBParameter("id", 1));
            cmd2.Parameters.Add(new DuckDBParameter("name", "fred"));
            var affectedRows = cmd2.ExecuteNonQuery();
            tran2.Commit();
            Assert.That(affectedRows, Is.EqualTo(1));
        }
    }
}

My csproj for my test project

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net9.0</TargetFramework>
    <LangVersion>latest</LangVersion>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <IsPackable>false</IsPackable>
    <WarningsAsErrors>true</WarningsAsErrors>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="coverlet.collector" Version="6.0.2" />
    <PackageReference Include="Dapper" Version="2.1.44" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
    <PackageReference Include="NUnit" Version="4.2.2" />
    <PackageReference Include="NUnit.Analyzers" Version="4.3.0" />
    <PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
  </ItemGroup>

  <ItemGroup>
    <Using Include="NUnit.Framework" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\MyApp.Data\MyApp.Data.csproj" />
  </ItemGroup>

</Project>

Editor environment: JetBrains Rider 2024.2.7 Build #RD-242.23726.100, built on October 22, 2024 macOS: 14.6.1 (23G93) (Sonoma)

Run on host without any docker or other runtimes.

Giorgi commented 3 hours ago

Check known issues, there is a workaround in the linked issue: https://github.com/Giorgi/DuckDB.NET?tab=readme-ov-file#known-issues