示例#1
0
        private async Task <IQueryResult <T> > PostAsync <T>(IQueryRequest queryRequest)
        {
            var queryResult = new QueryResult <T>();
            var content     = new FormUrlEncodedContent(queryRequest.GetFormValues());

            try
            {
                var request = await HttpClient.PostAsync(queryRequest.GetBaseUri(), content);

                using (var response = await request.Content.ReadAsStreamAsync())
                {
                    queryResult         = DataMapper.Map <QueryResult <T> >(response);
                    queryResult.Success = queryResult.Status == QueryStatus.Success;
                }
            }
            catch (AggregateException ae)
            {
                ae.Flatten().Handle(e =>
                {
                    Log.Error(e);
                    ProcessError(e, queryResult);
                    return(true);
                });
            }
            return(queryResult);
        }
示例#2
0
        /// <summary>
        /// Prepare an ad-hoc N1QL statement for later execution against a Couchbase Server.
        /// </summary>
        /// <param name="toPrepare">The <see cref="IQueryRequest" /> containing a N1QL statement to be prepared.</param>
        /// <returns>
        /// A <see cref="IQueryResult{T}" /> containing  the <see cref="IQueryPlan" /> representing the reusable
        /// and cachable execution plan for the statement.
        /// </returns>
        /// <remarks>
        /// Most parameters in the IQueryRequest will be ignored, appart from the Statement and the BaseUri.
        /// </remarks>
        public IQueryResult <IQueryPlan> Prepare(IQueryRequest toPrepare)
        {
            string statement = toPrepare.GetStatement();

            if (!statement.ToUpper().StartsWith("PREPARE "))
            {
                statement = string.Concat("PREPARE ", statement);
            }
            QueryRequest query = new QueryRequest(statement);

            query.BaseUri(toPrepare.GetBaseUri());
            IQueryResult <dynamic>    planResult = Query <dynamic>(query);
            IQueryResult <IQueryPlan> result     = new QueryResult <IQueryPlan>()
            {
                Message         = planResult.Message,
                ClientContextId = planResult.ClientContextId,
                Errors          = planResult.Errors,
                Exception       = planResult.Exception,
                Metrics         = planResult.Metrics,
                RequestId       = planResult.RequestId,
                Signature       = planResult.Signature,
                Status          = planResult.Status,
                Rows            = new List <IQueryPlan>(1),
                Success         = planResult.Success
            };

            if (planResult.Success)
            {
                var    rawPlan     = planResult.Rows.First();
                string encodedPlan = EncodeParameter(rawPlan);
                result.Rows.Add(new QueryPlan(encodedPlan));
            }
            return(result);
        }
示例#3
0
        /// <summary>
        /// Executes the <see cref="IQueryRequest"/> using HTTP POST to the Couchbase Server asynchronously.
        /// </summary>
        /// <typeparam name="T">The <see cref="Type"/> of each row returned by the query.</typeparam>
        /// <param name="queryRequest">The query request.</param>
        /// <returns></returns>
        /// <remarks>The format for the querying is JSON</remarks>
        private async Task <IQueryResult <T> > ExecuteQueryAsync <T>(IQueryRequest queryRequest)
        {
            var queryResult = new QueryResult <T>();

            using (var content = new StringContent(queryRequest.GetFormValuesAsJson(), System.Text.Encoding.UTF8, "application/json")) {
                try
                {
                    var request = await HttpClient.PostAsync(queryRequest.GetBaseUri(), content);

                    using (var response = await request.Content.ReadAsStreamAsync())
                    {
                        queryResult         = DataMapper.Map <QueryResult <T> >(response);
                        queryResult.Success = queryResult.Status == QueryStatus.Success;
                    }
                }
                catch (AggregateException ae)
                {
                    ae.Flatten().Handle(e =>
                    {
                        Log.Error(e);
                        ProcessError(e, queryResult);
                        return(true);
                    });
                }
            }

            return(queryResult);
        }
