comparison Source/Data/Sql/SqlFunction.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 SqlFunction : ISqlExpression//ISqlTableSource
9 {
10 [Obsolete]
11 public SqlFunction(string name, params ISqlExpression[] parameters)
12 : this(null, name, Sql.Precedence.Primary, parameters)
13 {
14 }
15
16 [Obsolete]
17 public SqlFunction(string name, int precedence, params ISqlExpression[] parameters)
18 : this(null, name, precedence, parameters)
19 {
20 }
21
22 public SqlFunction(Type systemType, string name, params ISqlExpression[] parameters)
23 : this(systemType, name, Sql.Precedence.Primary, parameters)
24 {
25 }
26
27 public SqlFunction(Type systemType, string name, int precedence, params ISqlExpression[] parameters)
28 {
29 //_sourceID = Interlocked.Increment(ref SqlQuery.SourceIDCounter);
30
31 if (parameters == null) throw new ArgumentNullException("parameters");
32
33 foreach (var p in parameters)
34 if (p == null) throw new ArgumentNullException("parameters");
35
36 SystemType = systemType;
37 Name = name;
38 Precedence = precedence;
39 Parameters = parameters;
40 }
41
42 public Type SystemType { get; private set; }
43 public string Name { get; private set; }
44
45 public int Precedence { get; private set; }
46 public ISqlExpression[] Parameters { get; private set; }
47
48 public static SqlFunction CreateCount (Type type, ISqlTableSource table) { return new SqlFunction(type, "Count", table.All); }
49
50 public static SqlFunction CreateAll (SqlQuery subQuery) { return new SqlFunction(typeof(bool), "ALL", Sql.Precedence.Comparison, subQuery); }
51 public static SqlFunction CreateSome (SqlQuery subQuery) { return new SqlFunction(typeof(bool), "SOME", Sql.Precedence.Comparison, subQuery); }
52 public static SqlFunction CreateAny (SqlQuery subQuery) { return new SqlFunction(typeof(bool), "ANY", Sql.Precedence.Comparison, subQuery); }
53 public static SqlFunction CreateExists(SqlQuery subQuery) { return new SqlFunction(typeof(bool), "EXISTS", Sql.Precedence.Comparison, subQuery); }
54
55 #region Overrides
56
57 #if OVERRIDETOSTRING
58
59 public override string ToString()
60 {
61 return ((IQueryElement)this).ToString(new StringBuilder(), new Dictionary<IQueryElement,IQueryElement>()).ToString();
62 }
63
64 #endif
65
66 #endregion
67
68 #region ISqlExpressionWalkable Members
69
70 [Obsolete]
71 ISqlExpression ISqlExpressionWalkable.Walk(bool skipColumns, Func<ISqlExpression,ISqlExpression> action)
72 {
73 for (var i = 0; i < Parameters.Length; i++)
74 Parameters[i] = Parameters[i].Walk(skipColumns, action);
75
76 return action(this);
77 }
78
79 #endregion
80
81 #region IEquatable<ISqlExpression> Members
82
83 bool IEquatable<ISqlExpression>.Equals(ISqlExpression other)
84 {
85 return Equals(other, SqlExpression.DefaultComparer);
86 }
87
88 #endregion
89
90 #region ISqlTableSource Members
91
92 /*
93 readonly int _sourceID;
94 public int SourceID { get { return _sourceID; } }
95
96 SqlField _all;
97 SqlField ISqlTableSource.All
98 {
99 get
100 {
101 if (_all == null)
102 {
103 _all = new SqlField(null, "*", "*", true, -1, null, null);
104 ((IChild<ISqlTableSource>)_all).Parent = this;
105 }
106
107 return _all;
108 }
109 }
110
111 IList<ISqlExpression> ISqlTableSource.GetKeys(bool allIfEmpty)
112 {
113 return null;
114 }
115 */
116
117 #endregion
118
119 #region ISqlExpression Members
120
121 public bool CanBeNull()
122 {
123 return true;
124 }
125
126 public bool Equals(ISqlExpression other, Func<ISqlExpression,ISqlExpression,bool> comparer)
127 {
128 if (this == other)
129 return true;
130
131 var func = other as SqlFunction;
132
133 if (func == null || Name != func.Name || Parameters.Length != func.Parameters.Length && SystemType != func.SystemType)
134 return false;
135
136 for (var i = 0; i < Parameters.Length; i++)
137 if (!Parameters[i].Equals(func.Parameters[i], comparer))
138 return false;
139
140 return comparer(this, other);
141 }
142
143 #endregion
144
145 #region ICloneableElement Members
146
147 public ICloneableElement Clone(Dictionary<ICloneableElement, ICloneableElement> objectTree, Predicate<ICloneableElement> doClone)
148 {
149 if (!doClone(this))
150 return this;
151
152 ICloneableElement clone;
153
154 if (!objectTree.TryGetValue(this, out clone))
155 {
156 objectTree.Add(this, clone = new SqlFunction(
157 SystemType,
158 Name,
159 Precedence,
160 Parameters.Select(e => (ISqlExpression)e.Clone(objectTree, doClone)).ToArray()));
161 }
162
163 return clone;
164 }
165
166 #endregion
167
168 #region IQueryElement Members
169
170 public QueryElementType ElementType { get { return QueryElementType.SqlFunction; } }
171
172 StringBuilder IQueryElement.ToString(StringBuilder sb, Dictionary<IQueryElement,IQueryElement> dic)
173 {
174 sb
175 .Append(Name)
176 .Append("(");
177
178 foreach (var p in Parameters)
179 {
180 p.ToString(sb, dic);
181 sb.Append(", ");
182 }
183
184 if (Parameters.Length > 0)
185 sb.Length -= 2;
186
187 return sb.Append(")");
188 }
189
190 #endregion
191 }
192 }