Skip to content

Commit

Permalink
fix: dotnet#27138, stream with 64-bit PK
Browse files Browse the repository at this point in the history
  • Loading branch information
cocowalla authored and bricelam committed Jan 14, 2022
1 parent 2dbdf1f commit 4a76535
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Microsoft.Data.Sqlite.Core/SqliteDataRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public virtual Stream GetStream(int ordinal)
}

var blobColumnName = sqlite3_column_origin_name(Handle, ordinal).utf8_to_string();
var rowid = GetInt32(_rowidOrdinal.Value);
var rowid = GetInt64(_rowidOrdinal.Value);

return new SqliteBlob(_connection, blobDatabaseName, blobTableName, blobColumnName, rowid, readOnly: true);
}
Expand Down
30 changes: 30 additions & 0 deletions test/Microsoft.Data.Sqlite.Tests/SqliteDataReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,36 @@ public void GetStream_Blob_works(string createTableCmd, string selectCmd)
}
}

[Fact]
public void GetStream_Blob_works_when_long_pk()
{
long pk = 9223372036854775800;

using (var connection = new SqliteConnection("Data Source=:memory:"))
{
connection.Open();

connection.ExecuteNonQuery(
"CREATE TABLE DataTable (Id INTEGER PRIMARY KEY, Data BLOB);" +
$"INSERT INTO DataTable VALUES ({pk}, X'01020304');");

var selectCommand = connection.CreateCommand();
selectCommand.CommandText = $"SELECT Id, Data FROM DataTable WHERE Id = {pk}";
using (var reader = selectCommand.ExecuteReader())
{
Assert.True(reader.Read());
using (var sourceStream = reader.GetStream(1))
{
Assert.IsType<SqliteBlob>(sourceStream);
var buffer = new byte[4];
var bytesRead = sourceStream.Read(buffer, 0, 4);
Assert.Equal(4, bytesRead);
Assert.Equal(new byte[] { 0x01, 0x02, 0x03, 0x04 }, buffer);
}
}
}
}

[Fact]
public void GetStream_works_when_composite_pk()
{
Expand Down

0 comments on commit 4a76535

Please sign in to comment.