Example #1
0
        public PqlCompilerState(
            IExpressionEvaluatorRuntime parentRuntime,
            ParsedRequest parsedRequest,
            DataRequestParams requestParams,
            DataContainerDescriptor containerDescriptor,
            Type contextType,
            Type returnType)
            : base(parentRuntime, contextType, returnType, null)
        {
            if (parsedRequest == null)
            {
                throw new ArgumentNullException("parsedRequest");
            }

            if (containerDescriptor == null)
            {
                throw new ArgumentNullException("containerDescriptor");
            }

            if (requestParams == null)
            {
                throw new ArgumentNullException("requestParams");
            }

            ParsedRequest = parsedRequest;
            RequestParameters = requestParams;
            ContainerDescriptor = containerDescriptor;
            FieldRefs = new Dictionary<int, Tuple<ParameterExpression, Expression>>();
            ParamRefs = new Dictionary<int, Tuple<ParameterExpression, Expression>>();
        }
        public RequestExecutionContextCacheInfo(long hashCode)
        {
            if (hashCode == 0)
            {
                throw new ArgumentOutOfRangeException("hashCode", hashCode, "hashCode must be non-zero");
            }

            HashCode = hashCode;
            ParsedRequest = new ParsedRequest(true);
            RequestParams = new DataRequestParams();
            RequestBulk = new DataRequestBulk();
        }
        /// <summary>
        /// Computes an Int64 hash value for a request for use with dictionary of prepared requests.
        /// </summary>
        public static long GetRequestHash(DataRequest request, DataRequestBulk requestBulk, DataRequestParams requestParams)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            if (request.HaveRequestBulk && requestBulk == null)
            {
                throw new ArgumentNullException("requestBulk");
            }

            if (request.HaveParameters && requestParams == null)
            {
                throw new ArgumentNullException("requestParams");
            }

            long hash;
            int modulo;
            InitHash(out hash, out modulo);

            if (request.HaveRequestBulk)
            {
                AppendToHash(ref hash, modulo, (requestBulk.EntityName ?? string.Empty).GetHashCode());
                AppendToHash(ref hash, modulo, (int)requestBulk.DbStatementType);

                foreach (var f in requestBulk.FieldNames)
                {
                    AppendToHash(ref hash, modulo, f.GetHashCode());
                }
            }
            else
            {
                AppendToHash(ref hash, modulo, (request.CommandText ?? string.Empty).GetHashCode());

                if (request.HaveParameters)
                {
                    foreach (var dataType in requestParams.DataTypes)
                    {
                        AppendToHash(ref hash, modulo, (int)dataType);
                    }

                    foreach (var bitVectorData in requestParams.IsCollectionFlags)
                    {
                        AppendToHash(ref hash, modulo, bitVectorData);
                    }
                }
            }

            return hash;
        }
        /// <summary>
        /// Reads portion of request headers appropriate for caching into the cache buffer.
        /// </summary>
        public void ReadRequestHeaders(DataRequest request, DataRequestParams requestParams, DataRequestBulk requestBulk, ParsedRequest parsedRequest)
        {
            if (request == null)
            {
                throw new ArgumentNullException("requestParams");
            }

            CommandText = request.CommandText;

            RequestParams.Clear();
            RequestBulk.Clear();

            if (request.HaveParameters)
            {
                RequestParams.DataTypes = requestParams.DataTypes;
                RequestParams.IsCollectionFlags = requestParams.IsCollectionFlags;
                RequestParams.Names = requestParams.Names;

                // some values are written into parsed request during request read process
                // have to replicate them in the cached version, because they are required for subsequent compilation
                ParsedRequest.Params.Names = parsedRequest.Params.Names;
                ParsedRequest.Params.DataTypes = parsedRequest.Params.DataTypes;
                ParsedRequest.Params.OrdinalToLocalOrdinal = parsedRequest.Params.OrdinalToLocalOrdinal;
            }

            if (request.HaveRequestBulk)
            {
                RequestBulk.DbStatementType = requestBulk.DbStatementType;
                RequestBulk.EntityName = requestBulk.EntityName;
                RequestBulk.FieldNames = requestBulk.FieldNames;

                // some values are written into parsed request during request read process
                // have to replicate them in the cached version, because they are required for subsequent compilation
                ParsedRequest.IsBulk = parsedRequest.IsBulk;
            }

            if (StringComparer.OrdinalIgnoreCase.Equals(CommandText, "defragment"))
            {
                ParsedRequest.SpecialCommand.IsSpecialCommand = true;
                ParsedRequest.SpecialCommand.CommandType = ParsedRequest.SpecialCommandData.SpecialCommandType.Defragment;
            }

            HaveRequestHeaders = true;
        }
        private PqlMessage SendCommand(IDataService channel, DataRequest dataRequest, DataRequestParams dataRequestParams, DataRequestBulk dataRequestBulk)
        {
            var authContext = m_connection.ClientSecurityContext;

            if (authContext == null)
            {
                throw new InvalidOperationException("Authentication context is not set on the thread");
            }

            if (string.IsNullOrWhiteSpace(authContext.TenantId))
            {
                throw new InvalidOperationException("Current authentication context does not have value for TenantId");
            }

            Message responseMessage;
            using (var holder = PqlDataConnection.CommandStreams.Take(m_connection.CancellationTokenSource.Token))
            {
                holder.Item.Attach(dataRequest, dataRequestParams, dataRequestBulk);

                var requestMessage = new PqlMessage(
                    holder.Item, new IDisposable[] {holder},
                    AuthContextSerializer.GetString(authContext),
                    m_connection.ConnectionProps.ScopeId,
                    m_connection.ConnectionProps.ProtocolVersion);

                responseMessage = channel.Process(requestMessage);
            }

            var pqlMessage = responseMessage as PqlMessage;

            if (pqlMessage != null)
            {
                return pqlMessage;
            }

            throw new DataException(string.Format(
                "Message must be of type {0}. Actual type that came from WCF transport was {1}",
                typeof(PqlMessage).AssemblyQualifiedName,
                responseMessage.GetType().AssemblyQualifiedName));
        }
        private DataRequestParams CreateRequestParams()
        {
            m_parameters.Validate();

            if (m_parameters.Count == 0)
            {
                return null;
            }

            var arr = m_parameters.ParametersData;

            var result = new DataRequestParams
                {
                    DataTypes = new DbType[arr.Count],
                    Names = new string[arr.Count],
                    IsCollectionFlags = new int[BitVector.GetArrayLength(arr.Count)]
                };

            for (var i = 0; i < arr.Count; i++)
            {
                result.DataTypes[i] = arr[i].DbType;
                result.Names[i] = arr[i].ParameterName;
                if (arr[i].IsValidatedCollection)
                {
                    BitVector.Set(result.IsCollectionFlags, i);
                }
            }

            result.Bulk = m_parameters;

            return result;
        }
        /// <summary>
        /// Ctr.
        /// </summary>
        /// <param name="process">Parent process, receives crash notifications</param>
        /// <param name="tracer">Tracer object</param>
        public RequestExecutionContext(IPqlEngineHostProcess process, ITracer tracer)
        {
            if (process == null)
            {
                throw new ArgumentNullException("process");
            }

            if (tracer == null)
            {
                throw new ArgumentNullException("tracer");
            }

            m_tracer = tracer;

            m_process = process;

            ParsedRequest = new ParsedRequest(false);
            Request = new DataRequest();
            RequestBulk = new DataRequestBulk();
            RequestParameters = new DataRequestParams();

            m_buffersRingItems = new []
                {
                    new RequestExecutionBuffer(),
                    new RequestExecutionBuffer(),
                    new RequestExecutionBuffer()
                };
        }