Channel::Channel(const std::string& name, uint32 channel_id, uint32 Team) : m_name(name), m_announce(true), m_moderate(false), m_channelId(channel_id), m_ownerGUID(0), m_password(""), m_flags(0), m_Team(Team) { // set special flags if built-in channel ChatChannelsEntry const* ch = GetChannelEntryFor(channel_id); if (ch) // it's built-in channel { channel_id = ch->ChannelID; // built-in channel m_announce = false; // no join/leave announces m_flags |= CHANNEL_FLAG_GENERAL; // for all built-in channels if (ch->flags & CHANNEL_DBC_FLAG_TRADE) // for trade channel m_flags |= CHANNEL_FLAG_TRADE; if (ch->flags & CHANNEL_DBC_FLAG_CITY_ONLY2) // for city only channels m_flags |= CHANNEL_FLAG_CITY; if (ch->flags & CHANNEL_DBC_FLAG_LFG) // for LFG channel m_flags |= CHANNEL_FLAG_LFG; else // for all other channels m_flags |= CHANNEL_FLAG_NOT_LFG; m_IsSaved = false; } else // it's custom channel { m_flags |= CHANNEL_FLAG_CUSTOM; //load not built in channel if saved std::string _name(name); CharacterDatabase.escape_string(_name); QueryResult_AutoPtr result = CharacterDatabase.PQuery("SELECT m_announce, m_moderate, m_public, m_password, BannedList FROM channels WHERE m_name = '%s' AND m_team = '%u'", _name.c_str(), m_Team); if (result)//load { Field *fields = result->Fetch(); m_announce = fields[0].GetBool(); m_moderate = fields[1].GetBool(); m_public = fields[2].GetBool(); m_password = fields[3].GetString(); const char* db_BannedList = fields[4].GetString(); m_IsSaved = true; if (db_BannedList) { Tokens tokens = StrSplit(db_BannedList, " "); Tokens::iterator iter; for (iter = tokens.begin(); iter != tokens.end(); ++iter) { uint64 banned_guid = atol((*iter).c_str()); if (banned_guid) { sLog.outDebug("Channel(%s) loaded banned guid: %u",name.c_str(), banned_guid); banned.insert(banned_guid); } } } } else // save { // _name is already escaped at this point. CharacterDatabase.PExecute("INSERT INTO channels (m_name, m_team, m_announce, m_moderate, m_public, m_password) " "VALUES ('%s', '%u', '1', '0', '1', '')", _name.c_str(), m_Team); sLog.outDebug("New Channel(%s) saved", name.c_str()); m_IsSaved = true; } } }
bool DatabaseMysql::Initialize(const char *infoString) { if (!Database::Initialize(infoString)) return false; tranThread = NULL; MYSQL *mysqlInit; mysqlInit = mysql_init(NULL); if (!mysqlInit) { sLog.outError("Could not initialize Mysql connection"); return false; } InitDelayThread(); Tokens tokens = StrSplit(infoString, ";"); Tokens::iterator iter; std::string host, port_or_socket, user, password, database; int port; char const* unix_socket; iter = tokens.begin(); if (iter != tokens.end()) host = *iter++; if (iter != tokens.end()) port_or_socket = *iter++; if (iter != tokens.end()) user = *iter++; if (iter != tokens.end()) password = *iter++; if (iter != tokens.end()) database = *iter++; mysql_options(mysqlInit,MYSQL_SET_CHARSET_NAME,"utf8"); #ifdef WIN32 if (host==".") // named pipe use option (Windows) { unsigned int opt = MYSQL_PROTOCOL_PIPE; mysql_options(mysqlInit,MYSQL_OPT_PROTOCOL,(char const*)&opt); port = 0; unix_socket = 0; } else // generic case { port = atoi(port_or_socket.c_str()); unix_socket = 0; } #else if (host==".") // socket use option (Unix/Linux) { unsigned int opt = MYSQL_PROTOCOL_SOCKET; mysql_options(mysqlInit,MYSQL_OPT_PROTOCOL,(char const*)&opt); host = "localhost"; port = 0; unix_socket = port_or_socket.c_str(); } else // generic case { port = atoi(port_or_socket.c_str()); unix_socket = 0; } #endif mMysql = mysql_real_connect(mysqlInit, host.c_str(), user.c_str(), password.c_str(), database.c_str(), port, unix_socket, 0); if (mMysql) { sLog.outDetail("Connected to MySQL database at %s", host.c_str()); sLog.outString("MySQL client library: %s", mysql_get_client_info()); sLog.outString("MySQL server ver: %s ", mysql_get_server_info( mMysql)); if (!mysql_autocommit(mMysql, 1)) sLog.outDetail("AUTOCOMMIT SUCCESSFULLY SET TO 1"); else sLog.outDetail("AUTOCOMMIT NOT SET TO 1"); // set connection properties to UTF8 to properly handle locales for different // server configs - core sends data in UTF8, so MySQL must expect UTF8 too PExecute("SET NAMES `utf8`"); PExecute("SET CHARACTER SET `utf8`"); #if MYSQL_VERSION_ID >= 50003 my_bool my_true = (my_bool)1; if (mysql_options(mMysql, MYSQL_OPT_RECONNECT, &my_true)) sLog.outDetail("Failed to turn on MYSQL_OPT_RECONNECT."); else sLog.outDetail("Successfully turned on MYSQL_OPT_RECONNECT."); #else #warning "Your mySQL client lib version does not support reconnecting after a timeout.\nIf this causes you any trouble we advice you to upgrade your mySQL client libs to at least mySQL 5.0.13 to resolve this problem." #endif return true; } else { sLog.outError("Could not connect to MySQL database at %s: %s\n", host.c_str(),mysql_error(mysqlInit)); mysql_close(mysqlInit); return false; } }
bool MySQLConnection::Initialize(const char *infoString) { MYSQL * mysqlInit = mysql_init(NULL); if (!mysqlInit) { sLog.outLog(LOG_DEFAULT, "ERROR: Could not initialize Mysql connection"); return false; } Tokens tokens = StrSplit(infoString, ";"); Tokens::iterator iter; std::string host, port_or_socket, user, password, database; int port; char const* unix_socket; iter = tokens.begin(); if(iter != tokens.end()) host = *iter++; if(iter != tokens.end()) port_or_socket = *iter++; if(iter != tokens.end()) user = *iter++; if(iter != tokens.end()) password = *iter++; if(iter != tokens.end()) database = *iter++; mysql_options(mysqlInit,MYSQL_SET_CHARSET_NAME,"utf8"); #ifdef WIN32 if(host==".") // named pipe use option (Windows) { unsigned int opt = MYSQL_PROTOCOL_PIPE; mysql_options(mysqlInit,MYSQL_OPT_PROTOCOL,(char const*)&opt); port = 0; unix_socket = 0; } else // generic case { port = atoi(port_or_socket.c_str()); unix_socket = 0; } #else if(host==".") // socket use option (Unix/Linux) { unsigned int opt = MYSQL_PROTOCOL_SOCKET; mysql_options(mysqlInit,MYSQL_OPT_PROTOCOL,(char const*)&opt); host = "localhost"; port = 0; unix_socket = port_or_socket.c_str(); } else // generic case { port = atoi(port_or_socket.c_str()); unix_socket = 0; } #endif mMysql = mysql_real_connect(mysqlInit, host.c_str(), user.c_str(), password.c_str(), database.c_str(), port, unix_socket, 0); if (mMysql) { //sLog.outString( "Connected to MySQL database at %s", // host.c_str()); //sLog.outString( "MySQL client library: %s", mysql_get_client_info()); //sLog.outString( "MySQL server ver: %s ", mysql_get_server_info( mMysql)); /*----------SET AUTOCOMMIT ON---------*/ // It seems mysql 5.0.x have enabled this feature // by default. In crash case you can lose data!!! // So better to turn this off // --- // This is wrong since mangos use transactions, // autocommit is turned of during it. // Setting it to on makes atomic updates work // --- // LEAVE 'AUTOCOMMIT' MODE ALWAYS ENABLED!!! // W/O IT EVEN 'SELECT' QUERIES WOULD REQUIRE TO BE WRAPPED INTO 'START TRANSACTION'<>'COMMIT' CLAUSES!!! if (!mysql_autocommit(mMysql, 1)) sLog.outDetail("AUTOCOMMIT SUCCESSFULLY SET TO 1"); else sLog.outDetail("AUTOCOMMIT NOT SET TO 1"); /*-------------------------------------*/ // set connection properties to UTF8 to properly handle locales for different // server configs - core sends data in UTF8, so MySQL must expect UTF8 too Execute("SET NAMES `utf8`"); Execute("SET CHARACTER SET `utf8`"); #if MYSQL_VERSION_ID >= 50003 my_bool my_true = (my_bool)1; if (mysql_options(mMysql, MYSQL_OPT_RECONNECT, &my_true)) sLog.outDetail("Failed to turn on MYSQL_OPT_RECONNECT."); else sLog.outDetail("Successfully turned on MYSQL_OPT_RECONNECT."); #else #warning "Your mySQL client lib version does not support reconnecting after a timeout.\nIf this causes you any trouble we advice you to upgrade your mySQL client libs to at least mySQL 5.0.13 to resolve this problem." #endif return true; } else { sLog.outLog(LOG_DEFAULT, "Could not connect to MySQL database at %s\n", host.c_str()); mysql_close(mysqlInit); return false; } }
bool DatabaseMysql::Initialize(const char *infoString) { if(!Database::Initialize(infoString)) return false; tranThread = NULL; MYSQL *mysqlInit; mysqlInit = mysql_init(NULL); if (!mysqlInit) { sLog.outError( "Could not initialize Mysql connection" ); return false; } InitDelayThread(); Tokens tokens = StrSplit(infoString, ";"); Tokens::iterator iter; std::string host, port_or_socket, user, password, database; int port; char const* unix_socket; iter = tokens.begin(); if(iter != tokens.end()) host = *iter++; if(iter != tokens.end()) port_or_socket = *iter++; if(iter != tokens.end()) user = *iter++; if(iter != tokens.end()) password = *iter++; if(iter != tokens.end()) database = *iter++; mysql_options(mysqlInit,MYSQL_SET_CHARSET_NAME,"utf8"); #ifdef WIN32 if(host==".") // named pipe use option (Windows) { unsigned int opt = MYSQL_PROTOCOL_PIPE; mysql_options(mysqlInit,MYSQL_OPT_PROTOCOL,(char const*)&opt); port = 0; unix_socket = 0; } else // generic case { port = atoi(port_or_socket.c_str()); unix_socket = 0; } #else if(host==".") // socket use option (Unix/Linux) { unsigned int opt = MYSQL_PROTOCOL_SOCKET; mysql_options(mysqlInit,MYSQL_OPT_PROTOCOL,(char const*)&opt); host = "localhost"; port = 0; unix_socket = port_or_socket.c_str(); } else // generic case { port = atoi(port_or_socket.c_str()); unix_socket = 0; } #endif mMysql = mysql_real_connect(mysqlInit, host.c_str(), user.c_str(), password.c_str(), database.c_str(), port, unix_socket, 0); if (mMysql) { sLog.outDetail( "Connected to MySQL database at %s", host.c_str()); sLog.outString( "MySQL client library: %s", mysql_get_client_info()); sLog.outString( "MySQL server ver: %s ", mysql_get_server_info( mMysql)); /*----------SET AUTOCOMMIT ON---------*/ // It seems mysql 5.0.x have enabled this feature // by default. In crash case you can lose data!!! // So better to turn this off // --- // This is wrong since mangos use transactions, // autocommit is turned of during it. // Setting it to on makes atomic updates work if (!mysql_autocommit(mMysql, 1)) sLog.outDetail("AUTOCOMMIT SUCCESSFULLY SET TO 1"); else sLog.outDetail("AUTOCOMMIT NOT SET TO 1"); /*-------------------------------------*/ // set connection properties to UTF8 to properly handle locales for different // server configs - core sends data in UTF8, so MySQL must expect UTF8 too PExecute("SET NAMES `utf8`"); PExecute("SET CHARACTER SET `utf8`"); return true; } else { sLog.outError( "Could not connect to MySQL database at %s: %s\n", host.c_str(),mysql_error(mysqlInit)); mysql_close(mysqlInit); return false; } }