示例#1
0
        /// <summary>
        /// Gets a <see cref="IDictionary{K, V}" /> of the name/value pairs to be POSTed to the service.
        /// </summary>
        /// <returns>
        /// The <see cref="IDictionary{K, V}" /> of the name/value pairs to be POSTed to the service.
        /// </returns>
        /// <exception cref="System.ArgumentException">A statement or prepared plan must be provided.</exception>
        /// <remarks>Since values will be POSTed as JSON, here we deal with unencoded typed values
        /// (like ints, Lists, etc...) rather than only strings.</remarks>
        public IDictionary <string, object> GetFormValues()
        {
            if (string.IsNullOrWhiteSpace(_statement) ||
                (_prepareEncoded && _preparedPayload == null))
            {
                throw new ArgumentException("A statement or prepared plan must be provided.");
            }

            //build the map of request parameters
            IDictionary <string, object> formValues = new Dictionary <string, object>();

            if (_maxServerParallelism.HasValue)
            {
                formValues.Add(QueryParameters.MaxServerParallelism, _maxServerParallelism.Value.ToString());
            }

            if (_prepareEncoded)
            {
                formValues.Add(QueryParameters.Prepared, _preparedPayload.Name);

                // don't include empty plan
                if (!string.IsNullOrEmpty(_preparedPayload.EncodedPlan))
                {
                    formValues.Add(QueryParameters.PreparedEncoded, _preparedPayload.EncodedPlan);
                }
            }
            else
            {
                formValues.Add(QueryParameters.Statement, _statement);
            }

            formValues.Add(QueryParameters.Timeout, (uint)_timeOut.TotalMilliseconds + "ms");

            if (_readOnly.HasValue)
            {
                formValues.Add(QueryParameters.Readonly, _readOnly.Value);
            }

            if (_includeMetrics.HasValue)
            {
                formValues.Add(QueryParameters.Metrics, _includeMetrics);
            }

            if (_parameters.Count > 0)
            {
                foreach (var parameter in _parameters)
                {
                    formValues.Add(
                        parameter.Key.Contains("$") ? parameter.Key : "$" + parameter.Key,
                        parameter.Value);
                }
            }

            if (_arguments.Count > 0)
            {
                formValues.Add(QueryParameters.Args, _arguments);
            }

            if (_scanConsistency.HasValue)
            {
                formValues.Add(QueryParameters.ScanConsistency, _scanConsistency.GetDescription());
            }

            if (_scanVectors != null)
            {
#pragma warning disable 618
                if (_scanConsistency != QueryScanConsistency.AtPlus)
#pragma warning restore 618
                {
                    throw new ArgumentException("Only ScanConsistency.AtPlus is supported for this query request.");
                }

                formValues.Add(QueryParameters.ScanVectors, _scanVectors);
            }

            if (_scanWait.HasValue)
            {
                formValues.Add(QueryParameters.ScanWait, $"{(uint) _scanWait.Value.TotalMilliseconds}ms");
            }

            if (_scanCapacity.HasValue)
            {
                formValues.Add(QueryParameters.ScanCapacity, _scanCapacity.Value.ToString());
            }

            if (_pipelineBatch.HasValue)
            {
                formValues.Add(QueryParameters.PipelineBatch, _pipelineBatch.Value.ToString());
            }

            if (_pipelineCapacity.HasValue)
            {
                formValues.Add(QueryParameters.PipelineCapacity, _pipelineCapacity.Value.ToString());
            }

            if (_profile != QueryProfile.Off)
            {
                formValues.Add(QueryParameters.Profile, _profile.ToString().ToLowerInvariant());
            }

            foreach (var parameter in _rawParameters)
            {
                formValues.Add(parameter.Key, parameter.Value);
            }

            if (_autoExecute)
            {
                formValues.Add(QueryParameters.AutoExecute, true);
            }

            formValues.Add(QueryParameters.ClientContextId, CurrentContextId);
            return(formValues);
        }
        private IDictionary <string, object> GetFormValues(bool generateNewId)
        {
            if (string.IsNullOrWhiteSpace(_statement) ||
                (_prepareEncoded && _preparedPayload == null))
            {
                throw new ArgumentException("A statement or prepared plan must be provided.");
            }

            //build the map of request parameters
            IDictionary <string, object> formValues = new Dictionary <string, object>();

            if (_maxServerParallelism.HasValue)
            {
                formValues.Add(QueryParameters.MaxServerParallelism, _maxServerParallelism.Value.ToString());
            }
            if (_prepareEncoded)
            {
                formValues.Add(QueryParameters.Prepared, _preparedPayload.Name);
                formValues.Add(QueryParameters.PreparedEncoded, _preparedPayload.EncodedPlan);
            }
            else
            {
                formValues.Add(QueryParameters.Statement, _statement);
            }

            if (_timeOut.HasValue && _timeOut.Value > TimeSpan.Zero)
            {
                formValues.Add(QueryParameters.Timeout, (uint)_timeOut.Value.TotalMilliseconds + "ms");
            }
            else
            {
                formValues.Add(QueryParameters.Timeout, string.Concat(TimeoutDefault, "ms"));
            }
            if (_readOnly.HasValue)
            {
                formValues.Add(QueryParameters.Readonly, _readOnly.Value);
            }
            if (_includeMetrics.HasValue)
            {
                formValues.Add(QueryParameters.Metrics, _includeMetrics);
            }
            if (_parameters.Count > 0)
            {
                foreach (var parameter in _parameters)
                {
                    formValues.Add(
                        parameter.Key.Contains("$") ? parameter.Key : "$" + parameter.Key,
                        parameter.Value);
                }
            }
            if (_arguments.Count > 0)
            {
                formValues.Add(QueryParameters.Args, _arguments);
            }
            if (_format.HasValue)
            {
                formValues.Add(QueryParameters.Format, _format.Value.ToString());
            }
            if (_encoding.HasValue)
            {
                formValues.Add(QueryParameters.Encoding, _encoding.Value.ToString());
            }
            if (_compression.HasValue)
            {
                formValues.Add(QueryParameters.Compression, _compression.Value.ToString());
            }
            if (_includeSignature.HasValue)
            {
                formValues.Add(QueryParameters.Signature, _includeSignature.Value);
            }
            if (_scanConsistency.HasValue)
            {
                formValues.Add(QueryParameters.ScanConsistency, ScanConsistencyResolver[_scanConsistency.Value]);
            }
            if (_scanVectors != null)
            {
#pragma warning disable 618
                if (_scanConsistency != Query.ScanConsistency.AtPlus)
#pragma warning restore 618
                {
                    throw new ArgumentException("Only ScanConsistency.AtPlus is supported for this query request.");
                }
                formValues.Add(QueryParameters.ScanVectors, _scanVectors);
            }
            if (_scanWait.HasValue)
            {
                formValues.Add(QueryParameters.ScanWait, string.Format("{0}ms", (uint)_scanWait.Value.TotalMilliseconds));
            }
            if (_pretty != null)
            {
                formValues.Add(QueryParameters.Pretty, _pretty.Value);
            }
            if (_credentials.Count > 0)
            {
                var creds = new List <dynamic>();
                foreach (var credential in _credentials)
                {
                    creds.Add(new { user = credential.Key, pass = credential.Value });
                }
                formValues.Add(QueryParameters.Creds, creds);
            }
            if (_scanCapacity.HasValue)
            {
                formValues.Add(QueryParameters.ScanCapacity, _scanCapacity.Value.ToString());
            }
            if (_pipelineBatch.HasValue)
            {
                formValues.Add(QueryParameters.PipelineBatch, _pipelineBatch.Value.ToString());
            }
            if (_pipelineCapacity.HasValue)
            {
                formValues.Add(QueryParameters.PipelineCapacity, _pipelineCapacity.Value.ToString());
            }
            if (generateNewId)
            {
                _requestContextId = QuerySequenceGenerator.GetNext();
            }
            if (_profile != QueryProfile.Off)
            {
                formValues.Add(QueryParameters.Profile, _profile.ToString().ToLowerInvariant());
            }
            foreach (var parameter in _rawParameters)
            {
                formValues.Add(parameter.Key, parameter.Value);
            }

            formValues.Add(QueryParameters.ClientContextId, CurrentContextId);
            return(formValues);
        }