PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Sources/CsvReader.Azure/AzureStreamingTable.cs

https://github.com/tpwalke2/DataTable
C# | 105 lines | 82 code | 17 blank | 6 comment | 4 complexity | 8ccc9ee2d49838254b0519153d434c98 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.WindowsAzure;
  6. using Microsoft.WindowsAzure.StorageClient;
  7. using System.IO;
  8. using System.Diagnostics;
  9. namespace DataAccess
  10. {
  11. // Used for reading from an Azure Table.
  12. // Determine columns based on first row.
  13. internal class AzureStreamingTable : DataTable
  14. {
  15. public CloudTableClient _tableClient;
  16. // This is different than the Name property, because Name is just a hint and can be redefined by the user.
  17. // Whereas this property really is the azure table name.
  18. public string _tableName;
  19. public string[] _columnNames;
  20. public override IEnumerable<string> ColumnNames
  21. {
  22. get
  23. {
  24. InitColumnNames();
  25. return _columnNames;
  26. }
  27. }
  28. private void InitColumnNames()
  29. {
  30. if (_columnNames == null)
  31. {
  32. TableServiceContext ctx = _tableClient.GetDataServiceContext();
  33. ctx.IgnoreMissingProperties = true;
  34. ctx.ReadingEntity += GenericTableReader.OnReadingEntity;
  35. var x = from o in ctx.CreateQuery<GenericEntity>(_tableName) select o;
  36. GenericEntity all = x.First();
  37. List<string> props = new List<string>();
  38. props.Add("PartitionKey");
  39. props.Add("RowKey");
  40. props.AddRange(all.properties.Keys);
  41. _columnNames = props.ToArray();
  42. }
  43. }
  44. public override IEnumerable<Row> Rows
  45. {
  46. get
  47. {
  48. InitColumnNames();
  49. TableServiceContext ctx = _tableClient.GetDataServiceContext();
  50. ctx.IgnoreMissingProperties = true;
  51. ctx.ReadingEntity += GenericTableReader.OnReadingEntity;
  52. var x = from o in ctx.CreateQuery<GenericEntity>(_tableName) select o;
  53. // Convert GenericEntity to Row
  54. foreach (GenericEntity entity in x)
  55. {
  56. string[] values = Array.ConvertAll(_columnNames,
  57. columnName =>
  58. {
  59. string result;
  60. entity.properties.TryGetValue(columnName, out result);
  61. if (result != null)
  62. {
  63. return result;
  64. }
  65. return string.Empty;
  66. });
  67. values[0] = entity.PartitionKey;
  68. values[1] = entity.RowKey;
  69. yield return new AzureRow { _parent = this, _values = values };
  70. }
  71. }
  72. }
  73. }
  74. // Rows must be in the same order
  75. internal class AzureRow : Row
  76. {
  77. public AzureStreamingTable _parent;
  78. public string[] _values;
  79. public override IEnumerable<string> ColumnNames
  80. {
  81. get { return _parent._columnNames; }
  82. }
  83. public override IList<string> Values
  84. {
  85. get { return _values; }
  86. }
  87. }
  88. }