/// <summary> /// Processes the body children. /// </summary> /// <param name="request">The request.</param> /// <param name="ctx">The CTX.</param> /// <param name="parameterObject">The parameter object.</param> /// <param name="childEnumerator">The child enumerator.</param> /// <param name="buffer">The buffer.</param> private void ProcessBodyChildren( RequestScope request, SqlTagContext ctx, object parameterObject, IEnumerator <ISqlChild> childEnumerator, StringBuilder buffer) { while (childEnumerator.MoveNext()) { ISqlChild child = childEnumerator.Current; if (child is SqlText) { // this represents the content within a mybatis xml tag, typically the hand-crafted sql itself var sqlText = (SqlText)child; if (sqlText.IsWhiteSpace) { buffer.Append(sqlText.Text); } else { // BODY OUT var textPropertyProbe = new TextPropertyProbe(sqlText.Text); // process the sql text content, replacing bindings and property usage. var sqlStatment = textPropertyProbe.Process(new DynamicSqlTextTokenHandler(ctx, sqlText)); buffer.Append(" "); buffer.Append(sqlStatment); ctx.AddParameterMappings(sqlText); } } else if (child is SqlTag) { SqlTag tag = (SqlTag)child; ISqlTagHandler handler = tag.Handler; int response = BaseTagHandler.INCLUDE_BODY; do { var body = new StringBuilder(); // if the tag is a bind element, add the bind element to a list so that we can later on work out how to replace variable usage. if (tag is Bind) { ctx.RememberBinding((Bind)tag); } response = handler.DoStartFragment(ctx, tag, parameterObject); // replace any bind variables found within the body content. // what's a bit confusing here is that the body element is essentially populated by actions that take place after this line // a recursive call could then be made which results in this being called again. ctx.ReplaceBindingVariables(body); if (response == BaseTagHandler.SKIP_BODY) { break; } ctx.CheckAssignFirstDynamicTagWithPrepend(tag); ProcessBodyChildren(request, ctx, parameterObject, tag.GetChildrenEnumerator(), body); response = handler.DoEndFragment(ctx, tag, parameterObject, body); handler.DoPrepend(ctx, tag, parameterObject, body); if (response == BaseTagHandler.SKIP_BODY) { break; } if (body.Length > 0) { // BODY OUT if (handler.IsPostParseRequired) { var sqlText = InlineParameterMapParser.ParseInlineParameterMap(dataExchangeFactory, statement.Id, null, body.ToString()); sqlText.Parent = tag; buffer.Append(sqlText.Text); ctx.AddParameterMappings(sqlText); } else { buffer.Append(" "); buffer.Append(body.ToString()); } if (tag.IsPrependAvailable && tag == ctx.FirstNonDynamicTagWithPrepend) { ctx.IsOverridePrepend = false; } } }while (response == BaseTagHandler.REPEAT_BODY); } } }
/// <summary> /// Processes the body children. /// 最终的处理的语句结果在buffer中,也即ctx中的StringBuilder,处理的参数结果在ctx中的ArrayList中 /// </summary> /// <param name="request">The request.</param> /// <param name="ctx">The CTX.</param> /// <param name="parameterObject">The parameter object.</param> /// <param name="childEnumerator">The child enumerator.</param> /// <param name="buffer">The buffer.</param> private void ProcessBodyChildren( RequestScope request, SqlTagContext ctx, object parameterObject, IEnumerator <ISqlChild> childEnumerator, StringBuilder buffer) { while (childEnumerator.MoveNext()) { ISqlChild child = childEnumerator.Current; if (child is SqlText) { SqlText sqlText = (SqlText)child; string sqlStatement = sqlText.Text; if (sqlText.IsWhiteSpace) { buffer.Append(sqlStatement); } else { // if (SimpleDynamicSql.IsSimpleDynamicSql(sqlStatement)) // { // sqlStatement = new SimpleDynamicSql(sqlStatement, _statement).GetSql(parameterObject); // SqlText newSqlText = _paramParser.ParseInlineParameterMap( null, sqlStatement ); // sqlStatement = newSqlText.Text; // ParameterProperty[] mappings = newSqlText.Parameters; // if (mappings != null) // { // for (int i = 0; i < mappings.Length; i++) // { // ctx.AddParameterMapping(mappings[i]); // } // } // } // BODY OUT buffer.Append(" "); buffer.Append(sqlStatement); //添加当前的SQL语句 //处理参数列表 ParameterProperty[] parameters = sqlText.Parameters; if (parameters != null) { int length = parameters.Length; for (int i = 0; i < length; i++) { ctx.AddParameterMapping(parameters[i]); } } } } else if (child is SqlTag) { SqlTag tag = (SqlTag)child; ISqlTagHandler handler = tag.Handler;//此处是DynamicTagHandler处理类 int response = BaseTagHandler.INCLUDE_BODY; do { StringBuilder body = new StringBuilder(); //返回INCLUDE_BODY,即1 此处为处理开头的情况准备 response = handler.DoStartFragment(ctx, tag, parameterObject); if (response != BaseTagHandler.SKIP_BODY) { if (ctx.IsOverridePrepend && ctx.FirstNonDynamicTagWithPrepend == null && tag.IsPrependAvailable && !(tag.Handler is DynamicTagHandler)) { //此处应该是判断第一个查询条件时的开头问题 ctx.FirstNonDynamicTagWithPrepend = tag; } //递归调用,当前SqlTag中处理后的SQL语句保存到了临时的body中 后面再加入到buffer中 ProcessBodyChildren(request, ctx, parameterObject, tag.GetChildrenEnumerator(), body); //处理结尾的片段部分 返回INCLUDE_BODY,1 response = handler.DoEndFragment(ctx, tag, parameterObject, body); //处理prepend属性节点的问题 handler.DoPrepend(ctx, tag, parameterObject, body); if (response != BaseTagHandler.SKIP_BODY) { if (body.Length > 0) { // BODY OUT if (handler.IsPostParseRequired) { //将body中的SQL语句分析参数和SQL语句到SqlText中 SqlText sqlText = InlineParameterMapParser.ParseInlineParameterMap(dataExchangeFactory, statement.Id, null, body.ToString()); buffer.Append(sqlText.Text); ParameterProperty[] mappings = sqlText.Parameters; if (mappings != null) { int length = mappings.Length; for (int i = 0; i < length; i++) { ctx.AddParameterMapping(mappings[i]); } } } else { buffer.Append(" "); buffer.Append(body.ToString()); } //此处是判断SQL语句查询条件时的关键字开头问题 if (tag.IsPrependAvailable && tag == ctx.FirstNonDynamicTagWithPrepend) { ctx.IsOverridePrepend = false; } } } } }while (response == BaseTagHandler.REPEAT_BODY); } } }
/// <summary> /// /// </summary> /// <param name="request"></param> /// <param name="ctx"></param> /// <param name="parameterObject"></param> /// <param name="localChildren"></param> /// <param name="buffer"></param> private void ProcessBodyChildren(RequestScope request, SqlTagContext ctx, object parameterObject, IEnumerator localChildren, StringBuilder buffer) { while (localChildren.MoveNext()) { ISqlChild child = (ISqlChild)localChildren.Current; if (child is SqlText) { SqlText sqlText = (SqlText)child; string sqlStatement = sqlText.Text; if (sqlText.IsWhiteSpace) { buffer.Append(sqlStatement); } else { // if (SimpleDynamicSql.IsSimpleDynamicSql(sqlStatement)) // { // sqlStatement = new SimpleDynamicSql(sqlStatement, _statement).GetSql(parameterObject); // SqlText newSqlText = _paramParser.ParseInlineParameterMap( null, sqlStatement ); // sqlStatement = newSqlText.Text; // ParameterProperty[] mappings = newSqlText.Parameters; // if (mappings != null) // { // for (int i = 0; i < mappings.Length; i++) // { // ctx.AddParameterMapping(mappings[i]); // } // } // } // BODY OUT buffer.Append(" "); buffer.Append(sqlStatement); ParameterProperty[] parameters = sqlText.Parameters; if (parameters != null) { int length = parameters.Length; for (int i = 0; i < length; i++) { ctx.AddParameterMapping(parameters[i]); } } } } else if (child is SqlTag) { SqlTag tag = (SqlTag)child; ISqlTagHandler handler = tag.Handler; int response = BaseTagHandler.INCLUDE_BODY; do { StringBuilder body = new StringBuilder(); response = handler.DoStartFragment(ctx, tag, parameterObject); if (response != BaseTagHandler.SKIP_BODY) { if (ctx.IsOverridePrepend && ctx.FirstNonDynamicTagWithPrepend == null && tag.IsPrependAvailable && !(tag.Handler is DynamicTagHandler)) { ctx.FirstNonDynamicTagWithPrepend = tag; } ProcessBodyChildren(request, ctx, parameterObject, tag.GetChildrenEnumerator(), body); response = handler.DoEndFragment(ctx, tag, parameterObject, body); handler.DoPrepend(ctx, tag, parameterObject, body); if (response != BaseTagHandler.SKIP_BODY) { if (body.Length > 0) { // BODY OUT if (handler.IsPostParseRequired) { SqlText sqlText = _paramParser.ParseInlineParameterMap(request, null, body.ToString()); buffer.Append(sqlText.Text); ParameterProperty[] mappings = sqlText.Parameters; if (mappings != null) { int length = mappings.Length; for (int i = 0; i < length; i++) { ctx.AddParameterMapping(mappings[i]); } } } else { buffer.Append(" "); buffer.Append(body.ToString()); } if (tag.IsPrependAvailable && tag == ctx.FirstNonDynamicTagWithPrepend) { ctx.IsOverridePrepend = false; } } } } }while (response == BaseTagHandler.REPEAT_BODY); } } }
private void ProcessBodyChildren(RequestScope request, SqlTagContext ctx, object parameterObject, IEnumerator localChildren, StringBuilder buffer) { while (localChildren.MoveNext()) { ISqlChild current = (ISqlChild)localChildren.Current; if (current is SqlText) { SqlText text = (SqlText)current; string str = text.Text; if (text.IsWhiteSpace) { buffer.Append(str); } else { buffer.Append(" "); buffer.Append(str); ParameterProperty[] parameters = text.Parameters; if (parameters != null) { int length = parameters.Length; for (int i = 0; i < length; i++) { ctx.AddParameterMapping(parameters[i]); } } } } else if (current is SqlTag) { SqlTag tag = (SqlTag)current; ISqlTagHandler handler = tag.Handler; int num3 = 1; do { StringBuilder builder = new StringBuilder(); num3 = handler.DoStartFragment(ctx, tag, parameterObject); if (num3 != 0) { if ((ctx.IsOverridePrepend && (ctx.FirstNonDynamicTagWithPrepend == null)) && (tag.IsPrependAvailable && !(tag.Handler is DynamicTagHandler))) { ctx.FirstNonDynamicTagWithPrepend = tag; } this.ProcessBodyChildren(request, ctx, parameterObject, tag.GetChildrenEnumerator(), builder); num3 = handler.DoEndFragment(ctx, tag, parameterObject, builder); handler.DoPrepend(ctx, tag, parameterObject, builder); if ((num3 != 0) && (builder.Length > 0)) { if (handler.IsPostParseRequired) { SqlText text2 = this._paramParser.ParseInlineParameterMap(request, null, builder.ToString()); buffer.Append(text2.Text); ParameterProperty[] propertyArray2 = text2.Parameters; if (propertyArray2 != null) { int num4 = propertyArray2.Length; for (int j = 0; j < num4; j++) { ctx.AddParameterMapping(propertyArray2[j]); } } } else { buffer.Append(" "); buffer.Append(builder.ToString()); } if (tag.IsPrependAvailable && (tag == ctx.FirstNonDynamicTagWithPrepend)) { ctx.IsOverridePrepend = false; } } } }while (num3 == 2); } } }