예제 #1
0
        public MySqlErrorCode Initialize(MySqlConnectionInfo connectionInfo)
        {
            _connectionInfo = connectionInfo;
            _updater        = new DatabaseUpdater <T>(this);

            try
            {
                using (var connection = _connectionInfo.GetConnection())
                {
                    connection.Open();
                    Log.outInfo(LogFilter.SqlDriver, $"Connected to MySQL(ver: {connection.ServerVersion}) Database: {_connectionInfo.Database}");
                    return(MySqlErrorCode.None);
                }
            }
            catch (MySqlException ex)
            {
                return(HandleMySQLException(ex));
            }
        }
예제 #2
0
        public void AddDatabase <T>(MySqlBase <T> database, string baseDBName)
        {
            bool updatesEnabled = database.IsAutoUpdateEnabled(_updateFlags);

            _open.Add(() =>
            {
                MySqlConnectionInfo connectionObject = new MySqlConnectionInfo
                {
                    Host     = ConfigMgr.GetDefaultValue(baseDBName + "DatabaseInfo.Host", ""),
                    Port     = ConfigMgr.GetDefaultValue(baseDBName + "DatabaseInfo.Port", ""),
                    Username = ConfigMgr.GetDefaultValue(baseDBName + "DatabaseInfo.Username", ""),
                    Password = ConfigMgr.GetDefaultValue(baseDBName + "DatabaseInfo.Password", ""),
                    Database = ConfigMgr.GetDefaultValue(baseDBName + "DatabaseInfo.Database", "")
                };

                var error = database.Initialize(connectionObject);
                if (error != MySqlErrorCode.None)
                {
                    // Database does not exist
                    if (error == MySqlErrorCode.UnknownDatabase && updatesEnabled && _autoSetup)
                    {
                        Log.outInfo(LogFilter.ServerLoading, $"Database \"{connectionObject.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);
                        }

                        Log.outInfo(LogFilter.ServerLoading, $"Creating database \"{connectionObject.Database}\"...");
                        string sqlString = $"CREATE DATABASE `{connectionObject.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(connectionObject) == MySqlErrorCode.None)
                        {
                            error = MySqlErrorCode.None;
                        }
                    }

                    // If the error wasn't handled quit
                    if (error != MySqlErrorCode.None)
                    {
                        Log.outError(LogFilter.ServerLoading, $"\nDatabase {connectionObject.Database} NOT opened. There were errors opening the MySQL connections. Check your SQLErrors for specific errors.");
                        return(false);
                    }

                    Log.outInfo(LogFilter.ServerLoading, "Done.");
                }
                return(true);
            });

            if (updatesEnabled)
            {
                // Populate and update only if updates are enabled for this pool
                _populate.Add(() =>
                {
                    if (!database.GetUpdater().Populate())
                    {
                        Log.outError(LogFilter.ServerLoading, $"Could not populate the {database.GetDatabaseName()} database, see log for details.");
                        return(false);
                    }
                    return(true);
                });

                _update.Add(() =>
                {
                    if (!database.GetUpdater().Update())
                    {
                        Log.outError(LogFilter.ServerLoading, $"Could not update the {database.GetDatabaseName()} database, see log for details.");
                        return(false);
                    }
                    return(true);
                });
            }

            _prepare.Add(() =>
            {
                database.LoadPreparedStatements();
                return(true);
            });
        }