Exemplo n.º 1
0
        public async Task <DbStatus> Execute()
        {
            MySqlConnection connection = connector.CreateConnection();

            try
            {
                await connector.OpenConnection(connection);
                await CreateProcedure(connection);

                MySqlCommand callCommand = new MySqlCommand
                {
                    Connection  = connection,
                    CommandType = System.Data.CommandType.StoredProcedure,
                    CommandText = GetProcedureName()
                };
                await callCommand.ExecuteNonQueryAsync();

                return(DbStatus.SUCCESS);
            }
            catch (MySqlException ex)
            {
                return(errorHandler.HandleException(ex));
            }
            catch (Exception)
            {
                return(DbStatus.DATABASE_ERROR);
            }
            finally
            {
                await connector.CloseConnection(connection);
            }
        }
Exemplo n.º 2
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.º 3
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);
            }
        }
Exemplo n.º 4
0
        public async Task <object> ExecuteCommand()
        {
            MySqlCommand    command    = new MySqlCommand();
            MySqlConnection connection = connector.CreateConnection();

            try
            {
                await connector.OpenConnection(connection);

                command.Connection  = connection;
                command.CommandType = System.Data.CommandType.StoredProcedure;
                command.Parameters.AddRange(SetParameters());
                command.CommandText = GetStoredProcedureName();
                return(await Execute(command));
            }
            catch (MySqlException ex)
            {
                errorHandler.HandleException(ex);
                return(null);
            }
            catch (Exception)
            {
                return(null);
            }
            finally
            {
                await connector.CloseConnection(connection);
            }
        }
Exemplo n.º 5
0
        public async Task <IConnection> CreateConnection(CancellationToken cancellationToken)
        {
            IConnection res = await _connector.CreateConnection(cancellationToken);

            try
            {
                _initialize.Invoke(res, cancellationToken);
            }
            catch (Exception)
            {
                res.Dispose();
                throw;
            }
            return(res);
        }
        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.º 7
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.º 8
0
        // Returns fully initialized session that is NOT the same as 'invalid'
        // (supposedly that one is malfunctioning). Initializes a new session if
        // necessary.
        //
        // Throws if the object has been disposed of. Never returns null.
        // Disposes of the invalid session.
        Session GetSession(Session invalid)
        {
            Session current = TryGetSession(invalid);

            if (current != null)
            {
                return(current);
            }
            lock (_sessionInitMonitor)
            {
                // Check if we managed to grab _sessionInitMonitor before anyone else.
                current = TryGetSession(null);
                if (current != null)
                {
                    return(current);
                }
                while (true)
                {
                    if (_cancellation.IsCancellationRequested)
                    {
                        throw new ObjectDisposedException("DurableConnection");
                    }
                    try
                    {
                        current = new Session(_connector.CreateConnection(_cancellation.Token).Result, ++_sessionID);
                        current.IncRef();
                        lock (_sessionMonitor) _session = current;
                        return(current);
                    }
                    catch (Exception e)
                    {
                        if (!_cancellation.IsCancellationRequested)
                        {
                            _log.Warn(e, "Failed to connect. Will retry in 1s.");
                        }
                    }
                    // Wait for 1 second before trying to reconnect.
                    if (!_cancellation.IsCancellationRequested)
                    {
                        Thread.Sleep(1000);
                    }
                }
            }
        }
Exemplo n.º 9
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;
        }
Exemplo n.º 10
0
        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);
        }
Exemplo n.º 11
0
        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.º 12
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.º 13
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.º 14
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);
        }