PageRenderTime 53ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/src/FluentNHibernate/Mapping/VersionPart.cs

https://github.com/dotnetchris/fluent-nhibernate
C# | 156 lines | 131 code | 25 blank | 0 comment | 1 complexity | c29dfc5bb169b196cbc41274c40ab1c2 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Reflection;
  5. using FluentNHibernate.Mapping.Providers;
  6. using FluentNHibernate.MappingModel;
  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<IVersionMappingProvider> 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<IVersionMappingProvider>(this, value => attributes.Set(x => x.Generated, value));
  25. }
  26. VersionMapping IVersionMappingProvider.GetVersionMapping()
  27. {
  28. var mapping = new VersionMapping(attributes.CloneInner());
  29. mapping.ContainingEntityType = entity;
  30. mapping.SetDefaultValue("Name", property.Name);
  31. mapping.SetDefaultValue("Type", property.PropertyType == typeof(DateTime) ? new TypeReference("timestamp") : new TypeReference(property.PropertyType));
  32. mapping.AddDefaultColumn(new ColumnMapping(columnAttributes.CloneInner()) { Name = property.Name });
  33. columns.ForEach(column => mapping.AddColumn(new ColumnMapping(columnAttributes.CloneInner()) { Name = column }));
  34. return mapping;
  35. }
  36. public VersionGeneratedBuilder<IVersionMappingProvider> Generated
  37. {
  38. get { return generated; }
  39. }
  40. public AccessStrategyBuilder<VersionPart> Access
  41. {
  42. get { return access; }
  43. }
  44. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  45. public VersionPart Not
  46. {
  47. get
  48. {
  49. nextBool = !nextBool;
  50. return this;
  51. }
  52. }
  53. public VersionPart Column(string name)
  54. {
  55. columns.Add(name);
  56. return this;
  57. }
  58. public VersionPart UnsavedValue(string value)
  59. {
  60. attributes.Set(x => x.UnsavedValue, value);
  61. return this;
  62. }
  63. public VersionPart Length(int length)
  64. {
  65. columnAttributes.Set(x => x.Length, length);
  66. return this;
  67. }
  68. public VersionPart Precision(int precision)
  69. {
  70. columnAttributes.Set(x => x.Precision, precision);
  71. return this;
  72. }
  73. public VersionPart Scale(int scale)
  74. {
  75. columnAttributes.Set(x => x.Scale, scale);
  76. return this;
  77. }
  78. public VersionPart Nullable()
  79. {
  80. columnAttributes.Set(x => x.NotNull, !nextBool);
  81. nextBool = true;
  82. return this;
  83. }
  84. public VersionPart Unique()
  85. {
  86. columnAttributes.Set(x => x.Unique, nextBool);
  87. nextBool = true;
  88. return this;
  89. }
  90. public VersionPart UniqueKey(string keyColumns)
  91. {
  92. columnAttributes.Set(x => x.UniqueKey, keyColumns);
  93. return this;
  94. }
  95. public VersionPart Index(string index)
  96. {
  97. columnAttributes.Set(x => x.Index, index);
  98. return this;
  99. }
  100. public VersionPart Check(string constraint)
  101. {
  102. columnAttributes.Set(x => x.Check, constraint);
  103. return this;
  104. }
  105. public VersionPart Default(object value)
  106. {
  107. columnAttributes.Set(x => x.Default, value.ToString());
  108. return this;
  109. }
  110. public VersionPart CustomType<T>()
  111. {
  112. attributes.Set(x => x.Type, new TypeReference(typeof(T)));
  113. return this;
  114. }
  115. public VersionPart CustomType(Type type)
  116. {
  117. attributes.Set(x => x.Type, new TypeReference(type));
  118. return this;
  119. }
  120. public VersionPart CustomType(string type)
  121. {
  122. attributes.Set(x => x.Type, new TypeReference(type));
  123. return this;
  124. }
  125. public VersionPart CustomSqlType(string sqlType)
  126. {
  127. columnAttributes.Set(x => x.SqlType, sqlType);
  128. return this;
  129. }
  130. }
  131. }