Ejemplo n.º 1
0
        public QueryParserResult Parse <T>(Query query)
        {
            var result = new QueryParserResult {
                Ok = true
            };

            List <PropertyInfo> properties = typeof(T).GetProperties().ToList();

            result.Projections = GetProjections(properties, query?.Fields);
            var selectionPredicate = GetSelectionPredicate(properties, query.Filters);

            result.Predicate  = selectionPredicate.predicate;
            result.Parameters = selectionPredicate.parameters;
            result.Sort       = GetOrderByQuery(properties, query.Sort);
            result.Skip       = query.Skip;
            result.Take       = query.Take;
            return(result);
        }
Ejemplo n.º 2
0
        public QueryParserResult Parse <T>(IDictionary <string, string> queryParams)
        {
            var result = new QueryParserResult
            {
                Ok = true,
                QueryParameters = new QueryParameters()
            };

            if (queryParams.Count == 0)
            {
                return(result);
            }
            List <PropertyInfo> properties = typeof(T).GetProperties().ToList();
            StringBuilder       predicate  = new StringBuilder("WHERE 1 = 1");
            StringBuilder       orderBy    = new StringBuilder("ORDER BY ");
            var parameters = new DPR.DynamicParameters();

            foreach (var kvp in queryParams)
            {
                PropertyInfo propertyInfo;
                if (kvp.Key.Equals("skip", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrWhiteSpace(kvp.Value))
                {
                    int skip;
                    if (!int.TryParse(kvp.Value, out skip))
                    {
                        result.Ok    = false;
                        result.Error = String.Format("Failed to parse skip value '{0}' as int", kvp.Value);
                        return(result);
                    }
                    result.QueryParameters.Skip = skip;
                }
                else if (kvp.Key.Equals("take", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrWhiteSpace(kvp.Value))
                {
                    int take;
                    if (!int.TryParse(kvp.Value, out take))
                    {
                        result.Ok    = false;
                        result.Error = String.Format("Failed to parse take value '{0}' as int", kvp.Value);
                        return(result);
                    }
                    result.QueryParameters.Take = take;
                }
                else if (kvp.Key.Equals("sort", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrWhiteSpace(kvp.Value))
                {
                    string[] sortParams = kvp.Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var sortParam in sortParams)
                    {
                        bool desc          = sortParam[0] == '-';
                        var  sortParamName = sortParam.TrimStart(new char[] { '-' });
                        if ((propertyInfo = properties.Where(p => p.Name.Equals(sortParamName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault()) != null)
                        {
                            orderBy.AppendFormat("[{0}]", propertyInfo.Name);
                            if (desc)
                            {
                                orderBy.Append(" DESC");
                            }
                            orderBy.Append(",");
                        }
                    }
                }
                else if ((propertyInfo = properties.Where(p => p.Name.Equals(kvp.Key, StringComparison.OrdinalIgnoreCase)).FirstOrDefault()) != null)
                {
                    predicate.AppendFormat(" AND [{0}] = @{0}", propertyInfo.Name);
                    parameters.Add(string.Format("@{0}", propertyInfo.Name), ChangeType(kvp.Value, propertyInfo.PropertyType));
                }
            }
            if (result.QueryParameters.Skip.HasValue ^ result.QueryParameters.Take.HasValue)
            {
                result.Ok    = false;
                result.Error = "Skip and take must both have values if either is used";
                return(result);
            }
            var predicateString = predicate.ToString();

            if (predicateString.Length > "WHERE 1 = 1 ".Length)
            {
                result.QueryParameters.Predicate = predicateString;
            }
            if (parameters.ParameterNames.Any())
            {
                result.QueryParameters.Parameters = parameters;
            }
            var orderByString = orderBy.ToString();

            orderByString = orderByString.TrimEnd(new char[] { ',' });
            if (orderByString.Length > "ORDER BY ".Length)
            {
                result.QueryParameters.Sort = orderByString;
            }
            return(result);
        }