Changeset - 343349cea6f2
[Not reviewed]
0 2 0
HanzZ - 14 years ago 2011-10-26 22:28:05
hanzz.k@gmail.com
Working MySQL reconnecting
2 files changed with 40 insertions and 35 deletions:
0 comments (0 inline, 0 general)
include/transport/mysqlbackend.h
Show inline comments
 
@@ -118,26 +118,24 @@ class MySQLBackend : public StorageBackend
 
				Statement& operator >> (std::string& t);
 
			private:
 
				MYSQL_STMT *m_stmt;
 
				MYSQL *m_conn;
 
				std::vector<MYSQL_BIND> m_params;
 
				std::vector<MYSQL_BIND> m_results;
 
				int m_resultOffset;
 
				int m_offset;
 
				int m_error;
 
				std::string m_string;
 
		};
 

	
 
		bool exec(Statement *stmt);
 

	
 
		MYSQL m_conn;
 
		Config *m_config;
 
		std::string m_prefix;
 

	
 
		// statements
 
// 		MYSQL_STMT *m_setUser;
 
		Statement *m_setUser;
 
		Statement *m_getUser;
 
		Statement *m_getUserSetting;
 
		Statement *m_setUserSetting;
 
		Statement *m_updateUserSetting;
 
		Statement *m_removeUser;
src/mysqlbackend.cpp
Show inline comments
 
@@ -20,25 +20,25 @@
 

	
 
#ifdef WITH_MYSQL
 

	
 
#include "transport/mysqlbackend.h"
 
#include <boost/bind.hpp>
 
#include "log4cxx/logger.h"
 

	
 
using namespace log4cxx;
 

	
 
#define MYSQL_DB_VERSION 2
 
#define CHECK_DB_RESPONSE(stmt) \
 
	if(stmt) { \
 
		sqlite3_exec(m_db, "ROLLBACK;", NULL, NULL, NULL); \
 
		sqlite3_EXEC(m_db, "ROLLBACK;", NULL, NULL, NULL); \
 
		return 0; \
 
	}
 

	
 
// Prepare the SQL statement
 
#define PREP_STMT(sql, str) \
 
	sql = mysql_stmt_init(&m_conn);\
 
	if (mysql_stmt_prepare(sql, std::string(str).c_str(), std::string(str).size())) {\
 
		LOG4CXX_ERROR(logger, str << " " << mysql_error(&m_conn)); \
 
		return false; \
 
	}
 

	
 
// Finalize the prepared statement
 
@@ -63,29 +63,44 @@ using namespace log4cxx;
 
							STATEMENT##_bind[STATEMENT##_id].is_null= 0;\
 
							STATEMENT##_bind[STATEMENT##_id++].length= VARIABLE.size();
 
#define RESET_GET_COUNTER(STATEMENT)	STATEMENT##_id_get = 0;
 
#define GET_INT(STATEMENT)	sqlite3_column_int(STATEMENT, STATEMENT##_id_get++)
 
#define GET_STR(STATEMENT)	(const char *) sqlite3_column_text(STATEMENT, STATEMENT##_id_get++)
 
#define EXECUTE_STATEMENT(STATEMENT, NAME) if (mysql_stmt_bind_param(STATEMENT, STATEMENT##_bind)) { \
 
		LOG4CXX_ERROR(logger, NAME << " " << mysql_error(&m_conn)); \
 
	} \
 
	if (mysql_stmt_execute(STATEMENT)) { \
 
		LOG4CXX_ERROR(logger, NAME << " " << mysql_error(&m_conn)); \
 
	}
 

	
 
#define EXEC(STMT, METHOD) \
 
	{\
 
	int ret = STMT->execute(); \
 
	if (ret == 0) \
 
		exec_ok = true; \
 
	else if (ret == 2013) { \
 
		disconnect(); \
 
		connect(); \
 
		return METHOD; \
 
	} \
 
	else \
 
		exec_ok = false; \
 
	}
 

	
 
using namespace boost;
 

	
 
