Example #1
0
 public void IsSupportedCommandNullCommandTest()
 {
     Assert.False(DbCommandWrapper.IsSupportedCommand(null));
 }
Example #2
0
        private ScriptExecutionResult DoBatchExecutionImpl(IDbConnection connection, string script)
        {
            Validate.IsNotNull(nameof(connection), connection);

            lock (this)
            {
                if (state == BatchState.Cancelling)
                {
                    state = BatchState.Initial;
                    return(ScriptExecutionResult.Cancel);
                }
            }

            ScriptExecutionResult result = ScriptExecutionResult.Success;

            // SqlClient event handlers setup
            SqlInfoMessageEventHandler     messageHandler            = new SqlInfoMessageEventHandler(OnSqlInfoMessageCallback);
            StatementCompletedEventHandler statementCompletedHandler = null;

            DbConnectionWrapper connectionWrapper = new DbConnectionWrapper(connection);

            connectionWrapper.InfoMessage += messageHandler;

            IDbCommand command = connection.CreateCommand();

            command.CommandText    = script;
            command.CommandTimeout = execTimeout;

            DbCommandWrapper commandWrapper = null;

            if (isScriptExecutionTracked && DbCommandWrapper.IsSupportedCommand(command))
            {
                statementCompletedHandler          = new StatementCompletedEventHandler(OnStatementExecutionFinished);
                commandWrapper                     = new DbCommandWrapper(command);
                commandWrapper.StatementCompleted += statementCompletedHandler;
            }

            lock (this)
            {
                state        = BatchState.Executing;
                this.command = command;
                command      = null;
            }

            try
            {
                result = this.ExecuteCommand();
            }
            catch (OutOfMemoryException)
            {
                throw;
            }
            catch (SqlException sqlEx)
            {
                result = HandleSqlException(sqlEx);
            }
            catch (Exception ex)
            {
                result = ScriptExecutionResult.Failure;
                HandleExceptionMessage(ex);
            }
            finally
            {
                if (messageHandler == null)
                {
                    Logger.Write(TraceEventType.Error, "Expected handler to be declared");
                }

                if (null != connectionWrapper)
                {
                    connectionWrapper.InfoMessage -= messageHandler;
                }

                if (commandWrapper != null)
                {
                    if (statementCompletedHandler == null)
                    {
                        Logger.Write(TraceEventType.Error, "Expect handler to be declared if we have a command wrapper");
                    }
                    commandWrapper.StatementCompleted -= statementCompletedHandler;
                }

                lock (this)
                {
                    state = BatchState.Initial;
                    if (command != null)
                    {
                        command.Dispose();
                        command = null;
                    }
                }
            }

            return(result);
        }