-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathMySqlBulkInsertProvider.cs
More file actions
117 lines (99 loc) · 3.79 KB
/
MySqlBulkInsertProvider.cs
File metadata and controls
117 lines (99 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.Logging;
using MySqlConnector;
using PhenX.EntityFrameworkCore.BulkInsert.Metadata;
using PhenX.EntityFrameworkCore.BulkInsert.Options;
namespace PhenX.EntityFrameworkCore.BulkInsert.MySql;
[UsedImplicitly]
internal class MySqlBulkInsertProvider(ILogger<MySqlBulkInsertProvider> logger) : BulkInsertProviderBase<MySqlServerDialectBuilder, MySqlBulkInsertOptions>(logger)
{
//language=sql
/// <inheritdoc />
protected override string AddTableCopyBulkInsertId => $"ALTER TABLE {{0}} ADD {BulkInsertId} INT AUTO_INCREMENT PRIMARY KEY;";
/// <inheritdoc />
public override bool SupportsOutputInsertedIds => false;
/// <inheritdoc />
protected override string GetTempTableName(string tableName) => $"#_temp_bulk_insert_{tableName}_{Helpers.RandomString(6)}";
/// <inheritdoc />
protected override MySqlBulkInsertOptions CreateDefaultOptions() => new()
{
Converters = [MySqlGeometryConverter.Instance]
};
/// <inheritdoc />
protected override IAsyncEnumerable<T> BulkInsertReturnEntities<T>(
bool sync,
DbContext context,
TableMetadata tableInfo,
IEnumerable<T> entities,
MySqlBulkInsertOptions options,
OnConflictOptions<T>? onConflict,
CancellationToken ctk)
{
throw new NotSupportedException("Provider does not support returning entities.");
}
/// <inheritdoc />
protected override async Task BulkInsert<T>(
bool sync,
DbContext context,
TableMetadata tableInfo,
IEnumerable<T> entities,
string tableName,
IReadOnlyList<ColumnMetadata> properties,
MySqlBulkInsertOptions options,
CancellationToken ctk
)
{
var connection = (MySqlConnection)context.Database.GetDbConnection();
var sqlTransaction = context.Database.CurrentTransaction!.GetDbTransaction()
?? throw new InvalidOperationException("No open transaction found.");
if (sqlTransaction is not MySqlTransaction mySqlTransaction)
{
throw new InvalidOperationException($"Invalid transaction foud, got {sqlTransaction.GetType()}.");
}
var bulkCopy = new MySqlBulkCopy(connection, mySqlTransaction);
bulkCopy.DestinationTableName = tableName;
bulkCopy.BulkCopyTimeout = options.GetCopyTimeoutInSeconds();
// Handle progress notifications
if (options is { NotifyProgressAfter: not null, OnProgress: not null })
{
bulkCopy.NotifyAfter = options.NotifyProgressAfter.Value;
bulkCopy.MySqlRowsCopied += (sender, e) =>
{
options.OnProgress(e.RowsCopied);
if (ctk.IsCancellationRequested)
{
e.Abort = true;
}
};
}
// If no progress notification is set, we still need to handle cancellation.
else
{
bulkCopy.MySqlRowsCopied += (sender, e) =>
{
if (ctk.IsCancellationRequested)
{
e.Abort = true;
}
};
}
var sourceOrdinal = 0;
foreach (var prop in properties)
{
bulkCopy.ColumnMappings.Add(new MySqlBulkCopyColumnMapping(sourceOrdinal, prop.ColumnName));
sourceOrdinal++;
}
var dataReader = new EnumerableDataReader<T>(entities, properties, options);
if (sync)
{
// ReSharper disable once MethodHasAsyncOverloadWithCancellation
bulkCopy.WriteToServer(dataReader);
}
else
{
await bulkCopy.WriteToServerAsync(dataReader, ctk);
}
}
}