/// <summary> /// Prepares the statement if the <see cref="IQueryRequest"/> is not ad-hoc and caches it for reuse.asynchronously. /// </summary> /// <param name="originalRequest">The original query request.</param> async Task PrepareStatementIfNotAdHocAsync(IQueryRequest originalRequest) { if (originalRequest.IsAdHoc) { return; } var statement = originalRequest.GetStatement(); if (statement == null) { statement = originalRequest.GetPreparedPayload().Text; } QueryPlan queryPlan; if (_queryCache.TryGetValue(statement, out queryPlan)) { originalRequest.Prepared(queryPlan); } else { var result = await PrepareAsync(originalRequest); if (!result.Success) { return; //FIXME propagate the error somehow } queryPlan = result.Rows.First(); queryPlan.Text = originalRequest.GetStatement(); if (_queryCache.TryAdd(statement, queryPlan)) { originalRequest.Prepared(queryPlan); } } }
/// <summary> /// Prepares the statement if the <see cref="IQueryRequest"/> is not ad-hoc and caches it for reuse. /// </summary> /// <param name="originalRequest">The original query request.</param> void PrepareStatementIfNotAdHoc(IQueryRequest originalRequest) { if (originalRequest.IsAdHoc) { return; } var statement = originalRequest.GetStatement(); if (statement == null) { statement = originalRequest.GetPreparedPayload().Text; } QueryPlan queryPlan; if (_queryCache.TryGetValue(statement, out queryPlan)) { originalRequest.Prepared(queryPlan); } else { var result = Prepare(originalRequest); //FIXME throw an exception? if (!result.Success) { return; } queryPlan = result.Rows.First(); queryPlan.Text = originalRequest.GetStatement(); //the plan Text will be used if we reprepare it if (_queryCache.TryAdd(statement, queryPlan)) { originalRequest.Prepared(queryPlan); } } }
/// <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); }
/// <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.GetStatement(); if (!statement.ToUpper().StartsWith("PREPARE ")) { statement = string.Concat("PREPARE ", statement); } var query = new QueryRequest(statement); query.BaseUri(toPrepare.GetBaseUri()); return(ExecuteQuery <QueryPlan>(query)); }