Exemplo n.º 1
0
        public async static Task <object> ExecuteScalarCommand(DbCommand <T> command)
        {
            IConnector     connector    = ConnectorFactory.CreateConnector();
            IErrorHandling errorHandler = ErrorHandlerFactory.CreateErrorHandler();

            object          scalarResult = null;
            MySqlConnection connection   = connector.CreateConnection();

            try
            {
                await connector.OpenConnection(connection);

                scalarResult = await CommandExecutor <T> .TryExecutingScalarRead(connection, command);

                return(scalarResult);
            }

            catch (MySqlException exception)
            {
                errorHandler.HandleException(exception);
                return(scalarResult);
            }
            finally
            {
                await connector.CloseConnection(connection);
            }
        }
Exemplo n.º 2
0
        public async static Task <List <T> > ExecuteSelectCommand(DbCommand <T> command, T entity = default(T))
        {
            IConnector     connector    = ConnectorFactory.CreateConnector();
            IErrorHandling errorHandler = ErrorHandlerFactory.CreateErrorHandler();

            List <T> clinics = new List <T>();

            MySqlConnection connection = connector.CreateConnection();

            try
            {
                await connector.OpenConnection(connection);

                var reader = await CommandExecutor <T> .TryExecutingSelectQueryDataReader(connection, command, entity);

                while (await reader.ReadAsync())
                {
                    clinics.Add(ReadOneObject(reader));
                }
                return(clinics);
            }

            catch (MySqlException exception)
            {
                errorHandler.HandleException(exception);
                return(clinics);
            }
            finally
            {
                await connector.CloseConnection(connection);
            }
        }
        public async Task <DbStatus> AddOrUpdate(LocalAccount localAccount)
        {
            List <string>   commandsToExecute = new List <string>();
            IConnector      connector         = ConnectorFactory.CreateConnector();
            MySqlConnection connection        = connector.CreateConnection();
            await connector.OpenConnection(connection);

            LocalAccount currentAccount = await GetByUniqueIdentifiers(new string[] { "Email" }, localAccount, false);

            DeleteOrUpdateRoles(currentAccount, localAccount, commandsToExecute, connection);

            commandsToExecute.Add(new InsertOrUpdateCommand <LocalAccount>().GetCommand(connection, localAccount.GetAssociatedDbTableName(), localAccount).CommandText);
            DbTransactionProcedure transactionProcedure = new UpdateDbTransactionProcedure(commandsToExecute.ToArray());
            DbStatus status = await transactionProcedure.Execute();

            return(status);
        }
Exemplo n.º 4
0
 public async Task<DbStatus> Update(Doctor doctor)
 {
     List<string> commandsToExecute = new List<string>();
     IConnector connector = ConnectorFactory.CreateConnector();
     MySqlConnection connection = connector.CreateConnection();
     await connector.OpenConnection(connection);
     Doctor currentDoctor = await GetByPrimaryKey(doctor);
     if (currentDoctor == null)
     {
         return DbStatus.NOT_FOUND;
     }
     DeleteOrUpdateClinics(currentDoctor, doctor, commandsToExecute, connection);
     DeleteOrUpdateTitles(currentDoctor, doctor, commandsToExecute, connection);
     commandsToExecute.Add(new UpdateCommand<Doctor>().GetCommand(connection, doctor.GetAssociatedDbTableName(), doctor).CommandText);
     DbTransactionProcedure transactionProcedure = new UpdateDbTransactionProcedure(commandsToExecute.ToArray());
     DbStatus status = await transactionProcedure.Execute();
     return status;
 }
Exemplo n.º 5
0
        public async Task<DbStatus> Delete(Doctor doctor)
        {
            List<string> commandsToExecute = new List<string>();
            IConnector connector = ConnectorFactory.CreateConnector();
            MySqlConnection connection = connector.CreateConnection();
            await connector.OpenConnection(connection);
            foreach (var clinic in doctor.GetClinics())
            {

                commandsToExecute.Add(new DeleteCommand<DoctorClinic>().GetCommand(connection, clinic.GetAssociatedDbTableName(), clinic).CommandText);
            }
            foreach (var title in doctor.GetMedicalTitles())
            {
                commandsToExecute.Add(new DeleteCommand<DoctorMedicalTitle>().GetCommand(connection, title.GetAssociatedDbTableName(), title).CommandText);
            }
            commandsToExecute.Add(new DeleteCommand<Doctor>().GetCommand(connection, doctor.GetAssociatedDbTableName(), doctor).CommandText);
            DbTransactionProcedure transactionProcedure = new DeleteDbTransactionProcedure(commandsToExecute.ToArray());
            DbStatus status = await transactionProcedure.Execute();
            return status;
        }
        public async Task <DbStatus> Update(LocalAccount localAccount)
        {
            List <string>   commandsToExecute = new List <string>();
            IConnector      connector         = ConnectorFactory.CreateConnector();
            MySqlConnection connection        = connector.CreateConnection();
            await connector.OpenConnection(connection);

            LocalAccount currentAccount = await GetByPrimaryKey(localAccount);

            if (currentAccount == null)
            {
                return(DbStatus.NOT_FOUND);
            }
            DeleteOrUpdateRoles(currentAccount, localAccount, commandsToExecute, connection);

            commandsToExecute.Add(new UpdateCommand <LocalAccount>().GetCommand(connection, localAccount.GetAssociatedDbTableName(), localAccount).CommandText);
            DbTransactionProcedure transactionProcedure = new UpdateDbTransactionProcedure(commandsToExecute.ToArray());
            DbStatus status = await transactionProcedure.Execute();

            return(status);
        }
        public async Task <DbStatus> Delete(LocalAccount localAccount)
        {
            List <string>   commandsToExecute = new List <string>();
            IConnector      connector         = ConnectorFactory.CreateConnector();
            MySqlConnection connection        = connector.CreateConnection();
            await connector.OpenConnection(connection);

            foreach (var role in localAccount.GetRoles())
            {
                LocalAccountRole lr = new LocalAccountRole()
                {
                    IdLocalAccount = localAccount.IdLocalAccount,
                    IdRole         = role.IdRole
                };
                commandsToExecute.Add(new DeleteCommand <LocalAccountRole>().GetCommand(connection, lr.GetAssociatedDbTableName(), lr).CommandText);
            }
            commandsToExecute.Add(new DeleteCommand <LocalAccount>().GetCommand(connection, localAccount.GetAssociatedDbTableName(), localAccount).CommandText);
            DbTransactionProcedure transactionProcedure = new DeleteDbTransactionProcedure(commandsToExecute.ToArray());
            DbStatus status = await transactionProcedure.Execute();

            return(status);
        }
Exemplo n.º 8
0
        public async static Task <DbStatus> ExecuteCRUDCommand(DbCommand <T> command, T entity)
        {
            IConnector     connector    = ConnectorFactory.CreateConnector();
            IErrorHandling errorHandler = ErrorHandlerFactory.CreateErrorHandler();

            MySqlConnection connection = connector.CreateConnection();

            try
            {
                await connector.OpenConnection(connection);

                return(await CommandExecutor <T> .TryExecutingCRUDQuery(connection, command, entity));
            }
            catch (MySqlException exception)
            {
                return(errorHandler.HandleException(exception));
            }
            finally
            {
                await connector.CloseConnection(connection);
            }
        }
