Skip to content

Commit

Permalink
Merge pull request #104 from Giorgi/Extract-Statements-API
Browse files Browse the repository at this point in the history
Extract statements api
  • Loading branch information
Giorgi authored Apr 24, 2023
2 parents 26cd536 + a4f46b7 commit 64d7515
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 86 deletions.
62 changes: 51 additions & 11 deletions DuckDB.NET.Data/DuckDBCommand.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;

namespace DuckDB.NET.Data
{
Expand All @@ -11,7 +13,7 @@ public class DuckDbCommand : DbCommand
private readonly DuckDBParameterCollection parameters = new();

private string commandText;
private PreparedStatement preparedStatement;
private List<PreparedStatement> preparedStatements;

protected override DbTransaction DbTransaction { get; set; }
protected override DbParameterCollection DbParameterCollection => parameters;
Expand All @@ -29,8 +31,8 @@ public override string CommandText
set
{
commandText = value;
preparedStatement?.Dispose();
preparedStatement = null;
ReleaseStatements();
preparedStatements = null;
}
}

Expand Down Expand Up @@ -65,8 +67,14 @@ public override int ExecuteNonQuery()

PrepareIfNeeded();

using var queryResult = preparedStatement.Execute(parameters);
return (int)NativeMethods.Query.DuckDBRowsChanged(queryResult);
var result = 0;
foreach (var statement in preparedStatements)
{
using var queryResult = statement.Execute(parameters);
result += (int)NativeMethods.Query.DuckDBRowsChanged(queryResult);
}

return result;
}

public override object ExecuteScalar()
Expand All @@ -79,12 +87,12 @@ public override object ExecuteScalar()

public new DuckDBDataReader ExecuteReader()
{
return (DuckDBDataReader) base.ExecuteReader();
return (DuckDBDataReader)base.ExecuteReader();
}

public new DuckDBDataReader ExecuteReader(CommandBehavior behavior)
{
return (DuckDBDataReader) base.ExecuteReader(behavior);
return (DuckDBDataReader)base.ExecuteReader(behavior);
}

protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
Expand All @@ -93,8 +101,29 @@ protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)

PrepareIfNeeded();

var queryResult = preparedStatement.Execute(parameters);
var reader = new DuckDBDataReader(this, queryResult, behavior);
var results = new List<DuckDBResult>();

foreach (var statement in preparedStatements)
{
try
{
var result = statement.Execute(parameters);
results.Add(result);
}
catch (DuckDBException ex)
{
var capture = ExceptionDispatchInfo.Capture(ex);

foreach (var result in results)
{
result.Dispose();
}

capture.Throw();
}
}

var reader = new DuckDBDataReader(this, results, behavior);

return reader;
}
Expand All @@ -105,12 +134,23 @@ protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)

protected override void Dispose(bool disposing)
{
preparedStatement?.Dispose();
ReleaseStatements();
}

private void ReleaseStatements()
{
if (preparedStatements != null)
{
foreach (var statement in preparedStatements)
{
statement.Dispose();
}
}
}

private void PrepareIfNeeded()
{
preparedStatement ??= PreparedStatement.Prepare(connection.NativeConnection, CommandText);
preparedStatements ??= PreparedStatement.PrepareMultiple(connection.NativeConnection, CommandText);
}

internal void CloseConnection()
Expand Down
Loading

0 comments on commit 64d7515

Please sign in to comment.