Skip to content

Commit

Permalink
Microsoft.Data.Sqlite: Enable GetFieldValue<Stream>() and TextReader
Browse files Browse the repository at this point in the history
Part of #19991
  • Loading branch information
bricelam committed Dec 7, 2020
1 parent 9b90d08 commit 0e02269
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Microsoft.Data.Sqlite.Core/SqliteDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -551,9 +551,11 @@ public override Stream GetStream(int ordinal)
/// <param name="ordinal">The zero-based column ordinal.</param>
/// <returns>The returned object.</returns>
public override TextReader GetTextReader(int ordinal)
=> IsDBNull(ordinal)
? (TextReader)new StringReader(string.Empty)
: new StreamReader(GetStream(ordinal), Encoding.UTF8);
=> _closed
? throw new InvalidOperationException(Resources.DataReaderClosed(nameof(GetTextReader)))
: _record == null
? throw new InvalidOperationException(Resources.NoData)
: _record.GetTextReader(ordinal);

/// <summary>
/// Gets the value of the specified column.
Expand Down
20 changes: 20 additions & 0 deletions src/Microsoft.Data.Sqlite.Core/SqliteDataRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ protected override long GetInt64Core(int ordinal)
protected override string GetStringCore(int ordinal)
=> sqlite3_column_text(Handle, ordinal).utf8_to_string();

public override T GetFieldValue<T>(int ordinal)
{
if (typeof(T) == typeof(Stream))
{
return (T)(object)GetStream(ordinal);
}

if (typeof(T) == typeof(TextReader))
{
return (T)(object)GetTextReader(ordinal);
}

return base.GetFieldValue<T>(ordinal);
}

protected override byte[] GetBlobCore(int ordinal)
=> sqlite3_column_blob(Handle, ordinal).ToArray();

Expand Down Expand Up @@ -299,6 +314,11 @@ public virtual Stream GetStream(int ordinal)
return new SqliteBlob(_connection, blobTableName, blobColumnName, rowid, readOnly: true);
}

public virtual TextReader GetTextReader(int ordinal)
=> IsDBNull(ordinal)
? new StringReader(string.Empty)
: new StreamReader(GetStream(ordinal), Encoding.UTF8);

public bool Read()
{
if (!_stepped)
Expand Down
39 changes: 39 additions & 0 deletions test/Microsoft.Data.Sqlite.Tests/SqliteDataReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,45 @@ public void GetFieldValue_of_Nullable_works()
"SELECT 1;",
(int?)1);

[Fact]
public void GetFieldValue_of_Stream_works()
{
using (var connection = new SqliteConnection("Data Source=:memory:"))
{
connection.Open();

using (var reader = connection.ExecuteReader("SELECT x'7E57';"))
{
var hasData = reader.Read();
Assert.True(hasData);

var stream = reader.GetFieldValue<Stream>(0);
Assert.Equal(0x7E, stream.ReadByte());
Assert.Equal(0x57, stream.ReadByte());
}
}
}

[Fact]
public void GetFieldValue_of_TextReader_works()
{
using (var connection = new SqliteConnection("Data Source=:memory:"))
{
connection.Open();

using (var reader = connection.ExecuteReader("SELECT 'test';"))
{
var hasData = reader.Read();
Assert.True(hasData);

using (var textReader = reader.GetFieldValue<TextReader>(0))
{
Assert.Equal("test", textReader.ReadToEnd());
}
}
}
}

[Fact]
public void GetFieldValue_of_TimeSpan_works()
=> GetFieldValue_works(
Expand Down

0 comments on commit 0e02269

Please sign in to comment.