예제 #1
0
        /// <summary>
        /// Removes data from local storage. This does not affect the backend.
        /// </summary>
        /// <returns>Details of the clear operation, including the number of entities that were cleared.</returns>
        /// <param name="query">Optional Query parameter.</param>
        public KinveyDeleteResponse ClearCache(IQueryable <T> query = null)
        {
            if (this.storeType == DataStoreType.NETWORK)
            {
                throw new KinveyException(EnumErrorCategory.ERROR_DATASTORE_NETWORK, EnumErrorCode.ERROR_DATASTORE_INVALID_CLEAR_CACHE_OPERATION, "");
            }

            var ret = cache.Clear(query?.Expression);

            if (ret?.IDs != null)
            {
                var pendings = ret.IDs.Select(entityId => syncQueue.GetByID(entityId));
                syncQueue.Remove(pendings);
            }
            else
            {
                syncQueue.RemoveAll();
            }

            // Remove any associated entry from the QueryCache (for delta set sync)
            string mongoQuery = null;

            if (query != null)
            {
                StringQueryBuilder queryBuilder = new StringQueryBuilder();

                KinveyQueryVisitor visitor = new KinveyQueryVisitor(queryBuilder, typeof(T), VisitorClause.Order | VisitorClause.SkipTake | VisitorClause.Where | VisitorClause.Select);
                var queryModel             = (query.Provider as KinveyQueryProvider)?.qm;

                queryBuilder.Write("{");
                queryModel?.Accept(visitor);
                queryBuilder.Write("}");

                mongoQuery = queryBuilder.BuildQueryString();
            }

            var qci = client.CacheManager.GetQueryCacheItem(CollectionName, mongoQuery, null);

            if (qci != null)
            {
                client.CacheManager.DeleteQueryCacheItem(qci);
            }

            return(ret);
        }
예제 #2
0
        public KinveyDeleteResponse DeleteByQuery(IQueryable <object> query)
        {
            var kdr = new KinveyDeleteResponse();

            try
            {
                var visitor    = new KinveyQueryVisitor(typeof(T), VisitorClause.Where);
                var queryModel = (query.Provider as KinveyQueryProvider)?.qm;
                //We call it here to find unsupported LINQ where clauses.
                queryModel?.Accept(visitor);

                int              skipNumber    = 0;
                int              takeNumber    = 0;
                bool             sortAscending = true;
                LambdaExpression exprSort      = null;

                var lambdaExpr = ConvertQueryExpressionToFunction(query.Expression, ref skipNumber, ref takeNumber, ref sortAscending, ref exprSort);

                var dataTable = dbConnectionSync.Table <T>();

                if (lambdaExpr == null)
                {
                    throw new KinveyException(EnumErrorCategory.ERROR_GENERAL, EnumErrorCode.ERROR_DATASTORE_WHERE_CLAUSE_IS_ABSENT_IN_QUERY, "'Where' clause is absent in query.");
                }

                dataTable = dataTable.Where(lambdaExpr);

                var matchIDs = new List <string>();
                foreach (var item in dataTable.ToList())
                {
                    var entity = item as IPersistable;
                    matchIDs.Add(entity.ID);
                }

                kdr = this.DeleteByIDs(matchIDs);
            }
            catch (SQLiteException ex)
            {
                throw new KinveyException(EnumErrorCategory.ERROR_DATASTORE_CACHE, EnumErrorCode.ERROR_DATASTORE_CACHE_REMOVING_ENTITIES_ACCORDING_TO_QUERY, string.Empty, ex);
            }

            return(kdr);
        }
예제 #3
0
        /// <summary>
        /// Builds the mongo-style query string to be run against the backend.
        /// </summary>
        /// <param name="query">LINQ-style query that can be used to filter delete results.</param>
        /// <param name="visitorClause">Enum to filter LINQ clauses which should be used in a query for network request.</param>
        /// <returns>The mongo-style query string.</returns>
        private static string GetQuery <T>(IQueryable <object> query, VisitorClause visitorClause)
        {
            if (query != null)
            {
                StringQueryBuilder queryBuilder = new StringQueryBuilder();

                KinveyQueryVisitor visitor    = new KinveyQueryVisitor(queryBuilder, typeof(T), visitorClause);
                QueryModel         queryModel = (query.Provider as KinveyQueryProvider)?.qm;

                queryBuilder.Write("{");
                queryModel?.Accept(visitor);
                queryBuilder.Write("}");

                string mongoQuery = queryBuilder.BuildQueryString();
                return(mongoQuery);
            }

            return(default(string));
        }