示例#1
0
        /// <summary>
        /// 解析查询模板
        /// </summary>
        /// <param name="builder">参数化查询构建器</param>
        /// <param name="template">查询模板</param>
        /// <returns>解析结果</returns>
        private ParameterizedQuery ParseTemplate(IParameterizedQueryBuilder builder, FormattableString template)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (template == null)
            {
                throw new ArgumentNullException(nameof(template));
            }



            lock (builder.SyncRoot)
            {
                var templateText = template.Format;

                for (var i = 0; i < templateText.Length; i++)
                {
                    var ch = templateText[i];

                    if (ch == '{')
                    {
                        if (i == templateText.Length - 1)
                        {
                            throw FormatError(templateText, i);
                        }

                        if (templateText[i + 1] == '{')
                        {
                            i++;
                            builder.Append('{');
                            continue;
                        }



                        Match match = null;

                        do
                        {
                            match = numberRegex.Match(templateText, i);

                            if (match.Success)
                            {
                                int parameterIndex;
                                if (!int.TryParse(match.Groups["index"].Value, out parameterIndex))
                                {
                                    throw FormatError(templateText, i);
                                }

                                AddParameter(builder, template.GetArgument(parameterIndex));
                                break;
                            }
                        } while (false);


                        if (match == null || !match.Success)
                        {
                            throw FormatError(templateText, i);
                        }
                        i += match.Length - 1;
                    }
                    else if (ch == '}')
                    {
                        if (i == templateText.Length - 1)
                        {
                            throw FormatError(templateText, i);
                        }

                        if (templateText[i + 1] == '}')
                        {
                            i++;
                            builder.Append('}');
                            continue;
                        }
                    }

                    else
                    {
                        builder.Append(ch);
                    }
                }


                return(builder.BuildQuery(null));
            }
        }