DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SqlExpressionTypeExtensions.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 using System;
18 
19 namespace Deveel.Data.Sql.Expressions {
20  internal static class SqlExpressionTypeExtensions {
21  public static SqlExpressionType Reverse(this SqlExpressionType type) {
22  if (type == SqlExpressionType.Equal ||
23  type == SqlExpressionType.NotEqual ||
24  type == SqlExpressionType.Is ||
25  type == SqlExpressionType.IsNot)
26  return type;
27  if (type == SqlExpressionType.GreaterThan)
28  return SqlExpressionType.SmallerThan;
29  if (type == SqlExpressionType.SmallerThan)
30  return SqlExpressionType.GreaterThan;
31  if (type == SqlExpressionType.GreaterOrEqualThan)
32  return SqlExpressionType.SmallerOrEqualThan;
33  if (type == SqlExpressionType.SmallerOrEqualThan)
34  return SqlExpressionType.GreaterOrEqualThan;
35 
36  throw new InvalidOperationException("Cannot reverse a non conditional operator.");
37  }
38 
39  public static bool IsLogical(this SqlExpressionType type) {
40  return type == SqlExpressionType.And ||
41  type == SqlExpressionType.Or;
42  }
43 
44  public static bool IsPattern(this SqlExpressionType type) {
45  return type == SqlExpressionType.Like ||
46  type == SqlExpressionType.NotLike;
47  }
48 
49  public static bool IsSubQuery(this SqlExpressionType type) {
50  return type.IsAny() || type.IsAll();
51  }
52 
53  public static bool IsAll(this SqlExpressionType type) {
54  return type == SqlExpressionType.AllEqual ||
55  type == SqlExpressionType.AllNotEqual ||
56  type == SqlExpressionType.AllGreaterThan ||
57  type == SqlExpressionType.AllSmallerThan ||
58  type == SqlExpressionType.AllGreaterOrEqualThan ||
59  type == SqlExpressionType.AllSmallerOrEqualThan;
60  }
61 
62  public static bool IsAny(this SqlExpressionType type) {
63  return type == SqlExpressionType.AnyEqual ||
64  type == SqlExpressionType.AnyNotEqual ||
65  type == SqlExpressionType.AnyGreaterThan ||
66  type == SqlExpressionType.AnySmallerThan ||
67  type == SqlExpressionType.AnyGreaterOrEqualThan ||
68  type == SqlExpressionType.AnySmallerOrEqualThan;
69  }
70 
72  if (type.IsAny()) {
73  if (type == SqlExpressionType.AnyEqual)
74  return SqlExpressionType.Equal;
75  if (type == SqlExpressionType.AnyNotEqual)
76  return SqlExpressionType.NotEqual;
77  if (type == SqlExpressionType.AnyGreaterThan)
78  return SqlExpressionType.GreaterThan;
79  if (type == SqlExpressionType.AnyGreaterOrEqualThan)
80  return SqlExpressionType.GreaterOrEqualThan;
81  if (type == SqlExpressionType.AnySmallerThan)
82  return SqlExpressionType.AnySmallerThan;
83  if (type == SqlExpressionType.AnySmallerOrEqualThan)
84  return SqlExpressionType.SmallerOrEqualThan;
85  }
86 
87  if (type.IsAll()) {
88  if (type == SqlExpressionType.AllEqual)
89  return SqlExpressionType.Equal;
90  if (type == SqlExpressionType.AllNotEqual)
91  return SqlExpressionType.NotEqual;
92  if (type == SqlExpressionType.AllGreaterThan)
93  return SqlExpressionType.GreaterThan;
94  if (type == SqlExpressionType.AllGreaterOrEqualThan)
95  return SqlExpressionType.GreaterOrEqualThan;
96  if (type == SqlExpressionType.AllSmallerThan)
97  return SqlExpressionType.AllSmallerThan;
98  if (type == SqlExpressionType.AllSmallerOrEqualThan)
99  return SqlExpressionType.SmallerOrEqualThan;
100  }
101 
102  throw new ArgumentException();
103  }
104 
105  public static SqlExpressionType Any(this SqlExpressionType type) {
106  if (type == SqlExpressionType.Equal)
107  return SqlExpressionType.AnyEqual;
108  if (type == SqlExpressionType.NotEqual)
109  return SqlExpressionType.AnyNotEqual;
110  if (type == SqlExpressionType.GreaterThan)
111  return SqlExpressionType.AnyGreaterThan;
112  if (type == SqlExpressionType.GreaterOrEqualThan)
113  return SqlExpressionType.AnyGreaterOrEqualThan;
114  if (type == SqlExpressionType.SmallerThan)
115  return SqlExpressionType.AnySmallerThan;
116  if (type == SqlExpressionType.SmallerOrEqualThan)
117  return SqlExpressionType.AnySmallerOrEqualThan;
118 
119  throw new ArgumentException();
120  }
121 
122  public static SqlExpressionType All(this SqlExpressionType type) {
123  if (type == SqlExpressionType.Equal)
124  return SqlExpressionType.AllEqual;
125  if (type == SqlExpressionType.NotEqual)
126  return SqlExpressionType.AllNotEqual;
127  if (type == SqlExpressionType.GreaterThan)
128  return SqlExpressionType.AllGreaterThan;
129  if (type == SqlExpressionType.GreaterOrEqualThan)
130  return SqlExpressionType.AllGreaterOrEqualThan;
131  if (type == SqlExpressionType.SmallerThan)
132  return SqlExpressionType.AllSmallerThan;
133  if (type == SqlExpressionType.SmallerOrEqualThan)
134  return SqlExpressionType.AllSmallerOrEqualThan;
135 
136  throw new ArgumentException();
137  }
138 
139  public static SqlExpressionType Inverse(this SqlExpressionType type) {
140  if (type.IsSubQuery()) {
141  var plainType = type.SubQueryPlainType();
142  var invType = plainType.Inverse();
143 
144  if (type.IsAny())
145  return invType.Any();
146  if (type.IsAll())
147  return invType.All();
148  }
149 
150  switch (type) {
151  case SqlExpressionType.Equal:
152  return SqlExpressionType.NotEqual;
153  case SqlExpressionType.NotEqual:
154  return SqlExpressionType.Equal;
155  case SqlExpressionType.GreaterThan:
156  return SqlExpressionType.SmallerOrEqualThan;
157  case SqlExpressionType.SmallerThan:
158  return SqlExpressionType.GreaterOrEqualThan;
159  case SqlExpressionType.GreaterOrEqualThan:
160  return SqlExpressionType.SmallerThan;
161  case SqlExpressionType.SmallerOrEqualThan:
162  return SqlExpressionType.GreaterThan;
163  case SqlExpressionType.And:
164  return SqlExpressionType.Or;
165  case SqlExpressionType.Or:
166  return SqlExpressionType.And;
167  case SqlExpressionType.Like:
168  return SqlExpressionType.NotLike;
169  case SqlExpressionType.NotLike:
170  return SqlExpressionType.Like;
171  case SqlExpressionType.Is:
172  return SqlExpressionType.IsNot;
173  case SqlExpressionType.IsNot:
174  return SqlExpressionType.Is;
175  }
176 
177  throw new ArgumentException();
178  }
179  }
180 }
static SqlExpressionType Any(this SqlExpressionType type)
static SqlExpressionType Reverse(this SqlExpressionType type)
static SqlExpressionType All(this SqlExpressionType type)
static SqlExpressionType Inverse(this SqlExpressionType type)
SqlExpressionType
All the possible type of SqlExpression supported
static SqlExpressionType SubQueryPlainType(this SqlExpressionType type)