Skip to content

Commit

Permalink
Destroy appender if create fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Giorgi committed Dec 20, 2022
1 parent 4124df8 commit dba806d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
9 changes: 8 additions & 1 deletion DuckDB.NET.Data/DuckDBConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,14 @@ public DuckDBAppender CreateAppender(string table)
{
if (NativeMethods.Appender.DuckDBAppenderCreate(NativeConnection, null, table, out var nativeAppender) == DuckDBState.DuckDBError)
{
DuckDBAppender.ThrowLastError(nativeAppender);
try
{
DuckDBAppender.ThrowLastError(nativeAppender);
}
finally
{
nativeAppender.Close();
}
}

return new DuckDBAppender(nativeAppender);
Expand Down
26 changes: 26 additions & 0 deletions DuckDB.NET.Test/ManagedAppenderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,31 @@ public void WrongTypesThrowException()
.EndRow();
}).Should().Throw<DuckDBException>();
}

[Fact]
public void ClosedAdapterThrowException()
{
using var connection = new DuckDBConnection("DataSource=:memory:");
connection.Open();

using (var duckDbCommand = connection.CreateCommand())
{
var table = "CREATE TABLE managedAppenderClosedAdapterTest(a BOOLEAN, c Date, b TINYINT);";
duckDbCommand.CommandText = table;
duckDbCommand.ExecuteNonQuery();
}

connection.Invoking(dbConnection =>
{
using var appender = dbConnection.CreateAppender("managedAppenderClosedAdapterTest");
appender.Close();
var row = appender.CreateRow();
row
.AppendValue(false)
.AppendValue((byte)1)
.AppendValue((short?)1)
.EndRow();
}).Should().Throw<InvalidOperationException>();
}
}
}

0 comments on commit dba806d

Please sign in to comment.