Ejemplo n.º 1
0
        private void executeDDL()
        {
            try
            {
                //eventos a serem disparados
                TaskLogStringDelegate    taskLogDelegate          = new TaskLogStringDelegate(taskLog);
                TaskLogExceptionDelegate taskLogExceptionDelegate = new TaskLogExceptionDelegate(addPkgError);
                TaskLogUpdateDelegate    taskLogUpdateDelegate    = new TaskLogUpdateDelegate(addPkgInfo);

                using (var connection = createConnection())
                {
                    var sqls = getQueriesSql();

                    connection.BeginTransaction();

                    foreach (var sql in sqls)
                    {
                        taskLog("Script file: " + sql.FileName);
                        taskLog("'============================================================================'");
                        string querySql = StringUtilIts.GetStringFromBytes(sql.DataFile);

                        try
                        {
                            StringBuilder queryLog = new StringBuilder();
                            queryLog.Append("Preparing Command: ...\n");
                            queryLog.Append("'============================================================================'\n");
                            queryLog.Append(querySql);
                            queryLog.Append("\n");
                            queryLog.Append("'============================================================================'");

                            //passa o parametro e dispara o delegate
                            taskLogDelegate.DynamicInvoke(queryLog.ToString());

                            //add comando para execução
                            connection.AddTransaction(querySql);

                            //delay para garantir a geração do log na tela
                            Thread.Sleep(100);
                        }

                        catch (Exception ex)
                        {
                            string scriptError = "Error in file: " + sql.FileName;
                            //passa o parametro e dispara o delegate
                            taskLogDelegate.DynamicInvoke(scriptError);

                            //dispara o delegate para add update info do erro
                            taskLogExceptionDelegate.DynamicInvoke(ex);

                            //delay para garantir a geração do log na tela
                            Thread.Sleep(100);

                            //para a porra toda
                            cancelation();
                        }
                    }

                    try
                    {
                        StringBuilder queryLog = new StringBuilder();

                        queryLog.Append("/****************************************************************************/\n");
                        queryLog.Append("Executing Query ...\n");
                        queryLog.Append("'****************************************************************************'\n");
                        //limpa o log builder
                        queryLog.Clear();

                        queryLog.Append("'============================================================================'\n");
                        queryLog.Append("Query executed sucessfully !\n");
                        queryLog.Append("'============================================================================'\n");

                        //delay para garantir a geração do log na tela
                        Thread.Sleep(100);

                        //passa o parametro e dispara o delegate
                        taskLogDelegate.DynamicInvoke(queryLog.ToString());

                        //efetiva transação
                        connection.CommitTransaction();

                        //se consegui chegar aqui ta tudo ok
                        taskLogUpdateDelegate.DynamicInvoke();
                    }
                    catch (Exception ex)
                    {
                        StringBuilder logScreen = new StringBuilder("Falha ao efetivar a transação.");
                        logScreen.Append("Mensagem: " + LoggerUtilIts.GetInnerException(ex));

                        //defaz todas as alterações
                        connection.RollbackTransaction();
                    }
                }
            }
            catch (OperationCanceledException oc)
            {
                StringBuilder queryLog = new StringBuilder();
                queryLog.Append("'****************************************************************************'\n");
                queryLog.Append("Failed sql statement.\n");
                queryLog.Append("'****************************************************************************'\n");
                queryLog.Append(oc.Message);

                taskLog(queryLog.ToString());
            }
        }