Ejemplo n.º 1
0
        public void AddDatabase <T, Statements>(DatabaseWorkerPool <T, Statements> pool, string name)
            where T : MySqlConnection <Statements>
            where Statements : unmanaged, Enum
        {
            bool updatesEnabledForThis = DBUpdater <T, Statements> .IsEnabled(_updateFlags);

            _open.Enqueue(() =>
            {
                var dbString = sConfigMgr.GetStringDefault(name + "DatabaseInfo", "");
                if (dbString.Length == 0)
                {
                    FEL_LOG_ERROR(_logger, "Database {0} not specified in configuration file!", name);
                    return(false);
                }

                var asyncThreads = (byte)sConfigMgr.GetIntDefault(name + "Database.WorkerThreads", 1);
                if (asyncThreads < 1 || asyncThreads > 32)
                {
                    FEL_LOG_ERROR(_logger, "{0} database: invalid number of worker threads specified. Please pick a value between 1 and 32.", name);
                    return(false);
                }

                var synchThreads = (byte)sConfigMgr.GetIntDefault(name + "Database.SynchThreads", 1);

                pool.SetConnectionInfo(dbString, asyncThreads, synchThreads);

                var error = pool.Open();
                if (error != 0)
                {
                    // Database does not exist
                    if (error == (int)ER_BAD_DB_ERROR && updatesEnabledForThis && _autoSetup)
                    {
                        // Try to create the database and connect again if auto setup is enabled
                        if (DBUpdater <T, Statements> .Create(pool) && (pool.Open() == 0))
                        {
                            error = 0;
                        }
                    }

                    // If the error wasn't handled quit
                    if (error != 0)
                    {
                        FEL_LOG_ERROR("sql.driver", "Database {0} NOT opened. There were errors opening the MySQL connections. Check your SQLDriverLogFile ", name);
                        return(false);
                    }
                }
                // Add the close operation
                _close.Push(() =>
                {
                    pool.Close();
                });
                return(true);
            });

            if (updatesEnabledForThis)
            {
                _populate.Enqueue(() =>
                {
                    if (!DBUpdater <T, Statements> .Populate(pool))
                    {
                        FEL_LOG_ERROR(_logger, "Could not populate the {0} database, see log for details.", name);
                        return(false);
                    }
                    return(true);
                });

                _update.Enqueue(() =>
                {
                    if (!DBUpdater <T, Statements> .Update(pool))
                    {
                        FEL_LOG_ERROR(_logger, "Could not update the {0} database, see log for details.", name);
                        return(false);
                    }
                    return(true);
                });
            }

            _prepare.Enqueue(() =>
            {
                if (!pool.PrepareStatements())
                {
                    FEL_LOG_ERROR(_logger, "Could not prepare statements of the {0} database, see log for details.", name);
                    return(false);
                }
                return(true);
            });
        }