public void Converter(DynamicQueryParam queryParam, Type propertyType, DynamicQueryKeyValueCollection collection)
        {
            if (string.IsNullOrWhiteSpace(queryParam.Value?.ToString()))
            {
                throw new ArgumentException("IN操作必须提供Value.");
            }
            var arr = queryParam.Value.ToString()
                      .Trim()
                      .Split(',')
                      .Where(i => !string.IsNullOrWhiteSpace(i))
                      .Select(i => ConverterHelper.ChangeType(i, propertyType)).ToList();

            var   listType = typeof(List <>);
            var   s        = listType.MakeGenericType(propertyType);
            IList list     = Activator.CreateInstance(s) as IList;

            foreach (var o in arr)
            {
                list.Add(o);
            }
            object valObj = list;

            if (!arr.Any())
            {
                throw new ArgumentException("IN操作必须提供Value.");
            }
            collection.Builder.Append($" @{collection.Ids++}.Contains({queryParam.Field}) ");
            collection.Values.Add(valObj);
        }
Exemple #2
0
        public void Converter(DynamicQueryParam queryParam, Type propertyType, DynamicQueryKeyValueCollection collection)
        {
            DateTime datetime;

            if (!DateTime.TryParse(queryParam.Value?.ToString(), out datetime))
            {
                throw new ArgumentException($"{queryParam.Value}不是有效的DateTime类型");
            }
            queryParam.Value = new DateTime(datetime.Year, datetime.Month, datetime.Day).AddDays(1).AddMilliseconds(-1);
            MethodProviderCollection.DefaultConverterProvider.Converter(queryParam, propertyType, collection);
        }
Exemple #3
0
        private DynamicQueryKeyValueCollection ConverterWhere()
        {
            var collection = new DynamicQueryKeyValueCollection();

            if (_queryModel.ParamGroup == null)
            {
                return(collection);
            }
            ConverterHelper.ConverterQueryParamGroup(_entityType, _queryModel.ParamGroup, collection);
            return(collection);
        }
        public void Converter(DynamicQueryParam queryParam, Type propertyType, DynamicQueryKeyValueCollection collection)
        {
            var conValue           = ConverterHelper.ChangeType(queryParam.Value, propertyType);
            var filed              = queryParam.Field;
            var whereParams        = collection.Values;
            var whereStringBuilder = collection.Builder;

            switch (queryParam.Operator)
            {
            case QueryOperation.Equal:
                whereParams.Add(conValue);
                whereStringBuilder.Append($"{filed} = @{collection.Ids++}");
                break;

            case QueryOperation.LessThan:
                whereParams.Add(conValue);
                whereStringBuilder.Append($"{filed} < @{collection.Ids++}");
                break;

            case QueryOperation.LessThanOrEqual:
                whereParams.Add(conValue);
                whereStringBuilder.Append($"{filed} <= @{collection.Ids++}");
                break;

            case QueryOperation.GreaterThan:
                whereParams.Add(conValue);
                whereStringBuilder.Append($"{filed} > @{collection.Ids++}");
                break;

            case QueryOperation.GreaterThanOrEqual:
                whereParams.Add(conValue);
                whereStringBuilder.Append($"{filed} >= @{collection.Ids++}");
                break;

            case QueryOperation.Contains:
                whereParams.Add(conValue);
                whereStringBuilder.Append($"{filed}.Contains(@{collection.Ids++})");
                break;

            case QueryOperation.StartsWith:
                whereParams.Add(conValue);
                whereStringBuilder.Append($"{filed}.StartsWith(@{collection.Ids++})");
                break;

            case QueryOperation.EndsWith:
                whereParams.Add(conValue);
                whereStringBuilder.Append($"{filed}.EndsWith(@{collection.Ids++})");
                break;

            default:
                throw new ArgumentException($"{nameof(QueryOperation)}无效");
            }
        }
Exemple #5
0
 public static void ConverterQueryParamGroup(Type entityType, DynamicQueryParamGroup group,
                                             DynamicQueryKeyValueCollection collection)
 {
     CheckQueryParamGroup(group);
     if (IsParam(group))
     {
         ConverterQueryParams(entityType, group.Params, collection, group.Relation);
     }
     else
     {
         ConverterQueryParamGroups(entityType, group.ChildGroups, collection, group.Relation);
     }
 }
        public void Converter(DynamicQueryParam queryParam, Type propertyType, DynamicQueryKeyValueCollection collection)
        {
            if (!propertyType.IsConstructedGenericType)
            {
                throw new Exception("只有泛型类型能够使用此方法");
            }
            var type = propertyType.GetGenericArguments().FirstOrDefault();

            if (type == null)
            {
                throw new Exception("只有泛型类型能够使用此方法");
            }
            var group = JsonConvert.DeserializeObject <DynamicQueryParamGroup>((queryParam.Value ?? "").ToString());

            if ((group == null) || (group.ChildGroups == null))
            {
                return;
            }
            ConverterHelper.CheckQueryParamGroup(group);
            collection.Builder.Append($"{queryParam.Field}.Any(");
            ConverterHelper.ConverterQueryParamGroup(type, group, collection);
            collection.Builder.Append($")");
        }
Exemple #7
0
        public static void ConverterQueryParams(Type entityType, [NotNull] List <DynamicQueryParam> queryParams,
                                                DynamicQueryKeyValueCollection collection, QueryRelation relation)
        {
            if (!queryParams.Any())
            {
                return;
            }
            var list = queryParams.ToList();

            collection.Builder.Append("(");
            for (var i = 0; i < list.Count; i++)
            {
                var item         = list[i];
                var propertyType = GetPropertyArr(entityType, item.Field).Last();
                var provider     = MethodProviderCollection.Get(item, propertyType.PropertyType);
                provider.Converter(item, propertyType.PropertyType, collection);
                if (i < list.Count - 1)
                {
                    collection.Builder.Append(relation == QueryRelation.Or ? " || " : " && ");
                }
            }
            collection.Builder.Append(")");
        }
Exemple #8
0
        public static void ConverterQueryParamGroups([NotNull] Type entityType,
                                                     [NotNull] List <DynamicQueryParamGroup> groups, [NotNull] DynamicQueryKeyValueCollection collection,
                                                     QueryRelation relation)
        {
            if (!groups.Any())
            {
                return;
            }
            var list = groups.ToList();

            for (var i = 0; i < list.Count; i++)
            {
                var item = list[i];
                CheckQueryParamGroup(item);
                if (IsParam(item))
                {
                    ConverterQueryParams(entityType, item.Params, collection, item.Relation);
                }
                else
                {
                    collection.Builder.Append("(");
                    ConverterQueryParamGroups(entityType, item.ChildGroups, collection, item.Relation);
                    collection.Builder.Append(")");
                }
                if (i < list.Count - 1)
                {
                    collection.Builder.Append(relation == QueryRelation.Or ? " || " : " && ");
                }
            }
        }