/// <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 += ")"; } 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> /// JoinParser /// </summary> /// <typeparam name="T2">泛型类型2</typeparam> /// <param name="expression">表达式树</param> /// <param name="leftOrRightJoin">左连接或者右连接</param> /// <returns>SqlBuilderCore</returns> private SqlBuilderCore <T> JoinParser <T2>(Expression <Func <T, T2, bool> > expression, string leftOrRightJoin = "") where T2 : class { string joinTableName = this._sqlPack.GetTableName(typeof(T2)); this._sqlPack.SetTableAlias(joinTableName); if (this._sqlPack.DatabaseType == DatabaseType.Oracle) { this._sqlPack.Sql.Append($"{(string.IsNullOrEmpty(leftOrRightJoin) ? "" : " " + leftOrRightJoin)} JOIN {joinTableName} {this._sqlPack.GetTableAlias(joinTableName)} ON "); } else { this._sqlPack.Sql.Append($"{(string.IsNullOrEmpty(leftOrRightJoin) ? "" : " " + leftOrRightJoin)} JOIN {joinTableName} AS {this._sqlPack.GetTableAlias(joinTableName)} ON "); } SqlBuilderProvider.Join(expression.Body, this._sqlPack); return(this); }
/// <summary> /// Join /// </summary> /// <param name="expression">表达式树</param> /// <param name="sqlPack">sql打包对象</param> /// <returns>SqlPack</returns> public override SqlPack Join(UnaryExpression expression, SqlPack sqlPack) { SqlBuilderProvider.Join(expression.Operand, sqlPack); return(sqlPack); }