Commits

Inside Zhou committed a094bf4

[bugfix]查找QueryPlan的索引计算错误,重构了这部分代码,将索引计算都放到_FindQueryPlanIndex中。

Comments (0)

Files changed (1)

 			var dt = this.GetDataTable();
 
 			return dt.ToDynamicObjects((dr) => {
-				var dyRow = _ParseQueryResult(dr, this).payload;
+				var dyRow = _ParseQueryResult(dr, this);
 				return null == this.RowHandler ? dyRow : this.RowHandler(dyRow);
 			});
 		}
 		/// 通过列的guid获取该列所属QueryPlan对象的别名,通过Join进入column数据结构的QueryPlan也在查询范围内并且返回的别名将依赖与其在column结构中的位置(除非它自身已经声明了别名)。
 		/// </summary>
 		/// <param name="queryPlan">作为查询起点的QueryPlan</param>
-		static string _FindTableAlias(QueryPlan queryPlan, string guid, int tbIndex = 0) {
-			foreach(var kv in queryPlan._column) {
-				dynamic col = kv.Value;
-				if(col._guid == guid) {
-					return _GenerateQueryPlanAlias(queryPlan, tbIndex);
-				}
-				else if(!ObjectHelper.IsNil(col["join"])) {
-					var c = _FindTableAlias(col.join.plan, guid, ++tbIndex);
-					if(!string.IsNullOrEmpty(c)) {
-						return c;
+		static DyObj _FindQueryPlanIndex(QueryPlan rootPlan, QueryPlan targetPlan, int tbIndex = 0) {
+			dynamic result = new DyObj();
+			result.found = false;
+
+			if(null == rootPlan) {
+				result.found = true;
+			}
+			else {
+				foreach(var kv in rootPlan._column) {
+					dynamic col = kv.Value;
+					if(!ObjectHelper.IsNil(col["join"])) {
+						++tbIndex;
+						var plan = col.join.plan;
+						if(targetPlan == plan) {
+							result.found = true;
+							break;
+						}
+						else {
+							var r = _FindQueryPlanIndex(plan, targetPlan, tbIndex);
+							tbIndex = r.index;
+							if(r.found) {
+								result.found = true;
+								break;
+							}
+						}
 					}
 				}
 			}
 
-			return null;
+			result.index = tbIndex;
+			return result;
 		}
 
-		static DyObj _GenerateSQLObject(QueryPlan queryPlan, int queryPlanIndexer = 0, int conditionIndexer = 0, List<DbParameter> parameters = null) {
+		static string _GenerateQueryPlanAlias(QueryPlan plan, int index) {
+			return string.IsNullOrEmpty(plan._alias) ? "t" + index : (plan._alias as string).Substr();
+		}
+
+		static DyObj _GenerateSQLObject(QueryPlan queryPlan, QueryPlan rootPlan = null, int conditionIndexer = 0, List<DbParameter> parameters = null) {
 			parameters = parameters ?? new List<DbParameter>();
 
 			var joinClause = new StringBuilder();
 			var contextualColumns = new DyObj();
 			var columnClause = new StringBuilder();
-			var tbIndex = queryPlanIndexer;
+			var tbIndex = (int)_FindQueryPlanIndex(rootPlan, queryPlan)["index"];
 			var tbAlias = _GenerateQueryPlanAlias(queryPlan, tbIndex);
 
 			foreach(var kv in queryPlan._column) {
 				contextualColumns[c._guid] = contextualCol;
 
 				if(!ObjectHelper.IsNil(c["join"])) {
-					dynamic join = _GenerateSQLObject(c.join.plan as QueryPlan, ++queryPlanIndexer, conditionIndexer, parameters);
+					dynamic join = _GenerateSQLObject(c.join.plan as QueryPlan, rootPlan ?? queryPlan, conditionIndexer, parameters);
 
-					queryPlanIndexer = join._queryplan_indexer;
 					conditionIndexer = join._condition_indexer;
 
 					var joinTxt = string.Empty;
 			result._contextual_columns = contextualColumns;
 			result._contextual_tb_alias = tbAlias;
 
-			result._queryplan_indexer = queryPlanIndexer;
 			result._condition_indexer = conditionIndexer;
 			result._join_clause = joinClause.ToString();
 
 			return result;
 		}
 
-		static string _GenerateQueryPlanAlias(QueryPlan plan, int index) {
-			return string.IsNullOrEmpty(plan._alias) ? "t" + index : (plan._alias as string).Substr();
-		}
-
-		//参数guid由父级传递进来,用于判断当前被join的对象是否应为DBNull.Value。
-		static dynamic _ParseQueryResult(DataRow dr, QueryPlan queryPlan, int qpCount = 0, string guid = null) {
-			dynamic payload = new DyObj();
-			var tbAlias = _GenerateQueryPlanAlias(queryPlan, qpCount);
+		//参数guid由父级传递进来,用于判断当前被join的对象是否应为null。
+		static dynamic _ParseQueryResult(DataRow dr, QueryPlan queryPlan, QueryPlan rootPlan = null, string guid = null) {
+			dynamic result = new DyObj();
+			var tbAlias = _GenerateQueryPlanAlias(queryPlan, (int)_FindQueryPlanIndex(rootPlan, queryPlan)["index"]);
 
 			foreach(var kv in queryPlan._column) {
 				dynamic c = kv.Value;
 				var colName = "{0}_{1}".Fmt(tbAlias, string.IsNullOrEmpty(c.alias) ? c.attr_name as string : (c.alias as string).Substr());
 				if(ObjectHelper.IsNil(c["join"])) {
 					if(!string.IsNullOrEmpty(guid) && c._guid == guid && ObjectHelper.IsNil(dr[colName])) {
-						dynamic r = new DyObj();
-						r.query_plan_count = qpCount;
-						r.payload = null;
-						return r;
+						return null;
 					}
-					payload[c.attr_name] = dr[colName] is DBNull ? null : dr[colName];
+					result[c.attr_name] = dr[colName] is DBNull ? null : dr[colName];
 				}
 				else {
-					var parsedJoinColumnResult = _ParseQueryResult(dr, c.join.plan, ++qpCount, c.join.column._guid);
-					qpCount = parsedJoinColumnResult.query_plan_count;
-					payload[c.attr_name] = parsedJoinColumnResult.payload;
+					result[c.attr_name] = _ParseQueryResult(dr, c.join.plan, rootPlan ?? queryPlan, c.join.column._guid);
 				}
 
 				if(dr.Table.Columns.Contains(Rownum)) {
-					payload[Rownum] = dr[Rownum];
+					result[Rownum] = dr[Rownum];
 				}
 			}
 
-			dynamic result = new DyObj();
-			result.query_plan_count = qpCount;
-			result.payload = payload;
 			return result;
 		}
 	}