Пример #1
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);
            });
        }
Пример #2
0
        public MySqlErrorCode Initialize(MySqlConnectionInfo connectionInfo, int asyncThreads = 1)
        {
            _connectionInfo = connectionInfo;
            _worker         = new DatabaseWorker <T>(_queue, this, asyncThreads);

            try
            {
                using (var connection = _connectionInfo.GetConnection())
                {
                    connection.Open();
                    Loggers.Server?.Info($"Connected to MySQL(ver: {connection.ServerVersion}) Database: {_connectionInfo.Database}");
                    return(MySqlErrorCode.None);
                }
            }
            catch (MySqlException ex)
            {
                return(HandleMySQLException(ex));
            }
        }