Skip to content

Commit

Permalink
feat: support half for Sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
ilmalte committed Jul 21, 2023
1 parent 81c2440 commit 6fcd0d7
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,11 @@ protected virtual void InferClrTypes(DbConnection connection, DatabaseTable tabl
column["ClrType"] = typeof(double);
}

if (defaultClrTpe != typeof(Half))
{
column["ClrType"] = typeof(Half);
}

continue;
}

Expand Down
15 changes: 15 additions & 0 deletions src/Microsoft.Data.Sqlite.Core/SqliteDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,21 @@ public override float GetFloat(int ordinal)
? throw new InvalidOperationException(Resources.NoData)
: _record.GetFloat(ordinal);


#if NET6_0_OR_GREATER
/// <summary>
/// Gets the value of the specified column as a <see cref="Half" />.
/// </summary>
/// <param name="ordinal">The zero-based column ordinal.</param>
/// <returns>The value of the column.</returns>
public virtual Half GetHalf(int ordinal)
=> _closed
? throw new InvalidOperationException(Resources.DataReaderClosed(nameof(GetHalf)))
: _record == null
? throw new InvalidOperationException(Resources.NoData)
: _record.GetHalf(ordinal);
#endif

/// <summary>
/// Gets the value of the specified column as a <see cref="Guid" />.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions src/Microsoft.Data.Sqlite.Core/SqliteValueBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ public virtual void Bind()
var value = (double)(float)_value;
BindDouble(value);
}
#if NET6_0_OR_GREATER
else if (type == typeof(Half))
{
var value = (double)(Half)_value;
BindDouble(value);
}
#endif
else if (type == typeof(Guid))
{
var guid = (Guid)_value;
Expand Down
12 changes: 12 additions & 0 deletions src/Microsoft.Data.Sqlite.Core/SqliteValueReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ public virtual double GetDouble(int ordinal)
public virtual float GetFloat(int ordinal)
=> (float)GetDouble(ordinal);

#if NET6_0_OR_GREATER
public virtual Half GetHalf(int ordinal)
=> (Half)GetDouble(ordinal);
#endif

public virtual Guid GetGuid(int ordinal)
{
var sqliteType = GetSqliteType(ordinal);
Expand Down Expand Up @@ -320,6 +325,13 @@ public virtual string GetString(int ordinal)
return (T)(object)GetFloat(ordinal);
}

#if NET6_0_OR_GREATER
if (type == typeof(Half))
{
return (T)(object)GetFloat(ordinal);
}
#endif

if (type == typeof(Guid))
{
return (T)(object)GetGuid(ordinal);
Expand Down
9 changes: 9 additions & 0 deletions test/Microsoft.Data.Sqlite.Tests/SqliteDataReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,15 @@ public void GetFloat_throws_when_closed()
public void GetFloat_throws_when_non_query()
=> X_throws_when_non_query(r => r.GetFloat(0));

#if NET6_0_OR_GREATER
[Fact]
public void GetHalf_throws_when_closed()
=> X_throws_when_closed(r => r.GetHalf(0), nameof(SqliteDataReader.GetHalf));

[Fact]
public void GetHalf_throws_when_non_query()
=> X_throws_when_non_query(r => r.GetHalf(0));
#endif
[Theory]
[InlineData("2.0", 2.0)]
[InlineData("9e999", double.PositiveInfinity)]
Expand Down

0 comments on commit 6fcd0d7

Please sign in to comment.