You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

263 lines
11 KiB

4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. namespace Apewer.Source
  5. {
  6. /// <summary>数据库中的列,类型默认为 NVarChar(191),错误类型将修正为默认类型。</summary>
  7. /// <remarks>注意:当一个数据模型中存在多个相同的 Field 时,将只有第一个被保留。</remarks>
  8. [Serializable]
  9. [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
  10. public sealed class ColumnAttribute : Attribute, IToJson
  11. {
  12. private PropertyInfo _property = null;
  13. internal string PropertyName = null;
  14. private string _field = null;
  15. private int _length = 0;
  16. private ColumnType _type;
  17. private bool _incremental = false;
  18. private bool _primarykey = false;
  19. private bool _noupdate = false;
  20. private bool _independent = false;
  21. private bool _valid = true;
  22. private bool _nullable = false;
  23. private List<IndexAttribute> _indexes = new List<IndexAttribute>();
  24. private void Init(string field, ColumnType type, int length)
  25. {
  26. _field = field;
  27. _type = type;
  28. switch (type)
  29. {
  30. case ColumnType.VarChar:
  31. case ColumnType.NVarChar:
  32. _length = length < 1 ? 191 : length;
  33. break;
  34. case ColumnType.VarChar191:
  35. case ColumnType.NVarChar191:
  36. _length = 191;
  37. break;
  38. default:
  39. _length = length;
  40. break;
  41. }
  42. }
  43. /// <summary>使用自动的列名称。当类型为 VarChar 或 NVarChar 时必须指定长度。</summary>
  44. /// <exception cref="System.ArgumentOutOfRangeException"></exception>
  45. public ColumnAttribute(ColumnType type = ColumnType.NVarChar191, int length = 191) => Init(null, type, length);
  46. /// <summary>使用指定的列名称。当类型为 VarChar 或 NVarChar 时必须指定长度。</summary>
  47. /// <exception cref="System.ArgumentOutOfRangeException"></exception>
  48. public ColumnAttribute(string field, ColumnType type = ColumnType.NVarChar191, int length = 191) => Init(field, type, length);
  49. /// <summary>字段名。</summary>
  50. public string Field { get => _field; }
  51. /// <summary>指定字段的最大长度。</summary>
  52. public int Length { get => _length; }
  53. /// <summary>字段类型。</summary>
  54. public ColumnType Type { get => _type; }
  55. /// <summary>字段上的索引。</summary>
  56. public IndexAttribute[] Indexes { get => _indexes.ToArray(); }
  57. #region 附加
  58. /// <summary>此特性有效。</summary>
  59. public bool Valid { get => _valid; }
  60. /// <summary>此列不被管理,Insert、Update 和 Create Table 将忽略此列。</summary>
  61. public bool Independent { get => _independent; }
  62. /// <summary>使用此特性的属性。</summary>
  63. public PropertyInfo Property { get => _property; }
  64. /// <summary>属性对应的字段是主键。</summary>
  65. public bool PrimaryKey { get => _primarykey; }
  66. /// <summary>属性对应的字段是自动增长的整数。</summary>
  67. public bool Incremental { get => _incremental; }
  68. /// <summary>属性可为 NULL 值。</summary>
  69. public bool Nullable { get => _nullable; }
  70. /// <summary>在执行 ORM 的 Update 方法时,不包含此属性。</summary>
  71. public bool NoUpdate { get => _noupdate; }
  72. #endregion
  73. /// <summary>生成 Json 对象。</summary>
  74. public Json ToJson()
  75. {
  76. var json = Json.NewObject();
  77. json.SetProperty("field", _field);
  78. json.SetProperty("type", _type.ToString());
  79. json.SetProperty("length", _length);
  80. if (_property != null)
  81. {
  82. var property = Json.NewObject();
  83. property.SetProperty("name", _property.Name);
  84. property.SetProperty("type", _property.PropertyType.FullName);
  85. json.SetProperty("property", property);
  86. json.SetProperty("nullable", _nullable);
  87. json.SetProperty("valid", _valid);
  88. json.SetProperty("independent", _independent);
  89. json.SetProperty("primarykey", _primarykey);
  90. json.SetProperty("incremental", _incremental);
  91. }
  92. return json;
  93. }
  94. /// <summary>从 <see cref="ColumnAttribute"/> 到 Boolean 的隐式转换,判断 <see cref="ColumnAttribute"/> 有效。</summary>
  95. public static implicit operator bool(ColumnAttribute instance) => instance != null;
  96. /// <summary>解析列特性。</summary>
  97. /// <remarks>注意:此方法不再抛出异常,当不存在正确的列特性时将返回 NULL 值</remarks>
  98. public static ColumnAttribute Parse(PropertyInfo property, bool force = false)
  99. {
  100. if (property == null) return null;
  101. // 属性带有 Independent 特性。
  102. if (property.Contains<IndependentAttribute>()) return null;
  103. // 检查 ColumnAttribute。
  104. ColumnAttribute ca;
  105. {
  106. var cas = property.GetCustomAttributes(typeof(ColumnAttribute), false);
  107. if (cas.LongLength < 1L)
  108. {
  109. if (!force) return null;
  110. ca = new ColumnAttribute();
  111. }
  112. else ca = (ColumnAttribute)cas[0];
  113. }
  114. // 检查属性方法。
  115. var getter = property.GetGetMethod(false);
  116. var setter = property.GetSetMethod(false);
  117. if (getter == null || getter.IsStatic) return null;
  118. if (setter == null || setter.IsStatic) return null;
  119. // 检查主键特性和自增特性。
  120. var pt = property.PropertyType;
  121. if (!ca._incremental && RuntimeUtility.Contains<IncrementalAttribute>(property))
  122. {
  123. if (pt.Equals(typeof(int)) || pt.Equals(typeof(long))) ca._incremental = true;
  124. }
  125. if (!ca._primarykey && RuntimeUtility.Contains<PrimaryKeyAttribute>(property)) ca._primarykey = true;
  126. // 获取索引。
  127. var indexes = IndexAttribute.Parse(property);
  128. foreach (var index in indexes)
  129. {
  130. index.SetColumn(ca);
  131. }
  132. ca._indexes.AddRange(indexes);
  133. // 检查免更新特性。
  134. var nu = RuntimeUtility.GetAttribute<NoUpdateAttribute>(property, false);
  135. if (nu) ca._noupdate = true;
  136. // 检查列名称。
  137. if (TextUtility.IsBlank(ca.Field)) ca._field = property.Name;
  138. // 类型兼容。
  139. if (pt.Equals(typeof(byte[]))) ca._type = ColumnType.Bytes;
  140. else if (pt.Equals(typeof(bool))) ca._type = ColumnType.Boolean;
  141. else if (pt.Equals(typeof(Byte))) ca._type = ColumnType.Integer;
  142. else if (pt.Equals(typeof(SByte))) ca._type = ColumnType.Integer;
  143. else if (pt.Equals(typeof(Int16))) ca._type = ColumnType.Integer;
  144. else if (pt.Equals(typeof(UInt16))) ca._type = ColumnType.Integer;
  145. else if (pt.Equals(typeof(Int32))) ca._type = ColumnType.Integer;
  146. else if (pt.Equals(typeof(UInt32))) ca._type = ColumnType.Integer;
  147. else if (pt.Equals(typeof(Int64))) ca._type = ColumnType.Integer;
  148. else if (pt.Equals(typeof(Single))) ca._type = ColumnType.Float;
  149. else if (pt.Equals(typeof(Double))) ca._type = ColumnType.Float;
  150. else if (pt.Equals(typeof(Decimal))) ca._type = ColumnType.Float;
  151. else if (pt.Equals(typeof(DateTime))) ca._type = ColumnType.DateTime;
  152. else if (pt.Equals(typeof(String)))
  153. {
  154. switch (ca.Type)
  155. {
  156. case ColumnType.Bytes:
  157. case ColumnType.Integer:
  158. case ColumnType.Float:
  159. case ColumnType.DateTime:
  160. //throw new Exception(TextGenerator.Merge("类 ", type.FullName, " 中,属性 ", property.Name, " 的类型不受支持。"));
  161. ca._type = ColumnType.NVarChar;
  162. ca._length = 191;
  163. break;
  164. }
  165. }
  166. #if !NET20
  167. else if (pt.Equals(typeof(Nullable<Boolean>))) { ca._type = ColumnType.Boolean; ca._nullable = true; }
  168. else if (pt.Equals(typeof(Nullable<Byte>))) { ca._type = ColumnType.Integer; ca._nullable = true; }
  169. else if (pt.Equals(typeof(Nullable<SByte>))) { ca._type = ColumnType.Integer; ca._nullable = true; }
  170. else if (pt.Equals(typeof(Nullable<Int16>))) { ca._type = ColumnType.Integer; ca._nullable = true; }
  171. else if (pt.Equals(typeof(Nullable<UInt16>))) { ca._type = ColumnType.Integer; ca._nullable = true; }
  172. else if (pt.Equals(typeof(Nullable<Int32>))) { ca._type = ColumnType.Integer; ca._nullable = true; }
  173. else if (pt.Equals(typeof(Nullable<UInt32>))) { ca._type = ColumnType.Integer; ca._nullable = true; }
  174. else if (pt.Equals(typeof(Nullable<Int64>))) { ca._type = ColumnType.Integer; ca._nullable = true; }
  175. else if (pt.Equals(typeof(Nullable<Single>))) { ca._type = ColumnType.Float; ca._nullable = true; }
  176. else if (pt.Equals(typeof(Nullable<Double>))) { ca._type = ColumnType.Float; ca._nullable = true; }
  177. else if (pt.Equals(typeof(Nullable<Decimal>))) { ca._type = ColumnType.Float; ca._nullable = true; }
  178. else if (pt.Equals(typeof(Nullable<DateTime>))) { ca._type = ColumnType.DateTime; ca._nullable = true; }
  179. #endif
  180. else
  181. {
  182. ca._type = ColumnType.NVarChar191;
  183. ca._length = 191;
  184. }
  185. ca._property = property;
  186. ca.PropertyName = property.Name;
  187. return ca;
  188. }
  189. /// <summary>对列特性排序,Key 和 Flag 将始终排在前部。</summary>
  190. public static ColumnAttribute[] Sort(ColumnAttribute[] columns, bool sort = false)
  191. {
  192. var total = columns.Length;
  193. var key = null as ColumnAttribute;
  194. var flag = null as ColumnAttribute;
  195. var temp = new List<ColumnAttribute>(total);
  196. for (var i = 0; i < total; i++)
  197. {
  198. var ca = columns[i];
  199. if (ca == null) continue;
  200. var pn = ca.Property.Name;
  201. if (pn == "Key") key = ca;
  202. else if (pn == "Flag") flag = ca;
  203. else temp.Add(ca);
  204. }
  205. if (sort && temp.Count > 0) temp.Sort((a, b) => a._field.CompareTo(b._field));
  206. if (key == null && flag == null) return temp.ToArray();
  207. total = 0;
  208. if (key != null) total += 1;
  209. if (flag != null) total += 1;
  210. total += temp.Count;
  211. var sorted = new List<ColumnAttribute>(total);
  212. if (key != null) sorted.Add(key);
  213. if (flag != null) sorted.Add(flag);
  214. sorted.AddRange(temp);
  215. return sorted.ToArray();
  216. }
  217. internal void SetPrimaryKey() => _primarykey = true;
  218. }
  219. }