Mercurial > pub > bltoolkit
comparison Source/Data/Sql/SqlExpression.cs @ 0:f990fcb411a9
Копия текущей версии из github
author | cin |
---|---|
date | Thu, 27 Mar 2014 21:46:09 +0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f990fcb411a9 |
---|---|
1 using System; | |
2 using System.Collections.Generic; | |
3 using System.Linq; | |
4 using System.Text; | |
5 | |
6 namespace BLToolkit.Data.Sql | |
7 { | |
8 public class SqlExpression : ISqlExpression | |
9 { | |
10 public SqlExpression(Type systemType, string expr, int precedence, params ISqlExpression[] parameters) | |
11 { | |
12 if (parameters == null) throw new ArgumentNullException("parameters"); | |
13 | |
14 foreach (var value in parameters) | |
15 if (value == null) throw new ArgumentNullException("parameters"); | |
16 | |
17 SystemType = systemType; | |
18 Expr = expr; | |
19 Precedence = precedence; | |
20 Parameters = parameters; | |
21 } | |
22 | |
23 public SqlExpression(string expr, int precedence, params ISqlExpression[] parameters) | |
24 : this(null, expr, precedence, parameters) | |
25 { | |
26 } | |
27 | |
28 public SqlExpression(Type systemType, string expr, params ISqlExpression[] parameters) | |
29 : this(systemType, expr, Sql.Precedence.Unknown, parameters) | |
30 { | |
31 } | |
32 | |
33 public SqlExpression(string expr, params ISqlExpression[] parameters) | |
34 : this(null, expr, Sql.Precedence.Unknown, parameters) | |
35 { | |
36 } | |
37 | |
38 public Type SystemType { get; private set; } | |
39 public string Expr { get; private set; } | |
40 | |
41 public int Precedence { get; private set; } | |
42 public ISqlExpression[] Parameters { get; private set; } | |
43 | |
44 #region Overrides | |
45 | |
46 #if OVERRIDETOSTRING | |
47 | |
48 public override string ToString() | |
49 { | |
50 return ((IQueryElement)this).ToString(new StringBuilder(), new Dictionary<IQueryElement,IQueryElement>()).ToString(); | |
51 } | |
52 | |
53 #endif | |
54 | |
55 #endregion | |
56 | |
57 #region ISqlExpressionWalkable Members | |
58 | |
59 [Obsolete] | |
60 ISqlExpression ISqlExpressionWalkable.Walk(bool skipColumns, Func<ISqlExpression,ISqlExpression> func) | |
61 { | |
62 for (var i = 0; i < Parameters.Length; i++) | |
63 Parameters[i] = Parameters[i].Walk(skipColumns, func); | |
64 | |
65 return func(this); | |
66 } | |
67 | |
68 #endregion | |
69 | |
70 #region IEquatable<ISqlExpression> Members | |
71 | |
72 bool IEquatable<ISqlExpression>.Equals(ISqlExpression other) | |
73 { | |
74 return Equals(other, DefaultComparer); | |
75 } | |
76 | |
77 #endregion | |
78 | |
79 #region ISqlExpression Members | |
80 | |
81 public bool CanBeNull() | |
82 { | |
83 foreach (var value in Parameters) | |
84 if (value.CanBeNull()) | |
85 return true; | |
86 | |
87 return false; | |
88 } | |
89 | |
90 public static Func<ISqlExpression,ISqlExpression,bool> DefaultComparer = (x, y) => true; | |
91 | |
92 public bool Equals(ISqlExpression other, Func<ISqlExpression,ISqlExpression,bool> comparer) | |
93 { | |
94 if (this == other) | |
95 return true; | |
96 | |
97 var expr = other as SqlExpression; | |
98 | |
99 if (expr == null || SystemType != expr.SystemType || Expr != expr.Expr || Parameters.Length != expr.Parameters.Length) | |
100 return false; | |
101 | |
102 for (var i = 0; i < Parameters.Length; i++) | |
103 if (!Parameters[i].Equals(expr.Parameters[i], comparer)) | |
104 return false; | |
105 | |
106 return comparer(this, other); | |
107 } | |
108 | |
109 #endregion | |
110 | |
111 #region ICloneableElement Members | |
112 | |
113 public ICloneableElement Clone(Dictionary<ICloneableElement, ICloneableElement> objectTree, Predicate<ICloneableElement> doClone) | |
114 { | |
115 if (!doClone(this)) | |
116 return this; | |
117 | |
118 ICloneableElement clone; | |
119 | |
120 if (!objectTree.TryGetValue(this, out clone)) | |
121 { | |
122 objectTree.Add(this, clone = new SqlExpression( | |
123 SystemType, | |
124 Expr, | |
125 Precedence, | |
126 Parameters.Select(e => (ISqlExpression)e.Clone(objectTree, doClone)).ToArray())); | |
127 } | |
128 | |
129 return clone; | |
130 } | |
131 | |
132 #endregion | |
133 | |
134 #region IQueryElement Members | |
135 | |
136 public QueryElementType ElementType { get { return QueryElementType.SqlExpression; } } | |
137 | |
138 StringBuilder IQueryElement.ToString(StringBuilder sb, Dictionary<IQueryElement,IQueryElement> dic) | |
139 { | |
140 var len = sb.Length; | |
141 var ss = Parameters.Select(p => | |
142 { | |
143 p.ToString(sb, dic); | |
144 var s = sb.ToString(len, sb.Length - len); | |
145 sb.Length = len; | |
146 return (object)s; | |
147 }); | |
148 | |
149 return sb.AppendFormat(Expr, ss.ToArray()); | |
150 } | |
151 | |
152 #endregion | |
153 | |
154 #region Public Static Members | |
155 | |
156 public static bool NeedsEqual(IQueryElement ex) | |
157 { | |
158 switch (ex.ElementType) | |
159 { | |
160 case QueryElementType.SqlParameter: | |
161 case QueryElementType.SqlField : | |
162 case QueryElementType.Column : return true; | |
163 case QueryElementType.SqlFunction : | |
164 | |
165 var f = (SqlFunction)ex; | |
166 | |
167 switch (f.Name) | |
168 { | |
169 case "EXISTS" : return false; | |
170 } | |
171 | |
172 return true; | |
173 } | |
174 | |
175 return false; | |
176 } | |
177 | |
178 #endregion | |
179 } | |
180 } |