using Apewer.Internals;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
namespace Apewer.Source
{
/// 数据库中的列,类型默认为 NVarChar(255),错误类型将修正为 NText。
[Serializable]
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public sealed class ColumnAttribute : Attribute
{
private PropertyInfo _property = null;
private string _field = "";
private int _length = 0;
private ColumnType _type;
private bool _independent = false;
/// 使用自动的列名称。当类型为 VarChar 或 NVarChar 时必须指定长度。
///
public ColumnAttribute(ColumnType type = ColumnType.NVarChar255, int length = 0)
{
New(null, type, length, true);
}
/// 使用指定的列名称。当类型为 VarChar 或 NVarChar 时必须指定长度。
///
public ColumnAttribute(string field, ColumnType type = ColumnType.NVarChar255, int length = 0)
{
New(field, type, length, false);
}
internal ColumnAttribute(string field, ColumnType type, int length, bool underline)
{
New(field, type, length, underline);
}
/// 属性。
public PropertyInfo Property { get { return _property; } internal set { _property = value; } }
/// 字段名。
public string Field { get { return _field; } set { _field = value; } }
/// 指定字段的最大长度。
public int Length { get { return _length; } set { _length = value; } }
/// 字段类型。
public ColumnType Type { get { return _type; } set { _type = value; } }
/// Independent 特性。
public bool Independent { get { return _independent; } internal set { _independent = value; } }
///
private void New(string field, ColumnType type, int length, bool underline)
{
_field = string.IsNullOrEmpty(field) ? "" : TableStructure.RestrictName(field, underline);
_type = type;
switch (type)
{
case ColumnType.VarChar:
case ColumnType.NVarChar:
if (length < 1) throw new ArgumentException("最大长度无效。");
_length = length;
break;
case ColumnType.VarChar255:
case ColumnType.NVarChar255:
_length = 255;
break;
default:
_length = length;
break;
}
}
}
}