Exemple #1
0
        private async void btnCriar_Click(object sender, EventArgs e)
        {
            DesabilitarCampos(true);
            try
            {
                if (Validar())
                {
                    this.UseWaitCursor = true;
                    SetTextLblProgresso("Processo iniciado");
                    PlanoExecucaoResult result = await CriarPlanoExecucao();

                    this.UseWaitCursor = false;
                    SetTextLblProgresso("Finalizado");
                    string mensagem = $"Finalizado \n Total Processado: {result.TotalProcessado} \n Total Sucesso: {result.TotalProcessadoSucesso} \n Total Falha: {result.TotalProcessadoErro}";
                    MessageBox.Show(mensagem);
                    btnResultado.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"OOPS! Algo deu errado");
                InserirLog($"Detalhe: {ex}");
                btnResultado.Enabled = true;
            }
            finally
            {
                DesabilitarCampos(false);
            }
        }
Exemple #2
0
        private async Task <PlanoExecucaoResult> CriarPlanoExecucao()
        {
            PlanoExecucaoResult result = new PlanoExecucaoResult();

            _log.Clear();
            try
            {
                DirectoryInfo diretorioOrigem = new DirectoryInfo(txtDiretorioOrigem.Text);

                var arquivos = diretorioOrigem.EnumerateFiles("*.sql", SearchOption.TopDirectoryOnly);

                if (chkIntegrarGit.Checked)
                {
                    var procedures = ObterArquivos(txtDiretorioOrigem.Text);
                    arquivos = arquivos.Where(item => procedures.Any(procedure => procedure.Equals(item.Name))).ToList();
                }

                int contador = 1;
                int total    = arquivos.Count();
                SetMaximumProgresso(total);
                string diretorio = string.Empty;

                foreach (FileInfo arquivo in arquivos)
                {
                    try
                    {
                        string nomeArquivo = Path.GetFileNameWithoutExtension(arquivo.FullName);
                        diretorio = Path.Combine(txtDiretorioDestino.Text, nomeArquivo);
                        string texto = $"Item {contador} de {total}: {nomeArquivo}";

                        SetTextLblProgresso(texto);
                        SetValueProgresso(contador);

                        bool existeDiretorio = Directory.Exists(diretorio);
                        if (!existeDiretorio || chkIntegrarGit.Checked || !File.Exists(Path.Combine(diretorio, $"{nomeArquivo}.sqlplan")))
                        {
                            result.TotalProcessado += 1;
                            if (!existeDiretorio)
                            {
                                Directory.CreateDirectory(diretorio);
                                InserirLog("======================================================================", false);
                                InserirLog($"Procedure: {nomeArquivo}", false);
                                InserirLog($"Novo diretório: {diretorio}");
                            }
                            else
                            {
                                InserirLog($"Diretório existente: {diretorio}");
                            }

                            string nomeArquivoCompleto = Path.Combine(diretorio, $"{nomeArquivo}.EXEC.sql");
                            string arquivoExecucao     = string.Empty;

                            if (File.Exists(nomeArquivoCompleto))
                            {
                                arquivoExecucao = File.ReadAllText(nomeArquivoCompleto);
                                InserirLog($"Obtido arquivo EXEC existente: {nomeArquivoCompleto}");
                            }
                            else
                            {
                                arquivoExecucao = ObterConteudoArquivoExecucao(nomeArquivo);
                                GravarArquivo(nomeArquivoCompleto, arquivoExecucao);
                                InserirLog($"Criado arquivo EXEC: {nomeArquivoCompleto}");
                            }

                            InserirLog("Inicio obter statisticas");
                            //obter estatisticas
                            EstatisticaSql estatisticaSql = new Estatistica(ObterConexao()).ObterEstatistaSql(TratarExecSql(arquivoExecucao));
                            InserirLog("Fim obter statisticas");

                            //cria arquivo IO
                            nomeArquivoCompleto = Path.Combine(diretorio, $"{nomeArquivo}.IO.txt");
                            GravarArquivo(nomeArquivoCompleto, estatisticaSql.EstatisticaIo);
                            InserirLog($"Criado arquivo IO: {nomeArquivoCompleto}");

                            //cria arquivo SQLPLAN
                            nomeArquivoCompleto = Path.Combine(diretorio, $"{nomeArquivo}.sqlplan");
                            GravarArquivo(nomeArquivoCompleto, estatisticaSql.EstatisticaXml);
                            InserirLog($"Criado arquivo SQLPLAN: {nomeArquivoCompleto}");

                            InserirLog("======================================================================", false);
                            result.TotalProcessadoSucesso += 1;
                        }
                    }
                    catch (Exception ex)
                    {
                        result.TotalProcessadoErro += 1;
                        InserirLog($"Ocorreu um erro: {ex}");

                        //if (!string.IsNullOrWhiteSpace(diretorio))
                        //{
                        //    InserirLog($"Exclusão do diretório [{diretorio}]");
                        //    Directory.Delete(diretorio, true);
                        //    InserirLog($"Diretório [{diretorio}] excluido com sucesso ");
                        //}
                        InserirLog("======================================================================", false);
                    }

                    contador += 1;
                }
            }
            catch (Exception e)
            {
                InserirLog($"OOPS! Algo deu errado durante a execução. Mensagem: {e.Message}");
                InserirLog($"Detalhe: {e}");
            }
            return(await Task.Run(() => result));
        }