namespace Transport {
 

	
 
static LoggerPtr logger = Logger::getLogger("MySQLBackend");
 
static bool exec_ok;
 

	
 
MySQLBackend::Statement::Statement(MYSQL *conn, const std::string &format, const std::string &statement) {
 
	m_resultOffset = -1;
 
	m_conn = conn;
 
	m_offset = 0;
 
	m_string = statement;
 
	m_stmt = mysql_stmt_init(conn);
 
	if (mysql_stmt_prepare(m_stmt, statement.c_str(), statement.size())) {
 
		LOG4CXX_ERROR(logger, statement << " " << mysql_error(conn));
 
		return;
 
	}
 

	
 
@@ -247,35 +262,36 @@ MySQLBackend::Statement& MySQLBackend::Statement::operator >> (std::string& t) {
 

	
 
	if (!m_results[m_resultOffset].is_null) {
 
		t = (char *) m_results[m_resultOffset].buffer;
 
	}
 

	
 
	if (++m_resultOffset == m_results.size())
 
		m_resultOffset = 0;
 
	return *this;
 
}
 

	
 
MySQLBackend::MySQLBackend(Config *config) {
 
	m_config = config;
 
	m_prefix = CONFIG_STRING(m_config, "database.prefix");
 
	mysql_init(&m_conn);
 
	my_bool my_true = 1;
 
	mysql_options(&m_conn, MYSQL_OPT_RECONNECT, &my_true);
 
	m_prefix = CONFIG_STRING(m_config, "database.prefix");
 
}
 

	
 
MySQLBackend::~MySQLBackend(){
 
	disconnect();
 
}
 

	
 
void MySQLBackend::disconnect() {
 
	LOG4CXX_INFO(logger, "Disconnecting");
 
	delete m_setUser;
 
	delete m_getUser;
 
	delete m_removeUser;
 
	delete m_removeUserBuddies;
 
	delete m_removeUserSettings;
 
	delete m_removeUserBuddiesSettings;
 
	delete m_addBuddy;
 
	delete m_updateBuddy;
 
	delete m_getBuddies;
 
	delete m_getBuddiesSettings;
 
	delete m_getUserSetting;
 
	delete m_setUserSetting;
 
@@ -382,119 +398,106 @@ bool MySQLBackend::createDatabase() {
 

	
 
	return true;
 
}
 

	
 
bool MySQLBackend::exec(const std::string &query) {
 
	if (mysql_query(&m_conn, query.c_str())) {
 
		LOG4CXX_ERROR(logger, query << " " << mysql_error(&m_conn));
 
		return false;
 
	}
 
	return true;
 
}
 

	
 
bool MySQLBackend::exec(Statement *stmt) {
 
	for (int i = 20; i > 0; i--) {
 
		int ret = stmt->execute();
 
		if (ret == 0)
 
			return true;
 
		if (ret == 2013) {
 
			disconnect();
 
			connect();
 
		}
 
		if (i != 20) {
 
			sleep(1);
 
		}
 
	}
 
	return false;
 
}
 

	
 
void MySQLBackend::setUser(const UserInfo &user) {
 
	*m_setUser << user.jid << user.uin << user.password << user.language << user.encoding << user.vip << user.password;
 
	exec(m_setUser);
 
	EXEC(m_setUser, setUser(user));
 
}
 

	
 
bool MySQLBackend::getUser(const std::string &barejid, UserInfo &user) {
 
	*m_getUser << barejid;
 
	if (!exec(m_getUser))
 
	EXEC(m_getUser, getUser(barejid, user));
 
	if (!exec_ok)
 
		return false;
 

	
 
	int ret = false;
 
	while (m_getUser->fetch() == 0) {
 
		ret = true;
 
		*m_getUser >> user.id >> user.jid >> user.uin >> user.password >> user.encoding >> user.language >> user.vip;
 
	}
 

	
 
	return ret;
 
}
 

	
 
void MySQLBackend::setUserOnline(long id, bool online) {
 
	*m_setUserOnline << online << id;
 
	exec(m_setUserOnline);
 
	EXEC(m_setUserOnline, setUserOnline(id, online));
 
}
 

	
 
long MySQLBackend::addBuddy(long userId, const BuddyInfo &buddyInfo) {
 
// 	"INSERT INTO " + m_prefix + "buddies (user_id, uin, subscription, groups, nickname, flags) VALUES (?, ?, ?, ?, ?, ?)"
 
	*m_addBuddy << userId << buddyInfo.legacyName << buddyInfo.subscription;
 
	*m_addBuddy << (buddyInfo.groups.size() == 0 ? "" : buddyInfo.groups[0]);
 
	*m_addBuddy << buddyInfo.alias << buddyInfo.flags;
 

	
 
	exec(m_addBuddy);
 
	EXEC(m_addBuddy, addBuddy(userId, buddyInfo));
 

	
 
	long id = (long) mysql_insert_id(&m_conn);
 

	
 
// 	INSERT OR REPLACE INTO " + m_prefix + "buddies_settings (user_id, buddy_id, var, type, value) VALUES (?, ?, ?, ?, ?)
 
	if (!buddyInfo.settings.find("icon_hash")->second.s.empty()) {
 
		*m_updateBuddySetting << userId << id << buddyInfo.settings.find("icon_hash")->first << (int) TYPE_STRING << buddyInfo.settings.find("icon_hash")->second.s << buddyInfo.settings.find("icon_hash")->second.s;
 
		exec(m_updateBuddySetting);
 
		EXEC(m_updateBuddySetting, addBuddy(userId, buddyInfo));
 
	}
 

	
 
	return id;
 
}
 

	
 
void MySQLBackend::updateBuddy(long userId, const BuddyInfo &buddyInfo) {
 
// 	"UPDATE " + m_prefix + "buddies SET groups=?, nickname=?, flags=?, subscription=? WHERE user_id=? AND uin=?"
 
	*m_updateBuddy << (buddyInfo.groups.size() == 0 ? "" : buddyInfo.groups[0]);
 
	*m_updateBuddy << buddyInfo.alias << buddyInfo.flags << buddyInfo.subscription;
 
	*m_updateBuddy << userId << buddyInfo.legacyName;
 

	
 
	exec(m_updateBuddy);
 
	EXEC(m_updateBuddy, updateBuddy(userId, buddyInfo));
 
}
 

	
 
bool MySQLBackend::getBuddies(long id, std::list<BuddyInfo> &roster) {
 
//	SELECT id, uin, subscription, nickname, groups, flags FROM " + m_prefix + "buddies WHERE user_id=? ORDER BY id ASC
 
	*m_getBuddies << id;
 

	
 
// 	"SELECT buddy_id, type, var, value FROM " + m_prefix + "buddies_settings WHERE user_id=? ORDER BY buddy_id ASC"
 
	*m_getBuddiesSettings << id;
 

	
 
	SettingVariableInfo var;
 
	long buddy_id = -1;
 
	std::string key;
 

	
 
	if (!exec(m_getBuddies))
 
	EXEC(m_getBuddies, getBuddies(id, roster));
 
	if (!exec_ok)
 
		return false;
 

	
 
	while (m_getBuddies->fetch() == 0) {
 
		BuddyInfo b;
 

	
 
		std::string group;
 
		*m_getBuddies >> b.id >> b.legacyName >> b.subscription >> b.alias >> group >> b.flags;
 

	
 
		if (!group.empty())
 
			b.groups.push_back(group);
 

	
 
		roster.push_back(b);
 
	}
 

	
 
	if (!exec(m_getBuddiesSettings))
 
	EXEC(m_getBuddiesSettings, getBuddies(id, roster));
 
	if (!exec_ok)
 
		return false;
 

	
 
	BOOST_FOREACH(BuddyInfo &b, roster) {
 
		if (buddy_id == b.id) {
 
// 			std::cout << "Adding buddy info setting " << key << "\n";
 
			b.settings[key] = var;
 
			buddy_id = -1;
 
		}
 

	
 
		while(buddy_id == -1 && m_getBuddiesSettings->fetch() == 0) {
 
			std::string val;
 
			*m_getBuddiesSettings >> buddy_id >> var.type >> key >> val;
 
@@ -522,60 +525,64 @@ bool MySQLBackend::getBuddies(long id, std::list<BuddyInfo> &roster) {
 
	}
 

	
 
	while(m_getBuddiesSettings->fetch() == 0) {
 
		// TODO: probably remove those settings, because there's no buddy for them.
 
		// It should not happend, but one never know...
 
	}
 
	
 
	return true;
 
}
 

	
 
bool MySQLBackend::removeUser(long id) {
 
	*m_removeUser << (int) id;
 
	if (!exec(m_removeUser))
 
	EXEC(m_removeUser, removeUser(id));
 
	if (!exec_ok)
 
		return false;
 

	
 
	*m_removeUserSettings << (int) id;
 
	if (!exec(m_removeUserSettings))
 
	EXEC(m_removeUserSettings, removeUser(id));
 
	if (!exec_ok)
 
		return false;
 

	
 
	*m_removeUserBuddies << (int) id;
 
	if (!exec(m_removeUserBuddies))
 
	EXEC(m_removeUserBuddies, removeUser(id));
 
	if (!exec_ok)
 
		return false;
 

	
 
	*m_removeUserBuddiesSettings << (int) id;
 
	if (!exec(m_removeUserBuddiesSettings))
 
	EXEC(m_removeUserBuddiesSettings, removeUser(id));
 
	if (!exec_ok)
 
		return false;
 

	
 
	return true;
 
}
 

	
 
void MySQLBackend::getUserSetting(long id, const std::string &variable, int &type, std::string &value) {
 
// 	"SELECT type, value FROM " + m_prefix + "users_settings WHERE user_id=? AND var=?"
 
	*m_getUserSetting << id << variable;
 
	exec(m_getUserSetting);
 
	EXEC(m_getUserSetting, getUserSetting(id, variable, type, value));
 
	if (m_getUserSetting->fetch() != 0) {
 
// 		"INSERT INTO " + m_prefix + "users_settings (user_id, var, type, value) VALUES (?,?,?,?)"
 
		*m_setUserSetting << id << variable << type << value;
 
		exec(m_setUserSetting);
 
		EXEC(m_setUserSetting, getUserSetting(id, variable, type, value));
 
	}
 
	else {
 
		*m_getUserSetting >> type >> value;
 
	}
 
}
 

	
 
void MySQLBackend::updateUserSetting(long id, const std::string &variable, const std::string &value) {
 
// 	"UPDATE " + m_prefix + "users_settings SET value=? WHERE user_id=? AND var=?"
 
	*m_updateUserSetting << value << id << variable;
 
	exec(m_updateUserSetting);
 
	EXEC(m_updateUserSetting, updateUserSetting(id, variable, value));
 
}
 

	
 
void MySQLBackend::beginTransaction() {
 
	exec("START TRANSACTION;");
 
}
 

	
 
void MySQLBackend::commitTransaction() {
 
	exec("COMMIT;");
 
}
 

	
 
}
 

	
0 comments (0 inline, 0 general)