-
Notifications
You must be signed in to change notification settings - Fork 1
/
AppleEPFReader.cs
612 lines (541 loc) · 20.7 KB
/
AppleEPFReader.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using System.IO;
/// <summary>
/// Apple EPF Reader
/// </summary>
public class AppleEPFReader : IDataReader, IEnumerable<string[]> {
#region Constants
/// <summary>
/// Field Separator
/// </summary>
private static readonly char FIELD_SEPARATOR = (char)1;
/// <summary>
/// Record Separator
/// </summary>
private static readonly char RECORD_SEPARATOR = (char)2;
/// <summary>
/// Comment Initializer
/// </summary>
private static readonly char COMMENT_INITIALIZER = '#';
/// <summary>
/// Row name separator
/// </summary>
private static readonly char COMMENT_ROW_NAME_SEPARATOR = ':';
#endregion
/// <summary>
/// DataReader status
/// </summary>
private bool _IsClosed = true;
/// <summary>
/// Table Schema
/// </summary>
private DataTable _SCHEMA;
/// <summary>
/// DB Column Types
/// </summary>
private string[] _TYPES;
/// <summary>
/// Foreign Keys
/// </summary>
private string[] _KEYS;
/// <summary>
/// Table Column Names
/// </summary>
private string[] _COLUMNS;
/// <summary>
/// Table Column Name Index
/// </summary>
private Dictionary<string, int> _COLUMN_INDEX;
/// <summary>
/// Stream reader
/// </summary>
private StreamReader _FILE_READER;
/// <summary>
/// Current row data
/// </summary>
private string[] _CURRENT_ROW;
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>The name.</value>
public string Name { get; private set; }
/// <summary>
/// Gets the columns.
/// </summary>
/// <value>The columns.</value>
public string[] Columns {
get {
return _COLUMNS;
}
}
/// <summary>
/// Gets or sets the export mode.
/// </summary>
/// <value>The export mode.</value>
public AppleEPFExportMode ExportMode { get; private set; }
#region Constructor
/// <summary>
/// Initializes a new instance of the <see cref="AppleEPFReader"/> class.
/// </summary>
/// <param name="filePath">Name of the file.</param>
public AppleEPFReader(string filePath) {
//Open file
_FILE_READER = new StreamReader(filePath);
//File name
this.Name = Path.GetFileName(filePath);
//Read scehma data
//Read column names
_COLUMNS = _FILE_READER.ReadLine()
.Trim(COMMENT_INITIALIZER)
.Split(FIELD_SEPARATOR);
_COLUMNS[_COLUMNS.Length - 1] = _COLUMNS[_COLUMNS.Length - 1].Trim(RECORD_SEPARATOR);
//Convert to a Dictionary with the column name as the key and index as the value
_COLUMN_INDEX = _COLUMNS.ToDictionary(s => s, s => Array.IndexOf(_COLUMNS, s));
//Read keys
_KEYS = _FILE_READER.ReadLine()
.Split(COMMENT_ROW_NAME_SEPARATOR)[1]
.Split(FIELD_SEPARATOR);
_KEYS[_KEYS.Length - 1] = _KEYS[_KEYS.Length - 1].Trim(RECORD_SEPARATOR);
//Read column data types
_TYPES = _FILE_READER.ReadLine()
.Split(COMMENT_ROW_NAME_SEPARATOR)[1]
.Split(FIELD_SEPARATOR);
_TYPES[_TYPES.Length - 1] = _TYPES[_TYPES.Length - 1].Trim(RECORD_SEPARATOR);
//Read export method full/incremental
string exportMode = _FILE_READER.ReadLine()
.Split(COMMENT_ROW_NAME_SEPARATOR)[1]
.Trim(COMMENT_INITIALIZER, FIELD_SEPARATOR, RECORD_SEPARATOR);
this.ExportMode = (AppleEPFExportMode)Enum.Parse(typeof(AppleEPFExportMode), exportMode, true);
//create schema table
_SCHEMA = new DataTable();
_SCHEMA.Columns.Add("ColumnName");
_SCHEMA.Columns.Add("DataType");
_SCHEMA.Columns.Add("IsKey");
for (int i = 0; i < _COLUMNS.Length; i++) {
DataRow row = _SCHEMA.NewRow();
row["ColumnName"] = _COLUMNS[i];
row["DataType"] = _TYPES[i];
row["IsKey"] = _KEYS.Contains(_COLUMNS[i]);
_SCHEMA.Rows.Add(row);
}
this._IsClosed = false;
}
#endregion
#region DataReader Implementation
/// <summary>
/// Closes the <see cref="T:System.Data.IDataReader"/> Object.
/// </summary>
public void Close() {
if (null != _FILE_READER) {
_FILE_READER.Close();
this._IsClosed = true;
}
}
/// <summary>
/// Gets a value indicating the depth of nesting for the current row.
/// </summary>
/// <value></value>
/// <returns>The level of nesting.</returns>
public int Depth {
get { return 0; }
}
/// <summary>
/// Returns a <see cref="T:System.Data.DataTable"/> that describes the column metadata of the <see cref="T:System.Data.IDataReader"/>.
/// </summary>
/// <returns>
/// A <see cref="T:System.Data.DataTable"/> that describes the column metadata.
/// </returns>
/// <exception cref="T:System.InvalidOperationException">The <see cref="T:System.Data.IDataReader"/> is closed. </exception>
public DataTable GetSchemaTable() {
return _SCHEMA;
}
/// <summary>
/// Gets a value indicating whether the data reader is closed.
/// </summary>
/// <value></value>
/// <returns>true if the data reader is closed; otherwise, false.</returns>
public bool IsClosed {
get {
return _IsClosed;
}
}
/// <summary>
/// Advances the data reader to the next result, when reading the results of batch SQL statements.
/// </summary>
/// <returns>
/// true if there are more rows; otherwise, false.
/// </returns>
public bool NextResult() {
return false;
}
/// <summary>
/// Advances the <see cref="T:System.Data.IDataReader"/> to the next record.
/// </summary>
/// <returns>
/// true if there are more rows; otherwise, false.
/// </returns>
public bool Read() {
if (null == _FILE_READER)
return false;
if (null != _FILE_READER && _FILE_READER.EndOfStream)
return false;
string data;
do {
data = FetchRecord(_FILE_READER, RECORD_SEPARATOR);
}
while (!_FILE_READER.EndOfStream && data[0] == COMMENT_INITIALIZER);
_CURRENT_ROW = (!_FILE_READER.EndOfStream) ? data.ToString().Split(FIELD_SEPARATOR) : null;
return null != _CURRENT_ROW;
}
/// <summary>
/// Gets the number of rows changed, inserted, or deleted by execution of the SQL statement.
/// </summary>
/// <value></value>
/// <returns>The number of rows changed, inserted, or deleted; 0 if no rows were affected or the statement failed; and -1 for SELECT statements.</returns>
public int RecordsAffected {
get {
return 0;
}
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose() {
if (null != _FILE_READER)
_FILE_READER.Dispose();
if (null != _SCHEMA)
_SCHEMA.Dispose();
_IsClosed = true;
}
/// <summary>
/// Gets the number of columns in the current row.
/// </summary>
/// <value></value>
/// <returns>When not positioned in a valid recordset, 0; otherwise, the number of columns in the current record. The default is -1.</returns>
public int FieldCount {
get {
return this._COLUMNS.Length;
}
}
/// <summary>
/// Gets the value of the specified column as a Boolean.
/// </summary>
/// <param name="i">The zero-based column ordinal.</param>
/// <returns>The value of the column.</returns>
/// <exception cref="T:System.IndexOutOfRangeException">The index passed was outside the range of 0 through <see cref="P:System.Data.IDataRecord.FieldCount"/>. </exception>
public bool GetBoolean(int i) {
return bool.Parse(_CURRENT_ROW[i]);
}
/// <summary>
/// Gets the 8-bit unsigned integer value of the specified column.
/// </summary>
/// <param name="i">The zero-based column ordinal.</param>
/// <returns>
/// The 8-bit unsigned integer value of the specified column.
/// </returns>
/// <exception cref="T:System.IndexOutOfRangeException">The index passed was outside the range of 0 through <see cref="P:System.Data.IDataRecord.FieldCount"/>. </exception>
public byte GetByte(int i) {
return Convert.ToByte(_CURRENT_ROW[i]);
}
/// <summary>
/// Reads a stream of bytes from the specified column offset into the buffer as an array, starting at the given buffer offset.
/// </summary>
/// <param name="i">The zero-based column ordinal.</param>
/// <param name="fieldOffset">The index within the field from which to start the read operation.</param>
/// <param name="buffer">The buffer into which to read the stream of bytes.</param>
/// <param name="bufferoffset">The index for <paramref name="buffer"/> to start the read operation.</param>
/// <param name="length">The number of bytes to read.</param>
/// <returns>The actual number of bytes read.</returns>
/// <exception cref="T:System.IndexOutOfRangeException">The index passed was outside the range of 0 through <see cref="P:System.Data.IDataRecord.FieldCount"/>. </exception>
long IDataRecord.GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length) {
throw new NotImplementedException();
}
/// <summary>
/// Gets the character value of the specified column.
/// </summary>
/// <param name="i">The zero-based column ordinal.</param>
/// <returns>
/// The character value of the specified column.
/// </returns>
/// <exception cref="T:System.IndexOutOfRangeException">The index passed was outside the range of 0 through <see cref="P:System.Data.IDataRecord.FieldCount"/>. </exception>
public char GetChar(int i) {
return Convert.ToChar(_CURRENT_ROW[i]);
}
/// <summary>
/// Reads a stream of characters from the specified column offset into the buffer as an array, starting at the given buffer offset.
/// </summary>
/// <param name="i">The zero-based column ordinal.</param>
/// <param name="fieldoffset">The index within the row from which to start the read operation.</param>
/// <param name="buffer">The buffer into which to read the stream of bytes.</param>
/// <param name="bufferoffset">The index for <paramref name="buffer"/> to start the read operation.</param>
/// <param name="length">The number of bytes to read.</param>
/// <returns>The actual number of characters read.</returns>
/// <exception cref="T:System.IndexOutOfRangeException">The index passed was outside the range of 0 through <see cref="P:System.Data.IDataRecord.FieldCount"/>. </exception>
long IDataRecord.GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length) {
throw new NotImplementedException();
}
/// <summary>
/// Returns an <see cref="T:System.Data.IDataReader"/> for the specified column ordinal.
/// </summary>
/// <param name="i">The index of the field to find.</param>
/// <returns>
/// An <see cref="T:System.Data.IDataReader"/>.
/// </returns>
/// <exception cref="T:System.IndexOutOfRangeException">The index passed was outside the range of 0 through <see cref="P:System.Data.IDataRecord.FieldCount"/>. </exception>
public IDataReader GetData(int i) {
return this;
}
/// <summary>
/// Gets the data type information for the specified field.
/// </summary>
/// <param name="i">The index of the field to find.</param>
/// <returns>
/// The data type information for the specified field.
/// </returns>
/// <exception cref="T:System.IndexOutOfRangeException">The index passed was outside the range of 0 through <see cref="P:System.Data.IDataRecord.FieldCount"/>. </exception>
public string GetDataTypeName(int i) {
return _TYPES[i];
}
/// <summary>
/// Gets the date and time data value of the specified field.
/// </summary>
/// <param name="i">The index of the field to find.</param>
/// <returns>
/// The date and time data value of the specified field.
/// </returns>
/// <exception cref="T:System.IndexOutOfRangeException">The index passed was outside the range of 0 through <see cref="P:System.Data.IDataRecord.FieldCount"/>. </exception>
public DateTime GetDateTime(int i) {
return DateTime.Parse(_CURRENT_ROW[i]);
}
/// <summary>
/// Gets the fixed-position numeric value of the specified field.
/// </summary>
/// <param name="i">The index of the field to find.</param>
/// <returns>
/// The fixed-position numeric value of the specified field.
/// </returns>
/// <exception cref="T:System.IndexOutOfRangeException">The index passed was outside the range of 0 through <see cref="P:System.Data.IDataRecord.FieldCount"/>. </exception>
public decimal GetDecimal(int i) {
return decimal.Parse(_CURRENT_ROW[i]);
}
/// <summary>
/// Gets the double-precision floating point number of the specified field.
/// </summary>
/// <param name="i">The index of the field to find.</param>
/// <returns>
/// The double-precision floating point number of the specified field.
/// </returns>
/// <exception cref="T:System.IndexOutOfRangeException">The index passed was outside the range of 0 through <see cref="P:System.Data.IDataRecord.FieldCount"/>. </exception>
public double GetDouble(int i) {
return double.Parse(_CURRENT_ROW[i]);
}
/// <summary>
/// Gets the <see cref="T:System.Type"/> information corresponding to the type of <see cref="T:System.Object"/> that would be returned from <see cref="M:System.Data.IDataRecord.GetValue(System.Int32)"/>.
/// </summary>
/// <param name="i">The index of the field to find.</param>
/// <returns>
/// The <see cref="T:System.Type"/> information corresponding to the type of <see cref="T:System.Object"/> that would be returned from <see cref="M:System.Data.IDataRecord.GetValue(System.Int32)"/>.
/// </returns>
/// <exception cref="T:System.IndexOutOfRangeException">The index passed was outside the range of 0 through <see cref="P:System.Data.IDataRecord.FieldCount"/>. </exception>
Type IDataRecord.GetFieldType(int i) {
throw new NotImplementedException();
}
/// <summary>
/// Gets the single-precision floating point number of the specified field.
/// </summary>
/// <param name="i">The index of the field to find.</param>
/// <returns>
/// The single-precision floating point number of the specified field.
/// </returns>
/// <exception cref="T:System.IndexOutOfRangeException">The index passed was outside the range of 0 through <see cref="P:System.Data.IDataRecord.FieldCount"/>. </exception>
public float GetFloat(int i) {
return float.Parse(_CURRENT_ROW[i]);
}
/// <summary>
/// Returns the GUID value of the specified field.
/// </summary>
/// <param name="i">The index of the field to find.</param>
/// <returns>The GUID value of the specified field.</returns>
/// <exception cref="T:System.IndexOutOfRangeException">The index passed was outside the range of 0 through <see cref="P:System.Data.IDataRecord.FieldCount"/>. </exception>
public Guid GetGuid(int i) {
return Guid.Parse(_CURRENT_ROW[i]);
}
/// <summary>
/// Gets the 16-bit signed integer value of the specified field.
/// </summary>
/// <param name="i">The index of the field to find.</param>
/// <returns>
/// The 16-bit signed integer value of the specified field.
/// </returns>
/// <exception cref="T:System.IndexOutOfRangeException">The index passed was outside the range of 0 through <see cref="P:System.Data.IDataRecord.FieldCount"/>. </exception>
public short GetInt16(int i) {
return short.Parse(_CURRENT_ROW[i]);
}
/// <summary>
/// Gets the 32-bit signed integer value of the specified field.
/// </summary>
/// <param name="i">The index of the field to find.</param>
/// <returns>
/// The 32-bit signed integer value of the specified field.
/// </returns>
/// <exception cref="T:System.IndexOutOfRangeException">The index passed was outside the range of 0 through <see cref="P:System.Data.IDataRecord.FieldCount"/>. </exception>
public int GetInt32(int i) {
return int.Parse(_CURRENT_ROW[i]);
}
/// <summary>
/// Gets the 64-bit signed integer value of the specified field.
/// </summary>
/// <param name="i">The index of the field to find.</param>
/// <returns>
/// The 64-bit signed integer value of the specified field.
/// </returns>
/// <exception cref="T:System.IndexOutOfRangeException">The index passed was outside the range of 0 through <see cref="P:System.Data.IDataRecord.FieldCount"/>. </exception>
public long GetInt64(int i) {
return long.Parse(_CURRENT_ROW[i]);
}
/// <summary>
/// Gets the name for the field to find.
/// </summary>
/// <param name="i">The index of the field to find.</param>
/// <returns>
/// The name of the field or the empty string (""), if there is no value to return.
/// </returns>
/// <exception cref="T:System.IndexOutOfRangeException">The index passed was outside the range of 0 through <see cref="P:System.Data.IDataRecord.FieldCount"/>. </exception>
public string GetName(int i) {
return _COLUMNS[i];
}
/// <summary>
/// Return the index of the named field.
/// </summary>
/// <param name="name">The name of the field to find.</param>
/// <returns>The index of the named field.</returns>
public int GetOrdinal(string name) {
return _COLUMN_INDEX[name];
}
/// <summary>
/// Gets the string value of the specified field.
/// </summary>
/// <param name="i">The index of the field to find.</param>
/// <returns>The string value of the specified field.</returns>
/// <exception cref="T:System.IndexOutOfRangeException">The index passed was outside the range of 0 through <see cref="P:System.Data.IDataRecord.FieldCount"/>. </exception>
public string GetString(int i) {
return _CURRENT_ROW[i];
}
/// <summary>
/// Return the value of the specified field.
/// </summary>
/// <param name="i">The index of the field to find.</param>
/// <returns>
/// The <see cref="T:System.Object"/> which will contain the field value upon return.
/// </returns>
/// <exception cref="T:System.IndexOutOfRangeException">The index passed was outside the range of 0 through <see cref="P:System.Data.IDataRecord.FieldCount"/>. </exception>
public object GetValue(int i) {
return _CURRENT_ROW[i];
}
/// <summary>
/// Populates an array of objects with the column values of the current record.
/// </summary>
/// <param name="values">An array of <see cref="T:System.Object"/> to copy the attribute fields into.</param>
/// <returns>
/// The number of instances of <see cref="T:System.Object"/> in the array.
/// </returns>
public int GetValues(object[] values) {
Array.Copy(_CURRENT_ROW, values, values.Length);
return values.Length;
}
/// <summary>
/// Return whether the specified field is set to null.
/// </summary>
/// <param name="i">The index of the field to find.</param>
/// <returns>
/// true if the specified field is set to null; otherwise, false.
/// </returns>
/// <exception cref="T:System.IndexOutOfRangeException">The index passed was outside the range of 0 through <see cref="P:System.Data.IDataRecord.FieldCount"/>. </exception>
public bool IsDBNull(int i) {
return null == _CURRENT_ROW[i];
}
/// <summary>
/// Gets the <see cref="System.Object"/> with the specified name.
/// </summary>
/// <value></value>
public object this[string name] {
get {
return this[_COLUMN_INDEX[name]];
}
}
/// <summary>
/// Gets the <see cref="System.Object"/> with the specified i.
/// </summary>
/// <value></value>
public object this[int i] {
get {
return _CURRENT_ROW[i];
}
}
#endregion
#region IEnumerable Implementation
/// <summary>
/// Gets the enumerator.
/// </summary>
/// <returns></returns>
public IEnumerator<string[]> GetEnumerator() {
while (!this.IsClosed && this.Read()) {
string[] values = new string[this.FieldCount];
this.GetValues(values);
yield return values;
}
}
/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>
/// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
/// </returns>
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
#endregion
#region Private Methods
/// <summary>
/// Fetches the record.
/// </summary>
/// <param name="strRd">The STR rd.</param>
/// <param name="recordSeparator">The record separator.</param>
/// <returns></returns>
private string FetchRecord(StreamReader strRd, char recordSeparator) {
StringBuilder data = new StringBuilder();
do {
data.Clear();
data.AppendLine(strRd.ReadLine());
} while (!strRd.EndOfStream && recordSeparator != data[data.Length - 1]);
Trim(data, recordSeparator);
return data.ToString();
}
/// <summary>
/// Trims the specified StringBuilder.
/// </summary>
/// <param name="sb">The sb.</param>
private void Trim(StringBuilder sb, char trimChar) {
while (trimChar == sb[sb.Length - 1])
sb.Length -= 1;
}
#endregion
}
/// <summary>
/// Apple EPF Export Mode
/// </summary>
public enum AppleEPFExportMode {
/// <summary>
/// Full Export
/// </summary>
FULL,
/// <summary>
/// Incremental Export
/// </summary>
INCREMENTAL
}