Skip to content

Commit 0b1155e

Browse files
committed
Minor fixups
1 parent 019f7f9 commit 0b1155e

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

DuckDB.NET.Data/DuckDBDataReader.cs

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ public class DuckDBDataReader : DbDataReader
3333

3434
internal DuckDBDataReader(DuckDBCommand command, IEnumerable<PreparedStatement.PreparedStatement> statements, CommandBehavior behavior)
3535
{
36-
command.DataReader = this;
37-
3836
this.command = command;
3937
this.behavior = behavior;
4038
statementEnumerator = statements.GetEnumerator();
4139

4240
InitNextReader();
41+
42+
// Do not modify the command's state if an exception was thrown in InitNextReader().
43+
command.DataReader = this;
4344
}
4445

4546
private bool InitNextReader()
@@ -49,20 +50,28 @@ private bool InitNextReader()
4950
currentResult?.Dispose();
5051
currentResult = null; // Prevent double disposal.
5152

52-
var current = statementEnumerator.Current.Execute();
53-
currentResult = current;
54-
55-
if (NativeMethods.Query.DuckDBResultReturnType(current) == DuckDBResultType.QueryResult)
53+
try
5654
{
57-
currentChunkIndex = 0;
55+
var current = statementEnumerator.Current.Execute();
56+
currentResult = current;
57+
58+
if (NativeMethods.Query.DuckDBResultReturnType(current) == DuckDBResultType.QueryResult)
59+
{
60+
currentChunkIndex = 0;
5861

59-
columnMapping = [];
60-
fieldCount = (int)NativeMethods.Query.DuckDBColumnCount(ref current);
61-
streamingResult = NativeMethods.Types.DuckDBResultIsStreaming(current) > 0;
62+
columnMapping = [];
63+
fieldCount = (int)NativeMethods.Query.DuckDBColumnCount(ref current);
64+
streamingResult = NativeMethods.Types.DuckDBResultIsStreaming(current) > 0;
6265

63-
hasRows = InitChunkData();
66+
hasRows = InitChunkData();
6467

65-
return true;
68+
return true;
69+
}
70+
}
71+
catch
72+
{
73+
Dispose();
74+
throw;
6675
}
6776
}
6877

@@ -349,6 +358,8 @@ public override void Close()
349358
{
350359
if (closed) return;
351360

361+
command.DataReader = null;
362+
352363
foreach (var reader in vectorReaders)
353364
{
354365
reader.Dispose();
@@ -359,15 +370,26 @@ public override void Close()
359370

360371
currentChunk?.Dispose();
361372

362-
if (behavior == CommandBehavior.CloseConnection)
373+
try
363374
{
364-
command.CloseConnection();
375+
// Try to consume the enumerator to ensure that all statements are prepared.
376+
while (statementEnumerator.MoveNext())
377+
{
378+
}
379+
}
380+
catch
381+
{
382+
// Dispose() must not throw exceptions.
365383
}
366384

367-
closed = true;
368385
statementEnumerator.Dispose();
369386

370-
command.DataReader = null;
387+
closed = true;
388+
389+
if (behavior == CommandBehavior.CloseConnection)
390+
{
391+
command.CloseConnection();
392+
}
371393
}
372394

373395
private void CheckRowRead()

DuckDB.NET.Data/PreparedStatement/PreparedStatement.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using DuckDB.NET.Native;
22
using System;
3-
using System.Collections.Generic;
43
using System.Linq;
54

65
namespace DuckDB.NET.Data.PreparedStatement;

0 commit comments

Comments
 (0)