Skip to content

Commit 1f10081

Browse files
committed
Load column whose name begins with @. Fixes #1365
1 parent d4ea819 commit 1f10081

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/MySqlConnector/MySqlBulkCopy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ private async ValueTask<MySqlBulkCopyResult> WriteToServerAsync(IOBehavior ioBeh
265265
{
266266
if (columnMapping.DestinationColumn.Length == 0)
267267
throw new InvalidOperationException($"MySqlBulkCopyColumnMapping.DestinationName is not set for SourceOrdinal {columnMapping.SourceOrdinal}");
268-
if (columnMapping.DestinationColumn[0] == '@')
268+
if (columnMapping.DestinationColumn[0] == '@' && columnMapping.Expression is not null)
269269
bulkLoader.Columns.Add(columnMapping.DestinationColumn);
270270
else
271271
bulkLoader.Columns.Add(QuoteIdentifier(columnMapping.DestinationColumn));

tests/IntegrationTests/BulkLoaderSync.cs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,6 @@ public void BulkCopyNullDataTable()
530530
Assert.Throws<ArgumentNullException>(() => bulkCopy.WriteToServer(default(DataTable)));
531531
}
532532

533-
#if !MYSQL_DATA
534533
[Fact]
535534
public void BulkCopyDataTableWithMySqlDecimal()
536535
{
@@ -655,7 +654,6 @@ public void BulkCopyDataTableWithTimeOnly()
655654
Assert.Equal(new TimeOnly(1, 2, 3, 456), reader.GetTimeOnly(2));
656655
}
657656
}
658-
#endif
659657
#endif
660658

661659
public static IEnumerable<object[]> GetBulkCopyData() =>
@@ -713,6 +711,50 @@ public void BulkCopyDataTable(string columnType, object[] rows)
713711
}
714712
}
715713

714+
[Fact]
715+
public void BulkCopyToColumnNeedingQuoting()
716+
{
717+
var dataTable = new DataTable()
718+
{
719+
Columns =
720+
{
721+
new DataColumn("id", typeof(int)),
722+
new DataColumn("@a", typeof(string)),
723+
},
724+
};
725+
dataTable.Rows.Add(2, "two");
726+
727+
using var connection = new MySqlConnection(GetLocalConnectionString());
728+
connection.Open();
729+
using (var cmd = new MySqlCommand($"""
730+
drop table if exists bulk_load_quoted_identifier;
731+
create table bulk_load_quoted_identifier(id int, `@a` text);
732+
insert into bulk_load_quoted_identifier values (1, 'one');
733+
""", connection))
734+
{
735+
cmd.ExecuteNonQuery();
736+
}
737+
738+
var bulkCopy = new MySqlBulkCopy(connection)
739+
{
740+
DestinationTableName = "bulk_load_quoted_identifier",
741+
};
742+
var result = bulkCopy.WriteToServer(dataTable);
743+
Assert.Equal(1, result.RowsInserted);
744+
Assert.Empty(result.Warnings);
745+
746+
using (var cmd = new MySqlCommand(@"select `@a` from bulk_load_quoted_identifier order by id;", connection))
747+
{
748+
using var reader = cmd.ExecuteReader();
749+
Assert.True(reader.Read());
750+
Assert.Equal("one", reader.GetString(0));
751+
Assert.True(reader.Read());
752+
Assert.Equal("two", reader.GetString(0));
753+
Assert.False(reader.Read());
754+
Assert.False(reader.NextResult());
755+
}
756+
}
757+
716758
[Fact]
717759
public void BulkCopyDataTableWithLongBlob()
718760
{

0 commit comments

Comments
 (0)