comparison Source/Mapping/TextDataReader.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Data;
3 #if !SILVERLIGHT
4 using System.Data.Common;
5 #endif
6 using System.IO;
7 using System.Text;
8
9 namespace BLToolkit.Mapping
10 {
11 public class TextDataReader : IDataReader
12 {
13 #region Constructors
14
15 public TextDataReader(Stream stream)
16 : this(stream, Map.DefaultSchema)
17 {
18 }
19
20 public TextDataReader(Stream stream, MappingSchema mappingSchema)
21 {
22 GC.SuppressFinalize(this);
23
24 if (mappingSchema == null) throw new ArgumentNullException("mappingSchema");
25
26 _reader = new StreamReader(stream);
27 _mappingSchema = mappingSchema;
28
29 ReadHeader();
30 }
31
32 #endregion
33
34 #region Protected Members
35
36 private readonly StreamReader _reader;
37 private readonly MappingSchema _mappingSchema;
38 private string _line = string.Empty;
39 private string[] _names = _empty;
40 private string[] _values = _empty;
41 private int _lineNumber = 0;
42
43 private static readonly string[] _empty = new string[0];
44
45 private bool IsEof
46 {
47 get { return _line == null; }
48 }
49
50 private bool ReadNextLine()
51 {
52 while (!IsEof)
53 {
54 _line = _reader.ReadLine();
55 _lineNumber++;
56
57 if (!string.IsNullOrEmpty(_line) && _line[0] == '*')
58 return true;
59 }
60
61 return false;
62 }
63
64 private void ReadHeader()
65 {
66 while (ReadNextLine())
67 {
68 if (_line.StartsWith("*:"))
69 {
70 _names = _line.Substring(2).Split(':');
71 _values = new string[_names.Length];
72
73 for (int i = 0; i < _names.Length; i++)
74 _names[i] = _names[i].Trim();
75 }
76 else if (_line.StartsWith("**") || _line.StartsWith("*-"))
77 break;
78 }
79 }
80
81 static string Encode(string value)
82 {
83 var arr = Convert.FromBase64String(value.Substring(1));
84
85 #if SILVERLIGHT
86 return new UnicodeEncoding(false, true).GetString(arr, 0, arr.Length);
87 #else
88 return Encoding.Unicode.GetString(arr);
89 #endif
90 }
91
92 bool ReadRecord()
93 {
94 if (!IsEof)
95 {
96 if (_line.StartsWith("*-"))
97 return false;
98
99 if (_line.StartsWith("**") && _line.Length > 3)
100 {
101 var values = _line.Substring(3).Split(_line[2]);
102
103 for (var i = 0; i < _values.Length && i < values.Length; i++)
104 {
105 var value = values[i];
106
107 _values[i] =
108 value.Length == 0? null:
109 value[0] == '*'? value.Substring(1):
110 value[0] == '+'? Encode(value.Substring(1)) : value;
111 }
112
113 ReadNextLine();
114
115 return true;
116 }
117
118 throw new MappingException(
119 string.Format("Invalid data format in the line {0}.", _lineNumber));
120 }
121
122 return false;
123 }
124
125 #endregion
126
127 #region IDataReader Members
128
129 public virtual void Close()
130 {
131 _line = null;
132 }
133
134 public virtual int Depth
135 {
136 get { return 0; }
137 }
138
139 public virtual Type GetFieldType(int index)
140 {
141 return typeof(string);
142 }
143
144 public virtual string GetName(int index)
145 {
146 return _names[index];
147 }
148
149 #if !SILVERLIGHT
150 private DataTable _schemaTable;
151
152 public virtual DataTable GetSchemaTable()
153 {
154 if (_schemaTable == null)
155 {
156 _schemaTable = new DataTable("SchemaTable");
157
158 _schemaTable.Columns.AddRange(new DataColumn[]
159 {
160 new DataColumn(SchemaTableColumn.ColumnName, typeof(string)),
161 new DataColumn(SchemaTableColumn.ColumnOrdinal, typeof(int)),
162 new DataColumn(SchemaTableColumn.ColumnSize, typeof(int)),
163 new DataColumn(SchemaTableColumn.NumericPrecision, typeof(short)),
164 new DataColumn(SchemaTableColumn.NumericScale, typeof(short)),
165 new DataColumn(SchemaTableColumn.DataType, typeof(Type)),
166 new DataColumn(SchemaTableColumn.NonVersionedProviderType, typeof(int)),
167 new DataColumn(SchemaTableColumn.ProviderType, typeof(int)),
168 new DataColumn(SchemaTableColumn.IsLong, typeof(bool)),
169 new DataColumn(SchemaTableColumn.AllowDBNull, typeof(bool)),
170 new DataColumn(SchemaTableColumn.IsUnique, typeof(bool)),
171 new DataColumn(SchemaTableColumn.IsKey, typeof(bool)),
172 new DataColumn(SchemaTableColumn.BaseSchemaName, typeof(string)),
173 new DataColumn(SchemaTableColumn.BaseTableName, typeof(string)),
174 new DataColumn(SchemaTableColumn.BaseColumnName, typeof(string)),
175 new DataColumn(SchemaTableColumn.IsAliased, typeof(bool)),
176 new DataColumn(SchemaTableColumn.IsExpression, typeof(bool)),
177 });
178
179 for (int i = 0; i < _names.Length; i++)
180 {
181 DataRow row = _schemaTable.NewRow();
182
183 row[SchemaTableColumn.ColumnName] = _names[i];
184 row[SchemaTableColumn.ColumnOrdinal] = i;
185 row[SchemaTableColumn.ColumnSize] = (int)byte.MaxValue;
186 row[SchemaTableColumn.NumericPrecision] = (short)0;
187 row[SchemaTableColumn.NumericScale] = (short)0;
188 row[SchemaTableColumn.DataType] = typeof(string);
189 row[SchemaTableColumn.NonVersionedProviderType] = 1;
190 row[SchemaTableColumn.ProviderType] = 1;
191 row[SchemaTableColumn.IsLong] = false;
192 row[SchemaTableColumn.AllowDBNull] = true;
193 row[SchemaTableColumn.IsUnique] = false;
194 row[SchemaTableColumn.IsKey] = false;
195 row[SchemaTableColumn.BaseSchemaName] = string.Empty;
196 row[SchemaTableColumn.BaseTableName] = string.Empty;
197 row[SchemaTableColumn.BaseColumnName] = string.Empty;
198 row[SchemaTableColumn.IsAliased] = false;
199 row[SchemaTableColumn.IsExpression] = false;
200
201 _schemaTable.Rows.Add(row);
202 }
203 }
204
205 return _schemaTable;
206 }
207
208 #endif
209
210 public virtual int FieldCount
211 {
212 get { return _names.Length; }
213 }
214
215 public virtual bool IsClosed
216 {
217 get { return IsEof; }
218 }
219
220 public virtual bool NextResult()
221 {
222 ReadHeader();
223 return !IsEof;
224 }
225
226 public virtual bool Read()
227 {
228 return ReadRecord();
229 }
230
231 public virtual int RecordsAffected
232 {
233 get { return -1; }
234 }
235
236 #endregion
237
238 #region IDisposable Members
239
240 public virtual void Dispose()
241 {
242 }
243
244 #endregion
245
246 #region IDataRecord Members
247
248 public virtual bool GetBoolean(int i)
249 {
250 return _mappingSchema.ConvertToBoolean(_values[i]);
251 }
252
253 public virtual byte GetByte(int i)
254 {
255 return _mappingSchema.ConvertToByte(_values[i]);
256 }
257
258 public virtual long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
259 {
260 throw new Exception("The method or operation is not implemented.");
261 }
262
263 public virtual char GetChar(int i)
264 {
265 return _mappingSchema.ConvertToChar(_values[i]);
266 }
267
268 public virtual long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)
269 {
270 throw new Exception("The method or operation is not implemented.");
271 }
272
273 public virtual IDataReader GetData(int i)
274 {
275 throw new Exception("The method or operation is not implemented.");
276 }
277
278 public virtual string GetDataTypeName(int i)
279 {
280 return typeof(string).FullName;
281 }
282
283 public virtual DateTime GetDateTime(int i)
284 {
285 return _mappingSchema.ConvertToDateTime(_values[i]);
286 }
287
288 public virtual DateTimeOffset GetDateTimeOffset(int i)
289 {
290 return _mappingSchema.ConvertToDateTimeOffset(_values[i]);
291 }
292
293 public virtual decimal GetDecimal(int i)
294 {
295 return _mappingSchema.ConvertToDecimal(_values[i]);
296 }
297
298 public virtual double GetDouble(int i)
299 {
300 return _mappingSchema.ConvertToDouble(_values[i]);
301 }
302
303 public virtual float GetFloat(int i)
304 {
305 return _mappingSchema.ConvertToSingle(_values[i]);
306 }
307
308 public virtual Guid GetGuid(int i)
309 {
310 return _mappingSchema.ConvertToGuid(_values[i]);
311 }
312
313 public virtual short GetInt16(int i)
314 {
315 return _mappingSchema.ConvertToInt16(_values[i]);
316 }
317
318 public virtual int GetInt32(int i)
319 {
320 return _mappingSchema.ConvertToInt32(_values[i]);
321 }
322
323 public virtual long GetInt64(int i)
324 {
325 return _mappingSchema.ConvertToInt64(_values[i]);
326 }
327
328 public virtual int GetOrdinal(string name)
329 {
330 for (int i = 0; i < _names.Length; i++)
331 if (_names[i] == name)
332 return i;
333
334 return -1;
335 }
336
337 public virtual string GetString(int i)
338 {
339 return _values[i];
340 }
341
342 public virtual object GetValue(int i)
343 {
344 return _values[i];
345 }
346
347 public virtual int GetValues(object[] values)
348 {
349 int n = Math.Min(values.Length, _values.Length);
350
351 for (int i = 0; i < n; i++)
352 values[i] = _values[i];
353
354 return n;
355 }
356
357 public virtual bool IsDBNull(int i)
358 {
359 return _values[i] == null;
360 }
361
362 public virtual object this[string name]
363 {
364 get
365 {
366 for (int i = 0; i < _names.Length; i++)
367 if (_names[i] == name)
368 return _values[i];
369
370 throw new ArgumentException(string.Format("Invalid field name '{0}'", name));
371 }
372 }
373
374 public virtual object this[int i]
375 {
376 get { return _values[i]; }
377 }
378
379 #endregion
380 }
381 }