PageRenderTime 34ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/source/library/Interlace/Utilities/DataSetPopulator.cs

https://bitbucket.org/VahidN/interlace
C# | 141 lines | 88 code | 28 blank | 25 comment | 13 complexity | 23b9c2a2e9237a640a8a13eb4c659199 MD5 | raw file
  1. #region Using Directives and Copyright Notice
  2. // Copyright (c) 2010, Bit Plantation
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above copyright
  10. // notice, this list of conditions and the following disclaimer in the
  11. // documentation and/or other materials provided with the distribution.
  12. // * Neither the name of the Bit Plantation nor the
  13. // names of its contributors may be used to endorse or promote products
  14. // derived from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. // ARE DISCLAIMED. IN NO EVENT SHALL COMPUTER CONSULTANCY PTY LTD BE LIABLE
  20. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  26. // DAMAGE.
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.ComponentModel;
  31. using System.Data;
  32. using System.IO;
  33. using System.Runtime.Serialization;
  34. using System.Text;
  35. using Interlace.Collections;
  36. #endregion
  37. namespace Interlace.Utilities
  38. {
  39. public class DataSetPopulator
  40. {
  41. string _xmlSchema;
  42. ObjectIDGenerator _generator;
  43. DataSet _dataSet;
  44. public DataSetPopulator(string xmlSchema)
  45. {
  46. _xmlSchema = xmlSchema;
  47. _generator = new ObjectIDGenerator();
  48. _dataSet = new DataSet();
  49. _dataSet.ReadXmlSchema(new StringReader(_xmlSchema));
  50. }
  51. public DataSet DataSet
  52. {
  53. get { return _dataSet; }
  54. }
  55. public void Add(string rootTableName, IList rootList)
  56. {
  57. DataTable rootTable = _dataSet.Tables[rootTableName];
  58. AddToTable(_dataSet, rootTable, rootList, null);
  59. }
  60. void AddToTable(DataSet dataSet, DataTable table, IList values, object parentValue)
  61. {
  62. if (values.Count == 0) return;
  63. object prototype = values[0];
  64. PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(prototype);
  65. foreach (object value in values)
  66. {
  67. AddValueToTable(table, properties, value, parentValue);
  68. }
  69. foreach (DataRelation relation in table.ChildRelations)
  70. {
  71. foreach (object value in values)
  72. {
  73. object valueListObject = properties[relation.RelationName].GetValue(value);
  74. if (valueListObject == null) continue;
  75. if (valueListObject is IList)
  76. {
  77. IList valueList = (IList)valueListObject;
  78. AddToTable(dataSet, relation.ChildTable, valueList, value);
  79. }
  80. else
  81. {
  82. AddToTable(dataSet, relation.ChildTable, new object[] { valueListObject }, value);
  83. }
  84. }
  85. }
  86. }
  87. static readonly Set<string> _internalColumnNames = new Set<string>("__RelationId", "__RelationParentId", "__This");
  88. void AddValueToTable(DataTable table, PropertyDescriptorCollection properties, object value, object parentValue)
  89. {
  90. DataRow row = table.Rows.Add();
  91. foreach (DataColumn column in table.Columns)
  92. {
  93. if (_internalColumnNames.Contains(column.ColumnName)) continue;
  94. object cell = properties[column.ColumnName].GetValue(value);
  95. if (cell == null) cell = DBNull.Value;
  96. row[column] = cell;
  97. }
  98. bool firstTime;
  99. if (table.Columns.Contains("__RelationId"))
  100. {
  101. row["__RelationId"] = _generator.GetId(value, out firstTime);
  102. }
  103. if (parentValue != null && table.Columns.Contains("__RelationParentId"))
  104. {
  105. row["__RelationParentId"] = _generator.GetId(parentValue, out firstTime);
  106. }
  107. if (table.Columns.Contains("__This"))
  108. {
  109. row["__This"] = value;
  110. }
  111. }
  112. }
  113. }