/// <summary> /// Binds the <see cref="string.Trim()"/> method. /// </summary> /// <param name="expression">The expression.</param> protected virtual Expression BindTrim(MethodCallExpression expression) { LinqStringBuilder.Append("RTRIM(LTRIM("); this.Visit(expression.Object); LinqStringBuilder.Append("))"); return(expression); }
/// <summary> /// Binds to <see cref="string.ToLower()"/> method. /// </summary> /// <param name="expression">The expression.</param> protected virtual Expression BindToLower(MethodCallExpression expression) { LinqStringBuilder.Append("LOWER("); Visit(expression.Object); LinqStringBuilder.Append(")"); return(expression); }
/// <summary> /// Binds to <see cref="string.ToUpper()"/> method. /// </summary> /// <param name="expression">The expression.</param> protected virtual Expression BindToUpper(MethodCallExpression expression) { LinqStringBuilder.Append("UPPER("); this.Visit(expression.Object); LinqStringBuilder.Append(")"); return(expression); }
/// <summary> /// Visits the unary. /// </summary> /// <param name="expression">The expression.</param> /// <returns></returns> /// <exception cref="System.NotSupportedException">If this unary expression is not supported.</exception> protected override Expression VisitUnary(UnaryExpression expression) { switch (expression.NodeType) { case ExpressionType.Not: if (IsBooleanPropertyExpression(expression.Operand)) { AppendBooleanCondition((MemberExpression)expression.Operand, true, true); } else { LinqStringBuilder.Append(" NOT "); Visit(expression.Operand); } break; case ExpressionType.Convert: case ExpressionType.ConvertChecked: Visit(expression.Operand); break; default: throw new NotSupportedException(string.Format(Resources.UnaryOperatorIsNotSupported, expression.NodeType)); } return(expression); }
/// <inheritdoc/> protected override Expression BindToLower(MethodCallExpression expression) { LinqStringBuilder.Append("LCASE("); this.Visit(expression.Object); LinqStringBuilder.Append(")"); return(expression); }
/// <summary> /// Binds the <see cref="string.EndsWith(string)"/> method. /// </summary> /// <param name="expression">The expression.</param> protected virtual Expression BindEndWith(MethodCallExpression expression) { LinqStringBuilder.Append("("); Visit(expression.Object); LinqStringBuilder.Append(" LIKE '%' + "); Visit(expression.Arguments[0]); LinqStringBuilder.Append(")"); return(expression); }
/// <summary> /// Binds the <see cref="string.StartsWith(string)"/> method. /// </summary> /// <param name="expression">The expression.</param> protected virtual Expression BindStartWith(MethodCallExpression expression) { LinqStringBuilder.Append("("); this.Visit(expression.Object); LinqStringBuilder.Append(" LIKE "); this.Visit(expression.Arguments[0]); LinqStringBuilder.Append(" + '%')"); return(expression); }
/// <summary> /// Binds the <see cref="string.IsNullOrEmpty(string)"/> method. /// </summary> /// <param name="expression">The expression.</param> protected virtual Expression BindIsNullOrEmpty(MethodCallExpression expression) { LinqStringBuilder.Append("("); this.Visit(expression.Arguments[0]); LinqStringBuilder.Append(" IS NULL OR "); this.Visit(expression.Arguments[0]); LinqStringBuilder.Append(" = '')"); return(expression); }
private Expression VisitOrderBy(MethodCallExpression expression, OrderType orderType) { var lambda = (LambdaExpression)StripQuotes(expression.Arguments[1]); var ret = Visit(lambda); Orders.Add(LinqStringBuilder.ToString() + " " + (orderType == OrderType.Ascending ? "ASC" : "DESC")); LinqStringBuilder.Clear(); return(ret); }
/// <summary> /// Visits the Linq aggregate methods. /// </summary> /// <param name="expression">The expression.</param> /// <param name="aggregateName">Name of aggreage method.</param> protected virtual Expression VisitAggregate(MethodCallExpression expression, string aggregateName) { LinqStringBuilder.Append($"{aggregateName}("); this.Visit(StripQuotes(expression.Arguments[1])); LinqStringBuilder.Append(")"); SelectExpression.SetColumnsExpression(new ColumnsExpression(LinqStringBuilder.ToString())); LinqStringBuilder.Clear(); return(expression); }
/// <summary> /// Binds the <see cref="string.Replace(string, string)"/> method. /// </summary> /// <param name="expression">The expression.</param> protected virtual Expression BindReplace(MethodCallExpression expression) { LinqStringBuilder.Append("REPLACE("); this.Visit(expression.Object); LinqStringBuilder.Append(", "); this.Visit(expression.Arguments[0]); LinqStringBuilder.Append(", "); this.Visit(expression.Arguments[1]); LinqStringBuilder.Append(")"); return(expression); }
/// <summary> /// Visits the Linq Binary. /// </summary> /// <param name="expression">The expression.</param> /// <returns></returns> /// <exception cref="System.NotSupportedException">If this binary expression is not supported.</exception> protected override Expression VisitBinary(BinaryExpression expression) { bool hasBooleanOperator = HasBooleanOperator(expression); LinqStringBuilder.Append("("); ProcessBinaryExpressionOperand(expression.Left, hasBooleanOperator); AppendOperator(expression); ProcessBinaryExpressionOperand(expression.Right, hasBooleanOperator); LinqStringBuilder.Append(")"); return(expression); }
/// <summary> /// Visits the Linq Where method. /// </summary> /// <param name="expression">The expression.</param> protected virtual Expression VisitWhere(MethodCallExpression expression) { LambdaExpression lambda = (LambdaExpression)StripQuotes(expression.Arguments[1]); this.Visit(lambda.Body); SelectExpression.SetWhereExpression(new WhereExpression(LinqStringBuilder.ToString(), LinqParameters.GetParams())); LinqParameters.Clear(); LinqStringBuilder = null; return(expression); }
/// <summary> /// Visits the member. /// </summary> /// <param name="expression">The expression.</param> /// <exception cref="System.NotSupportedException">If the member type is not supported.</exception> protected override Expression VisitMember(MemberExpression expression) { if (expression.Expression != null && (expression.Expression.NodeType == ExpressionType.Parameter || expression.Expression.NodeType == ExpressionType.Convert)) { var columnInfo = DatabaseMapper.GetTableInfo(expression.Member.DeclaringType).GetColumnInfoByPropertyName(expression.Member.Name); LinqStringBuilder.Append(columnInfo.Name); return(expression); } throw new NotSupportedException(string.Format("The member '{0}' is not supported.", expression.Member.Name)); }
private void AppendOperator(BinaryExpression expression) { string op = GetOperator(expression); if (string.IsNullOrEmpty(op)) { throw new NotSupportedException(string.Format(Resources.BinaryOperatorIsNotSupported, expression.NodeType)); } else { LinqStringBuilder.Append($" {op} "); } }
private Expression VisitOrderBy(MethodCallExpression expression, OrderType orderType) { var lambda = (LambdaExpression)StripQuotes(expression.Arguments[1]); var ret = this.Visit(lambda); _orders.Add(new OrderBy() { ColumnaName = LinqStringBuilder.ToString(), Type = orderType }); LinqStringBuilder.Clear(); return(ret); }
/// <summary> /// Visits the constant. /// </summary> /// <param name="expression">The expression.</param> /// <exception cref="System.NotSupportedException">If type of constant is <see cref="System.Object"/>.</exception> protected override Expression VisitConstant(ConstantExpression expression) { if (expression.Value == null) { LinqStringBuilder.Append("NULL"); } else if (expression.Value.GetType() == typeof(bool)) { LinqStringBuilder.Append((bool)expression.Value ? SqlTrue : SqlFalse); } else { AddParameterWithValue(GetConstantExpressionValue(expression)); } return(expression); }
/// <summary> /// Visits the Linq Take method. /// </summary> /// <param name="expression">The expression.</param> /// <exception cref="System.NotSupportedException">If call of Take method is not supported.</exception> protected virtual Expression VisitTake(MethodCallExpression expression) { this.Visit(StripQuotes(expression.Arguments[1])); LinqStringBuilder.Clear(); try { _top = (int)LinqParameters.GetParams().First(); LinqParameters = new Parameters(); return(expression); } catch (Exception e) { throw new NotSupportedException("This call of Take method is not supported", e); } }
/// <summary> /// Visits the Linq Take method. /// </summary> /// <param name="expression">The expression.</param> /// <exception cref="System.NotSupportedException">If call of Take method is not supported.</exception> protected virtual Expression VisitTake(MethodCallExpression expression) { Visit(StripQuotes(expression.Arguments[1])); LinqStringBuilder.Clear(); try { _top = (int)LinqParameters.GetParams().First(); LinqParameters = new Parameters(); return(expression); } catch (Exception ex) { throw new NotSupportedException(Resources.ThisCallOfTakeMethodIsNotSupported, ex); } }
/// <summary> /// Binds the <see cref="string.Substring(int)" autoUpgrade="true" /> method. /// </summary> /// <param name="expression">The expression.</param> protected virtual Expression BindSubstring(MethodCallExpression expression) { LinqStringBuilder.Append("SUBSTRING("); this.Visit(expression.Object); LinqStringBuilder.Append(", "); this.Visit(expression.Arguments[0]); LinqStringBuilder.Append(" + 1, "); if (expression.Arguments.Count == 2) { this.Visit(expression.Arguments[1]); } else { LinqStringBuilder.Append(LinqParameters.GetNextParamName()); LinqParameters.AddParameter(8000); } LinqStringBuilder.Append(")"); return(expression); }
/// <summary> /// Visits the Linq Binary. /// </summary> /// <param name="expression">The expression.</param> /// <returns></returns> /// <exception cref="System.NotSupportedException">If this binary expression is not supported.</exception> protected override Expression VisitBinary(BinaryExpression expression) { LinqStringBuilder.Append("("); this.Visit(expression.Left); var op = GetOperator(expression); switch (expression.NodeType) { case ExpressionType.And: case ExpressionType.AndAlso: case ExpressionType.Or: case ExpressionType.OrElse: case ExpressionType.Equal: case ExpressionType.NotEqual: case ExpressionType.LessThan: case ExpressionType.LessThanOrEqual: case ExpressionType.GreaterThan: case ExpressionType.GreaterThanOrEqual: case ExpressionType.Add: case ExpressionType.AddChecked: case ExpressionType.Subtract: case ExpressionType.SubtractChecked: case ExpressionType.Multiply: case ExpressionType.MultiplyChecked: case ExpressionType.Divide: case ExpressionType.Modulo: case ExpressionType.ExclusiveOr: LinqStringBuilder.Append($" {op} "); break; default: throw new NotSupportedException(string.Format("The binary operator '{0}' is not supported.", expression.NodeType)); } this.Visit(expression.Right); LinqStringBuilder.Append(")"); return(expression); }
/// <summary> /// Visits the unary. /// </summary> /// <param name="expression">The expression.</param> /// <returns></returns> /// <exception cref="System.NotSupportedException">If this unary expression is not supported.</exception> protected override Expression VisitUnary(UnaryExpression expression) { switch (expression.NodeType) { case ExpressionType.Not: LinqStringBuilder.Append(" NOT "); this.Visit(expression.Operand); break; case ExpressionType.Convert: case ExpressionType.ConvertChecked: this.Visit(expression.Operand); break; default: throw new NotSupportedException(string.Format("The unary operator '{0}' is not supported", expression.NodeType)); } return(expression); }
/// <summary> /// Binds the <see cref="string.Substring(int)" autoUpgrade="true" /> method. /// </summary> /// <param name="expression">The expression.</param> protected virtual Expression BindSubstring(MethodCallExpression expression) { const int substringMaxLength = 8000; LinqStringBuilder.Append("SUBSTRING("); Visit(expression.Object); LinqStringBuilder.Append(", "); Visit(expression.Arguments[0]); LinqStringBuilder.Append(" + 1, "); if (expression.Arguments.Count == 2) { Visit(expression.Arguments[1]); } else { AddParameterWithValue(substringMaxLength); } LinqStringBuilder.Append(")"); return(expression); }
/// <summary> /// Visits the unary. /// </summary> /// <param name="expression">The expression.</param> /// <returns></returns> /// <exception cref="System.NotSupportedException">If this unary expression is not supported.</exception> protected override Expression VisitUnary(UnaryExpression expression) { switch (expression.NodeType) { case ExpressionType.Not: LinqStringBuilder.Append(" NOT "); Visit(expression.Operand); break; case ExpressionType.Convert: case ExpressionType.ConvertChecked: Visit(expression.Operand); break; default: throw new NotSupportedException(string.Format(Resources.UnaryOperatorIsNotSupported, expression.NodeType)); } return(expression); }
/// <summary> /// Visits the constant. /// </summary> /// <param name="expression">The expression.</param> /// <exception cref="System.NotSupportedException">If type of constant is <see cref="System.Object"/>.</exception> protected override Expression VisitConstant(ConstantExpression expression) { if (expression.Value == null) { LinqStringBuilder.Append("NULL"); } else { var type = expression.Value.GetType(); if (Type.GetTypeCode(type) == TypeCode.Object && (type != typeof(Guid))) { throw new NotSupportedException(string.Format("The constant for '{0}' is not supported", expression.Value)); } else { LinqStringBuilder.Append(LinqParameters.GetNextParamName()); LinqParameters.AddParameter(expression.Value); } } return(expression); }
private void AppendBooleanCondition(MemberExpression node, bool value, bool negate) { string op = negate ? "<>" : "="; LinqStringBuilder.AppendFormat("({0} {1} {2})", GetColumnNameFromMember(node), op, value ? SqlTrue : SqlFalse); }
private void AppendMemberColumnName(MemberExpression node) => LinqStringBuilder.Append(GetColumnNameFromMember(node));