EntitySQLDocumentGenerator.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data.SqlClient;
  6. using System.Xml.Linq;
  7. using System.IO;
  8. namespace Bowin.Common.Linq.Entity
  9. {
  10. public class EntitySQLDocumentGenerator
  11. {
  12. public String ConnectionString { get; set; }
  13. public String InputFileName { get; set; }
  14. public String OutputFileName { get; set; }
  15. private SqlConnection _connection;
  16. public EntitySQLDocumentGenerator(String connectionString, String inputFileName, String outputFileName)
  17. {
  18. this.ConnectionString = connectionString;
  19. this.InputFileName = inputFileName;
  20. this.OutputFileName = outputFileName;
  21. this._connection = new SqlConnection(connectionString);
  22. this._connection.Open();
  23. }
  24. public void Dispose()
  25. {
  26. this._connection.Dispose();
  27. }
  28. private void CreateDocumentation()
  29. {
  30. XDocument doc = XDocument.Load(this.InputFileName);
  31. IEnumerable<XElement> entityTypeElements = doc.Descendants("{http://schemas.microsoft.com/ado/2008/09/edm}EntityType");
  32. int i = 0;
  33. foreach (XElement entityTypeElement in entityTypeElements)
  34. {
  35. String tableName = entityTypeElement.Attribute("Name").Value;
  36. IEnumerable<XElement> propertyElements = entityTypeElement.Descendants("{http://schemas.microsoft.com/ado/2008/09/edm}Property");
  37. this.AddNodeDocumentation(entityTypeElement, GetTableDocumentation(tableName));
  38. foreach (XElement propertyElement in propertyElements)
  39. {
  40. String columnName = propertyElement.Attribute("Name").Value;
  41. this.AddNodeDocumentation(propertyElement, GetColumnDocumentation(tableName, columnName));
  42. }
  43. }
  44. Console.WriteLine("Writing result to {0}", this.OutputFileName);
  45. if (File.Exists(this.OutputFileName))
  46. File.Delete(this.OutputFileName);
  47. doc.Save(this.OutputFileName);
  48. }
  49. private void AddNodeDocumentation(XElement element, String documentation)
  50. {
  51. if (String.IsNullOrEmpty(documentation))
  52. return;
  53. element.Descendants("{http://schemas.microsoft.com/ado/2008/09/edm}Documentation").Remove();
  54. element.AddFirst(new XElement("{http://schemas.microsoft.com/ado/2008/09/edm}Documentation", new XElement("{http://schemas.microsoft.com/ado/2008/09/edm}Summary", documentation)));
  55. }
  56. private String GetTableDocumentation(String tableName)
  57. {
  58. using (SqlCommand command = new SqlCommand(@" SELECT [value]
  59. FROM fn_listextendedproperty (
  60. 'MS_Description',
  61. 'schema', 'dbo',
  62. 'table', @TableName,
  63. null, null)", this._connection))
  64. {
  65. command.Parameters.AddWithValue("TableName", tableName);
  66. return command.ExecuteScalar() as String;
  67. }
  68. }
  69. private String GetColumnDocumentation(String tableName, String columnName)
  70. {
  71. using (SqlCommand command = new SqlCommand(@"SELECT [value]
  72. FROM fn_listextendedproperty (
  73. 'MS_Description',
  74. 'schema', 'dbo',
  75. 'table', @TableName,
  76. 'column', @columnName)", this._connection))
  77. {
  78. command.Parameters.AddWithValue("TableName", tableName);
  79. command.Parameters.AddWithValue("ColumnName", columnName);
  80. return command.ExecuteScalar() as String;
  81. }
  82. }
  83. }
  84. }