Skip to content

Commit 754cc11

Browse files
committed
Add support for new types to MySqlBulkCopy. Fixes #1143
1 parent 779c137 commit 754cc11

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/MySqlConnector/MySqlBulkCopy.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System.Buffers;
22
using System.Buffers.Text;
3+
using System.Globalization;
4+
using System.Numerics;
35
using System.Text;
46
using MySqlConnector.Core;
57
using MySqlConnector.Logging;
@@ -550,6 +552,16 @@ static bool WriteValue(MySqlConnection connection, object value, ref int inputIn
550552
// store as UTC as it will be read as such when deserialized from a timespan column
551553
return WriteString("{0:yyyy'-'MM'-'dd' 'HH':'mm':'ss'.'ffffff}".FormatInvariant(dateTimeOffsetValue.UtcDateTime), ref utf8Encoder, output, out bytesWritten);
552554
}
555+
#if NET6_0_OR_GREATER
556+
else if (value is DateOnly dateOnlyValue)
557+
{
558+
return WriteString("timestamp('{0:yyyy'-'MM'-'dd}')".FormatInvariant(dateOnlyValue), ref utf8Encoder, output, out bytesWritten);
559+
}
560+
else if (value is TimeOnly timeOnlyValue)
561+
{
562+
return WriteString("time '{0:HH':'mm':'ss'.'ffffff}'".FormatInvariant(timeOnlyValue), ref utf8Encoder, output, out bytesWritten);
563+
}
564+
#endif
553565
else if (value is TimeSpan ts)
554566
{
555567
var isNegative = false;
@@ -594,6 +606,14 @@ static bool WriteValue(MySqlConnection connection, object value, ref int inputIn
594606
{
595607
return WriteString("{0:d}".FormatInvariant(value), ref utf8Encoder, output, out bytesWritten);
596608
}
609+
else if (value is BigInteger bigInteger)
610+
{
611+
return WriteString(bigInteger.ToString(CultureInfo.InvariantCulture), ref utf8Encoder, output, out bytesWritten);
612+
}
613+
else if (value is MySqlDecimal mySqlDecimal)
614+
{
615+
return WriteString(mySqlDecimal.ToString(), ref utf8Encoder, output, out bytesWritten);
616+
}
597617
else
598618
{
599619
throw new NotSupportedException("Type {0} not currently supported. Value: {1}".FormatInvariant(value.GetType().Name, value));

tests/SideBySide/BulkLoaderSync.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,50 @@ public void BulkCopyNullDataTable()
515515
Assert.Throws<ArgumentNullException>(() => bulkCopy.WriteToServer(default(DataTable)));
516516
}
517517

518+
#if !BASELINE
519+
[Fact]
520+
public void BulkCopyDataTableWithMySqlDecimal()
521+
{
522+
var dataTable = new DataTable()
523+
{
524+
Columns =
525+
{
526+
new DataColumn("id", typeof(int)),
527+
new DataColumn("data", typeof(MySqlDecimal)),
528+
},
529+
Rows =
530+
{
531+
new object[] { 1, new MySqlDecimal("1.234") },
532+
new object[] { 2, new MySqlDecimal("2.345") },
533+
},
534+
};
535+
536+
using var connection = new MySqlConnection(GetLocalConnectionString());
537+
connection.Open();
538+
using (var cmd = new MySqlCommand(@"drop table if exists bulk_load_data_table;
539+
create table bulk_load_data_table(a int, b decimal(20, 10));", connection))
540+
{
541+
cmd.ExecuteNonQuery();
542+
}
543+
544+
var bulkCopy = new MySqlBulkCopy(connection)
545+
{
546+
DestinationTableName = "bulk_load_data_table",
547+
};
548+
var result = bulkCopy.WriteToServer(dataTable);
549+
Assert.Equal(2, result.RowsInserted);
550+
Assert.Empty(result.Warnings);
551+
552+
using (var cmd = new MySqlCommand(@"select sum(b) from bulk_load_data_table;", connection))
553+
{
554+
using var reader = cmd.ExecuteReader();
555+
Assert.True(reader.Read());
556+
Assert.Equal(3.579m, reader.GetValue(0));
557+
Assert.Equal("3.579", reader.GetMySqlDecimal(0).ToString().TrimEnd('0'));
558+
}
559+
}
560+
#endif
561+
518562
[Fact]
519563
public void BulkCopyDataTableWithLongBlob()
520564
{

0 commit comments

Comments
 (0)