PageRenderTime 65ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/src/FluentNHibernate/Mapping/VersionPart.cs

https://github.com/Autobase/fluent-nhibernate
C# | 226 lines | 131 code | 25 blank | 70 comment | 1 complexity | e0d6e622f6ab97d1f38d237ba114ab5c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using FluentNHibernate.Mapping.Providers;
  5. using FluentNHibernate.MappingModel;
  6. using NHibernate.UserTypes;
  7. namespace FluentNHibernate.Mapping
  8. {
  9. public class VersionPart : IVersionMappingProvider
  10. {
  11. private readonly Type entity;
  12. private readonly Member property;
  13. private readonly AccessStrategyBuilder<VersionPart> access;
  14. private readonly VersionGeneratedBuilder<VersionPart> generated;
  15. private readonly AttributeStore<VersionMapping> attributes = new AttributeStore<VersionMapping>();
  16. private readonly AttributeStore<ColumnMapping> columnAttributes = new AttributeStore<ColumnMapping>();
  17. private readonly List<string> columns = new List<string>();
  18. private bool nextBool = true;
  19. public VersionPart(Type entity, Member property)
  20. {
  21. this.entity = entity;
  22. this.property = property;
  23. access = new AccessStrategyBuilder<VersionPart>(this, value => attributes.Set(x => x.Access, value));
  24. generated = new VersionGeneratedBuilder<VersionPart>(this, value => attributes.Set(x => x.Generated, value));
  25. }
  26. /// <summary>
  27. /// Specify if this version is database generated
  28. /// </summary>
  29. public VersionGeneratedBuilder<VersionPart> Generated
  30. {
  31. get { return generated; }
  32. }
  33. /// <summary>
  34. /// Specify the access strategy
  35. /// </summary>
  36. public AccessStrategyBuilder<VersionPart> Access
  37. {
  38. get { return access; }
  39. }
  40. /// <summary>
  41. /// Invert the next boolean operation
  42. /// </summary>
  43. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  44. public VersionPart Not
  45. {
  46. get
  47. {
  48. nextBool = !nextBool;
  49. return this;
  50. }
  51. }
  52. /// <summary>
  53. /// Specify the column name
  54. /// </summary>
  55. /// <param name="name">Column name</param>
  56. public VersionPart Column(string name)
  57. {
  58. columns.Add(name);
  59. return this;
  60. }
  61. /// <summary>
  62. /// Specify the unsaved value
  63. /// </summary>
  64. /// <param name="value">Unsaved value</param>
  65. public VersionPart UnsavedValue(string value)
  66. {
  67. attributes.Set(x => x.UnsavedValue, value);
  68. return this;
  69. }
  70. /// <summary>
  71. /// Specify the column length
  72. /// </summary>
  73. /// <param name="length">Column length</param>
  74. public VersionPart Length(int length)
  75. {
  76. columnAttributes.Set(x => x.Length, length);
  77. return this;
  78. }
  79. /// <summary>
  80. /// Specify decimal precision
  81. /// </summary>
  82. /// <param name="precision">Decimal precision</param>
  83. public VersionPart Precision(int precision)
  84. {
  85. columnAttributes.Set(x => x.Precision, precision);
  86. return this;
  87. }
  88. /// <summary>
  89. /// Specify decimal scale
  90. /// </summary>
  91. /// <param name="scale">Decimal scale</param>
  92. public VersionPart Scale(int scale)
  93. {
  94. columnAttributes.Set(x => x.Scale, scale);
  95. return this;
  96. }
  97. /// <summary>
  98. /// Specify the nullability of the column
  99. /// </summary>
  100. public VersionPart Nullable()
  101. {
  102. columnAttributes.Set(x => x.NotNull, !nextBool);
  103. nextBool = true;
  104. return this;
  105. }
  106. /// <summary>
  107. /// Specify the uniqueness of the column
  108. /// </summary>
  109. public VersionPart Unique()
  110. {
  111. columnAttributes.Set(x => x.Unique, nextBool);
  112. nextBool = true;
  113. return this;
  114. }
  115. /// <summary>
  116. /// Specify a unique key constraint
  117. /// </summary>
  118. /// <param name="keyColumns">Constraint columns</param>
  119. public VersionPart UniqueKey(string keyColumns)
  120. {
  121. columnAttributes.Set(x => x.UniqueKey, keyColumns);
  122. return this;
  123. }
  124. /// <summary>
  125. /// Specify an index name
  126. /// </summary>
  127. /// <param name="index">Index name</param>
  128. public VersionPart Index(string index)
  129. {
  130. columnAttributes.Set(x => x.Index, index);
  131. return this;
  132. }
  133. /// <summary>
  134. /// Specify a check constraint
  135. /// </summary>
  136. /// <param name="constraint">Constraint name</param>
  137. public VersionPart Check(string constraint)
  138. {
  139. columnAttributes.Set(x => x.Check, constraint);
  140. return this;
  141. }
  142. /// <summary>
  143. /// Specify a default value
  144. /// </summary>
  145. /// <param name="value">Default value</param>
  146. public VersionPart Default(object value)
  147. {
  148. columnAttributes.Set(x => x.Default, value.ToString());
  149. return this;
  150. }
  151. /// <summary>
  152. /// Specify a custom type
  153. /// </summary>
  154. /// <remarks>Usually used with an <see cref="IUserType"/></remarks>
  155. /// <typeparam name="T">Custom type</typeparam>
  156. public VersionPart CustomType<T>()
  157. {
  158. attributes.Set(x => x.Type, new TypeReference(typeof(T)));
  159. return this;
  160. }
  161. /// <summary>
  162. /// Specify a custom type
  163. /// </summary>
  164. /// <remarks>Usually used with an <see cref="IUserType"/></remarks>
  165. /// <param name="type">Custom type</param>
  166. public VersionPart CustomType(Type type)
  167. {
  168. attributes.Set(x => x.Type, new TypeReference(type));
  169. return this;
  170. }
  171. /// <summary>
  172. /// Specify a custom type
  173. /// </summary>
  174. /// <remarks>Usually used with an <see cref="IUserType"/></remarks>
  175. /// <param name="type">Custom type</param>
  176. public VersionPart CustomType(string type)
  177. {
  178. attributes.Set(x => x.Type, new TypeReference(type));
  179. return this;
  180. }
  181. /// <summary>
  182. /// Specify a custom SQL type
  183. /// </summary>
  184. /// <param name="sqlType">SQL type</param>
  185. public VersionPart CustomSqlType(string sqlType)
  186. {
  187. columnAttributes.Set(x => x.SqlType, sqlType);
  188. return this;
  189. }
  190. VersionMapping IVersionMappingProvider.GetVersionMapping()
  191. {
  192. var mapping = new VersionMapping(attributes.CloneInner());
  193. mapping.ContainingEntityType = entity;
  194. mapping.SetDefaultValue("Name", property.Name);
  195. mapping.SetDefaultValue("Type", property.PropertyType == typeof(DateTime) ? new TypeReference("timestamp") : new TypeReference(property.PropertyType));
  196. mapping.AddDefaultColumn(new ColumnMapping(columnAttributes.CloneInner()) { Name = property.Name });
  197. columns.ForEach(column => mapping.AddColumn(new ColumnMapping(columnAttributes.CloneInner()) { Name = column }));
  198. return mapping;
  199. }
  200. }
  201. }