| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Diagnostics; |
| | 7 | | using System.Globalization; |
| | 8 | | using System.Linq.Expressions; |
| | 9 | | using System.Reflection; |
| | 10 | | using System.Text; |
| | 11 | |
|
| | 12 | | namespace Azure.Data.Tables.Queryable |
| | 13 | | { |
| | 14 | | internal class ExpressionWriter : LinqExpressionVisitor |
| | 15 | | { |
| | 16 | | internal readonly StringBuilder _builder; |
| | 17 | | private readonly Stack<Expression> _expressionStack; |
| | 18 | | private bool _cantTranslateExpression; |
| | 19 | |
|
| 460 | 20 | | protected ExpressionWriter() |
| | 21 | | { |
| 460 | 22 | | _builder = new StringBuilder(); |
| 460 | 23 | | _expressionStack = new Stack<Expression>(); |
| 460 | 24 | | _expressionStack.Push(null); |
| 460 | 25 | | } |
| | 26 | |
|
| | 27 | | internal static string ExpressionToString(Expression e) |
| | 28 | | { |
| 460 | 29 | | ExpressionWriter ew = new ExpressionWriter(); |
| 460 | 30 | | return ew.ConvertExpressionToString(e); |
| | 31 | | } |
| | 32 | |
|
| | 33 | | internal string ConvertExpressionToString(Expression e) |
| | 34 | | { |
| 460 | 35 | | string serialized = Translate(e); |
| | 36 | |
|
| 460 | 37 | | if (_cantTranslateExpression) |
| | 38 | | { |
| 0 | 39 | | throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, SR.ALinqCantTranslateExpressio |
| | 40 | | } |
| | 41 | |
|
| 460 | 42 | | return serialized; |
| | 43 | | } |
| | 44 | |
|
| | 45 | | internal override Expression Visit(Expression exp) |
| | 46 | | { |
| 3360 | 47 | | _expressionStack.Push(exp); |
| 3360 | 48 | | Expression result = base.Visit(exp); |
| 3360 | 49 | | _expressionStack.Pop(); |
| 3360 | 50 | | return result; |
| | 51 | | } |
| | 52 | |
|
| | 53 | | internal override Expression VisitMethodCall(MethodCallExpression m) |
| | 54 | | { |
| 212 | 55 | | if (ReflectionUtil.s_dictionaryMethodInfosHash.Contains(m.Method) && m.Arguments.Count == 1 && m.Arguments[0 |
| | 56 | | { |
| 212 | 57 | | _builder.Append(ce.Value as string); |
| | 58 | | } |
| | 59 | | else |
| | 60 | | { |
| 0 | 61 | | return base.VisitMethodCall(m); |
| | 62 | | } |
| | 63 | |
|
| 212 | 64 | | return m; |
| | 65 | | } |
| | 66 | |
|
| | 67 | | internal override Expression VisitMemberAccess(MemberExpression m) |
| | 68 | | { |
| 588 | 69 | | if (m.Member is FieldInfo) |
| | 70 | | { |
| 0 | 71 | | throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, SR.ALinqCantReferToPublicField |
| | 72 | | } |
| | 73 | |
|
| 588 | 74 | | Expression e = Visit(m.Expression); |
| 588 | 75 | | if (m.Member.Name == "Value" && m.Member.DeclaringType.IsGenericType |
| 588 | 76 | | && m.Member.DeclaringType.GetGenericTypeDefinition() == typeof(Nullable<>)) |
| | 77 | | { |
| 0 | 78 | | return m; |
| | 79 | | } |
| | 80 | |
|
| 588 | 81 | | if (!IsInputReference(e) && e.NodeType != ExpressionType.Convert && e.NodeType != ExpressionType.ConvertChec |
| | 82 | | { |
| 0 | 83 | | _builder.Append(UriHelper.FORWARDSLASH); |
| | 84 | | } |
| | 85 | |
|
| 588 | 86 | | _builder.Append(TranslateMemberName(m.Member.Name)); |
| | 87 | |
|
| 588 | 88 | | return m; |
| | 89 | | } |
| | 90 | |
|
| | 91 | | internal override Expression VisitConstant(ConstantExpression c) |
| | 92 | | { |
| | 93 | | string result; |
| 808 | 94 | | if (c.Value == null) |
| | 95 | | { |
| 0 | 96 | | _builder.Append(UriHelper.NULL); |
| 0 | 97 | | return c; |
| | 98 | | } |
| 808 | 99 | | else if (!ClientConvert.TryKeyPrimitiveToString(c.Value, out result)) |
| | 100 | | { |
| 0 | 101 | | throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, SR.ALinqCouldNotConvert, c |
| | 102 | | } |
| | 103 | |
|
| | 104 | | Debug.Assert(result != null, "result != null"); |
| | 105 | |
|
| | 106 | | // A Difference from WCF Data Services is that we will escape later when we execute the fully parsed query. |
| 808 | 107 | | _builder.Append(result); |
| 808 | 108 | | return c; |
| | 109 | | } |
| | 110 | |
|
| | 111 | | internal override Expression VisitUnary(UnaryExpression u) |
| | 112 | | { |
| 16 | 113 | | switch (u.NodeType) |
| | 114 | | { |
| | 115 | | case ExpressionType.Not: |
| 16 | 116 | | _builder.Append(UriHelper.NOT); |
| 16 | 117 | | _builder.Append(UriHelper.SPACE); |
| 16 | 118 | | VisitOperand(u.Operand); |
| 16 | 119 | | break; |
| | 120 | | case ExpressionType.Negate: |
| | 121 | | case ExpressionType.NegateChecked: |
| 0 | 122 | | _builder.Append(UriHelper.SPACE); |
| 0 | 123 | | _builder.Append(TranslateOperator(u.NodeType)); |
| 0 | 124 | | VisitOperand(u.Operand); |
| 0 | 125 | | break; |
| | 126 | | case ExpressionType.Convert: |
| | 127 | | case ExpressionType.ConvertChecked: |
| | 128 | | case ExpressionType.UnaryPlus: |
| | 129 | | break; |
| | 130 | | default: |
| 0 | 131 | | _cantTranslateExpression = true; |
| | 132 | | break; |
| | 133 | | } |
| | 134 | |
|
| 16 | 135 | | return u; |
| | 136 | | } |
| | 137 | |
|
| | 138 | | internal override Expression VisitBinary(BinaryExpression b) |
| | 139 | | { |
| 1148 | 140 | | VisitOperand(b.Left); |
| 1148 | 141 | | _builder.Append(UriHelper.SPACE); |
| 1148 | 142 | | string operatorString = TranslateOperator(b.NodeType); |
| 1148 | 143 | | if (string.IsNullOrEmpty(operatorString)) |
| | 144 | | { |
| 0 | 145 | | _cantTranslateExpression = true; |
| | 146 | | } |
| | 147 | | else |
| | 148 | | { |
| 1148 | 149 | | _builder.Append(operatorString); |
| | 150 | | } |
| | 151 | |
|
| 1148 | 152 | | _builder.Append(UriHelper.SPACE); |
| 1148 | 153 | | VisitOperand(b.Right); |
| 1148 | 154 | | return b; |
| | 155 | | } |
| | 156 | |
|
| | 157 | | internal override Expression VisitParameter(ParameterExpression p) |
| | 158 | | { |
| 588 | 159 | | return p; |
| | 160 | | } |
| | 161 | |
|
| | 162 | | private static bool IsInputReference(Expression exp) |
| | 163 | | { |
| 588 | 164 | | return exp is ParameterExpression; |
| | 165 | | } |
| | 166 | |
|
| | 167 | | private void VisitOperand(Expression e) |
| | 168 | | { |
| 2312 | 169 | | if (e is BinaryExpression || e is UnaryExpression) |
| | 170 | | { |
| 720 | 171 | | if (e is UnaryExpression unary && unary.NodeType == ExpressionType.TypeAs) |
| | 172 | | { |
| 8 | 173 | | Visit(unary.Operand); |
| | 174 | | } |
| | 175 | | else |
| | 176 | | { |
| 712 | 177 | | _builder.Append(UriHelper.LEFTPAREN); |
| 712 | 178 | | Visit(e); |
| 712 | 179 | | _builder.Append(UriHelper.RIGHTPAREN); |
| | 180 | | } |
| | 181 | |
|
| | 182 | | } |
| | 183 | | else |
| | 184 | | { |
| 1592 | 185 | | Visit(e); |
| | 186 | | } |
| 1592 | 187 | | } |
| | 188 | |
|
| | 189 | | private string Translate(Expression e) |
| | 190 | | { |
| 460 | 191 | | Visit(e); |
| 460 | 192 | | return _builder.ToString(); |
| | 193 | | } |
| | 194 | |
|
| | 195 | | protected virtual string TranslateMemberName(string memberName) |
| | 196 | | { |
| 588 | 197 | | return memberName; |
| | 198 | | } |
| | 199 | |
|
| | 200 | | protected virtual string TranslateOperator(ExpressionType type) |
| | 201 | | { |
| | 202 | | switch (type) |
| | 203 | | { |
| | 204 | | case ExpressionType.AndAlso: |
| | 205 | | case ExpressionType.And: |
| 272 | 206 | | return UriHelper.AND; |
| | 207 | | case ExpressionType.OrElse: |
| | 208 | | case ExpressionType.Or: |
| 76 | 209 | | return UriHelper.OR; |
| | 210 | | case ExpressionType.Equal: |
| 428 | 211 | | return UriHelper.EQ; |
| | 212 | | case ExpressionType.NotEqual: |
| 20 | 213 | | return UriHelper.NE; |
| | 214 | | case ExpressionType.LessThan: |
| 32 | 215 | | return UriHelper.LT; |
| | 216 | | case ExpressionType.LessThanOrEqual: |
| 4 | 217 | | return UriHelper.LE; |
| | 218 | | case ExpressionType.GreaterThan: |
| 12 | 219 | | return UriHelper.GT; |
| | 220 | | case ExpressionType.GreaterThanOrEqual: |
| 304 | 221 | | return UriHelper.GE; |
| | 222 | | case ExpressionType.Add: |
| | 223 | | case ExpressionType.AddChecked: |
| 0 | 224 | | return UriHelper.ADD; |
| | 225 | | case ExpressionType.Subtract: |
| | 226 | | case ExpressionType.SubtractChecked: |
| 0 | 227 | | return UriHelper.SUB; |
| | 228 | | case ExpressionType.Multiply: |
| | 229 | | case ExpressionType.MultiplyChecked: |
| 0 | 230 | | return UriHelper.MUL; |
| | 231 | | case ExpressionType.Divide: |
| 0 | 232 | | return UriHelper.DIV; |
| | 233 | | case ExpressionType.Modulo: |
| 0 | 234 | | return UriHelper.MOD; |
| | 235 | | case ExpressionType.Negate: |
| | 236 | | case ExpressionType.NegateChecked: |
| 0 | 237 | | return UriHelper.NEGATE; |
| | 238 | | case ExpressionType.ArrayIndex: |
| | 239 | | case ExpressionType.Power: |
| | 240 | | case ExpressionType.Coalesce: |
| | 241 | | case ExpressionType.ExclusiveOr: |
| | 242 | | case ExpressionType.LeftShift: |
| | 243 | | case ExpressionType.RightShift: |
| | 244 | | default: |
| 0 | 245 | | return null; |
| | 246 | | } |
| | 247 | | } |
| | 248 | | } |
| | 249 | | } |