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.

103 lines
3.7 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. internal static string GenerateKey() { return Guid.NewGuid().ToString().ToLower().Replace("-", ""); }
  37. internal static void FixProperties(Record record)
  38. {
  39. if (record == null) return;
  40. if (record.Flag == 0) record.Flag = 1;
  41. if (TextUtility.IsBlank(record.Key)) record.Key = TextUtility.NewGuid();
  42. var now = ClockUtility.Lucid;
  43. if (TextUtility.IsBlank(record.Created)) record.Created = now;
  44. if (TextUtility.IsBlank(record.Updated)) record.Updated = now;
  45. }
  46. /// <summary>枚举带有 Table 特性的 <typeparamref name="T"/> 派生类型。</summary>
  47. public static List<Type> EnumerateTableTypes<T>() where T : Record
  48. {
  49. var assemblies = AppDomain.CurrentDomain.GetAssemblies();
  50. var list = new List<Type>();
  51. foreach (var a in assemblies)
  52. {
  53. var types = ClassUtility.GetTypes(a);
  54. foreach (var t in types)
  55. {
  56. if (!EnumerateTableTypes(t, typeof(T))) continue;
  57. if (list.Contains(t)) continue;
  58. list.Add(t);
  59. }
  60. }
  61. return list;
  62. }
  63. private static bool EnumerateTableTypes(Type type, Type @base)
  64. {
  65. if (type == null || @base == null) return false;
  66. if (!type.IsAbstract)
  67. {
  68. if (ClassUtility.ContainsAttribute<TableAttribute>(type, false))
  69. {
  70. if (type.Equals(@base))
  71. {
  72. return true;
  73. }
  74. else
  75. {
  76. if (ClassUtility.IsInherits(type, @base)) return true; ;
  77. }
  78. }
  79. }
  80. return false;
  81. }
  82. }
  83. }