PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/Sources/DataAccess/TableWriter.cs

https://github.com/tpwalke2/DataTable
C# | 65 lines | 47 code | 9 blank | 9 comment | 2 complexity | b0fd27dcdd7c482bf0749acfb49f7339 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Diagnostics;
  6. using System.Text.RegularExpressions;
  7. using System.Linq;
  8. namespace DataAccess
  9. {
  10. // Easy for constructing a table in-memory by adding rows.
  11. // Useful when a table is a filter over another table (such as a where-clause or sample)
  12. // Optimized to avoid parsing and string operations.
  13. internal class TableWriter
  14. {
  15. private ViewTable _table;
  16. public TableWriter(DataTable source)
  17. {
  18. _table = new ViewTable();
  19. _table._ColumnNames = source.ColumnNames.ToArray(); // copy in case source is mutable
  20. }
  21. public void AddRow(Row row)
  22. {
  23. if (_table == null)
  24. {
  25. throw new InvalidOperationException("Can't add rows after table is finished construction");
  26. }
  27. // $$$ Verify row is in source, mainly so that we know columns match
  28. // Since Rows have backpointer to table, must clone the row.
  29. // But we can keep the same data.
  30. Row newRow = new RowFromStreamingTable(row.Values, _table);
  31. _table._rows.Add(newRow);
  32. }
  33. // Get the table we've built up.
  34. public DataTable CloseAndGetTable()
  35. {
  36. DataTable result = _table;
  37. _table = null; // mark as closed, prevents future rows.
  38. return result;
  39. }
  40. // Expose filter results as a datatable.
  41. // This is still a streaming version. Caller can make this mutable if they want.
  42. private class ViewTable : DataTable
  43. {
  44. public readonly List<Row> _rows = new List<Row>();
  45. public IEnumerable<string> _ColumnNames;
  46. public override IEnumerable<string> ColumnNames
  47. {
  48. get { return _ColumnNames; }
  49. }
  50. public override IEnumerable<Row> Rows
  51. {
  52. get { return _rows; }
  53. }
  54. }
  55. }
  56. }