/// <summary> /// Binds the <see cref="string.EndsWith(string)"/> method. /// </summary> /// <param name="expression">The expression.</param> protected virtual Expression BindEndWith(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); }
/// <summary> /// Binds the <see cref="string.StartsWith(string)"/> method. /// </summary> /// <param name="expression">The expression.</param> protected virtual Expression BindStartWith(MethodCallExpression expression) { LinqStringBuilder.Append("("); Visit(expression.Object); LinqStringBuilder.Append(" LIKE "); Visit(expression.Arguments[0]); LinqStringBuilder.Append(" + '%')"); 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 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> /// 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 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} "); } }
/// <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> /// 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 AppendMemberColumnName(MemberExpression node) => LinqStringBuilder.Append(GetColumnNameFromMember(node));