From 466c564a5b425255e296c52387be6a97ba0e4172 Mon Sep 17 00:00:00 2001 From: Giorgi Dalakishvili Date: Fri, 29 Nov 2024 16:12:11 +0400 Subject: [PATCH] Get collection item type only once --- DuckDB.NET.Data/Internal/ClrToDuckDBConverter.cs | 8 ++++---- DuckDB.NET.Data/Internal/PreparedStatement.cs | 4 +++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/DuckDB.NET.Data/Internal/ClrToDuckDBConverter.cs b/DuckDB.NET.Data/Internal/ClrToDuckDBConverter.cs index 9b78e9e..a625a75 100644 --- a/DuckDB.NET.Data/Internal/ClrToDuckDBConverter.cs +++ b/DuckDB.NET.Data/Internal/ClrToDuckDBConverter.cs @@ -9,15 +9,13 @@ namespace DuckDB.NET.Data.Internal; internal static class ClrToDuckDBConverter { - public static DuckDBValue ToDuckDBValue(this object? item, DuckDBLogicalType logicalType) + public static DuckDBValue ToDuckDBValue(this object? item, DuckDBLogicalType logicalType, DuckDBType duckDBType) { if (item.IsNull()) { return NativeMethods.Value.DuckDBCreateNullValue(); } - var duckDBType = NativeMethods.LogicalType.DuckDBGetTypeId(logicalType); - return (duckDBType, item) switch { (DuckDBType.Boolean, bool value) => NativeMethods.Value.DuckDBCreateBool(value), @@ -76,12 +74,14 @@ private static DuckDBValue CreateCollectionValue(DuckDBLogicalType logicalType, using var collectionItemType = isList ? NativeMethods.LogicalType.DuckDBListTypeChildType(logicalType) : NativeMethods.LogicalType.DuckDBArrayTypeChildType(logicalType); + var duckDBType = NativeMethods.LogicalType.DuckDBGetTypeId(collectionItemType); + var values = new DuckDBValue[collection.Count]; var index = 0; foreach (var item in collection) { - var duckDBValue = item.ToDuckDBValue(collectionItemType); + var duckDBValue = item.ToDuckDBValue(collectionItemType, duckDBType); values[index] = duckDBValue; index++; } diff --git a/DuckDB.NET.Data/Internal/PreparedStatement.cs b/DuckDB.NET.Data/Internal/PreparedStatement.cs index f4bd59f..7b52b57 100644 --- a/DuckDB.NET.Data/Internal/PreparedStatement.cs +++ b/DuckDB.NET.Data/Internal/PreparedStatement.cs @@ -111,7 +111,9 @@ private static void BindParameters(DuckDBPreparedStatement preparedStatement, Du private static void BindParameter(DuckDBPreparedStatement preparedStatement, long index, DuckDBParameter parameter) { using var parameterLogicalType = NativeMethods.PreparedStatements.DuckDBParamLogicalType(preparedStatement, index); - using var duckDBValue = parameter.Value.ToDuckDBValue(parameterLogicalType); + var duckDBType = NativeMethods.LogicalType.DuckDBGetTypeId(parameterLogicalType); + + using var duckDBValue = parameter.Value.ToDuckDBValue(parameterLogicalType, duckDBType); var result = NativeMethods.PreparedStatements.DuckDBBindValue(preparedStatement, index, duckDBValue);