internal IList <BindingReplacement> BuildTextBindingReplacements(SqlText tag)
        {
            if (tag == null)
            {
                throw new ArgumentNullException("tag");
            }

            return(InlineParameterMapParser.BuildBindingReplacements(_bindings, new StringBuilder(tag.Text)));
        }
        internal IList <BindingReplacement> BuildPropertyBindingReplacements(BaseTag tag)
        {
            if (tag == null)
            {
                throw new ArgumentNullException("tag");
            }

            return(InlineParameterMapParser.BuildBindingReplacements(_bindings, new StringBuilder(tag.Property)));
        }
        internal IList <BindingReplacement> BuildContentBindingReplacements(StringBuilder bodyContent)
        {
            if (bodyContent == null)
            {
                throw new ArgumentNullException("bodyContent");
            }

            return(InlineParameterMapParser.BuildBindingReplacements(_bindings, bodyContent));
        }
Beispiel #4
0
        public RequestScope GetRequestScope(IMappedStatement mappedStatement, object parameterObject, ISqlMapSession session)
        {
            RequestScope request = new RequestScope(this._dataExchangeFactory, session, this._statement);

            this._paramParser = new InlineParameterMapParser();
            string sqlStatement = this.Process(request, parameterObject);

            request.PreparedStatement = this.BuildPreparedStatement(session, request, sqlStatement);
            request.MappedStatement   = mappedStatement;
            return(request);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="DynamicSql"/> class.
        /// </summary>
        /// <param name="usePositionalParameters">if set to <c>true</c> [use positional parameters].</param>
        /// <param name="dbHelperParameterCache">The db helper parameter cache.</param>
        /// <param name="dataExchangeFactory">The data exchange factory.</param>
        /// <param name="statement">The statement.</param>
        public DynamicSql(
            bool usePositionalParameters,
            DBHelperParameterCache dbHelperParameterCache,
            DataExchangeFactory dataExchangeFactory,
            IStatement statement)
        {
            Contract.Require.That(dataExchangeFactory, Is.Not.Null).When("retrieving argument dataExchangeFactory in DynamicSql constructor");
            Contract.Require.That(dbHelperParameterCache, Is.Not.Null).When("retrieving argument dbHelperParameterCache in DynamicSql constructor");
            Contract.Require.That(statement, Is.Not.Null).When("retrieving argument statement in DynamicSql constructor");

            this.statement = statement;
            this.usePositionalParameters = usePositionalParameters;
            this.dataExchangeFactory     = dataExchangeFactory;
            this.dbHelperParameterCache  = dbHelperParameterCache;
            paramParser = new InlineParameterMapParser();
        }
        internal void ReplaceParameterIteratePropertiesAndVariables(SqlText sqlText)
        {
            if (sqlText.Parameters != null)
            {
                foreach (var parameter in sqlText.Parameters)
                {
                    var parameterBindingReplacements = InlineParameterMapParser.BuildBindingReplacements(_bindings, new StringBuilder(parameter.PropertyName));

                    foreach (var replacement in parameterBindingReplacements)
                    {
                        parameter.ReplaceBindingName(replacement);
                    }

                    parameter.ApplyIteratePropertyReferenceHandling(this, sqlText);
                }
            }
        }
        /// <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);
                }
            }
        }
Beispiel #8
0
        /// <summary>
        /// Parse dynamic tags
        /// </summary>
        /// <param name="statementConfig">The statement config.</param>
        /// <param name="dynamic">The dynamic.</param>
        /// <param name="sqlBuffer">The SQL buffer.</param>
        /// <param name="isDynamic">if set to <c>true</c> [is dynamic].</param>
        /// <param name="postParseRequired">if set to <c>true</c> [post parse required].</param>
        /// <param name="statement">The statement.</param>
        /// <returns></returns>
        private bool ParseDynamicTags(
            IConfiguration statementConfig,
            IDynamicParent dynamic,
            StringBuilder sqlBuffer,
            bool isDynamic,
            bool postParseRequired,
            IStatement statement)
        {
            ConfigurationCollection children = statementConfig.Children;
            int count = children.Count;

            for (int i = 0; i < count; i++)
            {
                IConfiguration child = children[i];
                if (child.Type == ConfigConstants.ELEMENT_TEXT || child.Type == ConfigConstants.ELEMENT_CDATA)
                {
                    string childValueString = child.Value;
                    if (statement.PreserveWhitespace)
                    {
                        childValueString = childValueString
                                           .Replace('\n', ' ')
                                           .Replace('\r', ' ')
                                           .Replace('\t', ' ')
                                           .Trim();
                    }

                    SqlText sqlText = null;
                    if (postParseRequired)
                    {
                        sqlText      = new SqlText();
                        sqlText.Text = childValueString;
                    }
                    else
                    {
                        sqlText = InlineParameterMapParser.ParseInlineParameterMap(modelStore.DataExchangeFactory, statementConfig.Id, null, childValueString);
                    }

                    dynamic.AddChild(sqlText);
                    sqlBuffer.Append(" " + childValueString);
                }
                else if (child.Type == ConfigConstants.ELEMENT_SELECTKEY || child.Type == ConfigConstants.ELEMENT_INCLUDE)
                {
                }
                else
                {
                    IDeSerializer serializer = deSerializerFactory.GetDeSerializer(child.Type);

                    if (serializer != null)
                    {
                        isDynamic = true;
                        SqlTag tag;

                        tag = serializer.Deserialize(child);

                        dynamic.AddChild(tag);

                        if (child.Children.Count > 0)
                        {
                            isDynamic = ParseDynamicTags(child, tag, sqlBuffer, isDynamic, tag.Handler.IsPostParseRequired, statement);
                        }
                    }
                }
            }

            return(isDynamic);
        }
