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.

44 lines
1.3 KiB

  1. using System;
  2. namespace Apewer.Source
  3. {
  4. /// <summary>索引。</summary>
  5. public sealed class TableIndex
  6. {
  7. /// <summary>索引名称。</summary>
  8. public string Name { get; }
  9. /// <summary>表。</summary>
  10. public TableAttribute Table { get; }
  11. /// <summary>索引字段。</summary>
  12. public IndexAttribute[] Fields { get; }
  13. /// <summary>创建索引实例。</summary>
  14. /// <exception cref="ArgumentNullException"></exception>
  15. internal TableIndex(string name, TableAttribute table, IndexAttribute[] fields)
  16. {
  17. if (name.IsEmpty()) throw new ArgumentNullException(nameof(name));
  18. if (table == null) throw new ArgumentNullException(nameof(table));
  19. if (fields == null) throw new ArgumentNullException(nameof(fields));
  20. Name = name;
  21. Table = table;
  22. Fields = fields;
  23. }
  24. /// <summary></summary>
  25. public override string ToString()
  26. {
  27. var fields = Fields.FindAll(x => x != null && x.Column != null);
  28. if (fields.IsEmpty()) return Name;
  29. var array = fields.Map(x => x.Order == Order.Ascend ? x.Column.Field : $"{x.Column.Field} DESC");
  30. return $"{Name} ({array.Join(", ")})";
  31. }
  32. }
  33. }