-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntityDataReader.cs
264 lines (208 loc) · 7.19 KB
/
EntityDataReader.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
using Microsoft.SqlServer.Server;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace SqlBulkCopyUsingDataReader
{
public abstract class EntityDataReader<T> : IDataReader, IEnumerable<SqlDataRecord>, IDisposable where T : class
{
IEnumerable<T> _dataList;
IEnumerator<T> _dataEnumerator;
private SqlMetaData[] _sqlMetadataList = null;
private List<ColumnMetadata> _columnMetadataList = null;
private Dictionary<int, ColumnMetadata> _ordinaltoColumnMetadataMappings;
private Dictionary<string, ColumnMetadata> _nameToColumnMetadataMappings;
public EntityDataReader(IEnumerable<T> data)
{
this._dataList = data;
this._dataEnumerator = data.GetEnumerator();
this.Initialize();
}
private void Initialize()
{
this._columnMetadataList = this.GetMetadata();
this.FieldCount = this._columnMetadataList.Count;
//Sort by ordinal
this._columnMetadataList.OrderBy(item => item.Ordinal);
this._sqlMetadataList = new SqlMetaData[this._columnMetadataList.Count];
for (int i = 0; i < this._columnMetadataList.Count; i++)
{
var colMetadata = this._columnMetadataList[i];
if (colMetadata.DbType == SqlDbType.NVarChar || colMetadata.DbType == SqlDbType.VarChar)
{
this._sqlMetadataList[i] = new SqlMetaData(colMetadata.Name, colMetadata.DbType, colMetadata.Width);
}
else
{
this._sqlMetadataList[i] = new SqlMetaData(colMetadata.Name, colMetadata.DbType);
}
}
this._ordinaltoColumnMetadataMappings = this._columnMetadataList.ToDictionary(x => x.Ordinal, y => y);
this._nameToColumnMetadataMappings = this._ordinaltoColumnMetadataMappings.ToDictionary(x => x.Value.Name, item => item.Value);
}
public abstract List<ColumnMetadata> GetMetadata();
protected abstract object GetValue(int index, T data);
public object this[int i] => this.GetValue(i);
public object this[string name] => this.GetValue(this.GetOrdinal(name));
public int Depth => 1;
public bool IsClosed => this._dataEnumerator == null;
public int RecordsAffected => -1;
public int FieldCount { get; private set; } = 0;
public void Close()
{
Dispose();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(true);
}
public void Dispose(bool disposing)
{
if (disposing)
{
if (this._dataEnumerator != null)
{
this._dataEnumerator.Dispose();
this._dataEnumerator = null;
this._dataList = null;
}
}
}
~EntityDataReader()
{
Dispose(false);
}
public bool GetBoolean(int i)
{
throw new NotImplementedException();
}
public byte GetByte(int i)
{
throw new NotImplementedException();
}
public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
{
throw new NotImplementedException();
}
public char GetChar(int i)
{
throw new NotImplementedException();
}
public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)
{
throw new NotImplementedException();
}
public IDataReader GetData(int i)
{
throw new NotImplementedException();
}
public string GetDataTypeName(int i)
{
throw new NotImplementedException();
}
public DateTime GetDateTime(int i)
{
throw new NotImplementedException();
}
public decimal GetDecimal(int i)
{
throw new NotImplementedException();
}
public double GetDouble(int i)
{
throw new NotImplementedException();
}
public Type GetFieldType(int i)
{
throw new NotImplementedException();
}
public float GetFloat(int i)
{
throw new NotImplementedException();
}
public Guid GetGuid(int i)
{
throw new NotImplementedException();
}
public short GetInt16(int i)
{
throw new NotImplementedException();
}
public int GetInt32(int i)
{
throw new NotImplementedException();
}
public long GetInt64(int i)
{
throw new NotImplementedException();
}
public string GetName(int i)
{
return this._ordinaltoColumnMetadataMappings[i].Name;
}
public int GetOrdinal(string name)
{
return this._nameToColumnMetadataMappings[name].Ordinal;
}
public DataTable GetSchemaTable()
{
throw new NotImplementedException();
}
public string GetString(int i)
{
throw new NotImplementedException();
}
public object GetValue(int i)
{
return this.GetValue(i, this._dataEnumerator.Current);
}
public int GetValues(object[] values)
{
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}
int copyLength = (values.Length < FieldCount) ? values.Length : FieldCount;
for (int i = 0; i < copyLength; i++)
{
values[i] = GetValue(i);
}
return copyLength;
}
public bool IsDBNull(int i)
{
if (this._ordinaltoColumnMetadataMappings[i].IsNullable)
{
var val = this.GetValue(i);
if (val == null) return true;
}
return false;
}
public bool NextResult()
{
return false;
}
public bool Read() => this._dataEnumerator.MoveNext();
#region IEnumerable<SqlDataRecord> Members
public IEnumerator<SqlDataRecord> GetEnumerator()
{
foreach (var item in this._dataList)
{
var record = new SqlDataRecord(this._sqlMetadataList);
if (!this.Read()) break;
var values = new object[this._sqlMetadataList.Length];
this.GetValues(values);
record.SetValues(values);
yield return record;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return this._dataEnumerator;
}
#endregion
}
}