From 82a0c46de7c38e17f97a5923c63ea35030210c19 Mon Sep 17 00:00:00 2001 From: cocowalla <800977+cocowalla@users.noreply.github.com> Date: Fri, 14 Jan 2022 20:58:44 +0000 Subject: [PATCH] fix: #27138, stream with 64-bit PK (#27187) * fix: #27138, stream with 64-bit PK * Clean up PR #27187 Co-authored-by: Brice Lambson --- .../SqliteDataRecord.cs | 2 +- .../SqliteDataReaderTest.cs | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.Data.Sqlite.Core/SqliteDataRecord.cs b/src/Microsoft.Data.Sqlite.Core/SqliteDataRecord.cs index 936237497e5..816d0db0474 100644 --- a/src/Microsoft.Data.Sqlite.Core/SqliteDataRecord.cs +++ b/src/Microsoft.Data.Sqlite.Core/SqliteDataRecord.cs @@ -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); } diff --git a/test/Microsoft.Data.Sqlite.Tests/SqliteDataReaderTest.cs b/test/Microsoft.Data.Sqlite.Tests/SqliteDataReaderTest.cs index f98863e7094..62a43f59737 100644 --- a/test/Microsoft.Data.Sqlite.Tests/SqliteDataReaderTest.cs +++ b/test/Microsoft.Data.Sqlite.Tests/SqliteDataReaderTest.cs @@ -456,6 +456,34 @@ public void GetStream_Blob_works(string createTableCmd, string selectCmd) } } + [Fact] + public void GetStream_Blob_works_when_long_pk() + { + 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 (2147483648, X'01020304');"); + + var selectCommand = connection.CreateCommand(); + selectCommand.CommandText = $"SELECT Id, Data FROM DataTable WHERE Id = 2147483648"; + using (var reader = selectCommand.ExecuteReader()) + { + Assert.True(reader.Read()); + using (var sourceStream = reader.GetStream(1)) + { + Assert.IsType(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() {