/// <summary>
        /// Handles the specified command.
        /// </summary>
        /// <param name="command">The command.</param>
        public void Handle(SetJobLastKnownEventCommand <IDbConnection, IDbTransaction> command)
        {
            using (var commandSql = command.Connection.CreateCommand())
            {
                commandSql.Transaction = commandSql.Transaction;
                commandSql.CommandText = _commandCache.GetCommand(CommandStringTypes.SetJobLastKnownEvent);

                var param = commandSql.CreateParameter();
                param.ParameterName = "@JobName";
                param.DbType        = DbType.AnsiString;
                param.Value         = command.JobName;
                commandSql.Parameters.Add(param);

                param = commandSql.CreateParameter();
                param.ParameterName = "@JobEventTime";
                param.DbType        = DbType.AnsiString;
                param.Value         = command.JobEventTime.ToString(CultureInfo.InvariantCulture);
                commandSql.Parameters.Add(param);

                param = commandSql.CreateParameter();
                param.ParameterName = "@JobScheduledTime";
                param.DbType        = DbType.AnsiString;
                param.Value         = command.JobScheduledTime.ToString(CultureInfo.InvariantCulture);
                commandSql.Parameters.Add(param);

                commandSql.ExecuteNonQuery();
            }
        }
        internal static IDbCommand GetMainCommand(SendMessageCommand commandSend,
                                                  IDbConnection connection,
                                                  IDbCommandStringCache commandCache,
                                                  IHeaders headers,
                                                  ICompositeSerialization serializer)
        {
            var command = connection.CreateCommand();

            command.CommandText = commandCache.GetCommand(CommandStringTypes.InsertMessageBody);
            var serialization =
                serializer.Serializer.MessageToBytes(new MessageBody {
                Body = commandSend.MessageToSend.Body
            }, commandSend.MessageToSend.Headers);

            var param = command.CreateParameter();

            param.ParameterName = "@Body";
            param.DbType        = DbType.Binary;
            param.Value         = serialization.Output;
            command.Parameters.Add(param);

            commandSend.MessageToSend.SetHeader(headers.StandardHeaders.MessageInterceptorGraph,
                                                serialization.Graph);

            param = command.CreateParameter();
            param.ParameterName = "@Headers";
            param.DbType        = DbType.Binary;
            param.Value         = serializer.InternalSerializer.ConvertToBytes(commandSend.MessageToSend.Headers);
            command.Parameters.Add(param);

            return(command);
        }
        public void Handle(RollbackMessageCommand <long> rollBackCommand)
        {
            if (!_databaseExists.Exists(_connectionInformation.ConnectionString))
            {
                return;
            }

            SetupSql();
            using (var connection = _dbFactory.CreateConnection(_connectionInformation.ConnectionString, false))
            {
                connection.Open();
                using (var trans = _dbFactory.CreateTransaction(connection).BeginTransaction())
                {
                    using (var command = connection.CreateCommand())
                    {
                        command.Transaction = trans;

                        var param = command.CreateParameter();
                        param.ParameterName = "@QueueID";
                        param.DbType        = DbType.Int64;
                        param.Value         = rollBackCommand.QueueId;
                        command.Parameters.Add(param);

                        if (_options.Value.EnableDelayedProcessing && rollBackCommand.IncreaseQueueDelay.HasValue)
                        {
                            if (rollBackCommand.LastHeartBeat.HasValue)
                            {
                                command.CommandText = GetRollbackSql(false, true);

                                param = command.CreateParameter();
                                param.ParameterName = "@HeartBeat";
                                param.DbType        = DbType.DateTime2;
                                param.Value         = rollBackCommand.LastHeartBeat.Value.Ticks;
                                command.Parameters.Add(param);
                            }
                            else
                            {
                                command.CommandText = GetRollbackSql(true, false);
                            }

                            var dtUtcDate          = _getUtcDateQuery.Create().GetCurrentUtcDate();
                            var dtUtcDateIncreased = dtUtcDate.Add(rollBackCommand.IncreaseQueueDelay.Value);

                            param = command.CreateParameter();
                            param.ParameterName = "@QueueProcessTime";
                            param.DbType        = DbType.DateTime2;
                            param.Value         = dtUtcDateIncreased.Ticks;
                            command.Parameters.Add(param);
                        }
                        else
                        {
                            if (rollBackCommand.LastHeartBeat.HasValue)
                            {
                                command.CommandText = GetRollbackSql(false, true);
                                param = command.CreateParameter();
                                param.ParameterName = "@HeartBeat";
                                param.DbType        = DbType.DateTime2;
                                param.Value         = rollBackCommand.LastHeartBeat.Value.Ticks;
                                command.Parameters.Add(param);
                            }
                            else
                            {
                                command.CommandText = GetRollbackSql(false, false);
                            }
                        }
                        command.ExecuteNonQuery();
                    }

                    if (_options.Value.EnableStatusTable)
                    {
                        using (var command = connection.CreateCommand())
                        {
                            command.Transaction = trans;
                            var param = command.CreateParameter();
                            param.ParameterName = "@QueueID";
                            param.DbType        = DbType.Int64;
                            param.Value         = rollBackCommand.QueueId;
                            command.Parameters.Add(param);

                            param = command.CreateParameter();
                            param.ParameterName = "@Status";
                            param.DbType        = DbType.Int32;
                            param.Value         = Convert.ToInt16(QueueStatuses.Waiting);
                            command.Parameters.Add(param);

                            command.CommandText =
                                _commandCache.GetCommand(CommandStringTypes.UpdateStatusRecord);
                            command.ExecuteNonQuery();
                        }
                    }
                    trans.Commit();
                }
            }
        }