PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NHibernate.Tool.hbm2net/src/NHibernate.Tool.Db2hbm/MappingGenerator.cs

https://bitbucket.org/dabide/nhcontrib
C# | 172 lines | 162 code | 10 blank | 0 comment | 21 complexity | 8aa701771d5b21444905fbe13c7ac4d3 MD5 | raw file
Possible License(s): BSD-3-Clause, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, Apache-2.0, LGPL-3.0, LGPL-2.1
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6. using System.Xml.Serialization;
  7. using log4net;
  8. using System.Reflection;
  9. using NHibernate.Connection;
  10. using NHibernate.Driver;
  11. using System.Data;
  12. using System.Data.Common;
  13. using System.IO;
  14. using cfg;
  15. namespace NHibernate.Tool.Db2hbm
  16. {
  17. public class MappingGenerator
  18. {
  19. private static readonly ILog log = LogManager.GetLogger("db2hbm");
  20. db2hbmconf cfg;
  21. IList<IMetadataStrategy> metaStrategies;
  22. public MappingGenerator()
  23. {
  24. metaStrategies = new List<IMetadataStrategy>();
  25. }
  26. public void Configure(XmlReader reader)
  27. {
  28. try
  29. {
  30. XmlDocument cfgdoc = new XmlDocument();
  31. cfgdoc.Load(reader);
  32. Validate(cfgdoc);
  33. XmlSerializer ser = new XmlSerializer(typeof(db2hbmconf));
  34. this.cfg = ser.Deserialize(XmlReader.Create(new StringReader(cfgdoc.InnerXml))) as db2hbmconf;
  35. PrepareServices();
  36. ConfigureMetaStrategies();
  37. RegisterForeignKeyCrawlers();
  38. TableEnumerator.Configuration = cfg;
  39. }
  40. catch (Exception e)
  41. {
  42. log.Error("Fatal error during configuration", e);
  43. }
  44. }
  45. private void RegisterForeignKeyCrawlers()
  46. {
  47. foreach (var v in cfg.foreignkeycrawlers)
  48. {
  49. System.Type t = System.Type.GetType(v);
  50. if (null == t)
  51. throw new Exception("Can't find ForeignKeyCrawlerFactory:" + v);
  52. var factory = Activator.CreateInstance(t) as IForeignKeyCrawlerFactory;
  53. if (null != factory)
  54. factory.Register();
  55. else
  56. throw new Exception("Type:" + t.Name + " cannot be created, or does not implement IForeignKeyCrawlerFactory");
  57. }
  58. }
  59. private void PrepareServices()
  60. {
  61. TypeFactory.RegisterServiceInstance(new TypeConverter(this.cfg));
  62. }
  63. private void ConfigureMetaStrategies()
  64. {
  65. foreach (var v in cfg.metadatastrategies)
  66. {
  67. metaStrategies.Add(TypeFactory.Create<IMetadataStrategy>(v.@class));
  68. }
  69. }
  70. public void Generate(IStreamProvider streamProvider)
  71. {
  72. GenerationContext ctx = new GenerationContext();
  73. ctx.Dialect = GetDialect();
  74. ctx.Model = new MappingModelImpl();
  75. IDriver driver = GetDriver();
  76. try
  77. {
  78. using (IDbConnection connection = driver.CreateConnection())
  79. {
  80. DbConnection dbConn = connection as DbConnection;
  81. if (null == dbConn)
  82. throw new Exception("Can't convert connection provided by driver to DbConnection");
  83. connection.ConnectionString = cfg.connectioninfo.connectionstring;
  84. connection.Open();
  85. ctx.Schema = ctx.Dialect.GetDataBaseSchema(dbConn);
  86. ctx.Configuration = cfg;
  87. ctx.Connection = dbConn;
  88. ctx.TableExceptions = new TableExceptions(cfg);
  89. ctx.NamingStrategy = TypeFactory.Create<INamingStrategy>(cfg.namingstrategy);
  90. log.Info("Retrieving working table list");
  91. ctx.FilteredTables.AddRange(TableEnumerator.GetInstance(ctx.Schema));
  92. foreach (IMetadataStrategy strategy in metaStrategies)
  93. strategy.Process(ctx);
  94. }
  95. foreach (var clazz in ctx.Model.GetEntities())
  96. {
  97. TextWriter target = streamProvider.GetTextWriter(clazz.name);
  98. hibernatemapping mapping = new hibernatemapping();
  99. mapping.Items = new object[] { clazz };
  100. if (!string.IsNullOrEmpty(cfg.entitiesnamespace))
  101. mapping.@namespace = cfg.entitiesnamespace;
  102. if (!string.IsNullOrEmpty(cfg.entitiesassembly))
  103. mapping.assembly = cfg.entitiesassembly;
  104. XmlSerializer ser = new XmlSerializer(typeof(hibernatemapping));
  105. XmlSerializerNamespaces empty = new XmlSerializerNamespaces();
  106. empty.Add(string.Empty, "urn:nhibernate-mapping-2.2");
  107. ser.Serialize(target, mapping,empty);
  108. target.Flush();
  109. target.Close();
  110. streamProvider.EndWrite();
  111. }
  112. }
  113. catch (Exception e)
  114. {
  115. throw e;
  116. }
  117. }
  118. private IDriver GetDriver()
  119. {
  120. if (string.IsNullOrEmpty(cfg.connectioninfo.connectiondriver))
  121. {
  122. throw new Exception("Connection driver must be specified");
  123. }
  124. System.Type tDriver = System.Type.GetType(cfg.connectioninfo.connectiondriver);
  125. if (null == tDriver)
  126. throw new Exception("Cannot create driver:" + cfg.connectioninfo.connectiondriver);
  127. if (!typeof(IDriver).IsAssignableFrom(tDriver))
  128. throw new Exception("Driver:" + cfg.connectioninfo.connectiondriver + " is not a valid driver.");
  129. IDriver driver = Activator.CreateInstance(tDriver) as IDriver;
  130. if (null == driver)
  131. {
  132. throw new Exception("Cannot instantiate:" + cfg.connectioninfo.connectiondriver+ " driver");
  133. }
  134. return driver;
  135. }
  136. private NHibernate.Dialect.Dialect GetDialect()
  137. {
  138. if (string.IsNullOrEmpty ( cfg.connectioninfo.dialect))
  139. {
  140. throw new Exception("Dialect must be specified");
  141. }
  142. System.Type tDialect = System.Type.GetType(cfg.connectioninfo.dialect);
  143. if (null == tDialect)
  144. throw new Exception("Cannot create dialect:" + cfg.connectioninfo.dialect);
  145. if( !typeof(NHibernate.Dialect.Dialect).IsAssignableFrom(tDialect) )
  146. throw new Exception("Dialect:" + cfg.connectioninfo.dialect+" is not a valid dialect.");
  147. NHibernate.Dialect.Dialect dialect = Activator.CreateInstance(tDialect) as NHibernate.Dialect.Dialect;
  148. if (null == dialect)
  149. {
  150. throw new Exception("Cannot instantiate:" + cfg.connectioninfo.dialect+" dialect");
  151. }
  152. return dialect;
  153. }
  154. private void Validate(XmlDocument cfg)
  155. {
  156. var cv = new ConfigurationValidator();
  157. cv.Validate(cfg);
  158. if( !string.IsNullOrEmpty(cv.WarningMessage) )
  159. log.Warn(cv.WarningMessage);
  160. }
  161. }
  162. }