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