Exemple #1
0
 public static void LogCommandError(this GlimpseDbCommand command, Guid commandId, TimeSpan timer, Exception exception, string type)
 {
     command.LogCommandError(commandId, timer, exception, type, false);
 }
        protected override int Update(DataRow[] dataRows, DataTableMapping tableMapping)
        {
            GlimpseDbCommand accountCommand = null;

            if (UpdateCommand != null)
            {
                InnerDataAdapter.UpdateCommand = RetrieveBaseType(UpdateCommand);
                accountCommand = accountCommand ?? UpdateCommand as GlimpseDbCommand;
            }

            if (InsertCommand != null)
            {
                InnerDataAdapter.InsertCommand = RetrieveBaseType(InsertCommand);
                accountCommand = accountCommand ?? InsertCommand as GlimpseDbCommand;
            }

            if (DeleteCommand != null)
            {
                InnerDataAdapter.DeleteCommand = RetrieveBaseType(DeleteCommand);
                accountCommand = accountCommand ?? DeleteCommand as GlimpseDbCommand;
            }

            if (accountCommand != null && accountCommand.MessageBroker != null)
            {
                var commandId = Guid.NewGuid();
                var timer     = accountCommand.LogCommandSeed();

                IList <CommandExecutedParamater>      parameters      = null;
                IList <CommandExecutedBatchParameter> batchParameters = null;
                if (accountCommand.Parameters.Count > 0)
                {
                    if (dataRows.Length > 0)
                    {
                        batchParameters = new List <CommandExecutedBatchParameter>(dataRows.Length);

                        var templates = new Dictionary <string, CommandExecutedParamater>();
                        var paramDefs = Support.ExtractParameters(accountCommand, templates);
                        var names     = new List <string>(templates.Keys);

                        for (int i = 0; i < dataRows.Length; i++)
                        {
                            var paramsOfARow = new List <CommandExecutedParamater>(names.Count);
                            var row          = dataRows[i];
                            var deleted      = row.RowState == DataRowState.Deleted;
                            foreach (var name in names)
                            {
                                var val      = deleted ? row[name, DataRowVersion.Original] : row[name];
                                var template = templates[name];
                                paramsOfARow.Add(new CommandExecutedParamater {
                                    Name = template.Name, Value = Support.TranslateValue(val), Size = template.Size, Type = template.Type
                                });
                            }
                            batchParameters.Add(new CommandExecutedBatchParameter()
                            {
                                Index = i, Value = paramsOfARow
                            });
                        }
                    }
                    else
                    {
                        parameters = Support.ExtractParameters(accountCommand, null);
                    }
                }
                if (batchParameters != null)
                {
                    accountCommand.MessageBroker.Publish(
                        new CommandExecutedMessage(accountCommand.InnerConnection.ConnectionId, commandId, accountCommand.CommandText, batchParameters, accountCommand.InnerCommand.Transaction != null, false)
                        .AsTimedMessage(timer));
                }
                else
                {
                    accountCommand.MessageBroker.Publish(
                        new CommandExecutedMessage(accountCommand.InnerConnection.ConnectionId, commandId, accountCommand.CommandText, parameters, accountCommand.InnerCommand.Transaction != null, false)
                        .AsTimedMessage(timer));
                }

                int result = 0;
                try
                {
                    result = InnerDataAdapter.Update(dataRows);
                }
                catch (Exception exception)
                {
                    accountCommand.LogCommandError(commandId, timer, exception, "DbDataAdapter:Update");
                    throw;
                }
                accountCommand.LogCommandEnd(commandId, timer, result, "DbDataAdapter:Update");
                return(result);
            }
            return(InnerDataAdapter.Update(dataRows));
        }