|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Runtime.CompilerServices; |
| 4 | +using DuckDB.NET.Native; |
| 5 | + |
| 6 | +namespace DuckDB.NET.Data.Internal.Writer; |
| 7 | + |
| 8 | +internal sealed unsafe class EnumVectorDataWriter : VectorDataWriterBase |
| 9 | +{ |
| 10 | + private readonly DuckDBType enumType; |
| 11 | + |
| 12 | + private readonly Dictionary<string, long> enumValueIndexDictionary; |
| 13 | + |
| 14 | + public EnumVectorDataWriter(IntPtr vector, void* vectorData, DuckDBLogicalType logicalType, DuckDBType columnType) : base(vector, vectorData, columnType) |
| 15 | + { |
| 16 | + enumType = NativeMethods.LogicalType.DuckDBEnumInternalType(logicalType); |
| 17 | + uint size = NativeMethods.LogicalType.DuckDBEnumDictionarySize(logicalType); |
| 18 | + enumValueIndexDictionary = []; |
| 19 | + for (long index = 0; index < size; index++) |
| 20 | + { |
| 21 | + string enumValue = NativeMethods.LogicalType.DuckDBEnumDictionaryValue(logicalType, index).ToManagedString(); |
| 22 | + enumValueIndexDictionary.Add(enumValue, index); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + internal override bool AppendString(string value, int rowIndex) |
| 27 | + { |
| 28 | + switch (enumType) |
| 29 | + { |
| 30 | + case DuckDBType.UnsignedTinyInt: |
| 31 | + { |
| 32 | + if (enumValueIndexDictionary.TryGetValue(value, out long enumValueIndex) && |
| 33 | + enumValueIndex >= byte.MinValue && enumValueIndex <= byte.MaxValue) |
| 34 | + { |
| 35 | + return AppendValueInternal((byte)enumValueIndex, rowIndex); |
| 36 | + } |
| 37 | + |
| 38 | + return false; |
| 39 | + } |
| 40 | + case DuckDBType.UnsignedSmallInt: |
| 41 | + { |
| 42 | + if (enumValueIndexDictionary.TryGetValue(value, out long enumValueIndex) && |
| 43 | + enumValueIndex >= ushort.MinValue && enumValueIndex <= ushort.MaxValue) |
| 44 | + { |
| 45 | + return AppendValueInternal((ushort)enumValueIndex, rowIndex); |
| 46 | + } |
| 47 | + |
| 48 | + return false; |
| 49 | + } |
| 50 | + case DuckDBType.UnsignedInteger: |
| 51 | + { |
| 52 | + if (enumValueIndexDictionary.TryGetValue(value, out long enumValueIndex) && |
| 53 | + enumValueIndex >= uint.MinValue && enumValueIndex <= uint.MaxValue) |
| 54 | + { |
| 55 | + return AppendValueInternal((uint)enumValueIndex, rowIndex); |
| 56 | + } |
| 57 | + |
| 58 | + return false; |
| 59 | + } |
| 60 | + default: |
| 61 | + return false; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + internal override bool AppendEnum<TEnum>(TEnum value, int rowIndex) |
| 66 | + { |
| 67 | + switch (enumType) |
| 68 | + { |
| 69 | + case DuckDBType.UnsignedTinyInt: |
| 70 | + { |
| 71 | + long enumValueIndex = Convert.ToInt64(value); |
| 72 | + if (enumValueIndex >= byte.MinValue && enumValueIndex <= byte.MaxValue) |
| 73 | + { |
| 74 | + return AppendValueInternal((byte)enumValueIndex, rowIndex); |
| 75 | + } |
| 76 | + |
| 77 | + return false; |
| 78 | + } |
| 79 | + case DuckDBType.UnsignedSmallInt: |
| 80 | + { |
| 81 | + long enumValueIndex = Convert.ToInt64(value); |
| 82 | + if (enumValueIndex >= ushort.MinValue && enumValueIndex <= ushort.MaxValue) |
| 83 | + { |
| 84 | + return AppendValueInternal((ushort)enumValueIndex, rowIndex); |
| 85 | + } |
| 86 | + |
| 87 | + return false; |
| 88 | + } |
| 89 | + case DuckDBType.UnsignedInteger: |
| 90 | + { |
| 91 | + long enumValueIndex = Convert.ToInt64(value); |
| 92 | + if (enumValueIndex >= uint.MinValue && enumValueIndex <= uint.MaxValue) |
| 93 | + { |
| 94 | + return AppendValueInternal((uint)enumValueIndex, rowIndex); |
| 95 | + } |
| 96 | + |
| 97 | + return false; |
| 98 | + } |
| 99 | + default: |
| 100 | + return false; |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments