Skip to content

Commit

Permalink
Extract interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Giorgi committed Dec 15, 2024
1 parent 834b60b commit 0a28952
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 32 deletions.
2 changes: 1 addition & 1 deletion DuckDB.NET.Data/DuckDBAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal DuckDBAppender(Native.DuckDBAppender appender, string qualifiedTableNam
dataChunk = NativeMethods.DataChunks.DuckDBCreateDataChunk(logicalTypeHandles, columnCount);
}

public DuckDBAppenderRow CreateRow()
public IDuckDBAppenderRow CreateRow()
{
if (closed)
{
Expand Down
88 changes: 59 additions & 29 deletions DuckDB.NET.Data/DuckDBAppenderRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace DuckDB.NET.Data;

public class DuckDBAppenderRow
public class DuckDBAppenderRow : IDuckDBAppenderRow
{
private int columnIndex = 0;
private readonly string qualifiedTableName;
Expand All @@ -28,77 +28,77 @@ public void EndRow()
}
}

public DuckDBAppenderRow AppendNullValue() => AppendValueInternal<int?>(null); //Doesn't matter what type T we pass to Append when passing null.
public IDuckDBAppenderRow AppendNullValue() => AppendValueInternal<int?>(null); //Doesn't matter what type T we pass to Append when passing null.

public DuckDBAppenderRow AppendValue(bool? value) => AppendValueInternal(value);
public IDuckDBAppenderRow AppendValue(bool? value) => AppendValueInternal(value);

public DuckDBAppenderRow AppendValue(byte[]? value) => AppendSpan(value);
public IDuckDBAppenderRow AppendValue(byte[]? value) => AppendSpan(value);

public DuckDBAppenderRow AppendValue(Span<byte> value) => AppendSpan(value);
public IDuckDBAppenderRow AppendValue(Span<byte> value) => AppendSpan(value);

public DuckDBAppenderRow AppendValue(string? value) => AppendValueInternal(value);
public IDuckDBAppenderRow AppendValue(string? value) => AppendValueInternal(value);

public DuckDBAppenderRow AppendValue(decimal? value) => AppendValueInternal(value);
public IDuckDBAppenderRow AppendValue(decimal? value) => AppendValueInternal(value);

public DuckDBAppenderRow AppendValue(Guid? value) => AppendValueInternal(value);
public IDuckDBAppenderRow AppendValue(Guid? value) => AppendValueInternal(value);

public DuckDBAppenderRow AppendValue(BigInteger? value) => AppendValueInternal(value);
public IDuckDBAppenderRow AppendValue(BigInteger? value) => AppendValueInternal(value);

#region Append Signed Int

public DuckDBAppenderRow AppendValue(sbyte? value) => AppendValueInternal(value);
public IDuckDBAppenderRow AppendValue(sbyte? value) => AppendValueInternal(value);

public DuckDBAppenderRow AppendValue(short? value) => AppendValueInternal(value);
public IDuckDBAppenderRow AppendValue(short? value) => AppendValueInternal(value);

public DuckDBAppenderRow AppendValue(int? value) => AppendValueInternal(value);
public IDuckDBAppenderRow AppendValue(int? value) => AppendValueInternal(value);

public DuckDBAppenderRow AppendValue(long? value) => AppendValueInternal(value);
public IDuckDBAppenderRow AppendValue(long? value) => AppendValueInternal(value);

#endregion

#region Append Unsigned Int

public DuckDBAppenderRow AppendValue(byte? value) => AppendValueInternal(value);
public IDuckDBAppenderRow AppendValue(byte? value) => AppendValueInternal(value);

public DuckDBAppenderRow AppendValue(ushort? value) => AppendValueInternal(value);
public IDuckDBAppenderRow AppendValue(ushort? value) => AppendValueInternal(value);

public DuckDBAppenderRow AppendValue(uint? value) => AppendValueInternal(value);
public IDuckDBAppenderRow AppendValue(uint? value) => AppendValueInternal(value);

public DuckDBAppenderRow AppendValue(ulong? value) => AppendValueInternal(value);
public IDuckDBAppenderRow AppendValue(ulong? value) => AppendValueInternal(value);

#endregion

#region Append Enum

public DuckDBAppenderRow AppendValue<TEnum>(TEnum? value) where TEnum : Enum => AppendValueInternal(value);
public IDuckDBAppenderRow AppendValue<TEnum>(TEnum? value) where TEnum : Enum => AppendValueInternal(value);

#endregion

#region Append Float

public DuckDBAppenderRow AppendValue(float? value) => AppendValueInternal(value);
public IDuckDBAppenderRow AppendValue(float? value) => AppendValueInternal(value);

public DuckDBAppenderRow AppendValue(double? value) => AppendValueInternal(value);
public IDuckDBAppenderRow AppendValue(double? value) => AppendValueInternal(value);

#endregion

#region Append Temporal
#if NET6_0_OR_GREATER
public DuckDBAppenderRow AppendValue(DateOnly? value) => AppendValueInternal(value);
public IDuckDBAppenderRow AppendValue(DateOnly? value) => AppendValueInternal(value);

public DuckDBAppenderRow AppendValue(TimeOnly? value) => AppendValueInternal(value);
public IDuckDBAppenderRow AppendValue(TimeOnly? value) => AppendValueInternal(value);
#endif

public DuckDBAppenderRow AppendValue(DuckDBDateOnly? value) => AppendValueInternal(value);
public IDuckDBAppenderRow AppendValue(DuckDBDateOnly? value) => AppendValueInternal(value);

public DuckDBAppenderRow AppendValue(DuckDBTimeOnly? value) => AppendValueInternal(value);
public IDuckDBAppenderRow AppendValue(DuckDBTimeOnly? value) => AppendValueInternal(value);


public DuckDBAppenderRow AppendValue(DateTime? value) => AppendValueInternal(value);
public IDuckDBAppenderRow AppendValue(DateTime? value) => AppendValueInternal(value);

public DuckDBAppenderRow AppendValue(DateTimeOffset? value) => AppendValueInternal(value);
public IDuckDBAppenderRow AppendValue(DateTimeOffset? value) => AppendValueInternal(value);

public DuckDBAppenderRow AppendValue(TimeSpan? value)
public IDuckDBAppenderRow AppendValue(TimeSpan? value)
{
return AppendValueInternal(value);
}
Expand All @@ -107,7 +107,7 @@ public DuckDBAppenderRow AppendValue(TimeSpan? value)

#region Composite Types

public DuckDBAppenderRow AppendValue<T>(IEnumerable<T>? value) => AppendValueInternal(value);
public IDuckDBAppenderRow AppendValue<T>(IEnumerable<T>? value) => AppendValueInternal(value);

#endregion

Expand All @@ -122,7 +122,7 @@ private DuckDBAppenderRow AppendValueInternal<T>(T? value)
return this;
}

private unsafe DuckDBAppenderRow AppendSpan(Span<byte> val)
private unsafe IDuckDBAppenderRow AppendSpan(Span<byte> val)
{
if (val == null)
{
Expand All @@ -148,3 +148,33 @@ private void CheckColumnAccess()
}
}
}

public interface IDuckDBAppenderRow
{
void EndRow();
IDuckDBAppenderRow AppendNullValue();
IDuckDBAppenderRow AppendValue(bool? value);
IDuckDBAppenderRow AppendValue(byte[]? value);
IDuckDBAppenderRow AppendValue(Span<byte> value);
IDuckDBAppenderRow AppendValue(string? value);
IDuckDBAppenderRow AppendValue(decimal? value);
IDuckDBAppenderRow AppendValue(Guid? value);
IDuckDBAppenderRow AppendValue(BigInteger? value);
IDuckDBAppenderRow AppendValue(sbyte? value);
IDuckDBAppenderRow AppendValue(short? value);
IDuckDBAppenderRow AppendValue(int? value);
IDuckDBAppenderRow AppendValue(long? value);
IDuckDBAppenderRow AppendValue(byte? value);
IDuckDBAppenderRow AppendValue(ushort? value);
IDuckDBAppenderRow AppendValue(uint? value);
IDuckDBAppenderRow AppendValue(ulong? value);
IDuckDBAppenderRow AppendValue<TEnum>(TEnum? value) where TEnum : Enum;
IDuckDBAppenderRow AppendValue(float? value);
IDuckDBAppenderRow AppendValue(double? value);
IDuckDBAppenderRow AppendValue(DuckDBDateOnly? value);
IDuckDBAppenderRow AppendValue(DuckDBTimeOnly? value);
IDuckDBAppenderRow AppendValue(DateTime? value);
IDuckDBAppenderRow AppendValue(DateTimeOffset? value);
IDuckDBAppenderRow AppendValue(TimeSpan? value);
IDuckDBAppenderRow AppendValue<T>(IEnumerable<T>? value);
}
4 changes: 2 additions & 2 deletions DuckDB.NET.Data/PreparedStatement/ClrToDuckDBConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static DuckDBValue ToDuckDBValue(this object? item, DuckDBLogicalType log
(DuckDBType.Blob, byte[] value) => NativeMethods.Value.DuckDBCreateBlob(value, value.Length),
(DuckDBType.List, ICollection value) => CreateCollectionValue(logicalType, value, true),
(DuckDBType.Array, ICollection value) => CreateCollectionValue(logicalType, value, false),
_ => throw new InvalidOperationException($"Cannot bind parameter type {item.GetType().FullName} to column of type {duckDBType}")
_ => throw new InvalidOperationException($"Cannot bind parameter type {item!.GetType().FullName} to column of type {duckDBType}")
};

T ConvertTo<T>()
Expand All @@ -69,7 +69,7 @@ T ConvertTo<T>()
}
catch (Exception)
{
throw new ArgumentOutOfRangeException($"Cannot bind parameter '{item}' type {item.GetType().FullName} to column of type {duckDBType}");
throw new ArgumentOutOfRangeException($"Cannot bind parameter '{item}' type {item!.GetType().FullName} to column of type {duckDBType}");
}
}
}
Expand Down

0 comments on commit 0a28952

Please sign in to comment.