Skip to content
Permalink
Browse files
Add ResultSetValue for ToXXX methods to prevent boxing
  • Loading branch information
Bond-009 committed Jun 5, 2021
1 parent 93a88a3 commit 500dc9760fb3f5aed7a90e560e8eada49893e86d
Showing with 95 additions and 4 deletions.
  1. +88 −0 SQLitePCL.pretty/SQLiteValue.cs
  2. +7 −4 tests/SQLitePCL.pretty.Tests/SQLiteValueTests.cs
@@ -370,6 +370,94 @@ public static Uri ToUri(this ISQLiteValue This)
var text = This.ToString();
return new Uri(text);
}

/// <summary>
/// Returns the SQLiteValue as a <see cref="bool"/>.
/// </summary>
public static bool ToBool(this ResultSetValue This)
=> Convert.ToBoolean(This.ToInt64());

/// <summary>
/// Returns the SQLiteValue as a <see cref="float"/>.
/// </summary>
public static float ToFloat(this ResultSetValue This)
=> (float)This.ToDouble();

/// <summary>
/// Returns the SQLiteValue as a <see cref="TimeSpan"/>.
/// </summary>
public static TimeSpan ToTimeSpan(this ResultSetValue This)
=> new TimeSpan(This.ToInt64());

/// <summary>
/// Returns the SQLiteValue as a <see cref="DateTime"/>.
/// </summary>
public static DateTime ToDateTime(this ResultSetValue This)
=> new DateTime(This.ToInt64());

/// <summary>
/// Returns the SQLiteValue as a <see cref="DateTimeOffset"/>.
/// </summary>
public static DateTimeOffset ToDateTimeOffset(this ResultSetValue This)
=> new DateTimeOffset(This.ToInt64(), TimeSpan.Zero);

/// <summary>
/// Returns the SQLiteValue as a <see cref="uint"/>.
/// </summary>
public static uint ToUInt32(this ResultSetValue This)
=> Convert.ToUInt32(This.ToInt64());

/// <summary>
/// Returns the SQLiteValue as a <see cref="decimal"/>.
/// </summary>
public static decimal ToDecimal(this ResultSetValue This)
=> Convert.ToDecimal(This.ToDouble());

/// <summary>
/// Returns the SQLiteValue as a <see cref="byte"/>.
/// </summary>
public static byte ToByte(this ResultSetValue This)
=> Convert.ToByte(This.ToInt64());

/// <summary>
/// Returns the SQLiteValue as a <see cref="UInt16"/>.
/// </summary>
public static UInt16 ToUInt16(this ResultSetValue This)
=> Convert.ToUInt16(This.ToInt64());

/// <summary>
/// Returns the SQLiteValue as a <see cref="short"/>.
/// </summary>
public static short ToShort(this ResultSetValue This)
=> Convert.ToInt16(This.ToInt64());

/// <summary>
/// Returns the SQLiteValue as a <see cref="sbyte"/>.
/// </summary>
public static sbyte ToSByte(this ResultSetValue This)
=> Convert.ToSByte(This.ToInt64());

/// <summary>
/// Returns the SQLiteValue as a <see cref="Guid"/>.
/// </summary>
public static Guid ToGuid(this ResultSetValue This)
{
if (!Utf8Parser.TryParse(This.ToBlob(), out Guid value, out _))
{
ThrowHelper.ThrowFormatException();
}

return value;
}

/// <summary>
/// Returns the SQLiteValue as a <see cref="Uri"/>.
/// </summary>
public static Uri ToUri(this ResultSetValue This)
{
var text = This.ToString();
return new Uri(text);
}
}

internal readonly struct NativeValue : ISQLiteValue
@@ -121,6 +121,9 @@ public void TestNullValue()
[InlineData(-195489100.8377)]
[InlineData(1.12345678901234567E100)]
[InlineData(-1.12345678901234567E100)]
[InlineData(float.MaxValue)]
[InlineData(float.MinValue)]
[InlineData(float.Epsilon)]
[InlineData(double.MaxValue)]
[InlineData(double.MinValue)]
[InlineData(double.Epsilon)]
@@ -139,9 +142,9 @@ public void TestFloatValue(double value)
var expected = row.Single();
var result = value.ToSQLiteValue();

Assert.Throws<NotSupportedException>(() => { var x = result.Length; });
Assert.Throws<NotSupportedException>(() => { result.ToString(); });
Assert.Throws<NotSupportedException>(() => { result.ToBlob(); });
Assert.Throws<NotSupportedException>(() => result.Length);
Assert.Throws<NotSupportedException>(() => result.ToString());
Assert.Throws<NotSupportedException>(() => result.ToBlob());

Assert.Equal(result.SQLiteType, expected.SQLiteType);
Assert.Equal(result.ToInt64(), expected.ToInt64());
@@ -225,7 +228,7 @@ public void TestZeroBlob(int i)

foreach (var row in db.Query("SELECT x FROM foo;"))
{
compare(test, row.First());
compare(test, row[0]);
}
db.Execute("DROP TABLE foo;");
}

0 comments on commit 500dc97

Please sign in to comment.