Exemplo n.º 1
0
        public SqlExpression <From> Filter <From>(ISqlExpression q, IQueryDb dto, IRequest req)
        {
            if (QueryFilters == null)
            {
                return((SqlExpression <From>)q);
            }

            QueryFilterDelegate filterFn = null;

            if (!QueryFilters.TryGetValue(dto.GetType(), out filterFn))
            {
                foreach (var type in dto.GetType().GetInterfaces())
                {
                    if (QueryFilters.TryGetValue(type, out filterFn))
                    {
                        break;
                    }
                }
            }

            if (filterFn != null)
            {
                filterFn(q, dto, req);
            }

            return((SqlExpression <From>)q);
        }
Exemplo n.º 2
0
        public SqlExpression <From> CreateQuery <From, Into>(IQueryDb <From, Into> dto, Dictionary <string, string> dynamicParams, IRequest req = null)
        {
            var typedQuery = GetTypedQuery(dto.GetType(), typeof(From));
            var q          = typedQuery.CreateQuery(GetDb <From>(req));

            return(Filter <From>(typedQuery.AddToQuery(q, dto, dynamicParams, this), dto, req));
        }
Exemplo n.º 3
0
        public ISqlExpression AddToQuery(
            ISqlExpression query,
            IQueryDb dto,
            Dictionary <string, string> dynamicParams,
            IAutoQueryOptions options = null)
        {
            dynamicParams = new Dictionary <string, string>(dynamicParams, StringComparer.OrdinalIgnoreCase);

            var q = (SqlExpression <From>)query;

            if (options != null && options.EnableSqlFilters)
            {
                AppendSqlFilters(q, dto, dynamicParams, options);
            }

            AppendJoins(q, dto);

            AppendLimits(q, dto, options);

            var dtoAttr     = dto.GetType().FirstAttribute <QueryDbAttribute>();
            var defaultTerm = dtoAttr != null && dtoAttr.DefaultTerm == QueryTerm.Or ? "OR" : "AND";

            var aliases = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
            var props   = typeof(From).GetProperties();

            foreach (var pi in props)
            {
                var attr = pi.FirstAttribute <DataMemberAttribute>();
                if (attr?.Name == null)
                {
                    continue;
                }
                aliases[attr.Name] = pi.Name;
            }

            AppendTypedQueries(q, dto, dynamicParams, defaultTerm, options, aliases);

            if (options?.EnableUntypedQueries == true && dynamicParams.Count > 0)
            {
                AppendUntypedQueries(q, dynamicParams, defaultTerm, options, aliases);
            }

            if (defaultTerm == "OR" && q.WhereExpression == null)
            {
                q.Where("1=0"); //Empty OR queries should be empty
            }

            if (!string.IsNullOrEmpty(dto.Fields))
            {
                var fields = dto.Fields.Split(',')
                             .Where(x => x.Trim().Length > 0)
                             .Map(x => x.Trim());

                q.Select(fields.ToArray());
            }

            return(q);
        }
Exemplo n.º 4
0
        public ISqlExpression CreateQuery(IQueryDb requestDto, Dictionary <string, string> dynamicParams, IRequest req = null)
        {
            var requestDtoType = requestDto.GetType();
            var fromType       = GetFromType(requestDtoType);
            var typedQuery     = GetTypedQuery(requestDtoType, fromType);
            var q = typedQuery.CreateQuery(GetDb(fromType));

            return(Filter(typedQuery.AddToQuery(q, requestDto, dynamicParams, this), requestDto, req));
        }
Exemplo n.º 5
0
        public ISqlExpression Filter(ISqlExpression q, IQueryDb dto, IRequest req)
        {
            if (QueryFilters == null)
            {
                return(q);
            }

            if (!QueryFilters.TryGetValue(dto.GetType(), out var filterFn))
            {
                foreach (var type in dto.GetType().GetInterfaces())
                {
                    if (QueryFilters.TryGetValue(type, out filterFn))
                    {
                        break;
                    }
                }
            }

            filterFn?.Invoke(q, dto, req);

            return(q);
        }
Exemplo n.º 6
0
        public IQueryResponse Execute(IQueryDb request, ISqlExpression q)
        {
            var             requestDtoType = request.GetType();
            ITypedQueryData typedQuery;

            Type fromType;
            Type intoType;
            var  intoTypeDef = requestDtoType.GetTypeWithGenericTypeDefinitionOf(typeof(IQueryDb <,>));

            if (intoTypeDef != null)
            {
                var args = intoTypeDef.GetGenericArguments();
                fromType = args[0];
                intoType = args[1];
            }
            else
            {
                var typeDef = requestDtoType.GetTypeWithGenericTypeDefinitionOf(typeof(IQueryDb <>));
                var args    = typeDef.GetGenericArguments();
                fromType = args[0];
                intoType = args[0];
            }

            if (genericAutoQueryCache.TryGetValue(fromType, out GenericAutoQueryDb typedApi))
            {
                return(typedApi.ExecuteObject(this, request, q));
            }

            var genericType = typeof(GenericAutoQueryDb <,>).MakeGenericType(fromType, intoType);
            var instance    = genericType.CreateInstance <GenericAutoQueryDb>();

            Dictionary <Type, GenericAutoQueryDb> snapshot, newCache;

            do
            {
                snapshot = genericAutoQueryCache;
                newCache = new Dictionary <Type, GenericAutoQueryDb>(genericAutoQueryCache)
                {
                    [requestDtoType] = instance
                };
            } while (!ReferenceEquals(
                         Interlocked.CompareExchange(ref genericAutoQueryCache, newCache, snapshot), snapshot));

            return(instance.ExecuteObject(this, request, q));
        }
Exemplo n.º 7
0
        private static void AppendJoins(SqlExpression <From> q, IQueryDb dto)
        {
            if (dto is IJoin)
            {
                var dtoInterfaces = dto.GetType().GetInterfaces();
                foreach (var innerJoin in dtoInterfaces.Where(x => x.Name.StartsWith("IJoin`")))
                {
                    var joinTypes = innerJoin.GetGenericArguments();
                    for (var i = 1; i < joinTypes.Length; i++)
                    {
                        q.Join(joinTypes[i - 1], joinTypes[i]);
                    }
                }

                foreach (var leftJoin in dtoInterfaces.Where(x => x.Name.StartsWith("ILeftJoin`")))
                {
                    var joinTypes = leftJoin.GetGenericArguments();
                    for (var i = 1; i < joinTypes.Length; i++)
                    {
                        q.LeftJoin(joinTypes[i - 1], joinTypes[i]);
                    }
                }
            }
        }
Exemplo n.º 8
0
        public QueryResponse <Into> Execute <From, Into>(IQueryDb <From, Into> model, SqlExpression <From> query)
        {
            var typedQuery = GetTypedQuery(model.GetType(), typeof(From));

            return(ResponseFilter(typedQuery.Execute <Into>(GetDb <From>(), query), query, model));
        }