/// <summary> /// Join /// </summary> /// <param name="expression">表达式树</param> /// <param name="sqlPack">sql打包对象</param> /// <returns>SqlPack</returns> public override SqlPack Join(BinaryExpression expression, SqlPack sqlPack) { SqlBuilderProvider.Join(expression.Left, sqlPack); var operatorIndex = sqlPack.Sql.Length; //嵌套条件 var flag = false; if (expression.Right is BinaryExpression binaryExpression && (binaryExpression.Right as BinaryExpression) != null) { flag = true; sqlPack += "("; } SqlBuilderProvider.Where(expression.Right, sqlPack); if (flag) { sqlPack += ")"; } var sqlLength = sqlPack.Sql.Length; if (sqlLength - operatorIndex == 5 && sqlPack.ToString().ToUpper().EndsWith("NULL")) { OperatorParser(expression.NodeType, operatorIndex, sqlPack, true); } else { OperatorParser(expression.NodeType, operatorIndex, sqlPack); } return(sqlPack); }
/// <summary> /// Join /// </summary> /// <param name="expression">表达式树</param> /// <param name="sqlPack">sql打包对象</param> /// <returns>SqlPack</returns> public override SqlPack Join(BinaryExpression expression, SqlPack sqlPack) { //左侧嵌套 var leftBinary = expression.Left as BinaryExpression; var isBinaryLeft = leftBinary?.Left is BinaryExpression; var isBoolMethodCallLeft = (leftBinary?.Left as MethodCallExpression)?.Method.ReturnType == typeof(bool); var isBinaryRight = leftBinary?.Right is BinaryExpression; var isBoolMethodCallRight = (leftBinary?.Right as MethodCallExpression)?.Method.ReturnType == typeof(bool); var leftNested = (isBinaryLeft || isBoolMethodCallLeft) && (isBinaryRight || isBoolMethodCallRight); if (leftNested) { sqlPack += "("; } SqlBuilderProvider.Join(expression.Left, sqlPack); if (leftNested) { sqlPack += ")"; } var operatorIndex = sqlPack.Sql.Length; //右侧嵌套 var rightBinary = expression.Right as BinaryExpression; isBinaryLeft = rightBinary?.Left is BinaryExpression; isBoolMethodCallLeft = (rightBinary?.Left as MethodCallExpression)?.Method.ReturnType == typeof(bool); isBinaryRight = rightBinary?.Right is BinaryExpression; isBoolMethodCallRight = (rightBinary?.Right as MethodCallExpression)?.Method.ReturnType == typeof(bool); var rightNested = (isBinaryLeft || isBoolMethodCallLeft) && (isBinaryRight || isBoolMethodCallRight); if (rightNested) { sqlPack += "("; } SqlBuilderProvider.Where(expression.Right, sqlPack); if (rightNested) { sqlPack += ")"; } //表达式左侧为bool类型常量且为true时,不进行sql拼接 if (!(expression.Left.NodeType == ExpressionType.Constant && expression.Left.ToObject() is bool b && b)) { var sqlLength = sqlPack.Sql.Length; if (sqlLength - operatorIndex == 5 && sqlPack.ToString().ToUpper().EndsWith("NULL")) { OperatorParser(expression.NodeType, operatorIndex, sqlPack, true); } else { OperatorParser(expression.NodeType, operatorIndex, sqlPack); } } return(sqlPack); }
/// <summary> /// Insert /// </summary> /// <param name="expression"></param> /// <param name="sqlPack"></param> /// <returns></returns> public override SqlPack Insert(MethodCallExpression expression, SqlPack sqlPack) { var fields = new List <string>(); var array = expression.ToObject() as object[]; for (var i = 0; i < array.Length; i++) { if (sqlPack.DatabaseType != DatabaseType.Oracle) { sqlPack.Sql.Append("("); } if (i > 0 && sqlPack.DatabaseType == DatabaseType.Oracle) { sqlPack.Sql.Append(" UNION ALL SELECT "); } var properties = array[i]?.GetType().GetProperties(); foreach (var p in properties) { var type = p.DeclaringType.ToString().Contains("AnonymousType") ? sqlPack.DefaultType : p.DeclaringType; (string columnName, bool isInsert, bool isUpdate) = sqlPack.GetColumnInfo(type, p); if (isInsert) { var value = p.GetValue(array[i], null); if (value != null || (sqlPack.IsEnableNullValue && value == null)) { sqlPack.AddDbParameter(value); if (!fields.Contains(columnName)) { fields.Add(columnName); } sqlPack += ","; } } } if (sqlPack[sqlPack.Length - 1] == ',') { sqlPack.Sql.Remove(sqlPack.Length - 1, 1); if (sqlPack.DatabaseType != DatabaseType.Oracle) { sqlPack.Sql.Append("),"); } else { sqlPack.Sql.Append(" FROM DUAL"); } } } if (sqlPack.Sql[sqlPack.Sql.Length - 1] == ',') { sqlPack.Sql.Remove(sqlPack.Sql.Length - 1, 1); } sqlPack.Sql = new StringBuilder(string.Format(sqlPack.ToString(), string.Join(",", fields).TrimEnd(','))); return(sqlPack); }
/// <summary> /// Join /// </summary> /// <param name="expression">表达式树</param> /// <param name="sqlPack">sql打包对象</param> /// <returns>SqlPack</returns> public override SqlPack Join(ConstantExpression expression, SqlPack sqlPack) { //表达式左侧为bool类型常量 if (expression.NodeType == ExpressionType.Constant && expression.Value is bool b) { var sql = sqlPack.ToString().ToUpper().Trim(); if (!b && (sql.EndsWith("AND") || sql.EndsWith("OR"))) { sqlPack += " 1 = 0 "; } } else { sqlPack.AddDbParameter(expression.Value); } return(sqlPack); }
/// <summary> /// Equals /// </summary> /// <param name="expression">表达式树</param> /// <param name="sqlPack">sql打包对象</param> private static void Equals(MethodCallExpression expression, SqlPack sqlPack) { if (expression.Object != null) { SqlBuilderProvider.Where(expression.Object, sqlPack); } var signIndex = sqlPack.Length; SqlBuilderProvider.Where(expression.Arguments[0], sqlPack); if (sqlPack.ToString().ToUpper().EndsWith("NULL")) { sqlPack.Sql.Insert(signIndex, " IS "); } else { sqlPack.Sql.Insert(signIndex, " = "); } }
/// <summary> /// Insert /// </summary> /// <param name="expression">表达式树</param> /// <param name="sqlPack">sql打包对象</param> /// <returns>SqlPack</returns> public override SqlPack Insert(NewExpression expression, SqlPack sqlPack) { if (sqlPack.DatabaseType != DatabaseType.Oracle) { sqlPack.Sql.Append("("); } var fields = new List <string>(); for (int i = 0; i < expression.Members?.Count; i++) { var m = expression.Members[i]; var t = m.DeclaringType.ToString().Contains("AnonymousType") ? sqlPack.DefaultType : m.DeclaringType; (string columnName, bool isInsert, bool isUpdate) = sqlPack.GetColumnInfo(t, m); if (isInsert) { var value = expression.Arguments[i]?.ToObject(); if (value != null || (sqlPack.IsEnableNullValue && value == null)) { sqlPack.AddDbParameter(value); if (!fields.Contains(columnName)) { fields.Add(columnName); } sqlPack += ","; } } } if (sqlPack[sqlPack.Length - 1] == ',') { sqlPack.Sql.Remove(sqlPack.Length - 1, 1); if (sqlPack.DatabaseType != DatabaseType.Oracle) { sqlPack.Sql.Append(")"); } else { sqlPack.Sql.Append(" FROM DUAL"); } } sqlPack.Sql = new StringBuilder(string.Format(sqlPack.ToString(), string.Join(",", fields).TrimEnd(','))); return(sqlPack); }
/// <summary> /// Where /// </summary> /// <param name="expression">表达式树</param> /// <param name="sqlPack">sql打包对象</param> /// <returns>SqlPack</returns> public override SqlPack Where(UnaryExpression expression, SqlPack sqlPack) { var startIndex = sqlPack.Length; SqlBuilderProvider.Where(expression.Operand, sqlPack); if (expression.NodeType == ExpressionType.Not) { var subString = sqlPack.ToString().Substring(startIndex, sqlPack.ToString().Length - startIndex).ToUpper(); //IS NOT、IS if (subString.Contains("IS NOT")) { var index = sqlPack.ToString().LastIndexOf("IS NOT"); if (index != -1) { sqlPack.Sql.Replace("IS NOT", "IS", index, 6); } } if (subString.Contains("IS") && subString.LastIndexOf("IS") != subString.LastIndexOf("IS NOT")) { var index = sqlPack.ToString().LastIndexOf("IS"); if (index != -1) { sqlPack.Sql.Replace("IS", "IS NOT", index, 2); } } //NOT LIKE、LIKE if (subString.Contains("NOT LIKE")) { var index = sqlPack.ToString().LastIndexOf("NOT LIKE"); if (index != -1) { sqlPack.Sql.Replace("NOT LIKE", "LIKE", index, 8); } } if (subString.Contains("LIKE") && subString.LastIndexOf("LIKE") != (subString.LastIndexOf("NOT LIKE") + 4)) { var index = sqlPack.ToString().LastIndexOf("LIKE"); if (index != -1) { sqlPack.Sql.Replace("LIKE", "NOT LIKE", index, 4); } } //NOT IN、IN if (subString.Contains("NOT IN")) { var index = sqlPack.ToString().LastIndexOf("NOT IN"); if (index != -1) { sqlPack.Sql.Replace("NOT IN", "IN", index, 6); } } if (subString.Contains("IN") && subString.LastIndexOf("IN") != (subString.LastIndexOf("NOT IN") + 4)) { var index = sqlPack.ToString().LastIndexOf("IN"); if (index != -1) { sqlPack.Sql.Replace("IN", "NOT IN", index, 2); } } //AND、OR if (subString.Contains("AND")) { var index = sqlPack.ToString().LastIndexOf("AND"); if (index != -1) { sqlPack.Sql.Replace("AND", "OR", index, 3); } } if (subString.Contains("OR")) { var index = sqlPack.ToString().LastIndexOf("OR"); if (index != -1) { sqlPack.Sql.Replace("OR", "AND", index, 2); } } //=、<> if (subString.Contains(" = ")) { var index = sqlPack.ToString().LastIndexOf(" = "); if (index != -1) { sqlPack.Sql.Replace(" = ", " <> ", index, 3); } } if (subString.Contains("<>")) { var index = sqlPack.ToString().LastIndexOf("<>"); if (index != -1) { sqlPack.Sql.Replace("<>", "=", index, 2); } } //>、< if (subString.Contains(" > ")) { var index = sqlPack.ToString().LastIndexOf(" > "); if (index != -1) { sqlPack.Sql.Replace(" > ", " <= ", index, 3); } } if (subString.Contains(" < ")) { var index = sqlPack.ToString().LastIndexOf(" < "); if (index != -1) { sqlPack.Sql.Replace(" < ", " >= ", index, 3); } } //>=、<= if (subString.Contains(" >= ")) { var index = sqlPack.ToString().LastIndexOf(" >= "); if (index != -1) { sqlPack.Sql.Replace(" >= ", " < ", index, 4); } } if (subString.Contains(" <= ")) { var index = sqlPack.ToString().LastIndexOf(" <= "); if (index != -1) { sqlPack.Sql.Replace(" <= ", " > ", index, 4); } } } return(sqlPack); }
/// <summary> /// Where /// </summary> /// <param name="expression">表达式树</param> /// <param name="sqlPack">sql打包对象</param> /// <returns>SqlPack</returns> public override SqlPack Where(BinaryExpression expression, SqlPack sqlPack) { var startIndex = sqlPack.Length; SqlBuilderProvider.Where(expression.Left, sqlPack); var signIndex = sqlPack.Length; //嵌套条件 var flag = false; if (expression.Right is BinaryExpression binaryExpression && (binaryExpression.Right as BinaryExpression) != null) { flag = true; sqlPack += "("; } SqlBuilderProvider.Where(expression.Right, sqlPack); if (flag) { sqlPack += ")"; } //表达式左侧为bool类型常量且为true时,不进行Sql拼接 if (!(expression.Left.NodeType == ExpressionType.Constant && expression.Left.ToObject() is bool b && b)) { //若表达式右侧为bool类型,且为false时,条件取非 if (expression.Right.NodeType == ExpressionType.Constant && expression.Right.ToObject() is bool r) { if (!r) { var subString = sqlPack.ToString().Substring(startIndex, sqlPack.ToString().Length - startIndex).ToUpper(); //IS NOT、IS if (subString.Contains("IS NOT")) { var index = sqlPack.ToString().LastIndexOf("IS NOT"); if (index != -1) { sqlPack.Sql.Replace("IS NOT", "IS", index, 6); } } if (subString.Contains("IS") && subString.LastIndexOf("IS") != subString.LastIndexOf("IS NOT")) { var index = sqlPack.ToString().LastIndexOf("IS"); if (index != -1) { sqlPack.Sql.Replace("IS", "IS NOT", index, 2); } } //NOT LIKE、LIKE if (subString.Contains("NOT LIKE")) { var index = sqlPack.ToString().LastIndexOf("NOT LIKE"); if (index != -1) { sqlPack.Sql.Replace("NOT LIKE", "LIKE", index, 8); } } if (subString.Contains("LIKE") && subString.LastIndexOf("LIKE") != (subString.LastIndexOf("NOT LIKE") + 4)) { var index = sqlPack.ToString().LastIndexOf("LIKE"); if (index != -1) { sqlPack.Sql.Replace("LIKE", "NOT LIKE", index, 4); } } //=、<> if (subString.Contains(" = ")) { var index = sqlPack.ToString().LastIndexOf(" = "); if (index != -1) { sqlPack.Sql.Replace(" = ", " <> ", index, 3); } } if (subString.Contains("<>")) { var index = sqlPack.ToString().LastIndexOf("<>"); if (index != -1) { sqlPack.Sql.Replace("<>", "=", index, 2); } } //>、< if (subString.Contains(" > ")) { var index = sqlPack.ToString().LastIndexOf(" > "); if (index != -1) { sqlPack.Sql.Replace(" > ", " <= ", index, 3); } } if (subString.Contains(" < ")) { var index = sqlPack.ToString().LastIndexOf(" < "); if (index != -1) { sqlPack.Sql.Replace(" < ", " >= ", index, 3); } } //>=、<= if (subString.Contains(" >= ")) { var index = sqlPack.ToString().LastIndexOf(" >= "); if (index != -1) { sqlPack.Sql.Replace(" >= ", " < ", index, 4); } } if (subString.Contains(" <= ")) { var index = sqlPack.ToString().LastIndexOf(" <= "); if (index != -1) { sqlPack.Sql.Replace(" <= ", " > ", index, 4); } } //AND、OR if (subString.Contains("AND")) { var index = sqlPack.ToString().LastIndexOf("AND"); if (index != -1) { sqlPack.Sql.Replace("AND", "OR", index, 3); } } if (subString.Contains("OR")) { var index = sqlPack.ToString().LastIndexOf("OR"); if (index != -1) { sqlPack.Sql.Replace("OR", "AND", index, 2); } } } } else { if (sqlPack.ToString().ToUpper().EndsWith("NULL")) { OperatorParser(expression.NodeType, signIndex, sqlPack, true); } else { OperatorParser(expression.NodeType, signIndex, sqlPack); } } } return(sqlPack); }
/// <summary> /// Where /// </summary> /// <param name="expression">表达式树</param> /// <param name="sqlPack">sql打包对象</param> /// <returns>SqlPack</returns> public override SqlPack Where(BinaryExpression expression, SqlPack sqlPack) { var startIndex = sqlPack.Length; //左侧嵌套 var leftBinary = expression.Left as BinaryExpression; var isBinaryLeft = leftBinary?.Left is BinaryExpression; var isBoolMethodCallLeft = (leftBinary?.Left as MethodCallExpression)?.Method.ReturnType == typeof(bool); var isBinaryRight = leftBinary?.Right is BinaryExpression; var isBoolMethodCallRight = (leftBinary?.Right as MethodCallExpression)?.Method.ReturnType == typeof(bool); var leftNested = (isBinaryLeft || isBoolMethodCallLeft) && (isBinaryRight || isBoolMethodCallRight); if (leftNested) { sqlPack += "("; } SqlBuilderProvider.Where(expression.Left, sqlPack); if (leftNested) { sqlPack += ")"; } var signIndex = sqlPack.Length; //右侧嵌套 var rightBinary = expression.Right as BinaryExpression; isBinaryLeft = rightBinary?.Left is BinaryExpression; isBoolMethodCallLeft = (rightBinary?.Left as MethodCallExpression)?.Method.ReturnType == typeof(bool); isBinaryRight = rightBinary?.Right is BinaryExpression; isBoolMethodCallRight = (rightBinary?.Right as MethodCallExpression)?.Method.ReturnType == typeof(bool); var rightNested = (isBinaryLeft || isBoolMethodCallLeft) && (isBinaryRight || isBoolMethodCallRight); if (rightNested) { sqlPack += "("; } SqlBuilderProvider.Where(expression.Right, sqlPack); if (rightNested) { sqlPack += ")"; } //表达式左侧为bool类型常量且为true时,不进行Sql拼接 if (!(expression.Left.NodeType == ExpressionType.Constant && expression.Left.ToObject() is bool b && b)) { //若表达式右侧为bool类型,且为false时,条件取非 if ((expression.Right.NodeType == ExpressionType.Constant || (expression.Right.NodeType == ExpressionType.Convert && expression.Right is UnaryExpression unary && unary.Operand.NodeType == ExpressionType.Constant)) && expression.Right.ToObject() is bool r) { if (!r) { var subString = sqlPack.ToString().Substring(startIndex, sqlPack.ToString().Length - startIndex).ToUpper(); //IS NOT、IS if (subString.Contains("IS NOT")) { var index = sqlPack.ToString().LastIndexOf("IS NOT"); if (index != -1) { sqlPack.Sql.Replace("IS NOT", "IS", index, 6); } } if (subString.Contains("IS") && subString.LastIndexOf("IS") != subString.LastIndexOf("IS NOT")) { var index = sqlPack.ToString().LastIndexOf("IS"); if (index != -1) { sqlPack.Sql.Replace("IS", "IS NOT", index, 2); } } //NOT LIKE、LIKE if (subString.Contains("NOT LIKE")) { var index = sqlPack.ToString().LastIndexOf("NOT LIKE"); if (index != -1) { sqlPack.Sql.Replace("NOT LIKE", "LIKE", index, 8); } } if (subString.Contains("LIKE") && subString.LastIndexOf("LIKE") != (subString.LastIndexOf("NOT LIKE") + 4)) { var index = sqlPack.ToString().LastIndexOf("LIKE"); if (index != -1) { sqlPack.Sql.Replace("LIKE", "NOT LIKE", index, 4); } } //NOT IN、IN if (subString.Contains("NOT IN")) { var index = sqlPack.ToString().LastIndexOf("NOT IN"); if (index != -1) { sqlPack.Sql.Replace("NOT IN", "IN", index, 6); } } if (subString.Contains("IN") && subString.LastIndexOf("IN") != (subString.LastIndexOf("NOT IN") + 4)) { var index = sqlPack.ToString().LastIndexOf("IN"); if (index != -1) { sqlPack.Sql.Replace("IN", "NOT IN", index, 2); } } //AND、OR if (subString.Contains("AND")) { var index = sqlPack.ToString().LastIndexOf("AND"); if (index != -1) { sqlPack.Sql.Replace("AND", "OR", index, 3); } } if (subString.Contains("OR")) { var index = sqlPack.ToString().LastIndexOf("OR"); if (index != -1) { sqlPack.Sql.Replace("OR", "AND", index, 2); } } //=、<> if (subString.Contains(" = ")) { var index = sqlPack.ToString().LastIndexOf(" = "); if (index != -1) { sqlPack.Sql.Replace(" = ", " <> ", index, 3); } } if (subString.Contains("<>")) { var index = sqlPack.ToString().LastIndexOf("<>"); if (index != -1) { sqlPack.Sql.Replace("<>", "=", index, 2); } } //>、< if (subString.Contains(" > ")) { var index = sqlPack.ToString().LastIndexOf(" > "); if (index != -1) { sqlPack.Sql.Replace(" > ", " <= ", index, 3); } } if (subString.Contains(" < ")) { var index = sqlPack.ToString().LastIndexOf(" < "); if (index != -1) { sqlPack.Sql.Replace(" < ", " >= ", index, 3); } } //>=、<= if (subString.Contains(" >= ")) { var index = sqlPack.ToString().LastIndexOf(" >= "); if (index != -1) { sqlPack.Sql.Replace(" >= ", " < ", index, 4); } } if (subString.Contains(" <= ")) { var index = sqlPack.ToString().LastIndexOf(" <= "); if (index != -1) { sqlPack.Sql.Replace(" <= ", " > ", index, 4); } } } }