Exemplo n.º 1
0
        /// <summary>
        /// Creates a formattable string from this parameterized SQL.
        /// </summary>
        /// <returns></returns>
        public FormattableString ToFormattableString()
        {
            var r = new Regex(@"@[A-Z_][A-Z0-9_]+", RegexOptions.IgnoreCase);

            var list = new List <object>();
            var fmt  = r.Replace(
                SqlText, (m) =>
            {
                var placeholder = $"{{{list.Count}}}";
                if (ParameterValues.ContainsKey(m.Value))
                {
                    list.Add(ParameterValues[m.Value]);
                    return(placeholder);
                }

                if (ParameterValues.ContainsKey(m.Value.Substring(1)))
                {
                    list.Add(ParameterValues[m.Value.Substring(1)]);
                    return(placeholder);
                }

                return(m.Value);
            }
                );

            return(FormattableStringFactory.Create(fmt, list.ToArray()));
        }
Exemplo n.º 2
0
 protected override void GenerateTop(SelectExpression selectExpression)
 {
     Check.NotNull(selectExpression, nameof(selectExpression));
     if (selectExpression.Offset != null)
     {
         if (selectExpression.Offset.NodeType == ExpressionType.Parameter && ParameterValues.ContainsKey(selectExpression.Offset.ToString()))
         {
             Sql.Append(" SKIP ").Append(ParameterValues[selectExpression.Offset.ToString()].ToString()).Append(" ");
         }
         else if (selectExpression.Offset.NodeType == ExpressionType.Constant)
         {
             Sql.Append(" SKIP ").Append(selectExpression.Offset).Append(" ");
         }
     }
     if (selectExpression.Limit != null)
     {
         if (selectExpression.Limit.NodeType == ExpressionType.Parameter && ParameterValues.ContainsKey(selectExpression.Limit.ToString()))
         {
             Sql.Append("FIRST ").Append(ParameterValues[selectExpression.Limit.ToString()].ToString()).Append(" ");
         }
         else if (selectExpression.Limit.NodeType == ExpressionType.Constant)
         {
             Sql.Append("FIRST ").Append(selectExpression.Limit).Append(" ");
         }
     }
 }
Exemplo n.º 3
0
        internal IRequestData PrepareRequestData()
        {
            var headers  = new HeaderValue[0];
            var rootNode = new BodyTree();

            foreach (ServiceParameter parameter in _method.Request.Parameters)
            {
                var           parameterName = parameter.Name;
                IBodyTreeItem value;
                if (!ParameterValues.ContainsKey(parameterName))
                {
                    value = new BodyTreeNodeParameterValue {
                        Parameter = parameter,
                        Value     = parameter.DefValue
                    };
                }
                else
                {
                    var customValue = ParameterValues[parameterName];
                    value = GetParameterValue(parameter, customValue);
                }
                rootNode.Add(value);
            }
            return(new RequestData(headers, rootNode));
        }
Exemplo n.º 4
0
 protected override void GenerateLimitOffset(SelectExpression selectExpression)
 {
     Check.NotNull(selectExpression, "selectExpression");
     if (!selectExpression.Projection.OfType <RowNumberExpression_DB2>().Any() && selectExpression.Limit != null)
     {
         if (selectExpression.Limit.NodeType == ExpressionType.Parameter && ParameterValues.ContainsKey(selectExpression.Limit.ToString()))
         {
             Sql.Append(" FETCH FIRST ").Append(ParameterValues[selectExpression.Limit.ToString()].ToString()).Append(" ROWS ONLY");
         }
         else if (selectExpression.Limit.NodeType == ExpressionType.Constant)
         {
             Sql.Append(" FETCH FIRST ").Append(selectExpression.Limit).Append(" ROWS ONLY");
         }
     }
 }
Exemplo n.º 5
0
        protected override Expression VisitParameter(ParameterExpression parameterExpression)
        {
            if (!_predicateGenerating || parameterExpression?.Type != typeof(string))
            {
                return(base.VisitParameter(parameterExpression));
            }

            var parameterName = SqlGenerator.GenerateParameterName(parameterExpression.Name);

            if (Sql.ParameterBuilder.Parameters
                .All(p => p.InvariantName != parameterExpression.Name))
            {
                var parameterType = parameterExpression.Type.UnwrapNullableType();
                var typeMapping   = TypeMappingSource.GetMapping(parameterType);

                if (ParameterValues.ContainsKey(parameterExpression.Name))
                {
                    var value = ParameterValues[parameterExpression.Name];

                    typeMapping = TypeMappingSource.GetMappingForValue(value);

                    if (typeMapping == null ||
                        (!typeMapping.ClrType.UnwrapNullableType().IsAssignableFrom(parameterType) &&
                         (parameterType.IsEnum ||
                          !typeof(IConvertible).IsAssignableFrom(parameterType))))
                    {
                        typeMapping = TypeMappingSource.GetMapping(parameterType);
                    }
                }

                Sql.AddParameter(
                    parameterExpression.Name,
                    parameterName,
                    typeMapping,
                    parameterExpression.Type.IsNullableType());
            }

            var parameterNamePlaceholder = SqlGenerator.GenerateParameterNamePlaceholder(parameterExpression.Name);

            AddLowerFunctionToSqlQuery(parameterNamePlaceholder);

            return(parameterExpression);
        }
        /// <summary>
        ///   Synchronously executes the <see cref="T:Microsoft.AspNetCore.Razor.TagHelpers.TagHelper" />
        ///   with the given <paramref name="context" /> and <paramref name="output" />. This is the
        ///   method responsible for localizing the html attributes.
        /// </summary>
        /// <param name="context">Contains information associated with the current HTML tag.</param>
        /// <param name="output">A stateful HTML element used to generate an HTML tag.</param>
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            foreach (var attribute in AttributeValues)
            {
                string key   = attribute.Key;
                string value = attribute.Value;
                if (!string.IsNullOrWhiteSpace(value))
                {
                    string newValue = null;

                    if (ParameterValues.ContainsKey(key))
                    {
                        newValue = localizer.GetString(value, ParameterValues[key].Split(';'));
                    }
                    else
                    {
                        newValue = localizer.GetString(value);
                    }

                    output.Attributes.Add(key, newValue);
                }
            }
        }
Exemplo n.º 7
0
        void GenerateReplaceSQL(SqlFunctionExpression fun)
        {
            string o = "";

            Sql.Append(fun.FunctionName + "(");
            foreach (Expression argument in fun.Arguments)
            {
                Sql.Append(o);
                if (argument.NodeType == ExpressionType.Parameter && ParameterValues.ContainsKey(argument.ToString()))
                {
                    Sql.Append("'" + ParameterValues[argument.ToString()] + "'");
                }
                else if (argument.NodeType == ExpressionType.Constant)
                {
                    Sql.Append(argument.ToString().StartsWith("\"", StringComparison.Ordinal) ? argument.ToString().Replace("\"", "'") : ("'" + argument + "'"));
                }
                else
                {
                    (this).Visit(argument);
                }
                o = ", ";
            }
            Sql.Append(")");
        }