Skip to content

Commit 055fd18

Browse files
committed
Support Memory<T> as MySqlParameter.Value.
1 parent 10f2b92 commit 055fd18

File tree

2 files changed

+34
-31
lines changed

2 files changed

+34
-31
lines changed

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -254,28 +254,30 @@ internal void AppendSqlString(ByteBufferWriter writer, StatementPreparerOptions
254254
{
255255
writer.WriteString(ulongValue);
256256
}
257-
else if (Value is byte[] || Value is ReadOnlyMemory<byte>)
257+
else if (Value is byte[] || Value is ReadOnlyMemory<byte> || Value is Memory<byte>)
258258
{
259-
var memoryValue = Value is byte[] byteArrayValue ? byteArrayValue.AsSpan() : ((ReadOnlyMemory<byte>) Value).Span;
259+
var inputSpan = Value is byte[] byteArray ? byteArray.AsSpan() :
260+
Value is Memory<byte> memory ? memory.Span :
261+
((ReadOnlyMemory<byte>) Value).Span;
260262

261263
// determine the number of bytes to be written
262-
var length = memoryValue.Length + s_binaryBytes.Length + 1;
263-
foreach (var by in memoryValue)
264+
var length = inputSpan.Length + s_binaryBytes.Length + 1;
265+
foreach (var by in inputSpan)
264266
{
265267
if (by == 0x27 || by == 0x5C)
266268
length++;
267269
}
268270

269-
var span = writer.GetSpan(length);
270-
s_binaryBytes.CopyTo(span);
271+
var outputSpan = writer.GetSpan(length);
272+
s_binaryBytes.CopyTo(outputSpan);
271273
var index = s_binaryBytes.Length;
272-
foreach (var by in memoryValue)
274+
foreach (var by in inputSpan)
273275
{
274276
if (by == 0x27 || by == 0x5C)
275-
span[index++] = 0x5C;
276-
span[index++] = by;
277+
outputSpan[index++] = 0x5C;
278+
outputSpan[index++] = by;
277279
}
278-
span[index++] = 0x27;
280+
outputSpan[index++] = 0x27;
279281
Debug.Assert(index == length, "index == length");
280282
writer.Advance(index);
281283
}
@@ -462,6 +464,11 @@ internal void AppendBinary(ByteBufferWriter writer, StatementPreparerOptions opt
462464
writer.WriteLengthEncodedInteger(unchecked((ulong) readOnlyMemoryValue.Length));
463465
writer.Write(readOnlyMemoryValue.Span);
464466
}
467+
else if (Value is Memory<byte> memoryValue)
468+
{
469+
writer.WriteLengthEncodedInteger(unchecked((ulong) memoryValue.Length));
470+
writer.Write(memoryValue.Span);
471+
}
465472
else if (Value is float floatValue)
466473
{
467474
writer.Write(BitConverter.GetBytes(floatValue));

tests/SideBySide/InsertTests.cs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Data;
34
using System.Linq;
45
using System.Threading.Tasks;
@@ -393,10 +394,11 @@ value set('one', 'two', 'four', 'eight') null
393394

394395

395396
#if !BASELINE
396-
[Fact]
397-
public void InsertReadOnlyMemory()
397+
[Theory]
398+
[MemberData(nameof(GetBlobs))]
399+
public void InsertBlob(object data, bool prepare)
398400
{
399-
using (var connection = new MySqlConnection(AppConfig.ConnectionString))
401+
using (var connection = new MySqlConnection(AppConfig.ConnectionString + ";IgnorePrepare=false"))
400402
{
401403
connection.Open();
402404
connection.Execute(@"drop table if exists insert_mysql_blob;
@@ -407,32 +409,26 @@ value mediumblob null
407409

408410
using (var cmd = new MySqlCommand("insert into insert_mysql_blob(value) values(@data);", connection))
409411
{
410-
cmd.Parameters.AddWithValue("@data", new ReadOnlyMemory<byte>(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, 1, 6));
412+
cmd.Parameters.AddWithValue("@data", data);
413+
if (prepare)
414+
cmd.Prepare();
411415
cmd.ExecuteNonQuery();
412416
}
413417
Assert.Equal(new byte[] { 1, 2, 3, 4, 5, 6 }, connection.Query<byte[]>(@"select value from insert_mysql_blob;").Single());
414418
}
415419
}
416420

417-
[Fact]
418-
public void InsertReadOnlyMemoryPrepared()
421+
public static IEnumerable<object[]> GetBlobs()
419422
{
420-
using (var connection = new MySqlConnection(AppConfig.ConnectionString + ";IgnorePrepare=false"))
423+
foreach (var blob in new object[]
421424
{
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());
425+
new byte[] { 1, 2, 3, 4, 5, 6 },
426+
new ReadOnlyMemory<byte>(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, 1, 6),
427+
new Memory<byte>(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, 1, 6),
428+
})
429+
{
430+
yield return new[] { blob, false };
431+
yield return new[] { blob, true };
436432
}
437433
}
438434
#endif

0 commit comments

Comments
 (0)