Exemplo n.º 9
0
        public async Task<DbStatus> Add(Doctor doctor)
        {
            List<string> commandsToExecute = new List<string>();
            IConnector connector = ConnectorFactory.CreateConnector();
            MySqlConnection connection = connector.CreateConnection();
            await connector.OpenConnection(connection);
            Doctor currentDoctor = await GetByUniqueIdentifiers(new string[] { "Name", "LastName", "IdLocalAccount" }, doctor);

            if (currentDoctor != null && currentDoctor.Deleted == 0)
            {
                return DbStatus.EXISTS;
            }
            else if (currentDoctor != null && currentDoctor.Deleted == 1)
            {
                doctor.IdDoctor = currentDoctor.IdDoctor;
                commandsToExecute.Add(new InsertOrUpdateCommand<Doctor>().GetCommand(connection, doctor.GetAssociatedDbTableName(), doctor).CommandText);
                DeleteOrUpdateClinics(currentDoctor, doctor, commandsToExecute, connection);
                DeleteOrUpdateTitles(currentDoctor, doctor, commandsToExecute, connection);

            }
            else
            {
                commandsToExecute.Add(new InsertCommand<Doctor>().GetCommand(connection, doctor.GetAssociatedDbTableName(), doctor).CommandText);
                foreach (var clinic in doctor.GetClinics())
                {
                    commandsToExecute.Add(new InsertIntoConnectionTableCommand<DoctorClinic>("IdDoctor").GetCommand(connection, clinic.GetAssociatedDbTableName(), clinic).CommandText);

                }
                foreach (var title in doctor.GetMedicalTitles())
                {
                    commandsToExecute.Add(new InsertIntoConnectionTableCommand<DoctorMedicalTitle>("IdDoctor").GetCommand(connection, title.GetAssociatedDbTableName(), title).CommandText);
                }
            }
            DbTransactionProcedure transactionProcedure = new InsertDbTransactionProcedure(commandsToExecute.ToArray());
            DbStatus status = await transactionProcedure.Execute();
            return status;

        }
