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.

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