Skip to content

Commit a48cd3f

Browse files
committed
Support ReadOnlyMemory<byte> as MySqlParameter.Value. Fixes #624
1 parent 49788c1 commit a48cd3f

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,13 @@ internal void AppendSqlString(ByteBufferWriter writer, StatementPreparerOptions
254254
{
255255
writer.WriteString(ulongValue);
256256
}
257-
else if (Value is byte[] byteArrayValue)
257+
else if (Value is byte[] || Value is ReadOnlyMemory<byte>)
258258
{
259+
var memoryValue = Value is byte[] byteArrayValue ? byteArrayValue.AsSpan() : ((ReadOnlyMemory<byte>) Value).Span;
260+
259261
// determine the number of bytes to be written
260-
var length = byteArrayValue.Length + s_binaryBytes.Length + 1;
261-
foreach (var by in byteArrayValue)
262+
var length = memoryValue.Length + s_binaryBytes.Length + 1;
263+
foreach (var by in memoryValue)
262264
{
263265
if (by == 0x27 || by == 0x5C)
264266
length++;
@@ -267,7 +269,7 @@ internal void AppendSqlString(ByteBufferWriter writer, StatementPreparerOptions
267269
var span = writer.GetSpan(length);
268270
s_binaryBytes.CopyTo(span);
269271
var index = s_binaryBytes.Length;
270-
foreach (var by in byteArrayValue)
272+
foreach (var by in memoryValue)
271273
{
272274
if (by == 0x27 || by == 0x5C)
273275
span[index++] = 0x5C;

tests/SideBySide/InsertTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,30 @@ value set('one', 'two', 'four', 'eight') null
391391
Assert.Equal(new[] { "one", "one,two", "one,four", "one,two,four" }, m_database.Connection.Query<string>(@"select value from insert_mysql_set where find_in_set('one', value) order by rowid"));
392392
}
393393

394+
395+
#if !BASELINE
396+
[Fact]
397+
public void InsertReadOnlyMemory()
398+
{
399+
using (var connection = new MySqlConnection(AppConfig.ConnectionString))
400+
{
401+
connection.Open();
402+
connection.Execute(@"drop table if exists insert_mysql_blob;
403+
create table insert_mysql_blob(
404+
rowid integer not null primary key auto_increment,
405+
value mediumblob null
406+
);");
407+
408+
using (var cmd = new MySqlCommand("insert into insert_mysql_blob(value) values(@data);", connection))
409+
{
410+
cmd.Parameters.AddWithValue("@data", new ReadOnlyMemory<byte>(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, 1, 6));
411+
cmd.ExecuteNonQuery();
412+
}
413+
Assert.Equal(new byte[] { 1, 2, 3, 4, 5, 6 }, connection.Query<byte[]>(@"select value from insert_mysql_blob;").Single());
414+
}
415+
}
416+
#endif
417+
394418
readonly DatabaseFixture m_database;
395419
}
396420
}

0 commit comments

Comments
 (0)