0
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3 using System.Text;
|
|
4
|
|
5 namespace BLToolkit.Data.Sql
|
|
6 {
|
|
7 public class SqlValue : SqlValueBase, ISqlExpression
|
|
8 {
|
|
9 public SqlValue(Type systemType, object value)
|
|
10 {
|
|
11 _systemType = systemType;
|
|
12 _value = value;
|
|
13 }
|
|
14
|
|
15 public SqlValue(object value)
|
|
16 {
|
|
17 _value = value;
|
|
18
|
|
19 if (value != null)
|
|
20 _systemType = value.GetType();
|
|
21 }
|
|
22
|
|
23 public override object Value
|
|
24 {
|
|
25 get
|
|
26 {
|
|
27 var rv = base.Value;
|
|
28
|
|
29 if (rv != null && rv.GetType() != _systemType)
|
|
30 {
|
|
31 _systemType = rv.GetType();
|
|
32 }
|
|
33
|
|
34 return rv;
|
|
35 }
|
|
36 }
|
|
37
|
|
38 Type _systemType; public Type SystemType { get { return _systemType; } }
|
|
39
|
|
40 #region Overrides
|
|
41
|
|
42 #if OVERRIDETOSTRING
|
|
43
|
|
44 public override string ToString()
|
|
45 {
|
|
46 return ((IQueryElement)this).ToString(new StringBuilder(), new Dictionary<IQueryElement,IQueryElement>()).ToString();
|
|
47 }
|
|
48
|
|
49 #endif
|
|
50
|
|
51 #endregion
|
|
52
|
|
53 #region ISqlExpression Members
|
|
54
|
|
55 public int Precedence
|
|
56 {
|
|
57 get { return Sql.Precedence.Primary; }
|
|
58 }
|
|
59
|
|
60 #endregion
|
|
61
|
|
62 #region ISqlExpressionWalkable Members
|
|
63
|
|
64 ISqlExpression ISqlExpressionWalkable.Walk(bool skipColumns, Func<ISqlExpression,ISqlExpression> func)
|
|
65 {
|
|
66 return func(this);
|
|
67 }
|
|
68
|
|
69 #endregion
|
|
70
|
|
71 #region IEquatable<ISqlExpression> Members
|
|
72
|
|
73 bool IEquatable<ISqlExpression>.Equals(ISqlExpression other)
|
|
74 {
|
|
75 if (this == other)
|
|
76 return true;
|
|
77
|
|
78 var value = other as SqlValue;
|
|
79 return
|
|
80 value != null &&
|
|
81 _systemType == value._systemType &&
|
|
82 (_value == null && value._value == null || _value != null && _value.Equals(value._value));
|
|
83 }
|
|
84
|
|
85 #endregion
|
|
86
|
|
87 #region ISqlExpression Members
|
|
88
|
|
89 public bool CanBeNull()
|
|
90 {
|
|
91 return Value == null;
|
|
92 }
|
|
93
|
|
94 public bool Equals(ISqlExpression other, Func<ISqlExpression, ISqlExpression, bool> comparer)
|
|
95 {
|
|
96 return ((ISqlExpression)this).Equals(other) && comparer(this, other);
|
|
97 }
|
|
98
|
|
99 #endregion
|
|
100
|
|
101 #region ICloneableElement Members
|
|
102
|
|
103 public ICloneableElement Clone(Dictionary<ICloneableElement, ICloneableElement> objectTree, Predicate<ICloneableElement> doClone)
|
|
104 {
|
|
105 if (!doClone(this))
|
|
106 return this;
|
|
107
|
|
108 ICloneableElement clone;
|
|
109
|
|
110 if (!objectTree.TryGetValue(this, out clone))
|
|
111 objectTree.Add(this, clone = new SqlValue(_systemType, _value));
|
|
112
|
|
113 return clone;
|
|
114 }
|
|
115
|
|
116 #endregion
|
|
117
|
|
118 #region IQueryElement Members
|
|
119
|
|
120 public QueryElementType ElementType { get { return QueryElementType.SqlValue; } }
|
|
121
|
|
122 StringBuilder IQueryElement.ToString(StringBuilder sb, Dictionary<IQueryElement,IQueryElement> dic)
|
|
123 {
|
|
124 return
|
|
125 Value == null ?
|
|
126 sb.Append("NULL") :
|
|
127 Value is string ?
|
|
128 sb
|
|
129 .Append('\'')
|
|
130 .Append(Value.ToString().Replace("\'", "''"))
|
|
131 .Append('\'')
|
|
132 :
|
|
133 sb.Append(Value);
|
|
134 }
|
|
135
|
|
136 #endregion
|
|
137 }
|
|
138 }
|