Skip to content

Commit 10f2b92

Browse files
committed
Support ReadOnyMemory<byte> in prepared commands.
1 parent 2b02a68 commit 10f2b92

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/MySqlConnector/MySql.Data.MySqlClient/MySqlParameter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,11 @@ internal void AppendBinary(ByteBufferWriter writer, StatementPreparerOptions opt
457457
writer.WriteLengthEncodedInteger(unchecked((ulong) byteArrayValue.Length));
458458
writer.Write(byteArrayValue);
459459
}
460+
else if (Value is ReadOnlyMemory<byte> readOnlyMemoryValue)
461+
{
462+
writer.WriteLengthEncodedInteger(unchecked((ulong) readOnlyMemoryValue.Length));
463+
writer.Write(readOnlyMemoryValue.Span);
464+
}
460465
else if (Value is float floatValue)
461466
{
462467
writer.Write(BitConverter.GetBytes(floatValue));

tests/SideBySide/InsertTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,28 @@ value mediumblob null
413413
Assert.Equal(new byte[] { 1, 2, 3, 4, 5, 6 }, connection.Query<byte[]>(@"select value from insert_mysql_blob;").Single());
414414
}
415415
}
416+
417+
[Fact]
418+
public void InsertReadOnlyMemoryPrepared()
419+
{
420+
using (var connection = new MySqlConnection(AppConfig.ConnectionString + ";IgnorePrepare=false"))
421+
{
422+
connection.Open();
423+
connection.Execute(@"drop table if exists insert_mysql_blob;
424+
create table insert_mysql_blob(
425+
rowid integer not null primary key auto_increment,
426+
value mediumblob null
427+
);");
428+
429+
using (var cmd = new MySqlCommand("insert into insert_mysql_blob(value) values(@data);", connection))
430+
{
431+
cmd.Parameters.AddWithValue("@data", new ReadOnlyMemory<byte>(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, 1, 6));
432+
cmd.Prepare();
433+
cmd.ExecuteNonQuery();
434+
}
435+
Assert.Equal(new byte[] { 1, 2, 3, 4, 5, 6 }, connection.Query<byte[]>(@"select value from insert_mysql_blob;").Single());
436+
}
437+
}
416438
#endif
417439

418440
readonly DatabaseFixture m_database;

0 commit comments

Comments
 (0)