public override IQueryOptions VisitQueryOptions(IQueryOptions query)
        {
            this.sqlBuilder.Append("SELECT * FROM {0}");
            if (query.Filter != null && !string.IsNullOrEmpty(query.Filter.RawValue))
            {
                this.sqlBuilder.Append(" WHERE ");
                this.Visit(query.Filter.Expression);
            }
            if (query.OrderBy != null && !string.IsNullOrEmpty(query.OrderBy.RawValue))
            {
                this.sqlBuilder.Append(" ORDER BY ");
                this.Visit(query.OrderBy.Expression);
            }
            if (query.Top != null && !string.IsNullOrEmpty(query.Top.RawValue))
            {
                this.sqlBuilder.Append(" LIMIT ");
                this.Visit(query.Top.Expression);
            }
            if (query.Skip != null && !string.IsNullOrEmpty(query.Skip.RawValue))
            {
                this.sqlBuilder.Append(" OFFSET ");
                this.Visit(query.Skip.Expression);
            }

            return query;
        }
        public virtual IQueryOptions VisitQueryOptions(IQueryOptions query)
        {
            this.Visit(query.Filter.Expression);
            this.Visit(query.OrderBy.Expression);
            this.Visit(query.Skip.Expression);
            this.Visit(query.Top.Expression);
            this.Visit(query.InlineCount.Expression);

            return query;
        }
        /// <summary>
        /// Gets the stored data.
        /// This method should not alter the database in any way.
        /// </summary>
        /// <param name="tableName">Name of the table.</param>
        /// <param name="query">The query.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception"></exception>
        public Task<JArray> GetStoredData(string tableName, IQueryOptions query)
        {
            Debug.WriteLine("Retrieving data for table {0}, query: {1}", tableName, query.ToString());

            EnsureDatabaseThread();

            try
            {
                //check if database exists
                if (!db.DoesTableExist(tableName))
                {
                    Debug.WriteLine("Table doesn't exist: {0}", tableName);
                    return Task.FromResult(new JArray());
                }

                Debug.WriteLine("Table exists: {0}", tableName);
                Debug.WriteLine("Querying table structure for table: {0}", tableName);

                IDictionary<string, Column> columns = db.GetColumnsForTable(tableName);

                Debug.WriteLine("Table {0} has columns: {1}", tableName, string.Join<Column>("\n", columns.Values));

                SQLiteExpressionVisitor visitor = new SQLiteExpressionVisitor();
                visitor.VisitQueryOptions(query);

                var sqlStatement = string.Format(visitor.SqlStatement, tableName);

                Debug.WriteLine("Executing sql: {0}", sqlStatement);

                return Task.FromResult(db.Query(sqlStatement, columns));
            }
            catch (SQLiteException ex)
            {
                throw new Exception(string.Format("Error occurred during interaction with the local database: {0}", ex.Message));
            }
        }
Beispiel #4
0
        public ISqlQueryMaker GetQuery(QueryType queryType, IQueryOptions options=null)
        {
            if (!IsBound)
            {
                throw new InvalidOperationException("This type is not bound to a query");
            }
            else
            {
                var clone = Query.Clone(queryType);
                if (options != null)
                {
                    if (!String.IsNullOrEmpty(options.PrimaryKey))
                    {
                        clone.PrimaryKey = options.PrimaryKey;
                    }
                    if (!String.IsNullOrEmpty(options.TableName))
                    {
                        clone.TableName = options.TableName;
                    }
                }

                return clone;
            }
        }
Beispiel #5
0
 public void MapTo(IQueryOptions optionUser)
 {
     if (!String.IsNullOrEmpty(TableName))
     {
         optionUser.TableName = TableName;
     }
     if (!String.IsNullOrEmpty(PrimaryKey))
     {
         optionUser.PrimaryKey = PrimaryKey;
     }
 }
Beispiel #6
0
 public bool TryFind(Expression <Func <T, bool> > predicate, IQueryOptions <T> queryOptions, out T entity)
 {
     return(Repository.TryFind(predicate, queryOptions, out entity));
 }
