-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathBulkInsertProviderBase.cs
More file actions
276 lines (241 loc) · 9.17 KB
/
BulkInsertProviderBase.cs
File metadata and controls
276 lines (241 loc) · 9.17 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
using System.Runtime.CompilerServices;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.Logging;
using PhenX.EntityFrameworkCore.BulkInsert.Dialect;
using PhenX.EntityFrameworkCore.BulkInsert.Extensions;
using PhenX.EntityFrameworkCore.BulkInsert.Metadata;
using PhenX.EntityFrameworkCore.BulkInsert.Options;
namespace PhenX.EntityFrameworkCore.BulkInsert;
internal abstract class BulkInsertProviderBase<TDialect, TOptions>(ILogger? logger) : BulkInsertProviderUntyped<TDialect, TOptions>
where TDialect : SqlDialectBuilder, new()
where TOptions : BulkInsertOptions, new()
{
protected virtual string BulkInsertId => "_bulk_insert_id";
protected abstract string AddTableCopyBulkInsertId { get; }
protected virtual string GetTempTableName(string tableName) => $"_temp_bulk_insert_{tableName}";
protected override async IAsyncEnumerable<T> BulkInsertReturnEntities<T>(
bool sync,
DbContext context,
TableMetadata tableInfo,
IEnumerable<T> entities,
TOptions options,
OnConflictOptions<T>? onConflict,
[EnumeratorCancellation] CancellationToken ctk) where T : class
{
using var activity = Telemetry.ActivitySource.StartActivity("BulkInsertReturnEntities");
activity?.AddTag("tableName", tableInfo.TableName);
activity?.AddTag("synchronous", sync);
var connection = await context.GetConnection(sync, ctk);
try
{
if (logger != null)
{
Log.UsingTempTableToReturnData(logger);
}
var tableName = await PerformBulkInsertAsync(sync, context, tableInfo, entities, options, tempTableRequired: true, ctk: ctk);
try
{
var result =
await CopyFromTempTableAsync<T, T>(sync, context, tableInfo, tableName, true, options, onConflict, ctk: ctk)
?? throw new InvalidOperationException("Copy returns null enumerable.");
await foreach (var item in result.WithCancellation(ctk))
{
yield return item;
}
}
finally
{
await PerformDropTempTableAsync(sync, context, tableName);
}
// Commit the transaction if we own them.
await connection.Commit(sync, ctk);
}
finally
{
await connection.Close(sync, ctk);
}
}
protected override async Task BulkInsert<T>(
bool sync,
DbContext context,
TableMetadata tableInfo,
IEnumerable<T> entities,
TOptions options,
OnConflictOptions<T>? onConflict,
CancellationToken ctk) where T : class
{
if (entities.TryGetNonEnumeratedCount(out var count) && count == 0)
{
throw new InvalidOperationException("No entities to insert.");
}
using var activity = Telemetry.ActivitySource.StartActivity("BulkInsert");
activity?.AddTag("tableName", tableInfo.TableName);
activity?.AddTag("synchronous", sync);
var connection = await context.GetConnection(sync, ctk);
try
{
if (onConflict != null)
{
if (logger != null)
{
Log.UsingTempTableToResolveConflicts(logger);
}
var tableName = await PerformBulkInsertAsync(sync, context, tableInfo, entities, options, tempTableRequired: true, ctk: ctk);
try
{
await CopyFromTempTableAsync<T, T>(sync, context, tableInfo, tableName, false, options, onConflict, ctk);
}
finally
{
await PerformDropTempTableAsync(sync, context, tableName);
}
}
else
{
if (logger != null)
{
Log.UsingDirectInsert(logger);
}
await PerformBulkInsertAsync(sync, context, tableInfo, entities, options, tempTableRequired: false, ctk: ctk);
}
// Commit the transaction if we own them.
await connection.Commit(sync, ctk);
}
finally
{
await connection.Close(sync, ctk);
}
}
private async Task<string> PerformBulkInsertAsync<T>(
bool sync,
DbContext context,
TableMetadata tableInfo,
IEnumerable<T> entities,
TOptions options,
bool tempTableRequired,
CancellationToken ctk) where T : class
{
var tableName = tempTableRequired
? await CreateTableCopyAsync<T>(sync, context, options, tableInfo, ctk)
: tableInfo.QuotedTableName;
var columns = tableInfo.GetColumns(options.CopyGeneratedColumns);
using var activity = Telemetry.ActivitySource.StartActivity("Insert");
activity?.AddTag("tempTable", tempTableRequired);
activity?.AddTag("synchronous", sync);
await BulkInsert(false, context, tableInfo, entities, tableName, columns, options, ctk);
return tableName;
}
/// <summary>
/// The main bulk insert method: will insert either in a temp table or directly in the target table.
/// </summary>
protected abstract Task BulkInsert<T>(
bool sync,
DbContext context,
TableMetadata tableInfo,
IEnumerable<T> entities,
string tableName,
IReadOnlyList<ColumnMetadata> columns,
TOptions options,
CancellationToken ctk) where T : class;
protected async Task<string> CreateTableCopyAsync<T>(
bool sync,
DbContext context,
BulkInsertOptions options,
TableMetadata tableInfo,
CancellationToken ctk) where T : class
{
var tempTableName = SqlDialect.QuoteTableName(null, GetTempTableName(tableInfo.TableName));
var tempColumns = tableInfo.GetColumns(options.CopyGeneratedColumns);
var query = SqlDialect.CreateTableCopySql(tempTableName, tableInfo, tempColumns);
await ExecuteAsync(sync, context, query, ctk);
await AddBulkInsertIdColumn<T>(sync, context, tempTableName, ctk);
return tempTableName;
}
protected virtual async Task AddBulkInsertIdColumn<T>(
bool sync,
DbContext context,
string tempTableName,
CancellationToken ctk) where T : class
{
if (string.IsNullOrEmpty(AddTableCopyBulkInsertId))
{
// No need to add an ID column in this provider
return;
}
var alterQuery = string.Format(AddTableCopyBulkInsertId, tempTableName);
await ExecuteAsync(sync, context, alterQuery, ctk);
}
private async Task<IAsyncEnumerable<TResult>?> CopyFromTempTableAsync<T, TResult>(
bool sync,
DbContext context,
TableMetadata tableInfo,
string tempTableName,
bool returnData,
TOptions options,
OnConflictOptions<T>? onConflict,
CancellationToken ctk) where T : class where TResult : class
{
var query =
SqlDialect.BuildMoveDataSql<T>(
context,
tableInfo,
tempTableName,
tableInfo.GetColumns(options.CopyGeneratedColumns),
returnData ? tableInfo.GetColumns() : [],
options,
onConflict);
if (returnData)
{
// Use EF to execute the query and return the results
return context.Set<TResult>().FromSqlRaw(query).AsAsyncEnumerable();
}
// If not returning data, just execute the command
await ExecuteAsync(sync, context, query, ctk);
return null;
}
private async Task PerformDropTempTableAsync(bool sync, DbContext dbContext, string tableName)
{
try
{
await DropTempTableAsync(sync, dbContext, tableName);
}
catch (Exception ex)
{
// The drop operation is not mandatory, therefore never fail the actual operation.
if (logger != null)
{
Log.DropTemporaryTableFailed(logger, ex);
}
}
}
/// <summary>
/// Drops the temporary table manually if needed.
/// </summary>
/// <param name="sync">Indicates if the operation is synchronous.</param>
/// <param name="dbContext">The context.</param>
/// <param name="tableName">The table name.</param>
protected virtual Task DropTempTableAsync(bool sync, DbContext dbContext, string tableName)
{
return Task.CompletedTask;
}
protected static async Task ExecuteAsync(
bool sync,
DbContext context,
string query,
CancellationToken ctk)
{
var command = context.Database.GetDbConnection().CreateCommand();
command.Transaction = context.Database.CurrentTransaction!.GetDbTransaction();
command.CommandText = query;
if (sync)
{
// ReSharper disable once MethodHasAsyncOverloadWithCancellation
command.ExecuteNonQuery();
}
else
{
await command.ExecuteNonQueryAsync(ctk);
}
}
}