示例#1
0
 public MySqlConnectionInfo(ConnectionObject connectionObject, int poolSize = 10)
 {
     Host     = connectionObject.Host;
     Port     = connectionObject.Port;
     Username = connectionObject.Username;
     Password = connectionObject.Password;
     Database = connectionObject.Database;
     Poolsize = poolSize;
 }
示例#2
0
        public MySqlErrorCode Initialize(ConnectionObject connectionObject)
        {
            _connectionInfo = new MySqlConnectionInfo(connectionObject);
            _updater        = new DatabaseUpdater <T>(this);

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

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

                var error = database.Initialize(connectionObject);
                if (error != MySqlErrorCode.None)
                {
                    // Database does not exist
                    if (error == MySqlErrorCode.UnknownDatabase && updatesEnabled && _autoSetup)
                    {
                        Log.outInfo(LogFilter.ServerLoading, "Database \"{0}\" does not exist, do you want to create it? [yes (default) / no]: ", name);

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

                        Log.outInfo(LogFilter.ServerLoading, "Creating database \"{0}\"...", name);
                        string sqlString = string.Format("CREATE DATABASE `{0}` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci", name);
                        // 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 {0} NOT opened. There were errors opening the MySQL connections. Check your SQLErrors for specific errors.", name);
                        return(false);
                    }

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

            if (updatesEnabled)
            {
                // Populate and update only if updates are enabled for this pool
                _populate.Add(() =>
                {
                    //Hack used to allow big querys
                    database.Apply("SET GLOBAL max_allowed_packet=1073741824;");
                    if (!database.GetUpdater().Populate())
                    {
                        Log.outError(LogFilter.ServerLoading, "Could not populate the {0} database, see log for details.", name);
                        return(false);
                    }
                    return(true);
                });

                _update.Add(() =>
                {
                    //Hack used to allow big querys
                    database.Apply("SET GLOBAL max_allowed_packet=1073741824;");
                    if (!database.GetUpdater().Update())
                    {
                        Log.outError(LogFilter.ServerLoading, "Could not update the {0} database, see log for details.", name);
                        return(false);
                    }
                    return(true);
                });
            }

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