0
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3 using System.Text;
|
|
4
|
|
5 namespace BLToolkit.Data.Sql
|
|
6 {
|
|
7 using DataAccess;
|
|
8 using Mapping;
|
|
9
|
|
10 public class SqlField : IChild<ISqlTableSource>, ISqlExpression
|
|
11 {
|
|
12 public SqlField()
|
|
13 {
|
|
14 }
|
|
15
|
|
16 public SqlField(SqlField field)
|
|
17 : this(field.SystemType, field.Name, field.PhysicalName, field.Nullable, field.PrimaryKeyOrder, field._nonUpdatableAttribute, field.MemberMapper)
|
|
18 {
|
|
19 }
|
|
20
|
|
21 public SqlField(
|
|
22 Type systemType,
|
|
23 string name,
|
|
24 string physicalName,
|
|
25 bool nullable,
|
|
26 int pkOrder,
|
|
27 NonUpdatableAttribute nonUpdatableAttribute,
|
|
28 MemberMapper memberMapper)
|
|
29 {
|
|
30 SystemType = systemType;
|
|
31 Alias = name.Replace('.', '_');
|
|
32 Name = name;
|
|
33 Nullable = nullable;
|
|
34 PrimaryKeyOrder = pkOrder;
|
|
35 _memberMapper = memberMapper;
|
|
36 _physicalName = physicalName;
|
|
37 _nonUpdatableAttribute = nonUpdatableAttribute;
|
|
38 }
|
|
39
|
|
40 public Type SystemType { get; set; }
|
|
41 public string Alias { get; set; }
|
|
42 public string Name { get; set; }
|
|
43 public bool Nullable { get; set; }
|
|
44 public int PrimaryKeyOrder { get; set; }
|
|
45 public ISqlTableSource Table { get; private set; }
|
|
46
|
|
47 readonly MemberMapper _memberMapper;
|
|
48 public MemberMapper MemberMapper
|
|
49 {
|
|
50 get { return _memberMapper; }
|
|
51 }
|
|
52
|
|
53 private string _physicalName;
|
|
54 public string PhysicalName
|
|
55 {
|
|
56 get { return _physicalName ?? Name; }
|
|
57 set { _physicalName = value; }
|
|
58 }
|
|
59
|
|
60 public bool IsIdentity { get { return _nonUpdatableAttribute != null && _nonUpdatableAttribute.IsIdentity; } }
|
|
61 public bool IsInsertable { get { return _nonUpdatableAttribute == null || !_nonUpdatableAttribute.OnInsert; } }
|
|
62 public bool IsUpdatable { get { return _nonUpdatableAttribute == null || !_nonUpdatableAttribute.OnUpdate; } }
|
|
63
|
|
64 public bool IsPrimaryKey { get { return PrimaryKeyOrder != int.MinValue; } }
|
|
65
|
|
66 readonly NonUpdatableAttribute _nonUpdatableAttribute;
|
|
67
|
|
68 ISqlTableSource IChild<ISqlTableSource>.Parent { get { return Table; } set { Table = value; } }
|
|
69
|
|
70 #region Overrides
|
|
71
|
|
72 #if OVERRIDETOSTRING
|
|
73
|
|
74 public override string ToString()
|
|
75 {
|
|
76 return ((IQueryElement)this).ToString(new StringBuilder(), new Dictionary<IQueryElement,IQueryElement>()).ToString();
|
|
77 }
|
|
78
|
|
79 #endif
|
|
80
|
|
81 #endregion
|
|
82
|
|
83 #region ISqlExpression Members
|
|
84
|
|
85 public bool CanBeNull()
|
|
86 {
|
|
87 return Nullable;
|
|
88 }
|
|
89
|
|
90 public bool Equals(ISqlExpression other, Func<ISqlExpression,ISqlExpression,bool> comparer)
|
|
91 {
|
|
92 return this == other;
|
|
93 }
|
|
94
|
|
95 public int Precedence
|
|
96 {
|
|
97 get { return Sql.Precedence.Primary; }
|
|
98 }
|
|
99
|
|
100 #endregion
|
|
101
|
|
102 #region ISqlExpressionWalkable Members
|
|
103
|
|
104 ISqlExpression ISqlExpressionWalkable.Walk(bool skipColumns, Func<ISqlExpression,ISqlExpression> func)
|
|
105 {
|
|
106 return func(this);
|
|
107 }
|
|
108
|
|
109 #endregion
|
|
110
|
|
111 #region IEquatable<ISqlExpression> Members
|
|
112
|
|
113 bool IEquatable<ISqlExpression>.Equals(ISqlExpression other)
|
|
114 {
|
|
115 return this == other;
|
|
116 }
|
|
117
|
|
118 #endregion
|
|
119
|
|
120 #region ICloneableElement Members
|
|
121
|
|
122 public ICloneableElement Clone(Dictionary<ICloneableElement, ICloneableElement> objectTree, Predicate<ICloneableElement> doClone)
|
|
123 {
|
|
124 if (!doClone(this))
|
|
125 return this;
|
|
126
|
|
127 Table.Clone(objectTree, doClone);
|
|
128 return objectTree[this];
|
|
129 }
|
|
130
|
|
131 #endregion
|
|
132
|
|
133 #region IQueryElement Members
|
|
134
|
|
135 public QueryElementType ElementType { get { return QueryElementType.SqlField; } }
|
|
136
|
|
137 StringBuilder IQueryElement.ToString(StringBuilder sb, Dictionary<IQueryElement,IQueryElement> dic)
|
|
138 {
|
|
139 return sb
|
|
140 .Append('t')
|
|
141 .Append(Table.SourceID)
|
|
142 .Append('.')
|
|
143 .Append(Name);
|
|
144 }
|
|
145
|
|
146 #endregion
|
|
147 }
|
|
148 }
|