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.

93 lines
3.0 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. using Apewer.Internals;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using System.Text;
  6. namespace Apewer.Source
  7. {
  8. /// <summary>数据库中的列,类型默认为 NVarChar(191),错误类型将修正为 NText。</summary>
  9. [Serializable]
  10. [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
  11. public sealed class ColumnAttribute : Attribute
  12. {
  13. private PropertyInfo _property = null;
  14. private string _field = "";
  15. private int _length = 0;
  16. private ColumnType _type;
  17. private bool _independent = false;
  18. /// <exception cref="System.ArgumentException"></exception>
  19. private void Init(string field, ColumnType type, int length, bool underline)
  20. {
  21. _field = string.IsNullOrEmpty(field) ? "" : TableStructure.RestrictName(field, underline);
  22. _type = type;
  23. switch (type)
  24. {
  25. case ColumnType.VarChar:
  26. case ColumnType.NVarChar:
  27. if (length < 1) throw new ArgumentException("最大长度无效。");
  28. _length = length;
  29. break;
  30. case ColumnType.VarChar255:
  31. case ColumnType.NVarChar255:
  32. _length = 255;
  33. break;
  34. default:
  35. _length = length;
  36. break;
  37. }
  38. }
  39. /// <summary>使用自动的列名称。当类型为 VarChar 或 NVarChar 时必须指定长度。</summary>
  40. /// <exception cref="System.ArgumentException"></exception>
  41. public ColumnAttribute(ColumnType type = ColumnType.NVarChar, int length = 191) => Init(null, type, length, true);
  42. /// <summary>使用指定的列名称。当类型为 VarChar 或 NVarChar 时必须指定长度。</summary>
  43. /// <exception cref="System.ArgumentException"></exception>
  44. public ColumnAttribute(string field, ColumnType type = ColumnType.NVarChar, int length = 191) => Init(field, type, length, false);
  45. internal ColumnAttribute(string field, ColumnType type, int length, bool underline) => Init(field, type, length, underline);
  46. /// <summary>属性。</summary>
  47. public PropertyInfo Property
  48. {
  49. get => _property;
  50. internal set => _property = value;
  51. }
  52. /// <summary>字段名。</summary>
  53. public string Field
  54. {
  55. get => _field;
  56. set => _field = value;
  57. }
  58. /// <summary>指定字段的最大长度。</summary>
  59. public int Length
  60. {
  61. get => _length;
  62. set => _length = value;
  63. }
  64. /// <summary>字段类型。</summary>
  65. public ColumnType Type
  66. {
  67. get => _type;
  68. set => _type = value;
  69. }
  70. /// <summary>Independent 特性。</summary>
  71. public bool Independent
  72. {
  73. get => _independent;
  74. internal set => _independent = value;
  75. }
  76. }
  77. }