Example #1
0
        public virtual bool Execute <T>(MySqlBase <T> mySqlBase)
        {
            MySqlErrorCode errorCode = TryExecute(mySqlBase);

            if (errorCode == MySqlErrorCode.None)
            {
                return(true);
            }

            if (errorCode == MySqlErrorCode.LockDeadlock)
            {
                // Make sure only 1 async thread retries a transaction so they don't keep dead-locking each other
                lock (_deadlockLock)
                {
                    byte loopBreaker = 5;  // Handle MySQL Errno 1213 without extending deadlock to the core itself
                    for (byte i = 0; i < loopBreaker; ++i)
                    {
                        if (TryExecute(mySqlBase) == MySqlErrorCode.None)
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Example #2
0
        public bool Execute <T>(MySqlBase <T> mySqlBase)
        {
            if (m_holder == null)
            {
                return(false);
            }

            // execute all queries in the holder and pass the results
            foreach (var pair in m_holder.m_queries)
            {
                m_holder.SetResult(pair.Key, mySqlBase.Query(pair.Value));
            }

            return(m_result.TrySetResult(m_holder));
        }
Example #3
0
        public DatabaseWorker(ProducerConsumerQueue <ISqlOperation> newQueue, MySqlBase <T> mySqlBase, int threads = 1)
        {
            _queue            = newQueue;
            _mySqlBase        = mySqlBase;
            _cancelationToken = false;

            _workerThreads = new Thread[threads <= 0 ? 1 : threads];

            for (int i = 0; i < threads; i++)
            {
                _workerThreads[i] = new Thread(WorkerThread)
                {
                    Name = $"DB {mySqlBase.ConnectionInfo.Database} Worker Thread#{i}"
                };
                _workerThreads[i].Start();
            }
        }
Example #4
0
        public bool Execute <T>(MySqlBase <T> mySqlBase)
        {
            if (_needsResult)
            {
                SQLResult result = mySqlBase.Query(m_stmt);
                if (result == null)
                {
                    m_result.SetResult(new SQLResult());
                    return(false);
                }

                m_result.SetResult(result);
                return(true);
            }

            return(mySqlBase.DirectExecute(m_stmt));
        }
Example #5
0
        public void AddDatabase <T>(MySqlBase <T> database, MySqlConnectionInfo connectionInfo, int asyncThreads = 1)
        {
            _open.Add(() =>
            {
                var error = database.Initialize(connectionInfo, asyncThreads);
                if (error != MySqlErrorCode.None)
                {
                    // Database does not exist
                    if (error == MySqlErrorCode.UnknownDatabase && _autoSetup)
                    {
                        Loggers.Server?.Info($"Database \"{connectionInfo.Database}\" does not exist, do you want to create it? [yes (default) / no]: ");

                        string answer = Console.ReadLine();
                        if (string.IsNullOrEmpty(answer) || answer[0] != 'y')
                        {
                            return(false);
                        }

                        Loggers.Server?.Info($"Creating database \"{connectionInfo.Database}\"...");
                        string sqlString = $"CREATE DATABASE `{connectionInfo.Database}` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci";
                        // Try to create the database and connect again if auto setup is enabled
                        if (database.Apply(sqlString) && database.Initialize(connectionInfo, asyncThreads) == MySqlErrorCode.None)
                        {
                            error = MySqlErrorCode.None;
                        }
                    }

                    // If the error wasn't handled quit
                    if (error != MySqlErrorCode.None)
                    {
                        Loggers.Server?.Error($"\nDatabase {connectionInfo.Database} NOT opened. There were errors opening the MySQL connections. Check your SQLErrors for specific errors.");
                        return(false);
                    }

                    Loggers.Server?.Info("Done.");
                }
                return(true);
            });

            _prepare.Add(() =>
            {
                database.LoadPreparedStatements();
                return(true);
            });
        }
Example #6
0
 public MySqlErrorCode TryExecute <T>(MySqlBase <T> mySqlBase)
 {
     return(mySqlBase.DirectCommitTransaction(m_trans));
 }