protected static Type GetSpecialExpressionTypeType(SpecialExpressionType specialExpressionType, IList<Expression> operands) { Type defaultType; if (operands.Count > 0) defaultType = operands[0].Type; else defaultType = null; switch (specialExpressionType) // SETuse { case SpecialExpressionType.IsNull: case SpecialExpressionType.IsNotNull: return typeof(bool); case SpecialExpressionType.Concat: return typeof(string); case SpecialExpressionType.Count: return typeof(int); case SpecialExpressionType.Like: return typeof(bool); case SpecialExpressionType.Min: case SpecialExpressionType.Max: case SpecialExpressionType.Sum: return defaultType; // for such methods, the type is related to the operands type case SpecialExpressionType.Average: return typeof(double); case SpecialExpressionType.StringLength: return typeof(int); case SpecialExpressionType.ToUpper: case SpecialExpressionType.ToLower: return typeof(string); case SpecialExpressionType.In: return typeof(bool); case SpecialExpressionType.Substring: return defaultType; case SpecialExpressionType.Trim: return typeof(string); case SpecialExpressionType.StringInsert: return typeof(string); case SpecialExpressionType.Replace: return typeof(string); case SpecialExpressionType.Remove: return typeof(string); case SpecialExpressionType.IndexOf: return typeof(int); case SpecialExpressionType.Year: case SpecialExpressionType.Month: case SpecialExpressionType.Day: case SpecialExpressionType.Hour: case SpecialExpressionType.Second: case SpecialExpressionType.Minute: case SpecialExpressionType.Millisecond: return typeof(int); case SpecialExpressionType.Now: return typeof(DateTime); case SpecialExpressionType.DateDiff: return typeof(TimeSpan); default: throw Error.BadArgument("S0058: Unknown SpecialExpressionType value {0}", specialExpressionType); } }
public SpecialExpression(SpecialExpressionType expressionType, params Expression[] operands) : base((ExpressionType)expressionType, GetSpecialExpressionTypeType(expressionType, operands), operands) { }
public SpecialExpression(SpecialExpressionType expressionType, IList<Expression> operands) : base((ExpressionType)expressionType, GetSpecialExpressionTypeType(expressionType, operands), operands) { }
/// <summary> /// Converts a special expression type to literal /// </summary> /// <param name="operationType"></param> /// <param name="p"></param> /// <returns></returns> public virtual SqlStatement GetLiteral(SpecialExpressionType operationType, IList<SqlStatement> p) { switch (operationType) // SETuse { case SpecialExpressionType.IsNull: return GetLiteralIsNull(p[0]); case SpecialExpressionType.IsNotNull: return GetLiteralIsNotNull(p[0]); case SpecialExpressionType.Concat: return GetLiteralStringConcat(p[0], p[1]); case SpecialExpressionType.Count: return GetLiteralCount(p[0]); case SpecialExpressionType.Exists: return GetLiteralExists(p[0]); case SpecialExpressionType.Like: return GetLiteralLike(p[0], p[1]); case SpecialExpressionType.Min: return GetLiteralMin(p[0]); case SpecialExpressionType.Max: return GetLiteralMax(p[0]); case SpecialExpressionType.Sum: return GetLiteralSum(p[0]); case SpecialExpressionType.Average: return GetLiteralAverage(p[0]); case SpecialExpressionType.StringLength: return GetLiteralStringLength(p[0]); case SpecialExpressionType.ToUpper: return GetLiteralStringToUpper(p[0]); case SpecialExpressionType.ToLower: return GetLiteralStringToLower(p[0]); case SpecialExpressionType.In: return GetLiteralIn(p[0], p[1]); case SpecialExpressionType.Substring: if (p.Count > 2) return GetLiteralSubString(p[0], p[1], p[2]); return GetLiteralSubString(p[0], p[1]); case SpecialExpressionType.Trim: case SpecialExpressionType.LTrim: case SpecialExpressionType.RTrim: return GetLiteralTrim(p[0]); case SpecialExpressionType.StringInsert: return GetLiteralStringInsert(p[0], p[1], p[2]); case SpecialExpressionType.Replace: return GetLiteralStringReplace(p[0], p[1], p[2]); case SpecialExpressionType.Remove: if (p.Count > 2) return GetLiteralStringRemove(p[0], p[1], p[2]); return GetLiteralStringRemove(p[0], p[1]); case SpecialExpressionType.IndexOf: if (p.Count == 2) return GetLiteralStringIndexOf(p[0], p[1]); else if (p.Count == 3) return GetLiteralStringIndexOf(p[0], p[1], p[2]); else if (p.Count == 4) return GetLiteralStringIndexOf(p[0], p[1], p[2], p[3]); break; case SpecialExpressionType.Year: case SpecialExpressionType.Month: case SpecialExpressionType.Day: case SpecialExpressionType.Hour: case SpecialExpressionType.Minute: case SpecialExpressionType.Second: case SpecialExpressionType.Millisecond: return GetLiteralDateTimePart(p[0], operationType); case SpecialExpressionType.Date: return p[0]; case SpecialExpressionType.DateDiffInMilliseconds: return GetLiteralDateDiff(p[0], p[1]); case SpecialExpressionType.Abs: return GetLiteralMathAbs(p[0]); case SpecialExpressionType.Exp: return GetLiteralMathExp(p[0]); case SpecialExpressionType.Floor: return GetLiteralMathFloor(p[0]); case SpecialExpressionType.Ln: return GetLiteralMathLn(p[0]); case SpecialExpressionType.Log: if (p.Count == 1) return GetLiteralMathLog(p[0]); else return GetLiteralMathLog(p[0], p[1]); case SpecialExpressionType.Pow: return GetLiteralMathPow(p[0], p[1]); case SpecialExpressionType.Round: return GetLiteralMathRound(p[0]); case SpecialExpressionType.Sign: return GetLiteralMathSign(p[0]); case SpecialExpressionType.Sqrt: return GetLiteralMathSqrt(p[0]); } throw new ArgumentException(operationType.ToString()); }
/// <summary> /// Gets the literal date time part. /// </summary> /// <param name="dateExpression">The date expression.</param> /// <param name="operationType">Type of the operation.</param> /// <returns></returns> protected virtual SqlStatement GetLiteralDateTimePart(SqlStatement dateExpression, SpecialExpressionType operationType) { return SqlStatement.Format("EXTRACT({0} FROM {1})", operationType.ToString().ToUpper(), dateExpression); }
protected override SqlStatement GetLiteralDateTimePart(SqlStatement dateExpression, SpecialExpressionType operationType) { return SqlStatement.Format("DATEPART({0},{1})", operationType.ToString().ToUpper(), dateExpression); }
private object EvaluateMathCallInvoke(SpecialExpressionType SpecialNodeType, ReadOnlyCollection<Expression> operands) { return typeof(Math).GetMethod(SpecialNodeType.ToString(), operands.Skip(1).Select(op => op.Type).ToArray()) .Invoke(null, operands.Skip(1).Select(op => op.Evaluate()).ToArray()); }
protected override Expression VisitMethodCall(MethodCallExpression node) { EnsureNotInSpecialExpression(); if ((node.Method.DeclaringType == null) || (node.Object == null) || !node.Method.IsSpecialName || !node.Method.Name.StartsWith("get_")) { throw new NotSupportedException("Method calls are not supported in subdocument paths."); } var property = node.Method.DeclaringType.GetProperties() .FirstOrDefault(p => p.GetMethod == node.Method); if (property == null) { throw new NotSupportedException("Method calls are not supported in subdocument paths."); } var obj = Visit(node.Object); if (node.Arguments.Count > 0) { if (node.Arguments.Count > 1) { throw new NotSupportedException( "Array/dictionary indices with more than one dimension are not supported in subdocument paths."); } // Ensure that indexed properties are only used if they are the default indexed property for the object if (!node.Method.DeclaringType.GetDefaultMembers().Contains(property)) { throw new NotSupportedException( "Only default indexed properties are not supported in subdocument paths."); } Expression argument; if (node.Arguments[0].Type == typeof(string)) { // This is a dictionary with a key string, so treat as property accessor _inSpecialExpression = SpecialExpressionType.DictionaryKey; PrependDotSmart(obj); argument = Visit(node.Arguments[0]); } else { _inSpecialExpression = SpecialExpressionType.ArrayIndex; _path.Append('['); argument = Visit(node.Arguments[0]); _path.Append(']'); } _inSpecialExpression = SpecialExpressionType.None; return(node.Update(obj, new[] { argument })); } else { PrependDotSmart(obj); WriteEscapedString(GetMemberName(property)); return(node.Update(obj, null)); } }
protected override SqlStatement GetLiteralDateTimePart(SqlStatement dateExpression, SpecialExpressionType operationType) { return(SqlStatement.Format("DATEPART({0},{1})", operationType.ToString().ToUpper(), dateExpression)); }
private object EvaluateMathCallInvoke(SpecialExpressionType SpecialNodeType, ReadOnlyCollection <Expression> operands) { return(typeof(Math).GetMethod(SpecialNodeType.ToString(), operands.Skip(1).Select(op => op.Type).ToArray()) .Invoke(null, operands.Skip(1).Select(op => op.Evaluate()).ToArray())); }
protected static Type GetSpecialExpressionTypeType(SpecialExpressionType specialExpressionType, IList <Expression> operands) { Type defaultType; if (operands.Count > 0) { defaultType = operands[0].Type; } else { defaultType = null; } switch (specialExpressionType) // SETuse { case SpecialExpressionType.IsNull: case SpecialExpressionType.IsNotNull: return(typeof(bool)); case SpecialExpressionType.Concat: return(typeof(string)); case SpecialExpressionType.Count: return(typeof(int)); case SpecialExpressionType.Exists: return(typeof(bool)); case SpecialExpressionType.Like: return(typeof(bool)); case SpecialExpressionType.Min: case SpecialExpressionType.Max: case SpecialExpressionType.Sum: return(defaultType); // for such methods, the type is related to the operands type case SpecialExpressionType.Average: return(typeof(double)); case SpecialExpressionType.StringLength: return(typeof(int)); case SpecialExpressionType.ToUpper: case SpecialExpressionType.ToLower: return(typeof(string)); case SpecialExpressionType.In: return(typeof(bool)); case SpecialExpressionType.Substring: return(defaultType); case SpecialExpressionType.Trim: case SpecialExpressionType.LTrim: case SpecialExpressionType.RTrim: return(typeof(string)); case SpecialExpressionType.StringInsert: return(typeof(string)); case SpecialExpressionType.Replace: return(typeof(string)); case SpecialExpressionType.Remove: return(typeof(string)); case SpecialExpressionType.IndexOf: return(typeof(int)); case SpecialExpressionType.Year: case SpecialExpressionType.Month: case SpecialExpressionType.Day: case SpecialExpressionType.Hour: case SpecialExpressionType.Second: case SpecialExpressionType.Minute: case SpecialExpressionType.Millisecond: return(typeof(int)); case SpecialExpressionType.Now: case SpecialExpressionType.Date: return(typeof(DateTime)); case SpecialExpressionType.DateDiffInMilliseconds: return(typeof(long)); case SpecialExpressionType.Abs: case SpecialExpressionType.Exp: case SpecialExpressionType.Floor: case SpecialExpressionType.Ln: case SpecialExpressionType.Log: case SpecialExpressionType.Pow: case SpecialExpressionType.Round: case SpecialExpressionType.Sign: case SpecialExpressionType.Sqrt: return(defaultType); default: throw Error.BadArgument("S0058: Unknown SpecialExpressionType value {0}", specialExpressionType); } }
public SpecialExpression(SpecialExpressionType expressionType, IList <Expression> operands) : base((ExpressionType)expressionType, GetSpecialExpressionTypeType(expressionType, operands), operands) { }
protected override string GetLiteralDateTimePart(string dateExpression, SpecialExpressionType operationType) { return string.Format("DATEPART({0},{1})", operationType.ToString().ToUpper(), dateExpression); }
protected virtual string GetLiteralDateTimePart(string dateExpression, SpecialExpressionType operationType) { return string.Format("EXTRACT({0} FROM {1})", operationType.ToString().ToUpper(), dateExpression); }
/// <summary> /// Converts a special expression type to literal /// </summary> /// <param name="operationType"></param> /// <param name="p"></param> /// <returns></returns> public virtual string GetLiteral(SpecialExpressionType operationType, IList<string> p) { switch (operationType) // SETuse { case SpecialExpressionType.IsNull: return GetLiteralIsNull(p[0]); case SpecialExpressionType.IsNotNull: return GetLiteralIsNotNull(p[0]); case SpecialExpressionType.Concat: return GetLiteralStringConcat(p[0], p[1]); case SpecialExpressionType.Count: return GetLiteralCount(p[0]); case SpecialExpressionType.Like: return GetLiteralLike(p[0], p[1]); case SpecialExpressionType.Min: return GetLiteralMin(p[0]); case SpecialExpressionType.Max: return GetLiteralMax(p[0]); case SpecialExpressionType.Sum: return GetLiteralSum(p[0]); case SpecialExpressionType.Average: return GetLiteralAverage(p[0]); case SpecialExpressionType.StringLength: return GetLiteralStringLength(p[0]); case SpecialExpressionType.ToUpper: return GetLiteralStringToUpper(p[0]); case SpecialExpressionType.ToLower: return GetLiteralStringToLower(p[0]); case SpecialExpressionType.In: return GetLiteralIn(p[0], p[1]); case SpecialExpressionType.Substring: if (p.Count > 2) return GetLiteralSubString(p[0], p[1], p[2]); return GetLiteralSubString(p[0], p[1]); case SpecialExpressionType.Trim: return GetLiteralTrim(p[0]); case SpecialExpressionType.StringInsert: return GetLiteralStringInsert(p[0], p[1], p[2]); case SpecialExpressionType.Replace: return GetLiteralStringReplace(p[0], p[1], p[2]); case SpecialExpressionType.Remove: if (p.Count > 2) return GetLiteralStringRemove(p[0], p[1], p[2]); return GetLiteralStringRemove(p[0], p[1]); case SpecialExpressionType.IndexOf: if (p.Count == 2) return GetLiteralStringIndexOf(p[0], p[1]); else if (p.Count == 3) return GetLiteralStringIndexOf(p[0], p[1], p[2]); else if (p.Count == 4) return GetLiteralStringIndexOf(p[0], p[1], p[2], p[3]); break; case SpecialExpressionType.Year: case SpecialExpressionType.Month: case SpecialExpressionType.Day: case SpecialExpressionType.Hour: case SpecialExpressionType.Minute: case SpecialExpressionType.Second: case SpecialExpressionType.Millisecond: return GetLiteralDateTimePart(p[0], operationType); case SpecialExpressionType.DateDiff: return GetLiteralDateDiff(p[0], p[1]); } throw new ArgumentException(operationType.ToString()); }