protected override ISqlBuilder Where(UnaryExpression expression, ISqlBuilder sqlBuilder) { //m=>!m.IsAmdin if (expression.NodeType == ExpressionType.Not && expression.Operand is MemberExpression && expression.Type == typeof(bool)) { var memberExp = expression.Operand as MemberExpression; var tableAliax = GetTableAlias(memberExp, sqlBuilder); var columnName = memberExp.Member.Name; var sqlParamName = sqlBuilder.SetSqlParameter(0); sqlBuilder.AppendWhereSql($"{tableAliax}{sqlBuilder.Formate(columnName)} "); sqlBuilder.AppendWhereSql("= "); sqlBuilder.AppendWhereSql($"{sqlParamName} "); } //DateTime.Now else if (expression.NodeType == ExpressionType.Convert) { var value = TryGetExpreesionValue(expression); var sqlParamName = sqlBuilder.SetSqlParameter(value); sqlBuilder.AppendWhereSql($"{sqlParamName} "); } else { SqlVistorProvider.Where(expression.Operand, sqlBuilder); } return(sqlBuilder); }
protected override ISqlBuilder ThenByDescending(NewExpression expression, ISqlBuilder sqlBuilder) { foreach (Expression item in expression.Arguments) { SqlVistorProvider.ThenByDescending(item, sqlBuilder); } return(sqlBuilder); }
protected override ISqlBuilder OrderBy(NewExpression expression, ISqlBuilder sqlBuilder) { foreach (Expression item in expression.Arguments) { SqlVistorProvider.OrderBy(item, sqlBuilder); } return(sqlBuilder); }
private static void LikeLeft(MethodCallExpression expression, ISqlBuilder sqlBuilder) { if (expression.Object != null) { var mermberExp = expression.Object as MemberExpression; if (MemberIsDataColumn(mermberExp, sqlBuilder)) { SqlVistorProvider.Where(mermberExp, sqlBuilder); sqlBuilder.AppendWhereSql("LIKE '%"); var valueExp = expression.Arguments[0]; if (valueExp is ConstantExpression) { var contantExp = valueExp as ConstantExpression; sqlBuilder.AppendWhereSql($"{contantExp.Value}"); } else { var memberExp = valueExp as MemberExpression; var value = TryGetExpreesionValue(memberExp); sqlBuilder.AppendWhereSql($"{value}"); } sqlBuilder.AppendWhereSql("' "); } } else { var mermberExp = expression.Arguments[0] as MemberExpression; if (MemberIsDataColumn(mermberExp, sqlBuilder)) { SqlVistorProvider.Where(mermberExp, sqlBuilder); sqlBuilder.AppendWhereSql("LIKE '%"); var valueExp = expression.Arguments[1]; if (valueExp is ConstantExpression) { var contantExp = valueExp as ConstantExpression; sqlBuilder.AppendWhereSql($"{contantExp.Value}"); } else { var memberExp = valueExp as MemberExpression; var value = TryGetExpreesionValue(memberExp); sqlBuilder.AppendWhereSql($"{value}"); } sqlBuilder.AppendWhereSql("' "); } } }
private static void InnerIn(MethodCallExpression expression, ISqlBuilder sqlBuilder) { if (expression.Arguments != null && expression.Arguments.Any()) { var mermberExp = expression.Arguments[0] as MemberExpression; if (MemberIsDataColumn(mermberExp, sqlBuilder)) { SqlVistorProvider.Where(mermberExp, sqlBuilder); sqlBuilder.AppendWhereSql("IN "); SqlVistorProvider.In(expression.Arguments[1], sqlBuilder); } } }
protected override ISqlBuilder Delete(BinaryExpression expression, ISqlBuilder sqlBuilder) { var expressionLeft = expression.Left; var expressionRight = expression.Right; if (expressionLeft is MemberExpression) { var memberExp = expressionLeft as MemberExpression; if (!MemberIsDataColumn(memberExp, sqlBuilder)) { throw new NotImplementedException($"{memberExp.Member.Name} is not data column"); } } if (expressionRight is MemberExpression) { var memberExp = expressionRight as MemberExpression; if (!MemberIsDataColumn(memberExp, sqlBuilder)) { throw new NotImplementedException($"{memberExp.Member.Name} is not data column"); } } var isRightHandle = false; if (IsNeedsParentheses(expression, expressionLeft)) { sqlBuilder.AppendWhereSql("( "); } if (expressionLeft is MemberExpression && expressionLeft.Type == typeof(bool) && expressionLeft.NodeType == ExpressionType.MemberAccess && (expression.NodeType == ExpressionType.AndAlso || expressionRight is ConstantExpression)) { SqlVistorProvider.Where(expressionLeft, sqlBuilder); sqlBuilder.AppendWhereSql($"{OperatorParser(ExpressionType.Equal)}"); var sqlParamName = string.Empty; //(m=>m.IsAdmin==true) if (expressionRight is ConstantExpression) { isRightHandle = true; var value = Convert.ToBoolean(((ConstantExpression)expressionRight).Value) ? 1 : 0; sqlParamName = sqlBuilder.SetSqlParameter(value); } else { //(m=>m.IsAdmin) sqlParamName = sqlBuilder.SetSqlParameter(1); } sqlBuilder.AppendWhereSql($"{sqlParamName} "); } else { SqlVistorProvider.Delete(expression.Left, sqlBuilder); } if (IsNeedsParentheses(expression, expressionLeft)) { sqlBuilder.AppendWhereSql(") "); } if (!isRightHandle) { var whereOperator = string.Empty; if ((expressionRight is ConstantExpression) && ((ConstantExpression)expressionRight).Value == null) { whereOperator = OperatorParser(expression.NodeType, true); } else { whereOperator = OperatorParser(expression.NodeType); } sqlBuilder.AppendWhereSql($"{whereOperator}"); if (IsNeedsParentheses(expression, expressionRight)) { sqlBuilder.AppendWhereSql("( "); } if (expressionRight is MemberExpression && expressionRight.Type == typeof(bool) && expression.NodeType == ExpressionType.AndAlso) { //(m=>m.IsAdmin) SqlVistorProvider.Where(expressionRight, sqlBuilder); sqlBuilder.AppendWhereSql($"{OperatorParser(ExpressionType.Equal)}"); var sqlParamName = sqlBuilder.SetSqlParameter(1); sqlBuilder.AppendWhereSql($"{sqlParamName} "); } else if (expressionRight is MemberExpression && IsNodeTypeSetValue(expression.NodeType)) { var memberExp = expressionRight as MemberExpression; var value = TryGetExpreesionValue(memberExp); if (value != null) { var sqlParamName = sqlBuilder.SetSqlParameter(value); sqlBuilder.AppendWhereSql($"{sqlParamName} "); } else { SqlVistorProvider.Join(expression.Right, sqlBuilder); } } else { SqlVistorProvider.Delete(expression.Right, sqlBuilder); } if (IsNeedsParentheses(expression, expressionRight)) { sqlBuilder.AppendWhereSql(") "); } } return(sqlBuilder); }
protected override ISqlBuilder Sum(ParameterExpression expression, ISqlBuilder sqlBuilder) { SqlVistorProvider.In(expression, sqlBuilder); return(sqlBuilder); }
protected override ISqlBuilder ThenBy(UnaryExpression expression, ISqlBuilder sqlBuilder) { SqlVistorProvider.ThenBy(expression.Operand, sqlBuilder); return(sqlBuilder); }
protected override ISqlBuilder OrderByDescending(UnaryExpression expression, ISqlBuilder sqlBuilder) { SqlVistorProvider.OrderByDescending(expression.Operand, sqlBuilder); return(sqlBuilder); }