Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions DuckDB.NET.Data/PreparedStatement/PreparedStatement.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Linq;
using DuckDB.NET.Data.Connection;

namespace DuckDB.NET.Data.PreparedStatement;
Expand Down Expand Up @@ -89,10 +88,26 @@ private static void BindParameters(DuckDBPreparedStatement preparedStatement, Du
throw new InvalidOperationException($"Invalid number of parameters. Expected {expectedParameters}, got {parameterCollection.Count}");
}

if (parameterCollection.OfType<DuckDBParameter>().Any(p => !string.IsNullOrEmpty(p.ParameterName)))
// Index-based iteration over the typed collection avoids the per-execution allocations of
// OfType<>().Any(...) and of the boxed List enumerator that `foreach (DuckDBParameter ...)`
// over the non-generic collection would produce. BindParameters runs on every execution.
var count = parameterCollection.Count;

var hasNamedParameters = false;
for (var i = 0; i < count; i++)
{
foreach (DuckDBParameter param in parameterCollection)
if (!string.IsNullOrEmpty(parameterCollection[i].ParameterName))
{
hasNamedParameters = true;
break;
}
}

if (hasNamedParameters)
{
for (var i = 0; i < count; i++)
{
var param = parameterCollection[i];
var state = NativeMethods.PreparedStatements.DuckDBBindParameterIndex(preparedStatement, out var index, param.ParameterName);
if (state.IsSuccess())
{
Expand Down
Loading