Skip to content

Commit 8497ffc

Browse files
authored
Fix mappings for extra columns in schema (#1594). Fixes #1568
This creates/updates column mappings for columns in the DB schema that have a higher ordinal number than columns in the source data. Signed-off-by: Bradley Grainger <[email protected]>
1 parent 7174c4c commit 8497ffc

File tree

4 files changed

+81
-3
lines changed

4 files changed

+81
-3
lines changed

src/MySqlConnector/MySqlBulkCopy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ private async ValueTask<MySqlBulkCopyResult> WriteToServerAsync(IOBehavior ioBeh
266266
using (var reader = await cmd.ExecuteReaderAsync(CommandBehavior.SchemaOnly, ioBehavior, cancellationToken).ConfigureAwait(false))
267267
{
268268
var schema = reader.GetColumnSchema();
269-
for (var i = 0; i < Math.Min(m_valuesEnumerator!.FieldCount, schema.Count); i++)
269+
for (var i = 0; i < schema.Count; i++)
270270
{
271271
var destinationColumn = reader.GetName(i);
272272
if (schema[i].DataTypeName == "BIT")
@@ -295,7 +295,7 @@ private async ValueTask<MySqlBulkCopyResult> WriteToServerAsync(IOBehavior ioBeh
295295
}
296296

297297
// set columns and expressions from the column mappings
298-
for (var i = 0; i < m_valuesEnumerator.FieldCount; i++)
298+
for (var i = 0; i < m_valuesEnumerator!.FieldCount; i++)
299299
{
300300
var columnMapping = columnMappings.FirstOrDefault(x => x.SourceOrdinal == i);
301301
if (columnMapping is null)

tests/IntegrationTests/BulkLoaderAsync.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,45 @@ public async Task BulkCopyNullDataReader()
635635
var bulkCopy = new MySqlBulkCopy(connection);
636636
await Assert.ThrowsAsync<ArgumentNullException>(async () => await bulkCopy.WriteToServerAsync(default(DbDataReader)));
637637
}
638+
639+
[Fact]
640+
public async Task BulkCopyGeometryAsync()
641+
{
642+
var dataTable = new DataTable()
643+
{
644+
Columns =
645+
{
646+
new DataColumn("geo_data", typeof(MySqlGeometry)),
647+
},
648+
Rows =
649+
{
650+
new object[] { MySqlGeometry.FromWkb(0, [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 240, 63, 0, 0, 0, 0, 0, 0, 240, 63]) },
651+
},
652+
};
653+
654+
using var connection = new MySqlConnection(GetLocalConnectionString());
655+
await connection.OpenAsync();
656+
using (var cmd = new MySqlCommand(@"drop table if exists bulk_load_data_table;
657+
create table bulk_load_data_table(id BIGINT UNIQUE NOT NULL AUTO_INCREMENT, geo_data GEOMETRY NOT NULL);", connection))
658+
{
659+
await cmd.ExecuteNonQueryAsync();
660+
}
661+
662+
var bc = new MySqlBulkCopy(connection)
663+
{
664+
DestinationTableName = "bulk_load_data_table",
665+
ColumnMappings =
666+
{
667+
new()
668+
{
669+
SourceOrdinal = 0,
670+
DestinationColumn = "geo_data",
671+
},
672+
},
673+
};
674+
675+
await bc.WriteToServerAsync(dataTable);
676+
}
638677
#endif
639678

640679
private static string GetConnectionString() => BulkLoaderSync.GetConnectionString();

tests/IntegrationTests/BulkLoaderSync.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,45 @@ public void BulkCopyDataTableConflictOption(MySqlBulkLoaderConflictOption confli
12841284
using (var cmd = new MySqlCommand("select b from bulk_load_data_table;", connection))
12851285
Assert.Equal(expected, cmd.ExecuteScalar());
12861286
}
1287+
1288+
[Fact]
1289+
public void BulkCopyGeometry()
1290+
{
1291+
var dataTable = new DataTable()
1292+
{
1293+
Columns =
1294+
{
1295+
new DataColumn("geo_data", typeof(MySqlGeometry)),
1296+
},
1297+
Rows =
1298+
{
1299+
new object[] { MySqlGeometry.FromWkb(0, [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 240, 63, 0, 0, 0, 0, 0, 0, 240, 63]) },
1300+
},
1301+
};
1302+
1303+
using var connection = new MySqlConnection(GetLocalConnectionString());
1304+
connection.Open();
1305+
using (var cmd = new MySqlCommand(@"drop table if exists bulk_load_data_table;
1306+
create table bulk_load_data_table(id BIGINT UNIQUE NOT NULL AUTO_INCREMENT, geo_data GEOMETRY NOT NULL);", connection))
1307+
{
1308+
cmd.ExecuteNonQuery();
1309+
}
1310+
1311+
var bc = new MySqlBulkCopy(connection)
1312+
{
1313+
DestinationTableName = "bulk_load_data_table",
1314+
ColumnMappings =
1315+
{
1316+
new()
1317+
{
1318+
SourceOrdinal = 0,
1319+
DestinationColumn = "geo_data",
1320+
},
1321+
},
1322+
};
1323+
1324+
bc.WriteToServer(dataTable);
1325+
}
12871326
#endif
12881327

12891328
internal static string GetConnectionString() => AppConfig.ConnectionString;

tests/IntegrationTests/IntegrationTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<ServerGarbageCollection>true</ServerGarbageCollection>
2424
<ConcurrentGarbageCollection>true</ConcurrentGarbageCollection>
2525
<ThreadPoolMinThreads>64</ThreadPoolMinThreads>
26-
<LangVersion>11.0</LangVersion>
26+
<LangVersion>12.0</LangVersion>
2727
<ImplicitUsings>enable</ImplicitUsings>
2828
<NoWarn>$(NoWarn);SA0001;SA1021;SA1133;xUnit1030</NoWarn>
2929
</PropertyGroup>

0 commit comments

Comments
 (0)