Beispiel #9
0
        /// <summary>
        /// Parse dynamic tags
        /// </summary>
        /// <param name="statementConfig">The statement config.</param>
        /// <param name="dynamic">The dynamic.</param>
        /// <param name="sqlBuffer">The SQL buffer.</param>
        /// <param name="isDynamic">if set to <c>true</c> [is dynamic].</param>
        /// <param name="postParseRequired">if set to <c>true</c> [post parse required].</param>
        /// <param name="statement">The statement.</param>
        /// <returns></returns>
        private bool ParseDynamicTags(
            IConfiguration statementConfig,
            IDynamicParent dynamic,
            StringBuilder sqlBuffer,
            bool isDynamic,
            bool postParseRequired,
            IStatement statement)
        {
            //具体文本的集合 可能是需要拼接成完整的SQL语句
            ConfigurationCollection children = statementConfig.Children;
            int count = children.Count;

            for (int i = 0; i < count; i++)
            {
                IConfiguration child = children[i];
                if (child.Type == ConfigConstants.ELEMENT_TEXT || child.Type == ConfigConstants.ELEMENT_CDATA)
                {
                    //第一步处理    获取当前文本的一个值 并处理"\r\n"
                    string childValueString = child.Value;
                    if (statement.PreserveWhitespace)
                    {
                        childValueString = childValueString.Replace('\n', ' ').Replace('\r', ' ').Replace('\t', ' ').Trim();
                    }

                    //第二部处理   将处理的当前一个部分SQL语句放入到SqlText类中
                    SqlText sqlText = null;
                    if (postParseRequired)
                    {
                        sqlText      = new SqlText();
                        sqlText.Text = childValueString;
                    }
                    else
                    {
                        //分析SQL语句中的参数 例如# # ,@{}等内部参数 最后的结果是一个语句保存 和 参数列表在SqlText类中
                        sqlText = InlineParameterMapParser.ParseInlineParameterMap(modelStore.DataExchangeFactory, statementConfig.Id, null, childValueString);
                    }
                    //将此次分析的SQL语句结果保存到dynamic类对象中 可能需要添加多次
                    dynamic.AddChild(sqlText);//按顺序放置 最后拼接即可

                    //sqlBuffer字符串拼接每一部分的sql语句 最后是当前节点的完整语句
                    sqlBuffer.Append(" " + childValueString);
                }
                else if (child.Type == ConfigConstants.ELEMENT_SELECTKEY || child.Type == ConfigConstants.ELEMENT_INCLUDE)
                {
                    //此处没有处理代码
                }
                else
                {
                    //从deSerializerFactory工厂类的字典serializerMap中获取对应的处理IDeSerializer类
                    IDeSerializer serializer = deSerializerFactory.GetDeSerializer(child.Type);//child.Type就是节点的名

                    if (serializer != null)
                    {
                        isDynamic = true;
                        SqlTag tag;

                        //当前child是一个element 进行解析
                        tag = serializer.Deserialize(child);

                        //添加到IList<ISqlChild> children中
                        dynamic.AddChild(tag);

                        if (child.Children.Count > 0)
                        {
                            //递归处理子节点 注意是子节点Tag了 递归下去 但sqlBuffer字符串一直是同一个
                            isDynamic = ParseDynamicTags(child, tag, sqlBuffer, isDynamic, tag.Handler.IsPostParseRequired, statement);
                        }
                    }
                }
            }

            return(isDynamic);
        }
Beispiel #10
0
        /// <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)
                {
                    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 = 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());
                                    }
                                    if (tag.IsPrependAvailable && tag == ctx.FirstNonDynamicTagWithPrepend)
                                    {
                                        ctx.IsOverridePrepend = false;
                                    }
                                }
                            }
                        }
                    }while (response == BaseTagHandler.REPEAT_BODY);
                }
            }
        }