コード例 #1
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 ? " || " : " && ");
                }
            }
        }
コード例 #2
0
        public DynamicQueryParamGroupBuilder CreateChildGroup(QueryRelation relation)
        {
            _paramGroup.CheckDynamicQueryParamGroup();
            var childGroup = new DynamicQueryParamGroup {
                Relation = relation
            };

            _paramGroup.ChildGroups.Add(childGroup);
            return(new DynamicQueryParamGroupBuilder(childGroup));
        }
コード例 #3
0
 public DynamicQueryParamGroupBuilder Relation(QueryRelation relation)
 {
     _paramGroup.Relation = relation;
     return(this);
 }
コード例 #4
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(")");
        }
コード例 #5
0
        public void ConverterQueryParamGroups(List <DynamicQueryParamGroup> groups, SqlBuiderResult collection, QueryRelation relation, SqlFieldMappings mappings, string parentKey)
        {
            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(item.Params, collection, item.Relation, mappings, parentKey);
                }
                else
                {
                    collection.SqlString.Append("(");
                    ConverterQueryParamGroups(item.ChildGroups, collection, item.Relation, mappings, parentKey);
                    collection.SqlString.Append(")");
                }
                if (i < list.Count - 1)
                {
                    collection.SqlString.Append(relation == QueryRelation.And ? " AND " : " OR ");
                }
            }
        }
コード例 #6
0
        public void ConverterQueryParams(List <DynamicQueryParam> queryParams, SqlBuiderResult collection, QueryRelation relation, SqlFieldMappings mappings, string parentKey)
        {
            if (!queryParams.Any())
            {
                return;
            }
            var list = queryParams.ToList();

            collection.SqlString.Append("(");
            for (var i = 0; i < list.Count; i++)
            {
                var item  = list[i];
                var field = item.Operator == QueryOperation.Any ? "" : GetSqlKey(mappings, item, parentKey);
                var param = $"@{__PAM_}{collection.Params.Count}";
                var val   = GetSqlValue(mappings, item, parentKey);

                switch (item.Operator)
                {
                case QueryOperation.Equal:
                    collection.SqlString.Append($"{field} = {param}");
                    collection.Params.Add(param, val);
                    break;

                case QueryOperation.LessThan:
                    collection.Params.Add(param, val);
                    collection.SqlString.Append($"{field} < {param}");
                    break;

                case QueryOperation.LessThanOrEqual:
                    collection.Params.Add(param, val);
                    collection.SqlString.Append($"{field} <= {param}");
                    break;

                case QueryOperation.GreaterThan:
                    collection.Params.Add(param, val);
                    collection.SqlString.Append($"{field} > {param}");
                    break;

                case QueryOperation.GreaterThanOrEqual:
                    collection.Params.Add(param, val);
                    collection.SqlString.Append($"{field} >= {param}");
                    break;

                case QueryOperation.Contains:
                    collection.Params.Add(param, $"%{val}%");
                    collection.SqlString.Append($"{field} like {param}");
                    break;

                case QueryOperation.StartsWith:
                    collection.Params.Add(param, $"{val}%");
                    collection.SqlString.Append($"{field} like {param}");
                    break;

                case QueryOperation.EndsWith:
                    collection.Params.Add(param, $"%{val}");
                    collection.SqlString.Append($"{field} like {param}");
                    break;

                case QueryOperation.DataTimeLessThanOrEqualThenDay:
                    if (DateTime.TryParse(val.ToString(), out DateTime date))
                    {
                        val = date.AddDays(1).AddMilliseconds(-1);
                    }
                    else
                    {
                        break;
                    }
                    collection.Params.Add(param, val);
                    collection.SqlString.Append($"{field} <= {param}");
                    break;

                case QueryOperation.In:
                    //collection.Params.Add(param, val);
                    collection.SqlString.Append($"{field} IN ({val.ToString().Trim(',')})");
                    break;

                case QueryOperation.Any:
                    var group = JsonConvert.DeserializeObject <DynamicQueryParamGroup>((val ?? "").ToString());
                    if ((group == null) || (group.ChildGroups == null))
                    {
                        break;
                    }
                    ConverterQueryParamGroup(group, collection, mappings, item.Field);
                    break;

                default:
                    throw new ArgumentException($"{nameof(QueryOperation)}无效");
                }


                if (i < list.Count - 1)
                {
                    collection.SqlString.Append(relation == QueryRelation.Or ? " OR " : " AND ");
                }
            }
            collection.SqlString.Append(")");
        }