Skip to content

Commit

Permalink
Load column whose name begins with @. Fixes #1365
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrainger committed Sep 4, 2023
1 parent d4ea819 commit 1f10081
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/MySqlConnector/MySqlBulkCopy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ private async ValueTask<MySqlBulkCopyResult> WriteToServerAsync(IOBehavior ioBeh
{
if (columnMapping.DestinationColumn.Length == 0)
throw new InvalidOperationException($"MySqlBulkCopyColumnMapping.DestinationName is not set for SourceOrdinal {columnMapping.SourceOrdinal}");
if (columnMapping.DestinationColumn[0] == '@')
if (columnMapping.DestinationColumn[0] == '@' && columnMapping.Expression is not null)
bulkLoader.Columns.Add(columnMapping.DestinationColumn);
else
bulkLoader.Columns.Add(QuoteIdentifier(columnMapping.DestinationColumn));
Expand Down
46 changes: 44 additions & 2 deletions tests/IntegrationTests/BulkLoaderSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,6 @@ public void BulkCopyNullDataTable()
Assert.Throws<ArgumentNullException>(() => bulkCopy.WriteToServer(default(DataTable)));
}

#if !MYSQL_DATA
[Fact]
public void BulkCopyDataTableWithMySqlDecimal()
{
Expand Down Expand Up @@ -655,7 +654,6 @@ public void BulkCopyDataTableWithTimeOnly()
Assert.Equal(new TimeOnly(1, 2, 3, 456), reader.GetTimeOnly(2));
}
}
#endif
#endif

public static IEnumerable<object[]> GetBulkCopyData() =>
Expand Down Expand Up @@ -713,6 +711,50 @@ public void BulkCopyDataTable(string columnType, object[] rows)
}
}

[Fact]
public void BulkCopyToColumnNeedingQuoting()
{
var dataTable = new DataTable()
{
Columns =
{
new DataColumn("id", typeof(int)),
new DataColumn("@a", typeof(string)),
},
};
dataTable.Rows.Add(2, "two");

using var connection = new MySqlConnection(GetLocalConnectionString());
connection.Open();
using (var cmd = new MySqlCommand($"""
drop table if exists bulk_load_quoted_identifier;
create table bulk_load_quoted_identifier(id int, `@a` text);
insert into bulk_load_quoted_identifier values (1, 'one');
""", connection))
{
cmd.ExecuteNonQuery();
}

var bulkCopy = new MySqlBulkCopy(connection)
{
DestinationTableName = "bulk_load_quoted_identifier",
};
var result = bulkCopy.WriteToServer(dataTable);
Assert.Equal(1, result.RowsInserted);
Assert.Empty(result.Warnings);

using (var cmd = new MySqlCommand(@"select `@a` from bulk_load_quoted_identifier order by id;", connection))
{
using var reader = cmd.ExecuteReader();
Assert.True(reader.Read());
Assert.Equal("one", reader.GetString(0));
Assert.True(reader.Read());
Assert.Equal("two", reader.GetString(0));
Assert.False(reader.Read());
Assert.False(reader.NextResult());
}
}

[Fact]
public void BulkCopyDataTableWithLongBlob()
{
Expand Down

0 comments on commit 1f10081

Please sign in to comment.