/// <summary> /// 处理手工联接及其对应的自动联接 /// </summary> private void BuildJoins(List <SqlJoin> joins, BuildQueryContext ctx) { foreach (var item in joins) { //先处理当前的联接 ctx.Append(GetJoinString(item.JoinType)); if (item.Right is SqlQueryJoin) { var j = (SqlQueryJoin)item.Right; var jModel = Runtime.RuntimeContext.Current.GetModelAsync <EntityModel>(j.T.ModelID).Result; ctx.AppendFormat("\"{0}\" {1} On ", jModel.GetSqlTableName(false, null), j.AliasName); BuildExpression(item.OnConditon, ctx); //再处理手工联接的自动联接 ctx.BuildQueryAutoJoins(j); } else //否则表示联接对象是SubQuery,注意:子查询没有自动联接 { SqlSubQuery sq = (SqlSubQuery)item.Right; ctx.Append("("); BuildNormalQuery(sq.Target, ctx); ctx.AppendFormat(") As {0} On ", ((SqlQueryBase)sq.Target).AliasName); BuildExpression(item.OnConditon, ctx); } //最后递归当前联接的右部是否还有手工的联接项 if (item.Right.HasJoins) { BuildJoins(item.Right.Joins, ctx); } } }
private void LoopAddSubQueryJoins(SqlSubQuery query) { if (query.HasJoins) { foreach (var item in query.Joins) { if (item.Right is SqlQueryJoin) //注意:子查询不具备自动联接 { LoopAddQueryJoins((SqlQueryBase)item.Right); } else { LoopAddSubQueryJoins((SqlSubQuery)item.Right); } } } }
private void BuildSubQuery(SqlSubQuery exp, BuildQueryContext ctx) { ctx.Append("("); BuildNormalQuery(exp.Target, ctx); ctx.Append(")"); }