Datei: NDODLL/Linq/LinqStringExtension.cs
Last Commit (a932b6c)
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 | ············return String.Compare( l, r ) > 0; |
128 | ········} |
129 | |
130 | ········/// <summary> |
131 | ········/// Compares two strings and converts the Method call to a Sql operator. |
132 | ········/// </summary> |
133 | ········/// <param name="l"></param> |
134 | ········/// <param name="r"></param> |
135 | ········/// <returns></returns> |
136 | ········public static bool LowerEqual( this string l, string r ) |
137 | ········{ |
138 | ············return String.Compare( l, r ) <= 0; |
139 | ········} |
140 | |
141 | ········/// <summary> |
142 | ········/// Compares two strings and converts the Method call to a Sql operator. |
143 | ········/// </summary> |
144 | ········/// <param name="l"></param> |
145 | ········/// <param name="r"></param> |
146 | ········/// <returns></returns> |
147 | ········public static bool LowerThan( this string l, string r ) |
148 | ········{ |
149 | ············return String.Compare( l, r ) < 0; |
150 | ········} |
151 | |
152 | ········/// <summary> |
153 | ········/// Overrides the Count() linq method to deliver the count using database queries. |
154 | ········/// </summary> |
155 | ········/// <typeparam name="T"></typeparam> |
156 | ········/// <param name="vt"></param> |
157 | ········/// <returns></returns> |
158 | ········public static int Count<T>(this VirtualTable<T> vt) |
159 | ········{ |
160 | ············return vt.Count; |
161 | ········} |
162 | |
163 | ········/// <summary> |
164 | ········/// Combines two partial expressions with a binary operator. |
165 | ········/// </summary> |
166 | ········/// <typeparam name="T">The type of the query result</typeparam> |
167 | ········/// <param name="ex1">First expression to be combined</param> |
168 | ········/// <param name="ex2">Second expression to be combined</param> |
169 | ········/// <param name="expressionType">Operation type. Use And, Or, AndAlso or OrElse</param> |
170 | ········/// <returns></returns> |
171 | ········public static Expression<Func<T,bool>>Combine<T>(this Expression<Func<T, bool>> ex1, Expression<Func<T, bool>> ex2, ExpressionType expressionType ) |
172 | ········{ |
173 | ············var lambdaParameter = ex1.Parameters[0]; |
174 | ············var newEx2 = new ReplaceVisitor( ex2.Parameters[0], lambdaParameter ).VisitAndConvert( ex2, String.Empty ); |
175 | ············var combined = Expression.Lambda<Func<T,bool>>( Expression.MakeBinary(expressionType, ex1.Body, newEx2.Body ), lambdaParameter); |
176 | ············return combined; |
177 | ········} |
178 | ····} |
179 | } |
180 |
New 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 |