5 changed files with 189 additions and 7 deletions
@ -0,0 +1,81 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Reflection; |
||||
|
|
||||
|
namespace Apewer.Source |
||||
|
{ |
||||
|
|
||||
|
/// <summary>索引。</summary>
|
||||
|
[Serializable] |
||||
|
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = true)] |
||||
|
public sealed class IndexAttribute : Attribute, IToJson |
||||
|
{ |
||||
|
|
||||
|
PropertyInfo _property = null; |
||||
|
ColumnAttribute _column; |
||||
|
|
||||
|
string _name = null; |
||||
|
Order _order = Order.Ascend; |
||||
|
|
||||
|
/// <summary>索引名称。</summary>
|
||||
|
public string Name { get => _name; } |
||||
|
|
||||
|
/// <summary>排序。</summary>
|
||||
|
public Order Order { get => _order; set => _order = value; } |
||||
|
|
||||
|
/// <summary>列。</summary>
|
||||
|
public ColumnAttribute Column { get => _column; } |
||||
|
|
||||
|
/// <summary>属性。</summary>
|
||||
|
public PropertyInfo Property { get => _property; } |
||||
|
|
||||
|
/// <summary>表示此属性是索引的一部分。</summary>
|
||||
|
/// <param name="name">索引名称。</param>
|
||||
|
public IndexAttribute(string name) |
||||
|
{ |
||||
|
if (string.IsNullOrEmpty(name)) throw new ArgumentNullException(nameof(name)); |
||||
|
_name = name; |
||||
|
} |
||||
|
|
||||
|
internal void SetColumn(ColumnAttribute column) |
||||
|
{ |
||||
|
_column = column; |
||||
|
} |
||||
|
|
||||
|
/// <summary>解析列的索引。</summary>
|
||||
|
/// <remarks>注意:此方法不再抛出异常,当不存在正确的列特性时将返回 NULL 值</remarks>
|
||||
|
public static IndexAttribute[] Parse(PropertyInfo property) |
||||
|
{ |
||||
|
if (property == null) return null; |
||||
|
|
||||
|
var attributes = property.GetCustomAttributes(typeof(IndexAttribute), true); |
||||
|
var ias = new List<IndexAttribute>(); |
||||
|
foreach (var attribute in attributes) |
||||
|
{ |
||||
|
var ia = attribute as IndexAttribute; |
||||
|
if (ia == null) continue; |
||||
|
if (ia._name.IsEmpty()) continue; |
||||
|
|
||||
|
ia._property = property; |
||||
|
ias.Add(ia); |
||||
|
} |
||||
|
return ias.ToArray(); |
||||
|
} |
||||
|
|
||||
|
/// <summary>生成 Json 对象。</summary>
|
||||
|
/// <exception cref="NotImplementedException"></exception>
|
||||
|
public Json ToJson() |
||||
|
{ |
||||
|
var json = Json.NewObject(); |
||||
|
json.SetProperty("name", _name); |
||||
|
json.SetProperty("order", _order.ToString()); |
||||
|
if (_column != null) json.SetProperty("column", _column.ToJson()); |
||||
|
return json; |
||||
|
} |
||||
|
|
||||
|
/// <summary></summary>
|
||||
|
public override string ToString() => $"Index = {_name}, Field = {_column?.Field}, Order = {_order}"; |
||||
|
|
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,16 @@ |
|||||
|
namespace Apewer.Source |
||||
|
{ |
||||
|
|
||||
|
/// <summary>排序。</summary>
|
||||
|
public enum Order |
||||
|
{ |
||||
|
|
||||
|
/// <summary>升序排序。</summary>
|
||||
|
Ascend, |
||||
|
|
||||
|
/// <summary>降序排序。</summary>
|
||||
|
Descend |
||||
|
|
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,44 @@ |
|||||
|
using System; |
||||
|
|
||||
|
namespace Apewer.Source |
||||
|
{ |
||||
|
|
||||
|
/// <summary>索引。</summary>
|
||||
|
public sealed class TableIndex |
||||
|
{ |
||||
|
|
||||
|
/// <summary>索引名称。</summary>
|
||||
|
public string Name { get; } |
||||
|
|
||||
|
/// <summary>表。</summary>
|
||||
|
public TableAttribute Table { get; } |
||||
|
|
||||
|
/// <summary>索引字段。</summary>
|
||||
|
public IndexAttribute[] Fields { get; } |
||||
|
|
||||
|
/// <summary>创建索引实例。</summary>
|
||||
|
/// <exception cref="ArgumentNullException"></exception>
|
||||
|
internal TableIndex(string name, TableAttribute table, IndexAttribute[] fields) |
||||
|
{ |
||||
|
if (name.IsEmpty()) throw new ArgumentNullException(nameof(name)); |
||||
|
if (table == null) throw new ArgumentNullException(nameof(table)); |
||||
|
if (fields == null) throw new ArgumentNullException(nameof(fields)); |
||||
|
|
||||
|
Name = name; |
||||
|
Table = table; |
||||
|
Fields = fields; |
||||
|
} |
||||
|
|
||||
|
/// <summary></summary>
|
||||
|
public override string ToString() |
||||
|
{ |
||||
|
var fields = Fields.FindAll(x => x != null && x.Column != null); |
||||
|
if (fields.IsEmpty()) return Name; |
||||
|
|
||||
|
var array = fields.Map(x => x.Order == Order.Ascend ? x.Column.Field : $"{x.Column.Field} DESC"); |
||||
|
return $"{Name} ({array.Join(", ")})"; |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue