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.

111 lines
3.9 KiB

  1. using Apewer;
  2. using Apewer.Internals;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace Apewer.Source
  6. {
  7. /// <summary>数据库记录通用字段模型。</summary>
  8. [Serializable]
  9. public class Record : IRecord
  10. {
  11. [NonSerialized]
  12. private string _created = "";
  13. [NonSerialized]
  14. private string _updated = "";
  15. [NonSerialized]
  16. private long _flag = 0;
  17. [NonSerialized]
  18. private string _remark = "";
  19. [NonSerialized]
  20. private string _key = GenerateKey();
  21. /// <summary>记录的创建时间,NVarChar 255 类型,值为执行 INSERT INTO 语句时的时间。</summary>
  22. [Column("_created", ColumnType.NVarChar255)]
  23. public virtual string Created { get { return _created; } set { _created = TextModifier.Compact(value, 255); } }
  24. /// <summary>记录的更新时间,NVarChar 255 类型,每次对此记录执行 UPDATE 时应更新此值为当前系统时间。</summary>
  25. [Column("_updated", ColumnType.NVarChar255)]
  26. public virtual string Updated { get { return _updated; } set { _updated = TextModifier.Compact(value, 255); } }
  27. /// <summary>记录的标记,Int64 类型,区分记录的状态。</summary>
  28. [Column("_flag", ColumnType.Integer)]
  29. public virtual long Flag { get { return _flag; } set { _flag = value; } }
  30. /// <summary>备注,NText 类型。</summary>
  31. [Column("_remark", ColumnType.NText)]
  32. public virtual string Remark { get { return _remark; } set { _remark = TextModifier.Compact(value); } }
  33. /// <summary>记录唯一键,NVarChar 128 类型,一般使用 GUID 的字符串形式。</summary>
  34. [Column("_key", ColumnType.NVarChar, 128)]
  35. public virtual string Key { get { return _key; } set { _key = TextModifier.Compact(value, 128); } }
  36. /// <summary>重置主键。</summary>
  37. internal static void ResetKey(Record record)
  38. {
  39. if (record == null) return;
  40. record.Key = GenerateKey();
  41. }
  42. /// <summary>生成新主键。</summary>
  43. public static string GenerateKey() => Guid.NewGuid().ToString().ToLower().Replace("-", "");
  44. internal static void FixProperties(Record record)
  45. {
  46. if (record == null) return;
  47. if (record.Flag == 0) record.Flag = 1;
  48. if (TextUtility.IsBlank(record.Key)) record.Key = TextUtility.NewGuid();
  49. var now = ClockUtility.LucidNow;
  50. if (TextUtility.IsBlank(record.Created)) record.Created = now;
  51. if (TextUtility.IsBlank(record.Updated)) record.Updated = now;
  52. }
  53. /// <summary>枚举带有 Table 特性的 <typeparamref name="T"/> 派生类型。</summary>
  54. public static List<Type> EnumerateTableTypes<T>() where T : Record
  55. {
  56. var assemblies = AppDomain.CurrentDomain.GetAssemblies();
  57. var list = new List<Type>();
  58. foreach (var a in assemblies)
  59. {
  60. var types = RuntimeUtility.GetTypes(a);
  61. foreach (var t in types)
  62. {
  63. if (!EnumerateTableTypes(t, typeof(T))) continue;
  64. if (list.Contains(t)) continue;
  65. list.Add(t);
  66. }
  67. }
  68. return list;
  69. }
  70. private static bool EnumerateTableTypes(Type type, Type @base)
  71. {
  72. if (type == null || @base == null) return false;
  73. if (!type.IsAbstract)
  74. {
  75. if (RuntimeUtility.ContainsAttribute<TableAttribute>(type, false))
  76. {
  77. if (type.Equals(@base))
  78. {
  79. return true;
  80. }
  81. else
  82. {
  83. if (RuntimeUtility.IsInherits(type, @base)) return true; ;
  84. }
  85. }
  86. }
  87. return false;
  88. }
  89. }
  90. }