/// <summary>
        /// Executes the query as statement.
        /// </summary>
        /// <returns>An <see cref="AceQLDataReader"/>object.</returns>
        /// <exception cref="AceQL.Client.Api.AceQLException">If any Exception occurs.</exception>
        private async Task <AceQLDataReader> ExecuteQueryAsStatementAsync()
        {
            try
            {
                IFile file = await GetUniqueResultSetFileAsync().ConfigureAwait(false);

                bool    isStoredProcedure   = (commandType == CommandType.StoredProcedure ? true : false);
                Boolean isPreparedStatement = false;
                Dictionary <string, string> parametersMap = null;

                using (Stream input = await aceQLHttpApi.ExecuteQueryAsync(cmdText, Parameters, isStoredProcedure, isPreparedStatement, parametersMap).ConfigureAwait(false))
                {
                    try
                    {
                        await CopyHttpStreamToFile(file, input).ConfigureAwait(false);
                    }
                    catch (Exception exception)
                    {
                        if (this.connection.RequestRetry && (this.executeQueryRetryCount < 1 || exception.Message.Contains("GZip")))
                        {
                            this.executeQueryRetryCount++;
                            Boolean saveGzipResultValue = this.aceQLHttpApi.GzipResult;
                            this.aceQLHttpApi.GzipResult = false;
                            AceQLDataReader dataReader = await ExecuteQueryAsStatementAsync().ConfigureAwait(false);

                            this.aceQLHttpApi.GzipResult = saveGzipResultValue;
                            return(dataReader);
                        }
                        else
                        {
                            throw;
                        }
                    }
                }

                this.executeQueryRetryCount = 0;

                StreamResultAnalyzer streamResultAnalyzer = new StreamResultAnalyzer(file, aceQLHttpApi.HttpStatusCode);
                if (!await streamResultAnalyzer.IsStatusOkAsync().ConfigureAwait(false))
                {
                    throw new AceQLException(streamResultAnalyzer.GetErrorMessage(),
                                             streamResultAnalyzer.GetErrorType(),
                                             streamResultAnalyzer.GetStackTrace(),
                                             aceQLHttpApi.HttpStatusCode);
                }

                int rowsCount = 0;

                using (Stream readStreamCout = await file.OpenAsync(FileAccess.Read).ConfigureAwait(false))
                {
                    RowCounter rowCounter = new RowCounter(readStreamCout);
                    rowsCount = rowCounter.Count();
                }

                if (isStoredProcedure)
                {
                    using (Stream readStreamOutParms = await file.OpenAsync(FileAccess.Read).ConfigureAwait(false))
                    {
                        UpdateOutParametersValues(readStreamOutParms, Parameters);
                    }
                }

                Stream readStream = await file.OpenAsync(FileAccess.Read).ConfigureAwait(false);

                AceQLDataReader aceQLDataReader = new AceQLDataReader(file, readStream, rowsCount, connection);
                return(aceQLDataReader);
            }
            catch (Exception exception)
            {
                await simpleTracer.TraceAsync(exception.ToString()).ConfigureAwait(false);

                if (exception.GetType() == typeof(AceQLException))
                {
                    throw;
                }
                else
                {
                    throw new AceQLException(exception.Message, 0, exception, aceQLHttpApi.HttpStatusCode);
                }
            }
        }
        /// <summary>
        /// Executes the query as prepared statement.
        /// </summary>
        /// <returns>An <see cref="AceQLDataReader"/> object.</returns>
        /// <exception cref="AceQL.Client.Api.AceQLException">If any Exception occurs.</exception>
        private async Task <AceQLDataReader> ExecuteQueryAsPreparedStatementAsync()
        {
            try
            {
                AceQLCommandUtil aceQLCommandUtil = new AceQLCommandUtil(cmdText, Parameters);

                // Get the parameters and build the result set
                Dictionary <string, string> statementParameters = aceQLCommandUtil.GetPreparedStatementParameters();

                foreach (string key in statementParameters.Keys)
                {
                    Debug("key:==> " + key + " / " + statementParameters[key]);
                }

                // Replace all @parms with ? in sql command
                cmdText = aceQLCommandUtil.ReplaceParmsWithQuestionMarks();

                IFile file = await GetUniqueResultSetFileAsync().ConfigureAwait(false);

                bool isStoredProcedure   = (commandType == CommandType.StoredProcedure ? true : false);
                bool isPreparedStatement = true;

                using (Stream input = await aceQLHttpApi.ExecuteQueryAsync(cmdText, Parameters, isStoredProcedure, isPreparedStatement, statementParameters).ConfigureAwait(false))
                {
                    try
                    {
                        await CopyHttpStreamToFile(file, input).ConfigureAwait(false);
                    }
                    catch (Exception exception)
                    {
                        if (this.connection.RequestRetry && (this.executeQueryRetryCount < 1 || exception.Message.Contains("GZip")))
                        {
                            this.executeQueryRetryCount++;
                            Boolean saveGzipResultValue = this.aceQLHttpApi.GzipResult;
                            this.aceQLHttpApi.GzipResult = false;
                            AceQLDataReader dataReader = await ExecuteQueryAsPreparedStatementAsync().ConfigureAwait(false);

                            this.aceQLHttpApi.GzipResult = saveGzipResultValue;
                            return(dataReader);
                        }
                        else
                        {
                            throw;
                        }
                    }
                }

                this.executeQueryRetryCount = 0;

                StreamResultAnalyzer streamResultAnalyzer = new StreamResultAnalyzer(file, aceQLHttpApi.HttpStatusCode);
                if (!await streamResultAnalyzer.IsStatusOkAsync().ConfigureAwait(false))
                {
                    throw new AceQLException(streamResultAnalyzer.GetErrorMessage(),
                                             streamResultAnalyzer.GetErrorType(),
                                             streamResultAnalyzer.GetStackTrace(),
                                             aceQLHttpApi.HttpStatusCode);
                }

                int rowsCount = 0;

                using (Stream readStreamCout = await file.OpenAsync(FileAccess.Read).ConfigureAwait(false))
                {
                    RowCounter rowCounter = new RowCounter(readStreamCout);
                    rowsCount = rowCounter.Count();
                }

                if (isStoredProcedure)
                {
                    using (Stream readStreamOutParms = await file.OpenAsync(FileAccess.Read).ConfigureAwait(false))
                    {
                        UpdateOutParametersValues(readStreamOutParms, Parameters);
                    }
                }

                Stream readStream = await file.OpenAsync(FileAccess.Read).ConfigureAwait(false);

                AceQLDataReader aceQLDataReader = new AceQLDataReader(file, readStream, rowsCount, connection);
                return(aceQLDataReader);
            }
            catch (Exception exception)
            {
                await simpleTracer.TraceAsync(exception.ToString()).ConfigureAwait(false);

                if (exception.GetType() == typeof(AceQLException))
                {
                    throw;
                }
                else
                {
                    throw new AceQLException(exception.Message, 0, exception, aceQLHttpApi.HttpStatusCode);
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// Executes the query as prepared statement.
        /// </summary>
        /// <returns>An <see cref="AceQLDataReader"/> object.</returns>
        /// <exception cref="AceQL.Client.Api.AceQLException">If any Exception occurs.</exception>
        private async Task <AceQLDataReader> ExecuteQueryAsPreparedStatementAsync()
        {
            try
            {
                AceQLCommandUtil aceQLCommandUtil = new AceQLCommandUtil(cmdText, Parameters);

                // Get the parameters and build the result set
                Dictionary <string, string> statementParameters = aceQLCommandUtil.GetPreparedStatementParameters();

                // Replace all @parms with ? in sql command
                cmdText = aceQLCommandUtil.ReplaceParmsWithQuestionMarks();

                IFile file = await GetUniqueResultSetFileAsync().ConfigureAwait(false);

                bool isStoredProcedure   = (commandType == CommandType.StoredProcedure ? true : false);
                bool isPreparedStatement = true;
                using (Stream input = await aceQLHttpApi.ExecuteQueryAsync(cmdText, Parameters, isStoredProcedure, isPreparedStatement, statementParameters).ConfigureAwait(false))
                {
                    if (input != null)
                    {
                        if (aceQLHttpApi.GzipResult)
                        {
                            using (GZipStream decompressionStream = new GZipStream(input, CompressionMode.Decompress))
                            {
                                using (var stream = await file.OpenAsync(PCLStorage.FileAccess.ReadAndWrite).ConfigureAwait(false))
                                {
                                    decompressionStream.CopyTo(stream);
                                }
                            }
                        }
                        else
                        {
                            using (var stream = await file.OpenAsync(PCLStorage.FileAccess.ReadAndWrite).ConfigureAwait(false))
                            {
                                input.CopyTo(stream);
                            }
                        }
                    }
                }

                StreamResultAnalyzer streamResultAnalyzer = new StreamResultAnalyzer(file, aceQLHttpApi.HttpStatusCode);
                if (!await streamResultAnalyzer.IsStatusOkAsync().ConfigureAwait(false))
                {
                    throw new AceQLException(streamResultAnalyzer.GetErrorMessage(),
                                             streamResultAnalyzer.GetErrorType(),
                                             streamResultAnalyzer.GetStackTrace(),
                                             aceQLHttpApi.HttpStatusCode);
                }

                int rowsCount = 0;

                using (Stream readStreamCout = await file.OpenAsync(FileAccess.Read).ConfigureAwait(false))
                {
                    RowCounter rowCounter = new RowCounter(readStreamCout);
                    rowsCount = rowCounter.Count();
                }

                if (isStoredProcedure)
                {
                    using (Stream readStreamOutParms = await file.OpenAsync(FileAccess.Read).ConfigureAwait(false))
                    {
                        UpdateOutParametersValues(readStreamOutParms, Parameters);
                    }
                }

                Stream readStream = await file.OpenAsync(FileAccess.Read).ConfigureAwait(false);

                AceQLDataReader aceQLDataReader = new AceQLDataReader(file, readStream, rowsCount, connection);
                return(aceQLDataReader);
            }
            catch (Exception exception)
            {
                await simpleTracer.TraceAsync(exception.ToString()).ConfigureAwait(false);

                if (exception.GetType() == typeof(AceQLException))
                {
                    throw exception;
                }
                else
                {
                    throw new AceQLException(exception.Message, 0, exception, aceQLHttpApi.HttpStatusCode);
                }
            }
        }