|
| 1 | +using System.Runtime.InteropServices; |
1 | 2 | using Xunit.Sdk; |
2 | 3 |
|
3 | 4 | namespace IntegrationTests; |
@@ -588,6 +589,71 @@ public void BulkCopyDataTableWithMySqlDecimal() |
588 | 589 | } |
589 | 590 | } |
590 | 591 |
|
| 592 | + [SkippableTheory(ServerFeatures.Vector)] |
| 593 | + [InlineData("byte[]")] |
| 594 | + [InlineData("float[]")] |
| 595 | + public void BulkCopyDataTableWithVector(string dataType) |
| 596 | + { |
| 597 | + var dataTable = new DataTable() |
| 598 | + { |
| 599 | + Columns = |
| 600 | + { |
| 601 | + new DataColumn("id", typeof(int)), |
| 602 | + new DataColumn("data", |
| 603 | +#pragma warning disable SA1118 // Parameter should not span multiple lines |
| 604 | + dataType switch |
| 605 | + { |
| 606 | + "byte[]" => typeof(byte[]), |
| 607 | + "float[]" => typeof(float[]), |
| 608 | + _ => throw new ArgumentOutOfRangeException(nameof(dataType)), |
| 609 | + }), |
| 610 | +#pragma warning restore SA1118 // Parameter should not span multiple lines |
| 611 | + }, |
| 612 | + Rows = |
| 613 | + { |
| 614 | + new object[] { 1, GetDataRowValue([0, 0, 0], dataType) }, |
| 615 | + new object[] { 2, GetDataRowValue([1f, 2f, 3f], dataType) }, |
| 616 | + }, |
| 617 | + }; |
| 618 | + |
| 619 | + using var connection = new MySqlConnection(GetLocalConnectionString()); |
| 620 | + connection.Open(); |
| 621 | + using (var cmd = new MySqlCommand(@"drop table if exists bulk_load_data_table; |
| 622 | +create table bulk_load_data_table(a int, b vector(3));", connection)) |
| 623 | + { |
| 624 | + cmd.ExecuteNonQuery(); |
| 625 | + } |
| 626 | + |
| 627 | + var bulkCopy = new MySqlBulkCopy(connection) |
| 628 | + { |
| 629 | + DestinationTableName = "bulk_load_data_table", |
| 630 | + }; |
| 631 | + var result = bulkCopy.WriteToServer(dataTable); |
| 632 | + Assert.Equal(2, result.RowsInserted); |
| 633 | + Assert.Empty(result.Warnings); |
| 634 | + |
| 635 | + using (var cmd = new MySqlCommand(@"select b from bulk_load_data_table order by a;", connection)) |
| 636 | + { |
| 637 | + using var reader = cmd.ExecuteReader(); |
| 638 | + Assert.True(reader.Read()); |
| 639 | + Assert.Equal(new float[3], GetFloatArray(reader, 0)); |
| 640 | + Assert.True(reader.Read()); |
| 641 | + Assert.Equal([1f, 2f, 3f], GetFloatArray(reader, 0)); |
| 642 | + Assert.False(reader.Read()); |
| 643 | + } |
| 644 | + |
| 645 | + static object GetDataRowValue(float[] data, string dataType) => |
| 646 | + dataType == "byte[]" ? MemoryMarshal.Cast<float, byte>(data).ToArray() : data; |
| 647 | + |
| 648 | + static float[] GetFloatArray(MySqlDataReader reader, int ordinal) => |
| 649 | + reader.GetValue(ordinal) switch |
| 650 | + { |
| 651 | + ReadOnlyMemory<float> romf => romf.ToArray(), |
| 652 | + byte[] b => MemoryMarshal.Cast<byte, float>(b).ToArray(), |
| 653 | + { } x => throw new NotSupportedException(x.GetType().Name), |
| 654 | + }; |
| 655 | + } |
| 656 | + |
591 | 657 | #if NET6_0_OR_GREATER |
592 | 658 | [Fact] |
593 | 659 | public void BulkCopyDataTableWithDateOnly() |
|
0 commit comments