Example #1
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();

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

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

                String filePath = FileUtil2.GetUniqueResultSetFile();

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

                using (Stream input = await aceQLHttpApi.ExecuteQueryAsync(cmdText, Parameters, isStoredProcedure, isPreparedStatement, statementParameters).ConfigureAwait(false))
                {
                    try
                    {
                        FileUtil2.CopyHttpStreamToFile(filePath, input, aceQLHttpApi.GzipResult);
                    }
                    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(filePath, aceQLHttpApi.HttpStatusCode);
                if (!streamResultAnalyzer.IsStatusOK())
                {
                    throw new AceQLException(streamResultAnalyzer.GetErrorMessage(),
                                             streamResultAnalyzer.GetErrorType(),
                                             streamResultAnalyzer.GetStackTrace(),
                                             aceQLHttpApi.HttpStatusCode);
                }

                int rowsCount = 0;

                using (Stream readStreamCout = File.OpenRead(filePath))
                {
                    RowCounter rowCounter = new RowCounter(readStreamCout);
                    rowsCount = rowCounter.Count();
                }

                if (isStoredProcedure)
                {
                    using (Stream readStreamOutParms = File.OpenRead(filePath))
                    {
                        UpdateOutParametersValues(readStreamOutParms, Parameters);
                    }
                }

                Stream readStream = File.OpenRead(filePath);

                AceQLDataReader aceQLDataReader = new AceQLDataReader(filePath, readStream, rowsCount, connection);
                return(aceQLDataReader);
            }
            catch (Exception exception)
            {
                simpleTracer.Trace(exception.ToString());

                if (exception.GetType() == typeof(AceQLException))
                {
                    throw;
                }
                else
                {
                    throw new AceQLException(exception.Message, 0, exception, aceQLHttpApi.HttpStatusCode);
                }
            }
        }
Example #2
0
        /// <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
            {
                string filePath = FileUtil2.GetUniqueResultSetFile();

                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
                    {
                        FileUtil2.CopyHttpStreamToFile(filePath, input, aceQLHttpApi.GzipResult);
                    }
                    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(filePath, aceQLHttpApi.HttpStatusCode);
                if (!streamResultAnalyzer.IsStatusOK())
                {
                    throw new AceQLException(streamResultAnalyzer.GetErrorMessage(),
                                             streamResultAnalyzer.GetErrorType(),
                                             streamResultAnalyzer.GetStackTrace(),
                                             aceQLHttpApi.HttpStatusCode);
                }

                int rowsCount = 0;

                using (Stream readStreamCout = File.OpenRead(filePath))
                {
                    RowCounter rowCounter = new RowCounter(readStreamCout);
                    rowsCount = rowCounter.Count();
                }

                if (isStoredProcedure)
                {
                    using (Stream readStreamOutParms = File.OpenRead(filePath))
                    {
                        UpdateOutParametersValues(readStreamOutParms, Parameters);
                    }
                }

                Stream readStream = File.OpenRead(filePath);

                AceQLDataReader aceQLDataReader = new AceQLDataReader(filePath, readStream, rowsCount, connection);
                return(aceQLDataReader);
            }
            catch (Exception exception)
            {
                simpleTracer.Trace(exception.ToString());

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