Ejemplo n.º 1
0
        private SFBaseResultSet BuildResultSet(QueryExecResponse response, CancellationToken cancellationToken)
        {
            if (response.success)
            {
                return(new SFResultSet(response.data, this, cancellationToken));
            }

            throw new SnowflakeDbException(response.data.sqlState,
                                           response.code, response.message, response.data.queryId);
        }
Ejemplo n.º 2
0
        internal async Task <SFBaseResultSet> ExecuteAsync(int timeout, string sql, Dictionary <string, BindingDTO> bindings, bool describeOnly,
                                                           CancellationToken cancellationToken)
        {
            registerQueryCancellationCallback(timeout, cancellationToken);
            var queryRequest = BuildQueryRequest(sql, bindings, describeOnly);

            try
            {
                QueryExecResponse response      = null;
                bool receivedFirstQueryResponse = false;
                while (!receivedFirstQueryResponse)
                {
                    response = await _restRequester.PostAsync <QueryExecResponse>(queryRequest, cancellationToken).ConfigureAwait(false);

                    if (SessionExpired(response))
                    {
                        SfSession.renewSession();
                        queryRequest.authorizationToken = string.Format(SF_AUTHORIZATION_SNOWFLAKE_FMT, SfSession.sessionToken);
                    }
                    else
                    {
                        receivedFirstQueryResponse = true;
                    }
                }

                var lastResultUrl = response.data?.getResultUrl;

                while (RequestInProgress(response) || SessionExpired(response))
                {
                    var req = BuildResultRequest(lastResultUrl);
                    response = await _restRequester.GetAsync <QueryExecResponse>(req, cancellationToken).ConfigureAwait(false);

                    if (SessionExpired(response))
                    {
                        logger.Info("Ping pong request failed with session expired, trying to renew the session.");
                        SfSession.renewSession();
                    }
                    else
                    {
                        lastResultUrl = response.data?.getResultUrl;
                    }
                }

                return(BuildResultSet(response, cancellationToken));
            }
            catch
            {
                logger.Error("Query execution failed.");
                throw;
            }
            finally
            {
                ClearQueryRequestId();
            }
        }
Ejemplo n.º 3
0
 private bool SessionExpired(QueryExecResponse r) => r.code == SF_SESSION_EXPIRED_CODE;
Ejemplo n.º 4
0
 private bool RequestInProgress(QueryExecResponse r) =>
 r.code == SF_QUERY_IN_PROGRESS || r.code == SF_QUERY_IN_PROGRESS_ASYNC;
Ejemplo n.º 5
0
        internal SFBaseResultSet execute(string sql, Dictionary <string, BindingDTO> bindings, bool describeOnly)
        {
            if (requestId != null)
            {
                logger.Info("Another query is running.");
                throw new SnowflakeDbException(SFError.STATEMENT_ALREADY_RUNNING_QUERY);
            }
            this.requestId = Guid.NewGuid().ToString();


            UriBuilder uriBuilder = new UriBuilder();

            uriBuilder.Scheme = sfSession.properties[SFSessionProperty.SCHEME];
            uriBuilder.Host   = sfSession.properties[SFSessionProperty.HOST];
            uriBuilder.Port   = Int32.Parse(sfSession.properties[SFSessionProperty.PORT]);
            uriBuilder.Path   = SF_QUERY_PATH;

            var queryString = HttpUtility.ParseQueryString(string.Empty);

            queryString[SF_QUERY_REQUEST_ID] = requestId;
            uriBuilder.Query = queryString.ToString();

            QueryRequest postBody = new QueryRequest()
            {
                sqlText           = sql,
                parameterBindings = bindings,
                describeOnly      = describeOnly,
            };

            SFRestRequest queryRequest = new SFRestRequest();

            queryRequest.uri = uriBuilder.Uri;
            queryRequest.authorizationToken = String.Format(SF_AUTHORIZATION_SNOWFLAKE_FMT, sfSession.sessionToken);
            queryRequest.jsonBody           = postBody;
            queryRequest.httpRequestTimeout = -1;

            try
            {
                JObject           rawResponse  = restRequest.post(queryRequest);
                QueryExecResponse execResponse = rawResponse.ToObject <QueryExecResponse>();

                if (execResponse.code == SF_SESSION_EXPIRED_CODE)
                {
                    sfSession.renewSession();
                    this.execute(sql, bindings, describeOnly);
                }
                else if (execResponse.code == SF_QUERY_IN_PROGRESS ||
                         execResponse.code == SF_QUERY_IN_PROGRESS_ASYNC)
                {
                    logger.Info("Query execution in progress.");
                    bool   isSessionRenewed = false;
                    string getResultUrl     = null;
                    while (execResponse.code == SF_QUERY_IN_PROGRESS ||
                           execResponse.code == SF_QUERY_IN_PROGRESS_ASYNC)
                    {
                        if (!isSessionRenewed)
                        {
                            getResultUrl = execResponse.data.getResultUrl;
                        }

                        UriBuilder getResultUriBuilder = new UriBuilder();
                        getResultUriBuilder.Scheme = sfSession.properties[SFSessionProperty.SCHEME];
                        getResultUriBuilder.Host   = sfSession.properties[SFSessionProperty.HOST];
                        getResultUriBuilder.Port   = Int32.Parse(sfSession.properties[SFSessionProperty.PORT]);
                        getResultUriBuilder.Path   = getResultUrl;

                        SFRestRequest getResultRequest = new SFRestRequest()
                        {
                            uri = getResultUriBuilder.Uri,
                            authorizationToken = String.Format(SF_AUTHORIZATION_SNOWFLAKE_FMT, sfSession.sessionToken)
                        };
                        getResultRequest.httpRequestTimeout = -1;

                        execResponse = null;
                        execResponse = restRequest.get(getResultRequest).ToObject <QueryExecResponse>();

                        if (execResponse.code == SF_SESSION_EXPIRED_CODE)
                        {
                            logger.Info("Ping pong request failed with session expired, trying to renew the session.");
                            sfSession.renewSession();
                            isSessionRenewed = true;
                        }
                        else
                        {
                            isSessionRenewed = false;
                        }
                    }
                }

                if (execResponse.success)
                {
                    return(new SFResultSet(execResponse.data, this));
                }
                else
                {
                    SnowflakeDbException e = new SnowflakeDbException(
                        execResponse.data.sqlState, execResponse.code, execResponse.message,
                        execResponse.data.queryId);
                    logger.Error("Query execution failed.", e);
                    throw e;
                }
            }
            finally
            {
                this.requestId = null;
            }
        }