11using DuckDB . NET . Data . Extensions ;
22using DuckDB . NET . Native ;
33using System ;
4+ using System . Collections ;
45using System . Collections . Generic ;
56using System . Data ;
7+ using System . Linq ;
68using System . Numerics ;
79
810namespace DuckDB . NET . Data . Internal ;
@@ -76,6 +78,8 @@ public static DbType GetDbTypeForValue(object? value)
7678 {
7779 return dbType ;
7880 }
81+
82+ return DbType . Object ;
7983 throw new InvalidOperationException ( $ "Values of type { type . FullName } are not supported.") ;
8084 }
8185
@@ -89,3 +93,36 @@ public static DuckDBLogicalType GetLogicalType<T>()
8993 throw new InvalidOperationException ( $ "Cannot map type { typeof ( T ) . FullName } to DuckDBType.") ;
9094 }
9195}
96+
97+ internal static class ValueConverter
98+ {
99+ public static DuckDBValue ToDuckDBValue ( this object value )
100+ {
101+ return value switch
102+ {
103+ int intValue => NativeMethods . Value . DuckDBCreateInt32 ( intValue ) ,
104+ long longValue => NativeMethods . Value . DuckDBCreateInt64 ( longValue ) ,
105+ ICollection < int > ints => CreateListValue ( DuckDBType . Integer , ints ) ,
106+ _ => throw new InvalidCastException (
107+ $ "Cannot convert value of type { value . GetType ( ) . FullName } to DuckDBValue.")
108+ } ;
109+ }
110+
111+ private static DuckDBValue CreateListValue < T > ( DuckDBType duckDBType , ICollection < T > collection )
112+ {
113+ using var logicalType = NativeMethods . LogicalType . DuckDBCreateLogicalType ( duckDBType ) ;
114+
115+ var values = new IntPtr [ collection . Count ] ;
116+
117+ var index = 0 ;
118+ foreach ( var item in collection )
119+ {
120+ using var duckDBValue = item . ToDuckDBValue ( ) ;
121+ values [ index ] = duckDBValue . DangerousGetHandle ( ) ;
122+ index ++ ;
123+ }
124+
125+ return NativeMethods . Value . DuckDBCreateListValue ( logicalType ,
126+ collection . Select ( i => i . ToDuckDBValue ( ) . DangerousGetHandle ( ) ) . ToArray ( ) , collection . Count ) ;
127+ }
128+ }
0 commit comments