Exemplo n.º 10
0
        private async void btnTestarConexao_Click(object sender, RoutedEventArgs e)
        {
            AbstractConnector connector = ConnectorFactory.CreateConnector(_ConfiguracoesSistema.TipoDeBase, _ConfiguracoesSistema);

            MostrarAguarde(progressTestarConexao);
            btnTestarConexao.Visibility = Visibility.Collapsed;
            (bool Success, string Message)resposta;

            try
            {
                resposta = await connector.TryConnection();
            }
            finally
            {
                EsconderAguarde(progressTestarConexao);
                btnTestarConexao.Visibility = Visibility.Visible;
            }

            if (!resposta.Success)
            {
                System.Windows.MessageBox.Show(resposta.Message, "Ocorreu um erro", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Exemplo n.º 11
0
        public async Task <DbStatus> Add(LocalAccount localAccount)
        {
            List <string>   commandsToExecute = new List <string>();
            IConnector      connector         = ConnectorFactory.CreateConnector();
            MySqlConnection connection        = connector.CreateConnection();
            await connector.OpenConnection(connection);

            LocalAccount currentAccount = await GetByUniqueIdentifiers(new string[] { "Email" }, localAccount);

            if (currentAccount != null && currentAccount.Deleted == 0)
            {
                return(DbStatus.EXISTS);
            }
            else if (currentAccount != null && currentAccount.Deleted == 1)
            {
                DeleteOrUpdateRoles(currentAccount, localAccount, commandsToExecute, connection);

                commandsToExecute.Add(new InsertOrUpdateCommand <LocalAccount>().GetCommand(connection, localAccount.GetAssociatedDbTableName(), localAccount).CommandText);
            }
            else
            {
                commandsToExecute.Add(new InsertCommand <LocalAccount>().GetCommand(connection, localAccount.GetAssociatedDbTableName(), localAccount).CommandText);
                foreach (var role in localAccount.GetRoles())
                {
                    LocalAccountRole lr = new LocalAccountRole()
                    {
                        IdRole = role.IdRole
                    };
                    commandsToExecute.Add(new InsertIntoConnectionTableCommand <LocalAccountRole>("IdLocalAccount").GetCommand(connection, lr.GetAssociatedDbTableName(), lr).CommandText);
                }
            }
            DbTransactionProcedure transactionProcedure = new UpdateDbTransactionProcedure(commandsToExecute.ToArray());
            DbStatus status = await transactionProcedure.Execute();

            return(status);
        }
Exemplo n.º 12
0
        public static void Process(string coonnectionString, Filter filter, string logFile, CancellationToken ct)
        {
            try
            {
                ConnectorBase Connector = ConnectorFactory.CreateConnector(coonnectionString);
                Connector.Connect();

                Package package;

                if (logFile != null && logFile != string.Empty)
                {
                    logFileFileStream   = new FileStream(logFile, FileMode.OpenOrCreate);
                    logFileStreamWriter = new StreamWriter(logFileFileStream);
                }
                bool first         = true;
                int  packagesCount = 0;
                var  sw            = new Stopwatch();

                while (true)
                {
                    Thread.Sleep(1);
                    while (Connector.Receive(out package))
                    {
                        packagesCount++;
                        if (first)
                        {
                            sw.Start();
                            first = false;
                        }
                        try
                        {
                            bool processMessage = filter.applyFilter(package);
                            if (processMessage)
                            {
                                package.PrintToConsole(packagesCount, ConsoleColor.Green);

                                if (logFileStreamWriter != null)
                                {
                                    logFileStreamWriter.WriteLine("{0}", package.ToString());
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine(string.Format("SendMessages Exception: {0}", ex.Message));
                            Console.ForegroundColor = ConsoleColor.Green;
                            return;
                        }
                        if (packagesCount == 1000)
                        {
                            sw.Stop();
                            Console.WriteLine("{0} Packages in {1} Milliseconds ~ {2} packages per millisecond and {3} packages per second.", packagesCount, sw.ElapsedMilliseconds, packagesCount / sw.ElapsedMilliseconds, (packagesCount / sw.ElapsedMilliseconds) * 1000);
                        }
                        if (ct.IsCancellationRequested)
                        {
                            break;
                        }
                    }
                    if (ct.IsCancellationRequested)
                    {
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(e.Message);
                Console.ForegroundColor = ConsoleColor.Green;
            }

            return;
        }
Exemplo n.º 13
0
        static void Main(string[] args)
        {
            ConnectorTcp Connector = (ConnectorTcp)ConnectorFactory.CreateConnector("127.0.0.1");

            Connector.Connect();
            Thread.Sleep(1000);
            var       r      = new Random();
            int       count2 = 0;
            int       z      = 0;
            TestClass t      = new TestClass()
            {
                Id = 1, Name = "Test"
            };
            string testData = XmlSerializationHelper.Serialize(t);

            Package testMessage1 = new Package(1, 2, 1, Method.GET, testData);
            Package testMessage2 = new Package(DateTime.Now.AddMilliseconds(z).Ticks, 1, 2, 2, Method.GET, testData);
            Package testMessage3 = new Package(1, 2, 3, Method.GET, testData);
            Package testMessage4 = new Package(1, 2, 4, Method.GET, testData);
            Package testMessage5 = new Package(1, 2, 5, Method.GET, testData);


            bool due = true;
            {
                if (due)
                {
                    Package sendMessage = new Package();
                    for (int count = 0; count < 200000; count++)
                    {
                        count2++;
                        if (count2++ > 9)
                        {
                            count2 = 1;
                        }
                        if (count2 == 7)
                        {
                            z += 70;
                        }

                        if (count2 == 7)
                        {
                            z += 70;
                        }
                        testMessage5.Time = DateTime.Now.AddMilliseconds(z).Ticks;
                        testMessage1.Time = DateTime.Now.Ticks;
                        testMessage2.Time = DateTime.Now.Ticks;
                        testMessage3.Time = DateTime.Now.Ticks;
                        testMessage4.Time = DateTime.Now.Ticks;

                        Connector.Send(testMessage1);
                        Connector.Send(testMessage2);
                        Connector.Send(testMessage3);
                        Connector.Send(testMessage4);
                        Connector.Send(testMessage5);
                        //Thread.Sleep(interval);
                    }
                    Connector.Close();
                }
                due = false;
            }
        }