示例#4
0
 /// <summary>
 /// Sends a request for a N1QL query to the server.
 /// </summary>
 /// <typeparam name="T">The <see cref="Type" /> T of the body for each row (or document) result.</typeparam>
 /// <param name="queryRequest">A <see cref="IQueryRequest" /> object.</param>
 /// <returns></returns>
 Task <IQueryResult <T> > IServer.SendAsync <T>(IQueryRequest queryRequest)
 {
     if (queryRequest.GetBaseUri() == null)
     {
         var uri = new Uri(GetBaseQueryUri());
         queryRequest.BaseUri(uri);
     }
     return(QueryClient.QueryAsync <T>(queryRequest));
 }
示例#5
0
 /// <summary>
 /// Prepare an ad-hoc N1QL statement for later execution against a Couchbase Server.
 /// </summary>
 /// <param name="toPrepare">The <see cref="IQueryRequest" /> containing a N1QL statement to be prepared.</param>
 /// <returns>
 /// A <see cref="IQueryResult{T}" /> containing  the <see cref="QueryPlan" /> representing the reusable
 /// and cachable execution plan for the statement.
 /// </returns>
 /// <remarks>
 /// Most parameters in the IQueryRequest will be ignored, appart from the Statement and the BaseUri.
 /// </remarks>
 public IQueryResult<QueryPlan> Prepare(IQueryRequest toPrepare)
 {
     var statement = toPrepare.GetOriginalStatement();
     if (!statement.ToUpper().StartsWith("PREPARE "))
     {
         statement = string.Concat("PREPARE ", statement);
     }
     var query = new QueryRequest(statement);
     query.BaseUri(toPrepare.GetBaseUri());
     return ExecuteQuery<QueryPlan>(query);
 }
示例#6
0
 /// <summary>
 /// Prepare an ad-hoc N1QL statement for later execution against a Couchbase Server asynchronously
 /// </summary>
 /// <param name="toPrepare">The <see cref="IQueryRequest" /> containing a N1QL statement to be prepared.</param>
 /// <param name="cancellationToken">Token which can cancel the query.</param>
 /// <returns>
 /// A <see cref="IQueryResult{T}" /> containing  the <see cref="QueryPlan" /> representing the reusable
 /// and cachable execution plan for the statement.
 /// </returns>
 /// <remarks>
 /// Most parameters in the IQueryRequest will be ignored, appart from the Statement and the BaseUri.
 /// </remarks>
 public async Task<IQueryResult<QueryPlan>> PrepareAsync(IQueryRequest toPrepare, CancellationToken cancellationToken)
 {
     var statement = toPrepare.GetOriginalStatement();
     if (!statement.ToUpper().StartsWith("PREPARE "))
     {
         statement = string.Concat("PREPARE ", statement);
     }
     var query = new QueryRequest(statement);
     query.BaseUri(toPrepare.GetBaseUri());
     return await ExecuteQueryAsync<QueryPlan>(query, cancellationToken).ContinueOnAnyContext();
 }
