Пример #1
0
            public IDatabaseResult ExecuteQuery(IDatabaseConnection connection, IDatabaseQuery query)
            {
                ThrowIf.Null(query, "query");
                SqlDataReader sqlDataReader = null;
                Guid          dataAccessInstrumentationId = Guid.NewGuid();

                using (ChannelDbQueryBoundaryPerfContext perfContext = new ChannelDbQueryBoundaryPerfContext())
                {
                    string methodName    = GetCallerName();
                    string queryString   = query.BuildQuery(this.GetDatabaseQueryBuilder()) ?? string.Empty;
                    string parameterList = string.Join(", ", query.Parameters.Select(p => string.Format("{0}={1}", p.Key, p.Value)));

                    // get the string between the "FROM" and next whitespace
                    Match match = FromClauseRegex.Match(queryString);

                    // when match is not found, take the first FromClauseNonMatchLengthLimit characters or whatever is available
                    string fromClause = match.Success
                        ? string.Format(
                        "[{0}].[{1}]",
                        match.Groups[SchemaGroupName].Success ? match.Groups[SchemaGroupName].Value : DefaultSchemaName,
                        match.Groups[ObjectGroupName].Success ? match.Groups[ObjectGroupName].Value : string.Empty)
                        : queryString.Substring(0, Math.Min(FromClauseNonMatchLengthLimit, queryString.Length));

                    RetailLogger.Log.CrtDataAccessExecuteQueryStarted(fromClause, dataAccessInstrumentationId);

                    DatabaseConnection databaseConnection = GetDatabaseConnection(connection);
                    SqlConnection      sqlConnection      = databaseConnection.SqlConnection;

                    using (SqlCommand command = new SqlCommand(queryString, sqlConnection))
                    {
                        command.CommandTimeout = 0; // infinite wait timeout for command execution

                        CreateInputParameters(command, query.Parameters);

                        try
                        {
                            RetailLogger.Log.CrtDataAccessSqlServerSelectStarted(methodName, dataAccessInstrumentationId, queryString, parameterList, fromClause);

                            sqlDataReader = command.ExecuteReader();

                            perfContext.CallWasSuccessful();
                            RetailLogger.Log.CrtDataAccessSqlServerSelectFinished(
                                methodName,
                                callWasSuccessful: true,
                                correlationId: dataAccessInstrumentationId,
                                sqlQuery: queryString,
                                parameterList: parameterList,
                                fromClause: fromClause);
                        }
                        catch (SqlException sqlException)
                        {
                            RetailLogger.Log.CrtDataAccessSqlServerSelectFinished(
                                methodName,
                                callWasSuccessful: false,
                                correlationId: dataAccessInstrumentationId,
                                sqlQuery: queryString,
                                parameterList: parameterList,
                                fromClause: fromClause);

                            RetailLogger.Log.CrtDataAccessExecuteQueryFinished(numberOfResults: 0, wasSuccessful: false, correlationId: dataAccessInstrumentationId);

                            SqlTypeHelper.HandleException(sqlException);
                        }
                    }
                }

                DatabaseResult databaseResult = new DatabaseResult(sqlDataReader);

                databaseResult.ConfigureMonitoringEvent(dataAccessInstrumentationId);
                return(databaseResult);
            }