Beispiel #7
0
 public TResult Find <TResult>(Expression <Func <T, bool> > predicate, Expression <Func <T, TResult> > selector, IQueryOptions <T> queryOptions = null)
 {
     return(Repository.Find(predicate, selector, queryOptions));
 }
Beispiel #8
0
 public T Find(Expression <Func <T, bool> > predicate, IQueryOptions <T> queryOptions = null)
 {
     return(Repository.Find(predicate, queryOptions));
 }
Beispiel #9
0
        /// <summary>
        /// Returns a new <see cref="Dictionary{TDictionaryKey, TElement}" /> according to the specified <paramref name="keySelector" />, and an element selector function with entities that satisfies the criteria specified by the <paramref name="options" /> in the repository.
        /// </summary>
        /// <typeparam name="TEntity">The type of the of the entity.</typeparam>
        /// <typeparam name="TDictionaryKey">The type of the dictionary key.</typeparam>
        /// <typeparam name="TElement">The type of the value returned by elementSelector.</typeparam>
        /// <param name="options">The options to apply to the query.</param>
        /// <param name="keySelector">A function to extract a key from each entity.</param>
        /// <param name="elementSelector">A transform function to produce a result element value from each element.</param>
        /// <returns>A new <see cref="Dictionary{TDictionaryKey, TEntity}" /> that contains keys and values that satisfies the criteria specified by the <paramref name="options" /> in the repository.</returns>
        public virtual IPagedQueryResult <Dictionary <TDictionaryKey, TElement> > ToDictionary <TEntity, TDictionaryKey, TElement>([CanBeNull] IQueryOptions <TEntity> options, [NotNull] Expression <Func <TEntity, TDictionaryKey> > keySelector, [NotNull] Expression <Func <TEntity, TElement> > elementSelector) where TEntity : class
        {
            Guard.NotNull(keySelector, nameof(keySelector));
            Guard.NotNull(elementSelector, nameof(elementSelector));

            var keySelectFunc       = keySelector.Compile();
            var elementSelectorFunc = elementSelector.Compile();

            var query = ApplyFetchingOptions(AsQueryable <TEntity>(), options)
                        .ApplySpecificationOptions(options)
                        .ApplySortingOptions(Conventions, options);

            Dictionary <TDictionaryKey, TElement> result;
            int total;

            if (options != null && options.PageSize != -1)
            {
                total = query.Count();

                result = query
                         .ApplyPagingOptions(options)
                         .ToDictionary(keySelectFunc, elementSelectorFunc);
            }
            else
            {
                // Gets the total count from memory
                result = query.ToDictionary(keySelectFunc, elementSelectorFunc);
                total  = result.Count;
            }

            return(new PagedQueryResult <Dictionary <TDictionaryKey, TElement> >(result, total));
        }
Beispiel #10
0
 public IEnumerable <TResult> GetAll <TResult>(Expression <Func <T, TResult> > selector, IQueryOptions <T> queryOptions, IFetchStrategy <T> fetchStrategy)
 {
     return(Repository.GetAll(selector, queryOptions, fetchStrategy));
 }
Beispiel #11
0
 protected virtual string FindCacheKey <TResult>(ISpecification <TEntity> criteria, IQueryOptions <TEntity> queryOptions,
                                                 Expression <Func <TEntity, TResult> > selector)
 {
     return(String.Format("{0}/{1}", CachePrefix,
                          String.Join("::", "Find", criteria, queryOptions, GetExpressionBody(selector))));
 }
Beispiel #12
0
 protected virtual string GetAllCacheKey <TResult>(IQueryOptions <TEntity> queryOptions,
                                                   Expression <Func <TEntity, TResult> > selector)
 {
     return(String.Format("{0}/{1}", CachePrefix, String.Join("::", "All", queryOptions, GetExpressionBody(selector))));
 }
Beispiel #13
0
 public void SaveFindResult <TResult>(ISpecification <TEntity> criteria, IQueryOptions <TEntity> queryOptions,
                                      Expression <Func <TEntity, TResult> > selector, TResult result)
 {
     InternalTryPush(FindAllCacheKey(criteria, queryOptions, selector), result, queryOptions);
 }
