-
Notifications
You must be signed in to change notification settings - Fork 453
/
BulkCopyReader.cs
294 lines (235 loc) · 11.5 KB
/
BulkCopyReader.cs
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using System.Threading;
namespace LinqToDB.DataProvider
{
using Async;
using Common;
using Data;
using Mapping;
public class BulkCopyReader<T> : BulkCopyReader, IAsyncDisposable
{
readonly IEnumerator<T>? _enumerator;
readonly IAsyncEnumerator<T>? _asyncEnumerator;
public BulkCopyReader(DataConnection dataConnection, List<ColumnDescriptor> columns, IEnumerable<T> collection)
: base(dataConnection, columns)
{
_enumerator = collection.GetEnumerator();
}
public BulkCopyReader(DataConnection dataConnection, List<ColumnDescriptor> columns, IAsyncEnumerable<T> collection, CancellationToken cancellationToken)
: base(dataConnection, columns)
{
_asyncEnumerator = collection.GetAsyncEnumerator(cancellationToken);
}
protected override bool MoveNext()
{
if (_enumerator != null)
return _enumerator.MoveNext();
return SafeAwaiter.Run(() => _asyncEnumerator!.MoveNextAsync());
}
protected override object Current
=> (_enumerator != null ? _enumerator.Current : _asyncEnumerator!.Current)!;
protected override ValueTask<bool> MoveNextAsync()
=> _enumerator != null ? new ValueTask<bool>(_enumerator.MoveNext()) : _asyncEnumerator!.MoveNextAsync();
#region Implementation of IDisposable
#pragma warning disable CA2215 // CA2215: Dispose methods should call base class dispose
protected override void Dispose(bool disposing)
#pragma warning restore CA2215 // CA2215: Dispose methods should call base class dispose
{
if (disposing)
{
_enumerator?.Dispose();
if (_asyncEnumerator != null)
{
SafeAwaiter.Run(_asyncEnumerator.DisposeAsync);
}
}
}
#if NET6_0_OR_GREATER
#pragma warning disable CA2215 // CA2215: Dispose methods should call base class dispose
public override ValueTask DisposeAsync()
#pragma warning restore CA2215 // CA2215: Dispose methods should call base class dispose
#else
public ValueTask DisposeAsync()
#endif
{
_enumerator?.Dispose();
return _asyncEnumerator?.DisposeAsync() ?? default;
}
#endregion
}
public abstract class BulkCopyReader : DbDataReader
{
public int Count;
readonly DataConnection _dataConnection;
readonly DbDataType[] _columnTypes;
readonly List<ColumnDescriptor> _columns;
readonly Parameter _valueConverter = new ();
readonly IReadOnlyDictionary<string, int> _ordinals;
protected abstract bool MoveNext();
protected abstract ValueTask<bool> MoveNextAsync();
protected abstract object Current { get; }
protected BulkCopyReader(DataConnection dataConnection, List<ColumnDescriptor> columns)
{
_dataConnection = dataConnection;
_columns = columns;
_columnTypes = _columns.Select(c => c.GetConvertedDbDataType()).ToArray();
_ordinals = _columns.Select((c, i) => new { c, i }).ToDictionary(_ => _.c.ColumnName, _ => _.i);
}
public class Parameter : DbParameter
{
public override DbType DbType { get; set; }
public override ParameterDirection Direction { get; set; }
public override bool IsNullable { get => Value == null || Value is DBNull; set { } }
[AllowNull]
public override string ParameterName { get; set; }
public override int Size { get; set; }
[AllowNull]
public override string SourceColumn { get; set; }
public override DataRowVersion SourceVersion { get; set; }
public override bool SourceColumnNullMapping { get; set; }
public override object? Value { get; set; }
public override void ResetDbType() { }
}
#region Implementation of IDataRecord
public override string GetName(int ordinal)
{
return _columns[ordinal].ColumnName;
}
public override Type GetFieldType(int ordinal)
{
return _dataConnection.DataProvider.ConvertParameterType(_columns[ordinal].MemberType, _columnTypes[ordinal]);
}
public override object GetValue(int ordinal) => GetValueInternal(ordinal) ?? throw new InvalidOperationException("Value is NULL");
private object? GetValueInternal(int ordinal)
{
var value = _columns[ordinal].GetProviderValue(Current);
_dataConnection.DataProvider.SetParameter(_dataConnection, _valueConverter, string.Empty, _columnTypes[ordinal], value);
return _valueConverter.Value;
}
public override int GetValues(object?[] values)
{
var count = _columns.Count;
var obj = Current;
for (var it = 0; it < count; ++it)
{
var value = _columns[it].GetProviderValue(obj);
_dataConnection.DataProvider.SetParameter(_dataConnection, _valueConverter, string.Empty, _columnTypes[it], value);
values[it] = _valueConverter.Value;
}
return count;
}
public override int FieldCount => _columns.Count;
public override long GetBytes(int ordinal, long dataOffset, byte[]? buffer, int bufferOffset, int length)
{
throw new NotImplementedException();
}
public override long GetChars(int ordinal, long dataOffset, char[]? buffer, int bufferOffset, int length)
{
throw new NotImplementedException();
}
public override string GetDataTypeName(int ordinal) => throw new NotImplementedException();
public override int GetOrdinal (string name) => _ordinals[name];
public override bool GetBoolean (int ordinal) => throw new NotImplementedException();
public override byte GetByte (int ordinal) => throw new NotImplementedException();
public override char GetChar (int ordinal) => throw new NotImplementedException();
public override Guid GetGuid (int ordinal) => throw new NotImplementedException();
public override short GetInt16 (int ordinal) => throw new NotImplementedException();
public override int GetInt32 (int ordinal) => throw new NotImplementedException();
public override long GetInt64 (int ordinal) => throw new NotImplementedException();
public override float GetFloat (int ordinal) => throw new NotImplementedException();
public override double GetDouble (int ordinal) => throw new NotImplementedException();
public override string GetString (int ordinal) => throw new NotImplementedException();
public override decimal GetDecimal (int ordinal) => throw new NotImplementedException();
public override DateTime GetDateTime (int ordinal) => throw new NotImplementedException();
public override bool IsDBNull (int ordinal) => GetValueInternal(ordinal) == null;
public override object this[int i] => throw new NotImplementedException();
public override object this[string name] => throw new NotImplementedException();
#endregion
#region Implementation of IDataReader
public override void Close()
{
//do nothing
}
public override DataTable GetSchemaTable()
{
var table = new DataTable("SchemaTable")
{
Locale = CultureInfo.InvariantCulture
};
table.Columns.Add(new DataColumn(SchemaTableColumn.ColumnName, typeof(string)));
table.Columns.Add(new DataColumn(SchemaTableColumn.ColumnOrdinal, typeof(int)));
table.Columns.Add(new DataColumn(SchemaTableColumn.ColumnSize, typeof(int)));
table.Columns.Add(new DataColumn(SchemaTableColumn.NumericPrecision, typeof(short)));
table.Columns.Add(new DataColumn(SchemaTableColumn.NumericScale, typeof(short)));
table.Columns.Add(new DataColumn(SchemaTableColumn.DataType, typeof(Type)));
table.Columns.Add(new DataColumn(SchemaTableOptionalColumn.ProviderSpecificDataType, typeof(Type)));
table.Columns.Add(new DataColumn(SchemaTableColumn.NonVersionedProviderType, typeof(int)));
table.Columns.Add(new DataColumn(SchemaTableColumn.ProviderType, typeof(int)));
table.Columns.Add(new DataColumn(SchemaTableColumn.IsLong, typeof(bool)));
table.Columns.Add(new DataColumn(SchemaTableColumn.AllowDBNull, typeof(bool)));
table.Columns.Add(new DataColumn(SchemaTableOptionalColumn.IsReadOnly, typeof(bool)));
table.Columns.Add(new DataColumn(SchemaTableOptionalColumn.IsRowVersion, typeof(bool)));
table.Columns.Add(new DataColumn(SchemaTableColumn.IsUnique, typeof(bool)));
table.Columns.Add(new DataColumn(SchemaTableColumn.IsKey, typeof(bool)));
table.Columns.Add(new DataColumn(SchemaTableOptionalColumn.IsAutoIncrement, typeof(bool)));
table.Columns.Add(new DataColumn(SchemaTableOptionalColumn.IsHidden, typeof(bool)));
table.Columns.Add(new DataColumn(SchemaTableOptionalColumn.BaseCatalogName, typeof(string)));
table.Columns.Add(new DataColumn(SchemaTableColumn.BaseSchemaName, typeof(string)));
table.Columns.Add(new DataColumn(SchemaTableColumn.BaseTableName, typeof(string)));
table.Columns.Add(new DataColumn(SchemaTableColumn.BaseColumnName, typeof(string)));
table.Columns.Add(new DataColumn(SchemaTableOptionalColumn.BaseServerName, typeof(string)));
table.Columns.Add(new DataColumn(SchemaTableColumn.IsAliased, typeof(bool)));
table.Columns.Add(new DataColumn(SchemaTableColumn.IsExpression, typeof(bool)));
for (var i = 0; i < _columns.Count; ++i)
{
var columnDescriptor = _columns[i];
var convertedType = columnDescriptor.GetConvertedDbDataType();
var row = table.NewRow();
row[SchemaTableColumn.ColumnName] = columnDescriptor.ColumnName;
row[SchemaTableColumn.DataType] = _dataConnection.DataProvider.ConvertParameterType(convertedType.SystemType, _columnTypes[i]);
row[SchemaTableColumn.IsKey] = columnDescriptor.IsPrimaryKey;
row[SchemaTableOptionalColumn.IsAutoIncrement] = columnDescriptor.IsIdentity;
row[SchemaTableColumn.AllowDBNull] = columnDescriptor.CanBeNull;
//length cannot be null(DBNull) or 0
row[SchemaTableColumn.ColumnSize] =
columnDescriptor.Length.HasValue && columnDescriptor.Length > 0 ?
columnDescriptor.Length.Value : 0x7FFFFFFF;
if (columnDescriptor.Precision != null)
row[SchemaTableColumn.NumericPrecision] = (short)columnDescriptor.Precision.Value;
if (columnDescriptor.Scale != null)
row[SchemaTableColumn.NumericScale] = (short)columnDescriptor.Scale.Value;
table.Rows.Add(row);
}
return table;
}
public override bool NextResult() => false;
public override bool Read()
{
var b = MoveNext();
if (b)
Count++;
return b;
}
public override async Task<bool> ReadAsync(CancellationToken cancellationToken)
{
var b = await MoveNextAsync().ConfigureAwait(Configuration.ContinueOnCapturedContext);
if (b)
Count++;
return b;
}
public override int Depth => throw new NotImplementedException();
public override bool IsClosed => false;
public override int RecordsAffected => throw new NotImplementedException();
#endregion
public override IEnumerator GetEnumerator() => throw new NotImplementedException();
public override bool HasRows => throw new NotImplementedException();
}
}