Exemplo n.º 1
0
        /// <summary>
        /// 创建查询命令
        /// </summary>
        /// <param name="query">参数化查询</param>
        /// <returns>查询命令</returns>
        public TCommand Parse(ParameterizedQuery query)
        {
            var length = query.ParameterValues.Length;

            TParameter[] parameters            = new TParameter[length];
            string[]     parameterPlaceholders = new string[length];

            lock ( SyncRoot )
            {
                var regex = ParameterizedQuery.ParameterPlaceholdRegex;

                var text = regex.Replace(query.TextTemplate, (match) =>
                {
                    var index = int.Parse(match.Groups["index"].Value);

                    if (index >= length)
                    {
                        throw new IndexOutOfRangeException("分析参数化查询时遇到错误,参数索引超出边界");
                    }

                    var placeholder = parameterPlaceholders[index];
                    if (placeholder == null)
                    {
                        placeholder = parameterPlaceholders[index] = GetParameterPlaceholder(query.ParameterValues[index], index, out parameters[index]);
                    }

                    return(placeholder);
                });


                return(CreateCommand(text.Replace("##", "#"), parameters.ToArray()));
            }
        }
        /// <summary>
        /// 创建查询命令
        /// </summary>
        /// <param name="query">参数化查询</param>
        /// <returns>查询命令</returns>
        public TCommand Parse(ParameterizedQuery query)
        {
            var regex = ParameterizedQuery.ParameterPlaceholdRegex;



            var text = regex.Replace(query.TextTemplate, (match) =>
            {
                var index = int.Parse(match.Groups["index"].Value);

                return(GetLiteralValue(query.ParameterValues[index]));
            });


            return(CreateCommand(text.Replace("##", "#")));
        }