Guest User

Untitled

a guest
Jan 10th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 KB | None | 0 0
  1. mapping.References(x => x.SomeReference).SetAttribute("index", "IX_index");
  2. mapping.Map(x => x.SomeField).SetAttribute("index", "IX_index");
  3.  
  4. create index IX_index on ApplicantProgramDatas (SomeField, SomeReferenceId)
  5.  
  6. create index IX_index on ApplicantProgramDatas (SomeReferenceId, SomeField)
  7.  
  8. <hibernate-mapping xmlns="urn:nhiernate-mapping-2.2">
  9. <database-object>
  10. <create>VALID SQL</create>
  11. <drop>VALID SQL</create>
  12. </database-object>
  13. </hibernate-mapping>
  14.  
  15. namespace NHibernate.Mapping {
  16. public interface IAuxiliaryDatabaseObject : IRelationalModel {
  17. void AddDialectScope(string dialectName);
  18. bool AppliesToDialect(Dialect dialect);
  19. void SetParameterValues(IDictionary<string, string> parameters);
  20. }
  21. public interface IRelationalModel {
  22. string SqlCreateString(Dialect dialect, IMapping p, string defaultCatalog, string defaultSchema);
  23. string SqlDropString(Dialect dialect, string defaultCatalog, string defaultSchema);
  24. }
  25. }
  26.  
  27. var sqlCreate = "CREATION SCRIPT";
  28. var sqlDrop = "DROP SCRIPT";
  29. cfg.AddAuxiliaryDatabaseObject(new SimpleAuxiliaryDatabaseObject(sqlCreate, sqlDrop));
  30.  
  31. foreach (var collection in Collections)
  32. visitor.Visit(collection);
  33.  
  34. foreach (var property in Properties)
  35. visitor.Visit(property);
  36.  
  37. foreach (var reference in References)
  38. visitor.Visit(reference);
  39.  
  40. using System;
  41. using System.Collections.Generic;
  42. using System.Diagnostics;
  43. using System.Reflection;
  44. using System.Text;
  45. using FluentNHibernate.Cfg;
  46. using FluentNHibernate.Cfg.Db;
  47. using FluentNHibernate.Mapping;
  48. using NHibernate.Cfg;
  49. using NHibernate.Tool.hbm2ddl;
  50.  
  51. namespace FluentNHib
  52. {
  53.  
  54. public class Master
  55. {
  56. public int Id { get; set; }
  57. }
  58.  
  59. public class Child
  60. {
  61. public int Id { get; set; }
  62. [MCIndex("A", 0)]
  63. public Master Master { get; set; }
  64. [MCIndex("A", 1)]
  65. public string Name { get; set; }
  66. }
  67.  
  68. public class MCIndexAttribute : Attribute
  69. {
  70. public string indexName;
  71. public int indexOrder;
  72.  
  73. public MCIndexAttribute(string indexName, int i)
  74. {
  75. this.indexName = indexName;
  76. this.indexOrder = i;
  77. }
  78. }
  79.  
  80. public class MasterMap : ClassMap
  81. {
  82. public MasterMap()
  83. {
  84. Id(x => x.Id);
  85. }
  86. }
  87.  
  88. public class ChildMap : ClassMap
  89. {
  90. public ChildMap()
  91. {
  92. Id(x => x.Id);
  93. References(x => x.Master).Index("A");
  94. Map(x => x.Name).Index("A");
  95.  
  96. }
  97. }
  98.  
  99. class MySchemaExport : SchemaExport
  100. {
  101. internal struct MCIndexField
  102. {
  103. internal int index;
  104. internal string Name;
  105.  
  106. }
  107.  
  108. internal class MCIndex
  109. {
  110. internal string IndexName;
  111. public readonly IList fields = new List();
  112. public string Table;
  113.  
  114. public void AddField(string name, int indexOrder)
  115. {
  116. fields.Add(new MCIndexField {index = indexOrder, Name = name});
  117. }
  118. }
  119.  
  120. private readonly Dictionary indexes = new Dictionary();
  121.  
  122. MCIndex ByName(string name, string table)
  123. {
  124. MCIndex result;
  125. if (!indexes.TryGetValue(name, out result))
  126. {
  127. result = new MCIndex
  128. {
  129. IndexName = name
  130. };
  131. indexes.Add(name, result);
  132. }
  133. return result;
  134. }
  135.  
  136. public MySchemaExport(Configuration cfg) : base(cfg)
  137. {
  138. foreach (var type in typeof(ChildMap).Assembly.GetTypes())
  139. {
  140. foreach (var prop in type.GetProperties())
  141. {
  142. var attr = prop.GetCustomAttributes(typeof (MCIndexAttribute), true);
  143. if (attr.Length == 1)
  144. {
  145. var attribute = (MCIndexAttribute) attr[0];
  146. ByName(attribute.indexName, type.Name).AddField(prop.Name, attribute.indexOrder);
  147. }
  148. }
  149. }
  150.  
  151.  
  152. var createSqlProp = typeof(SchemaExport).GetField("createSQL", BindingFlags.NonPublic | BindingFlags.Instance);
  153. var wasSql = createSqlProp.GetValue(this);
  154.  
  155. var sb = new StringBuilder();
  156. sb.AppendLine("");
  157. foreach (var mcIndex in indexes)
  158. {
  159. sb.AppendLine(string.Format("create index {0} on {1} ({2})", mcIndex.Value.IndexName, mcIndex.Value.Table, mcIndex.Value.fields));
  160. }
  161. createSqlProp.SetValue(this, wasSql + sb.ToString());
  162. }
  163. }
  164.  
  165. class Program
  166. {
  167.  
  168. private static void BuildSchema(Configuration config)
  169. {
  170. new MySchemaExport(config)
  171. .Create(s =>
  172. {
  173. Debug.WriteLine(s);
  174. }, true);
  175. }
  176.  
  177. const string fileName = "c:\temp\temp.fdb";
  178.  
  179. private static string GetConnectionString()
  180. {
  181. const string userName = "sysdba";
  182. const string password = "masterkey";
  183. return String.Format("ServerType=1;User={0};Password={1};Dialect=3;Database={2}", userName, password, fileName);
  184. }
  185.  
  186. private static FluentConfiguration Configurate()
  187. {
  188. var fbc = new FirebirdConfiguration();
  189. return Fluently.Configure()
  190. .Database(fbc.ShowSql().ConnectionString(GetConnectionString()))
  191. .Mappings(m => m.FluentMappings
  192. .AddFromAssemblyOf()
  193. )
  194. .ExposeConfiguration(BuildSchema);
  195. }
  196.  
  197. static void Main(string[] args)
  198. {
  199. FluentConfiguration fluentConfiguration = Configurate();
  200.  
  201. Configuration cfg = fluentConfiguration.BuildConfiguration();
  202. }
  203. }
  204. }
Add Comment
Please, Sign In to add comment