Ejemplo n.º 1
0
        public async Task <IList <Dictionary <string, (Type, object)> > > ExecuteSelectSql(string query)
        {
            if (string.IsNullOrEmpty(query) || !IsConnected)
            {
                return(new List <Dictionary <string, (Type, object)> >());
            }

            databaseLogger.Log(query, null, TraceLevel.Info);
            using var writeLock = await DatabaseLock.WriteLock();

            MySqlConnection conn = new(worldConnectionString.ConnectionString);

            try
            {
                conn.Open();
            }
            catch (Exception e)
            {
                throw new IMySqlExecutor.CannotConnectToDatabaseException(e);
            }

            MySqlDataReader reader;

            try
            {
                MySqlCommand cmd = new(query, conn);
                reader = await cmd.ExecuteReaderAsync();
            }
            catch (MySqlConnector.MySqlException e)
            {
                await conn.CloseAsync();

                throw new IMySqlExecutor.QueryFailedDatabaseException(e.Message, e);
            }
            catch (Exception ex)
            {
                await conn.CloseAsync();

                throw new IMySqlExecutor.QueryFailedDatabaseException(ex);
            }

            List <Dictionary <string, (Type, object)> > result = new();

            while (reader.Read())
            {
                var fields = new Dictionary <string, (Type, object)>(reader.FieldCount);
                for (int i = 0; i < reader.FieldCount; ++i)
                {
                    fields.Add(reader.GetName(i), (reader.GetFieldType(i), reader.GetValue(i)));
                }

                result.Add(fields);
            }

            await reader.CloseAsync();

            await conn.CloseAsync();

            return(result);
        }
Ejemplo n.º 2
0
        public async Task ExecuteSql(string query)
        {
            using var writeLock = await DatabaseLock.WriteLock();

            MySqlConnection  conn = new(connectionString.ConnectionString);
            MySqlTransaction transaction;

            try
            {
                conn.Open();
                transaction = await conn.BeginTransactionAsync();
            }
            catch (Exception e)
            {
                throw new IMySqlExecutor.CannotConnectToDatabaseException(e);
            }

            try
            {
                MySqlCommand cmd = new(query, conn, transaction);
                await cmd.ExecuteNonQueryAsync();

                await transaction.CommitAsync();
            }
            catch (Exception ex)
            {
                await transaction.RollbackAsync();

                await conn.CloseAsync();

                throw new IMySqlExecutor.QueryFailedDatabaseException(ex);
            }
            await conn.CloseAsync();
        }
Ejemplo n.º 3
0
        public async Task ExecuteSql(string query)
        {
            if (string.IsNullOrWhiteSpace(query) || !IsConnected)
            {
                return;
            }

            databaseLogger.Log(query, null, TraceLevel.Info);

            using var writeLock = await DatabaseLock.WriteLock();

            MySqlConnection  conn = new(worldConnectionString.ConnectionString);
            MySqlTransaction transaction;

            try
            {
                conn.Open();
                transaction = await conn.BeginTransactionAsync();
            }
            catch (Exception e)
            {
                throw new IMySqlExecutor.CannotConnectToDatabaseException(e);
            }

            try
            {
                MySqlCommand cmd = new(query, conn, transaction);
                await cmd.ExecuteNonQueryAsync();

                await transaction.CommitAsync();
            }
            catch (MySqlConnector.MySqlException e)
            {
                await transaction.RollbackAsync();

                await conn.CloseAsync();

                throw new IMySqlExecutor.QueryFailedDatabaseException(e.Message, e);
            }
            catch (Exception ex)
            {
                await transaction.RollbackAsync();

                await conn.CloseAsync();

                throw new IMySqlExecutor.QueryFailedDatabaseException(ex);
            }
            await conn.CloseAsync();

            foreach (var tableName in queryEvaluator.Extract(query).Select(q => q.TableName).Distinct())
            {
                mainThread.Dispatch(() =>
                                    eventAggregator.GetEvent <DatabaseTableChanged>().Publish(tableName));
            }
        }
Ejemplo n.º 4
0
        public async Task ExecuteSql(string query)
        {
            if (string.IsNullOrEmpty(query) || !IsConnected)
            {
                return;
            }

            databaseLogger.Log(query, null, TraceLevel.Info);

            using var writeLock = await DatabaseLock.WriteLock();

            MySqlConnection  conn = new(authConnectionString.ConnectionString);
            MySqlTransaction transaction;

            try
            {
                conn.Open();
                transaction = await conn.BeginTransactionAsync();
            }
            catch (Exception e)
            {
                throw new IMySqlExecutor.CannotConnectToDatabaseException(e);
            }

            try
            {
                MySqlCommand cmd = new(query, conn, transaction);
                await cmd.ExecuteNonQueryAsync();

                await transaction.CommitAsync();
            }
            catch (MySqlConnector.MySqlException e)
            {
                await transaction.RollbackAsync();

                await conn.CloseAsync();

                throw new IMySqlExecutor.QueryFailedDatabaseException(e.Message, e);
            }
            catch (Exception ex)
            {
                await transaction.RollbackAsync();

                await conn.CloseAsync();

                throw new IMySqlExecutor.QueryFailedDatabaseException(ex);
            }
            await conn.CloseAsync();
        }
Ejemplo n.º 5
0
        public async Task <IList <Dictionary <string, object> > > ExecuteSelectSql(string query)
        {
            using var writeLock = await DatabaseLock.WriteLock();

            MySqlConnection conn = new(connectionString.ConnectionString);

            try
            {
                conn.Open();
            }
            catch (Exception e)
            {
                throw new IMySqlExecutor.CannotConnectToDatabaseException(e);
            }

            MySqlDataReader reader;

            try
            {
                MySqlCommand cmd = new(query, conn);
                reader = await cmd.ExecuteReaderAsync();
            }
            catch (Exception ex)
            {
                await conn.CloseAsync();

                throw new IMySqlExecutor.QueryFailedDatabaseException(ex);
            }

            List <Dictionary <string, object> > result = new();

            while (reader.Read())
            {
                var fields = new Dictionary <string, object>(reader.FieldCount);
                for (int i = 0; i < reader.FieldCount; ++i)
                {
                    fields.Add(reader.GetName(i), reader.GetValue(i));
                }

                result.Add(fields);
            }

            await reader.CloseAsync();

            await conn.CloseAsync();

            return(result);
        }