Beispiel #14
0
 public void SaveGetAllResult <TResult>(IQueryOptions <TEntity> queryOptions, Expression <Func <TEntity, TResult> > selector,
                                        IEnumerable <TResult> result)
 {
     InternalTryPush(GetAllCacheKey(queryOptions, selector), result, queryOptions);
 }
Beispiel #15
0
 /// <summary>
 /// Apply a fetching options to the specified entity's query.
 /// </summary>
 /// <returns>The entity's query with the applied options.</returns>
 protected virtual IQueryable <TEntity> ApplyFetchingOptions <TEntity>(IQueryable <TEntity> query, IQueryOptions <TEntity> options) where TEntity : class
 {
     return(query.ApplyFetchingOptions(Conventions, options, InvokeAsQueryable));
 }
Beispiel #16
0
        /// <summary>
        /// Returns a new <see cref="IEnumerable{TResult}" /> according to the specified <paramref name="keySelector" />, and an element selector function.
        /// </summary>
        /// <typeparam name="TEntity">The type of the of the entity.</typeparam>
        /// <typeparam name="TGroupKey">The type of the group key.</typeparam>
        /// <typeparam name="TResult">The type of the value returned by resultSelector.</typeparam>
        /// <param name="options">The options to apply to the query.</param>
        /// <param name="keySelector">A function to extract a key from each entity.</param>
        /// <param name="resultSelector">A function to project each entity into a new form</param>
        /// <returns>A new <see cref="IEnumerable{TResult}" /> that contains the grouped result that satisfies the criteria specified by the <paramref name="options" /> in the repository.</returns>
        public virtual IPagedQueryResult <IEnumerable <TResult> > GroupBy <TEntity, TGroupKey, TResult>([CanBeNull] IQueryOptions <TEntity> options, [NotNull] Expression <Func <TEntity, TGroupKey> > keySelector, [NotNull] Expression <Func <TGroupKey, IEnumerable <TEntity>, TResult> > resultSelector) where TEntity : class
        {
            Guard.NotNull(keySelector, nameof(keySelector));
            Guard.NotNull(resultSelector, nameof(resultSelector));

            var keySelectFunc      = keySelector.Compile();
            var resultSelectorFunc = resultSelector.Compile();

            var query = ApplyFetchingOptions(AsQueryable <TEntity>(), options)
                        .ApplySpecificationOptions(options)
                        .ApplySortingOptions(Conventions, options);

            var total = query.Count();

            var result = query
                         .ApplyPagingOptions(options)
                         .GroupBy(keySelectFunc, resultSelectorFunc)
                         .ToList();

            return(new PagedQueryResult <IEnumerable <TResult> >(result, total));
        }
Beispiel #17
0
 public IEnumerable <TResult> GetAll <TResult>(Expression <Func <T, TResult> > selector, IQueryOptions <T> queryOptions, params Expression <Func <T, object> >[] includePaths)
 {
     return(Repository.GetAll(selector, queryOptions, includePaths));
 }
Beispiel #18
0
        /// <summary>
        /// Finds the collection of projected entity results in the repository that satisfied the criteria specified by the <paramref name="options" />.
        /// </summary>
        /// <typeparam name="TEntity">The type of the of the entity.</typeparam>
        /// <typeparam name="TResult">The type of the value returned by selector.</typeparam>
        /// <param name="options">The options to apply to the query.</param>
        /// <param name="selector">A function to project each entity into a new form.</param>
        /// <returns>The collection of projected entity results in the repository that satisfied the criteria specified by the <paramref name="options" />.</returns>
        public virtual IPagedQueryResult <IEnumerable <TResult> > FindAll <TEntity, TResult>([CanBeNull] IQueryOptions <TEntity> options, [NotNull] Expression <Func <TEntity, TResult> > selector) where TEntity : class
        {
            Guard.NotNull(selector, nameof(selector));

            var query = ApplyFetchingOptions(AsQueryable <TEntity>(), options)
                        .ApplySpecificationOptions(options)
                        .ApplySortingOptions(Conventions, options);

            var total = query.Count();

            var result = query
                         .ApplyPagingOptions(options)
                         .Select(selector)
                         .ToList();

            return(new PagedQueryResult <IEnumerable <TResult> >(result, total));
        }