示例#7
0
        /// <summary>
        /// Prepare an ad-hoc N1QL statement for later execution against a Couchbase Server asynchronously
        /// </summary>
        /// <param name="toPrepare">The <see cref="IQueryRequest" /> containing a N1QL statement to be prepared.</param>
        /// <returns>
        /// A <see cref="IQueryResult{T}" /> containing  the <see cref="QueryPlan" /> representing the reusable
        /// and cachable execution plan for the statement.
        /// </returns>
        /// <remarks>
        /// Most parameters in the IQueryRequest will be ignored, appart from the Statement and the BaseUri.
        /// </remarks>
        public async Task <IQueryResult <QueryPlan> > PrepareAsync(IQueryRequest toPrepare)
        {
            var statement = toPrepare.GetStatement();

            if (!statement.ToUpper().StartsWith("PREPARE "))
            {
                statement = string.Concat("PREPARE ", statement);
            }
            var query = new QueryRequest(statement);

            query.BaseUri(toPrepare.GetBaseUri());
            return(await ExecuteQueryAsync <QueryPlan>(query));
        }
        /// <summary>
        /// Executes the <see cref="IQueryRequest"/> using HTTP POST to the Couchbase Server.
        /// </summary>
        /// <typeparam name="T">The <see cref="Type"/> of each row returned by the query.</typeparam>
        /// <param name="queryRequest">The query request.</param>
        /// <returns></returns>
        /// <remarks>The format for the querying is JSON</remarks>
        IQueryResult <T> ExecuteQuery <T>(IQueryRequest queryRequest)
        {
            var queryResult = new QueryResult <T>();

            try
            {
                var request = WebRequest.Create(queryRequest.GetBaseUri());
                request.Timeout     = (int)_clientConfig.QueryRequestTimeout;
                request.Method      = "POST";
                request.ContentType = "application/json";

                var json  = queryRequest.GetFormValuesAsJson();
                var bytes = System.Text.Encoding.UTF8.GetBytes(json);
                request.ContentLength = bytes.Length;

                using (var stream = request.GetRequestStream())
                {
                    stream.Write(bytes, 0, bytes.Length);
                }

                var response = request.GetResponse();
                using (var stream = response.GetResponseStream())
                {
                    queryResult         = DataMapper.Map <QueryResult <T> >(stream);
                    queryResult.Success = queryResult.Status == QueryStatus.Success;
                }
            }
            catch (WebException e)
            {
                if (e.Response != null)
                {
                    var stream = e.Response.GetResponseStream();
                    queryResult = DataMapper.Map <QueryResult <T> >(stream);
                    queryResult.HttpStatusCode = ((HttpWebResponse)e.Response).StatusCode;
                }
                queryResult.Exception = e;
                Log.Error(e);
            }
            catch (Exception e)
            {
                ProcessError(e, queryResult);
                Log.Error(e);
            }
            return(queryResult);
        }
示例#9
0
        IQueryResult <T> Post <T>(IQueryRequest queryRequest)
        {
            var queryResult = new QueryResult <T>();

            try
            {
                var request = WebRequest.Create(queryRequest.GetBaseUri());
                request.Timeout     = _clientConfig.ViewRequestTimeout;
                request.Method      = "POST";
                request.ContentType = "application/x-www-form-urlencoded";

                var bytes = System.Text.Encoding.UTF8.GetBytes(queryRequest.GetQueryParameters());
                request.ContentLength = bytes.Length;

                using (var stream = request.GetRequestStream())
                {
                    stream.Write(bytes, 0, bytes.Length);
                }

                var response = request.GetResponse();
                using (var stream = response.GetResponseStream())
                {
                    queryResult         = DataMapper.Map <QueryResult <T> >(stream);
                    queryResult.Success = queryResult.Status == QueryStatus.Success;
                }
            }
            catch (WebException e)
            {
                if (e.Response != null)
                {
                    var stream = e.Response.GetResponseStream();
                    queryResult = DataMapper.Map <QueryResult <T> >(stream);
                }
                queryResult.Exception = e;
                Log.Error(e);
            }
            catch (Exception e)
            {
                ProcessError(e, queryResult);
                Log.Error(e);
            }
            return(queryResult);
        }
示例#10
0
        /// <summary>
        /// Sends a request for a N1QL query to the server asynchronously.
        /// </summary>
        /// <typeparam name="T">The <see cref="Type" /> T of the body for each row (or document) result.</typeparam>
        /// <param name="queryRequest">A <see cref="IQueryRequest" /> object.</param>
        /// <returns>
        /// An <see cref="Task{IQueryResult}" /> object representing the asynchronous operation.
        /// </returns>
        IQueryResult <T> IServer.Send <T>(IQueryRequest queryRequest)
        {
            IQueryResult <T> result;

            try
            {
                if (queryRequest.GetBaseUri() == null)
                {
                    var uri = new Uri(GetBaseQueryUri());
                    queryRequest.BaseUri(uri);
                }
                result = QueryClient.Query <T>(queryRequest);
            }
            catch (Exception e)
            {
                result = new QueryResult <T>
                {
                    Exception = e,
                    Message   = e.Message,
                    Success   = false,
                };
            }
            return(result);
        }