Esempio n. 1
0
        /// <summary>
        /// Reads data from local store by executing the query.
        /// </summary>
        /// <param name="query">The query to execute on local store.</param>
        /// <returns>A task that will return with results when the query finishes.</returns>
        public override Task <JToken> ReadAsync(MobileServiceTableQueryDescription query)
        {
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }

            this.EnsureInitialized();

            var    formatter = new SqlQueryFormatter(query);
            string sql       = formatter.FormatSelect();

            IList <JObject> rows   = this.ExecuteQuery(query.TableName, sql, formatter.Parameters);
            JToken          result = new JArray(rows.ToArray());

            if (query.IncludeTotalCount)
            {
                sql = formatter.FormatSelectCount();
                IList <JObject> countRows = this.ExecuteQuery(query.TableName, sql, formatter.Parameters);
                long            count     = countRows[0].Value <long>("count");
                result = new JObject()
                {
                    { "results", result },
                    { "count", count }
                };
            }

            return(Task.FromResult(result));
        }
        /// <summary>
        /// Deletes items from local table that match the given query.
        /// </summary>
        /// <param name="query">A query to find records to delete.</param>
        /// <returns>A task that completes when delete query has executed.</returns>
        /// <exception cref="ArgumentNullException">You must supply a query value</exception>
        public override Task DeleteAsync(MobileServiceTableQueryDescription query)
        {
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }

            this.EnsureInitialized();

            var    formatter = new SqlQueryFormatter(query);
            string sql       = formatter.FormatDelete();

            return(this.operationSemaphore.WaitAsync()
                   .ContinueWith(t =>
            {
                try
                {
                    this.ExecuteNonQueryInternal(sql, formatter.Parameters);
                }
                finally
                {
                    this.operationSemaphore.Release();
                }
            }));
        }
Esempio n. 3
0
        /// <summary>
        /// Deletes items from local table that match the given query.
        /// </summary>
        /// <param name="query">A query to find records to delete.</param>
        /// <returns>A task that completes when delete query has executed.</returns>
        public override Task DeleteAsync(MobileServiceTableQueryDescription query)
        {
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }

            this.EnsureInitialized();

            var    formatter = new SqlQueryFormatter(query);
            string sql       = formatter.FormatDelete();

            this.ExecuteNonQuery(sql, formatter.Parameters);

            return(Task.FromResult(0));
        }
        public string FormatDelete()
        {
            var delQuery = this.query.Clone(); // create a copy to avoid modifying the original

            delQuery.Selection.Clear();
            delQuery.Selection.Add(MobileServiceSystemColumns.Id);
            delQuery.IncludeTotalCount = false;

            var    formatter     = new SqlQueryFormatter(delQuery);
            string selectIdQuery = formatter.FormatSelect();
            string idMemberName  = SqlHelpers.FormatMember(MobileServiceSystemColumns.Id);
            string tableName     = SqlHelpers.FormatTableName(delQuery.TableName);
            string command       = string.Format("DELETE FROM {0} WHERE {1} IN ({2})", tableName, idMemberName, selectIdQuery);

            this.Parameters = formatter.Parameters;

            return(command);
        }
        /// <summary>
        /// Reads data from local store by executing the query.
        /// </summary>
        /// <param name="query">The query to execute on local store.</param>
        /// <returns>A task that will return with results when the query finishes.</returns>
        public override Task <JToken> ReadAsync(MobileServiceTableQueryDescription query)
        {
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }

            this.EnsureInitialized();

            var    formatter = new SqlQueryFormatter(query);
            string sql       = formatter.FormatSelect();

            return(this.operationSemaphore.WaitAsync()
                   .ContinueWith(t =>
            {
                try
                {
                    IList <JObject> rows = this.ExecuteQueryInternal(query.TableName, sql, formatter.Parameters);
                    JToken result = new JArray(rows.ToArray());

                    if (query.IncludeTotalCount)
                    {
                        sql = formatter.FormatSelectCount();
                        IList <JObject> countRows = null;

                        countRows = this.ExecuteQueryInternal(query.TableName, sql, formatter.Parameters);


                        long count = countRows[0].Value <long>("count");
                        result = new JObject()
                        {
                            { "results", result },
                            { "count", count }
                        };
                    }

                    return result;
                }
                finally
                {
                    this.operationSemaphore.Release();
                }
            }));
        }