Skip to content

Commit 683ccf2

Browse files
committed
Fix BulkCopy for DateOnly and TimeOnly. Fixes #1146
1 parent dcbd32a commit 683ccf2

File tree

2 files changed

+87
-3
lines changed

2 files changed

+87
-3
lines changed

src/MySqlConnector/MySqlBulkCopy.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -555,11 +555,11 @@ static bool WriteValue(MySqlConnection connection, object value, ref int inputIn
555555
#if NET6_0_OR_GREATER
556556
else if (value is DateOnly dateOnlyValue)
557557
{
558-
return WriteString("timestamp('{0:yyyy'-'MM'-'dd}')".FormatInvariant(dateOnlyValue), ref utf8Encoder, output, out bytesWritten);
558+
return WriteString("{0:yyyy'-'MM'-'dd}".FormatInvariant(dateOnlyValue), ref utf8Encoder, output, out bytesWritten);
559559
}
560560
else if (value is TimeOnly timeOnlyValue)
561561
{
562-
return WriteString("time '{0:HH':'mm':'ss'.'ffffff}'".FormatInvariant(timeOnlyValue), ref utf8Encoder, output, out bytesWritten);
562+
return WriteString("{0:HH':'mm':'ss'.'ffffff}".FormatInvariant(timeOnlyValue), ref utf8Encoder, output, out bytesWritten);
563563
}
564564
#endif
565565
else if (value is TimeSpan ts)
@@ -570,7 +570,7 @@ static bool WriteValue(MySqlConnection connection, object value, ref int inputIn
570570
isNegative = true;
571571
ts = TimeSpan.FromTicks(-ts.Ticks);
572572
}
573-
return WriteString("{0}{1}:{2:mm':'ss'.'ffffff}'".FormatInvariant(isNegative ? "-" : "", ts.Days * 24 + ts.Hours, ts), ref utf8Encoder, output, out bytesWritten);
573+
return WriteString("{0}{1}:{2:mm':'ss'.'ffffff}".FormatInvariant(isNegative ? "-" : "", ts.Days * 24 + ts.Hours, ts), ref utf8Encoder, output, out bytesWritten);
574574
}
575575
else if (value is Guid guidValue)
576576
{

tests/SideBySide/BulkLoaderSync.cs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,90 @@ public void BulkCopyDataTableWithMySqlDecimal()
557557
Assert.Equal("3.579", reader.GetMySqlDecimal(0).ToString().TrimEnd('0'));
558558
}
559559
}
560+
561+
#if NET6_0_OR_GREATER
562+
[Fact]
563+
public void BulkCopyDataTableWithDateOnly()
564+
{
565+
var dataTable = new DataTable()
566+
{
567+
Columns =
568+
{
569+
new DataColumn("id", typeof(int)),
570+
new DataColumn("date1", typeof(DateOnly)),
571+
},
572+
Rows =
573+
{
574+
new object[] { 1, new DateOnly(2021, 3, 4) },
575+
},
576+
};
577+
578+
using var connection = new MySqlConnection(GetLocalConnectionString());
579+
connection.Open();
580+
using (var cmd = new MySqlCommand(@"drop table if exists bulk_load_data_table;
581+
create table bulk_load_data_table(a int, date1 date);", connection))
582+
{
583+
cmd.ExecuteNonQuery();
584+
}
585+
586+
var bulkCopy = new MySqlBulkCopy(connection)
587+
{
588+
DestinationTableName = "bulk_load_data_table",
589+
};
590+
var result = bulkCopy.WriteToServer(dataTable);
591+
Assert.Equal(1, result.RowsInserted);
592+
Assert.Empty(result.Warnings);
593+
594+
using (var cmd = new MySqlCommand(@"select * from bulk_load_data_table;", connection))
595+
{
596+
using var reader = cmd.ExecuteReader();
597+
Assert.True(reader.Read());
598+
Assert.Equal(new DateOnly(2021, 3, 4), reader.GetDateOnly(1));
599+
}
600+
}
601+
602+
[Fact]
603+
public void BulkCopyDataTableWithTimeOnly()
604+
{
605+
var dataTable = new DataTable()
606+
{
607+
Columns =
608+
{
609+
new DataColumn("id", typeof(int)),
610+
new DataColumn("time1", typeof(TimeOnly)),
611+
new DataColumn("time2", typeof(TimeOnly)),
612+
},
613+
Rows =
614+
{
615+
new object[] { 1, new TimeOnly(1, 2, 3, 456), new TimeOnly(1, 2, 3, 456) },
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, time1 time, time2 time(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(1, result.RowsInserted);
633+
Assert.Empty(result.Warnings);
634+
635+
using (var cmd = new MySqlCommand(@"select * from bulk_load_data_table;", connection))
636+
{
637+
using var reader = cmd.ExecuteReader();
638+
Assert.True(reader.Read());
639+
Assert.Equal(new TimeOnly(1, 2, 3), reader.GetTimeOnly(1));
640+
Assert.Equal(new TimeOnly(1, 2, 3, 456), reader.GetTimeOnly(2));
641+
}
642+
}
643+
#endif
560644
#endif
561645

562646
[Fact]

0 commit comments

Comments
 (0)