Datei: NDODLL/Linq/LinqStringExtension.cs

Last Commit (10db6e2)
1 //
2 // Copyright (c) 2002-2016 Mirko Matytschak
3 // (www.netdataobjects.de)
4 //
5 // Author: Mirko Matytschak
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8 // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
10 // Software, and to permit persons to whom the Software is furnished to do so, subject to the following
11 // conditions:
12
13 // The above copyright notice and this permission notice shall be included in all copies or substantial portions
14 // of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
17 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21
22
23 using System;
24 using System.Collections;
25 using System.Linq;
26 using System.Linq.Expressions;
27
28 namespace NDO.Linq
29 {
30 ····/// <summary>
31 ····/// This class helps formulating linq queries.
32 ····/// </summary>
33 ····public static class LinqStringExtension
34 ····{
35 ········/// <summary>
36 ········/// Helper Method for creating the LIKE statement
37 ········/// </summary>
38 ········/// <param name="s"></param>
39 ········/// <param name="parameter"></param>
40 ········/// <returns></returns>
41 ········public static bool Like(this object s, object parameter)
42 ········{
43 ············return true;
44 ········}
45
46 ········/// <summary>
47 ········/// Helper Method for creating BETWEEN statements
48 ········/// </summary>
49 ········/// <param name="s"></param>
50 ········/// <param name="firstParameter"></param>
51 ········/// <param name="secondParameter"></param>
52 ········/// <returns></returns>
53 ········public static bool Between(this object s, object firstParameter, object secondParameter)
54 ········{
55 ············return true;
56 ········}
57
58 ········/// <summary>
59 ········/// Helper Method for Linq queries.
60 ········/// </summary>
61 ········/// <param name="o"></param>
62 ········/// <returns></returns>
63 ········public static ObjectId Oid(this object o)
64 ········{
65 ············// If Oid() is used at the right side of a comparison
66 ············// we try to return the NDOObjectId.
67 ············IPersistentObject po = o as IPersistentObject;
68 ············if (po == null)
69 ················return default(ObjectId);
70 ············return po.NDOObjectId;
71 ········}
72
73 ········/// <summary>
74 ········/// Helper Method for Linq queries.
75 ········/// </summary>
76 ········/// <param name="o"></param>
77 ········/// <param name="list"></param>
78 ········/// <returns></returns>
79 ········public static bool In(this object o, IEnumerable list)
80 ········{
81 ············// In Linq queries this code doesn't execute
82 ············foreach (object o2 in list)
83 ············{
84 ················if (object.ReferenceEquals(o, o2))
85 ····················return true;
86 ············}
87
88 ············return false;
89 ········}
90
91
92 ········/// <summary>
93 ········/// Helper Method for Linq queries. Used to query for columns of multikey oids.
94 ········/// </summary>
95 ········/// <param name="o"></param>
96 ········/// <param name="index">The index of the Multikey value to compare with</param>
97 ········/// <returns></returns>
98 ········public static object Oid( this object o, int index )
99 ········{
100 ············// If Oid() is used at the right side of a comparison
101 ············// we try to return the NDOObjectId.
102 ············IPersistentObject po = o as IPersistentObject;
103 ············if (po == null)
104 ················return null;
105 ············return po.NDOObjectId.Id.Values[index];
106 ········}
107
108 ········/// <summary>
109 ········/// Compares two strings and converts the Method call to a Sql operator.
110 ········/// </summary>
111 ········/// <param name="l"></param>
112 ········/// <param name="r"></param>
113 ········/// <returns></returns>
114 ········public static bool GreaterEqual(this string l, string r)
115 ········{
116 ············return String.Compare( l, r ) >= 0;
117 ········}
118
119 ········/// <summary>
 
 
 
 
 
 
 
 
 
 
 
 
120 ········/// Compares two strings and converts the Method call to a Sql operator.
121 ········/// </summary>
122 ········/// <param name="l"></param>
123 ········/// <param name="r"></param>
124 ········/// <returns></returns>
125 ········public static bool GreaterThan( this string l, string r )
126 ········{
127 #error GreaterThan only supports strings. Should support byte[] for rowversions.············
128 ············return String.Compare( l, r ) > 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129 ········}
130
131 ········/// <summary>
 
 
 
 
 
 
 
 
 
 
 
132 ········/// Compares two strings and converts the Method call to a Sql operator.
133 ········/// </summary>
134 ········/// <param name="l"></param>
135 ········/// <param name="r"></param>
136 ········/// <returns></returns>
137 ········public static bool LowerEqual( this string l, string r )
138 ········{
139 ············return String.Compare( l, r ) <= 0;
140 ········}
141
142 ········/// <summary>
 
 
 
 
 
 
 
 
 
 
 
 
143 ········/// Compares two strings and converts the Method call to a Sql operator.
144 ········/// </summary>
145 ········/// <param name="l"></param>
146 ········/// <param name="r"></param>
147 ········/// <returns></returns>
148 ········public static bool LowerThan( this string l, string r )
149 ········{
150 ············return String.Compare( l, r ) < 0;
151 ········}
 
 
 
 
 
 
 
 
 
 
 
 
152
153 ········/// <summary>
154 ········/// Overrides the Count() linq method to deliver the count using database queries.
155 ········/// </summary>
156 ········/// <typeparam name="T"></typeparam>
157 ········/// <param name="vt"></param>
158 ········/// <returns></returns>
159 ········public static int Count<T>(this VirtualTable<T> vt)
160 ········{
161 ············return vt.Count;
162 ········}
163
164 ········/// <summary>
165 ········/// Combines two partial expressions with a binary operator.
166 ········/// </summary>
167 ········/// <typeparam name="T">The type of the query result</typeparam>
168 ········/// <param name="ex1">First expression to be combined</param>
169 ········/// <param name="ex2">Second expression to be combined</param>
170 ········/// <param name="expressionType">Operation type. Use And, Or, AndAlso or OrElse</param>
171 ········/// <returns></returns>
172 ········public static Expression<Func<T,bool>>Combine<T>(this Expression<Func<T, bool>> ex1, Expression<Func<T, bool>> ex2, ExpressionType expressionType )
173 ········{
174 ············var lambdaParameter = ex1.Parameters[0];
175 ············var newEx2 = new ReplaceVisitor( ex2.Parameters[0], lambdaParameter ).VisitAndConvert( ex2, String.Empty );
176 ············var combined = Expression.Lambda<Func<T,bool>>( Expression.MakeBinary(expressionType, ex1.Body, newEx2.Body ), lambdaParameter);
177 ············return combined;
178 ········}
179 ····}
180 }
181
New Commit (8c50417)
1 //
2 // Copyright (c) 2002-2016 Mirko Matytschak
3 // (www.netdataobjects.de)
4 //
5 // Author: Mirko Matytschak
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8 // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
10 // Software, and to permit persons to whom the Software is furnished to do so, subject to the following
11 // conditions:
12
13 // The above copyright notice and this permission notice shall be included in all copies or substantial portions
14 // of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
17 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21
22
23 using System;
24 using System.Collections;
25 using System.Linq;
26 using System.Linq.Expressions;
27
28 namespace NDO.Linq
29 {
30 ····/// <summary>
31 ····/// This class helps formulating linq queries.
32 ····/// </summary>
33 ····public static class LinqStringExtension
34 ····{
35 ········/// <summary>
36 ········/// Helper Method for creating the LIKE statement
37 ········/// </summary>
38 ········/// <param name="s"></param>
39 ········/// <param name="parameter"></param>
40 ········/// <returns></returns>
41 ········public static bool Like(this object s, object parameter)
42 ········{
43 ············return true;
44 ········}
45
46 ········/// <summary>
47 ········/// Helper Method for creating BETWEEN statements
48 ········/// </summary>
49 ········/// <param name="s"></param>
50 ········/// <param name="firstParameter"></param>
51 ········/// <param name="secondParameter"></param>
52 ········/// <returns></returns>
53 ········public static bool Between(this object s, object firstParameter, object secondParameter)
54 ········{
55 ············return true;
56 ········}
57
58 ········/// <summary>
59 ········/// Helper Method for Linq queries.
60 ········/// </summary>
61 ········/// <param name="o"></param>
62 ········/// <returns></returns>
63 ········public static ObjectId Oid(this object o)
64 ········{
65 ············// If Oid() is used at the right side of a comparison
66 ············// we try to return the NDOObjectId.
67 ············IPersistentObject po = o as IPersistentObject;
68 ············if (po == null)
69 ················return default(ObjectId);
70 ············return po.NDOObjectId;
71 ········}
72
73 ········/// <summary>
74 ········/// Helper Method for Linq queries.
75 ········/// </summary>
76 ········/// <param name="o"></param>
77 ········/// <param name="list"></param>
78 ········/// <returns></returns>
79 ········public static bool In(this object o, IEnumerable list)
80 ········{
81 ············// In Linq queries this code doesn't execute
82 ············foreach (object o2 in list)
83 ············{
84 ················if (object.ReferenceEquals(o, o2))
85 ····················return true;
86 ············}
87
88 ············return false;
89 ········}
90
91
92 ········/// <summary>
93 ········/// Helper Method for Linq queries. Used to query for columns of multikey oids.
94 ········/// </summary>
95 ········/// <param name="o"></param>
96 ········/// <param name="index">The index of the Multikey value to compare with</param>
97 ········/// <returns></returns>
98 ········public static object Oid( this object o, int index )
99 ········{
100 ············// If Oid() is used at the right side of a comparison
101 ············// we try to return the NDOObjectId.
102 ············IPersistentObject po = o as IPersistentObject;
103 ············if (po == null)
104 ················return null;
105 ············return po.NDOObjectId.Id.Values[index];
106 ········}
107
108 ········/// <summary>
109 ········/// Compares two strings and converts the Method call to a Sql operator.
110 ········/// </summary>
111 ········/// <param name="l"></param>
112 ········/// <param name="r"></param>
113 ········/// <returns></returns>
114 ········public static bool GreaterEqual(this string l, string r)
115 ········{
116 ············return String.Compare( l, r ) >= 0;
117 ········}
118
119 ········/// <summary>
120 ········/// Compares two byte arrays and converts the Method call to a Sql operator.
121 ········/// </summary>
122 ········/// <param name="l"></param>
123 ········/// <param name="r"></param>
124 ········/// <returns></returns>
125 ········public static bool GreaterEqual( this byte[] l, byte[] r )
126 ········{
127 ············return Compare( l, r ) >= 0;
128 ········}
129
130
131 ········/// <summary>
132 ········/// Compares two strings and converts the Method call to a Sql operator.
133 ········/// </summary>
134 ········/// <param name="l"></param>
135 ········/// <param name="r"></param>
136 ········/// <returns></returns>
137 ········public static bool GreaterThan( this string l, string r )
138 ········{
 
139 ············return String.Compare( l, r ) > 0;
140 ········}
141
142 ········static int Compare(byte[] l, byte[] r)
143 ········{
144 ············// Note: This implementation won't be used in the most cases. Greater Than will be converted to SQL code.
145 ············// We try anyway to provide a working dummy.
146 ············int i = 0;
147 ············var end = r.Length - 1;
148 ············foreach (var b in l)
149 ············{
150 ················if (end < i)··// l has more elements
151 ····················return 1;
152 ················if (b > r[i])
153 ····················return 1;
154 ················if (b < r[i])
155 ····················return -1;
156 ················// elements are identical, try the next
157 ················i++;
158 ············}
159 ············// if both arrays are identical, i == r.length
160 ············if (i == r.Length)
161 ················return 0;
162 ············// r is longer than l
163 ············return -1;
164 ········}
165
166 ········/// <summary>
167 ········/// Compares two byte arrays and converts the Method call to a Sql operator.
168 ········/// </summary>
169 ········/// <param name="l"></param>
170 ········/// <param name="r"></param>
171 ········/// <returns></returns>
172 ········public static bool GreaterThan( this byte[] l, byte[] r )
173 ········{
174 ············return Compare( l, r ) > 0;
175 ········}
176
177 ········/// <summary>
178 ········/// Compares two strings and converts the Method call to a Sql operator.
179 ········/// </summary>
180 ········/// <param name="l"></param>
181 ········/// <param name="r"></param>
182 ········/// <returns></returns>
183 ········public static bool LowerEqual( this string l, string r )
184 ········{
185 ············return String.Compare( l, r ) <= 0;
186 ········}
187
188 ········/// <summary>
189 ········/// Compares two byte arrays and converts the Method call to a Sql operator.
190 ········/// </summary>
191 ········/// <param name="l"></param>
192 ········/// <param name="r"></param>
193 ········/// <returns></returns>
194 ········public static bool LowerEqual( this byte[] l, byte[] r )
195 ········{
196 ············return Compare( l, r ) <= 0;
197 ········}
198
199
200 ········/// <summary>
201 ········/// Compares two strings and converts the Method call to a Sql operator.
202 ········/// </summary>
203 ········/// <param name="l"></param>
204 ········/// <param name="r"></param>
205 ········/// <returns></returns>
206 ········public static bool LowerThan( this string l, string r )
207 ········{
208 ············return String.Compare( l, r ) < 0;
209 ········}
210
211 ········/// <summary>
212 ········/// Compares two byte arrays and converts the Method call to a Sql operator.
213 ········/// </summary>
214 ········/// <param name="l"></param>
215 ········/// <param name="r"></param>
216 ········/// <returns></returns>
217 ········public static bool LowerThan( this byte[] l, byte[] r )
218 ········{
219 ············return Compare( l, r ) < 0;
220 ········}
221
222
223 ········/// <summary>
224 ········/// Overrides the Count() linq method to deliver the count using database queries.
225 ········/// </summary>
226 ········/// <typeparam name="T"></typeparam>
227 ········/// <param name="vt"></param>
228 ········/// <returns></returns>
229 ········public static int Count<T>(this VirtualTable<T> vt)
230 ········{
231 ············return vt.Count;
232 ········}
233
234 ········/// <summary>
235 ········/// Combines two partial expressions with a binary operator.
236 ········/// </summary>
237 ········/// <typeparam name="T">The type of the query result</typeparam>
238 ········/// <param name="ex1">First expression to be combined</param>
239 ········/// <param name="ex2">Second expression to be combined</param>
240 ········/// <param name="expressionType">Operation type. Use And, Or, AndAlso or OrElse</param>
241 ········/// <returns></returns>
242 ········public static Expression<Func<T,bool>>Combine<T>(this Expression<Func<T, bool>> ex1, Expression<Func<T, bool>> ex2, ExpressionType expressionType )
243 ········{
244 ············var lambdaParameter = ex1.Parameters[0];
245 ············var newEx2 = new ReplaceVisitor( ex2.Parameters[0], lambdaParameter ).VisitAndConvert( ex2, String.Empty );
246 ············var combined = Expression.Lambda<Func<T,bool>>( Expression.MakeBinary(expressionType, ex1.Body, newEx2.Body ), lambdaParameter);
247 ············return combined;
248 ········}
249 ····}
250 }
251