Changeset - 82b712663310
[Not reviewed]
0 2 0
HanzZ - 14 years ago 2011-09-08 10:13:42
hanzz.k@gmail.com
mysql support finished :)
2 files changed with 25 insertions and 14 deletions:
0 comments (0 inline, 0 general)
include/transport/mysqlbackend.h
Show inline comments
 
@@ -131,15 +131,15 @@ class MySQLBackend : public StorageBackend
 
		std::string m_prefix;
 

	
 
		// statements
 
// 		MYSQL_STMT *m_setUser;
 
		Statement *m_setUser;
 
		Statement *m_getUser;
 
		MYSQL_STMT *m_getUserSetting;
 
		MYSQL_STMT *m_setUserSetting;
 
		MYSQL_STMT *m_updateUserSetting;
 
		Statement *m_getUserSetting;
 
		Statement *m_setUserSetting;
 
		Statement *m_updateUserSetting;
 
		Statement *m_removeUser;
 
		Statement *m_removeUserBuddies;
 
		Statement *m_removeUserSettings;
 
		Statement *m_removeUserBuddiesSettings;
 
		Statement *m_addBuddy;
 
		Statement *m_updateBuddy;
src/mysqlbackend.cpp
Show inline comments
 
@@ -258,26 +258,25 @@ MySQLBackend::MySQLBackend(Config *config) {
 
	m_config = config;
 
	mysql_init(&m_conn);
 
	m_prefix = CONFIG_STRING(m_config, "database.prefix");
 
}
 

	
 
MySQLBackend::~MySQLBackend(){
 
// 	FINALIZE_STMT(m_setUser);
 
	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;
 
	FINALIZE_STMT(m_getUserSetting);
 
	FINALIZE_STMT(m_setUserSetting);
 
	FINALIZE_STMT(m_updateUserSetting);
 
	delete m_getUserSetting;
 
	delete m_setUserSetting;
 
	delete m_updateUserSetting;
 
	delete m_updateBuddySetting;
 
	mysql_close(&m_conn);
 
}
 

	
 
bool MySQLBackend::connect() {
 
	LOG4CXX_INFO(logger, "Connecting MySQL server " << CONFIG_STRING(m_config, "database.server") << ", user " <<
 
@@ -307,15 +306,15 @@ bool MySQLBackend::connect() {
 
	m_addBuddy = new Statement(&m_conn, "issssi", "INSERT INTO " + m_prefix + "buddies (user_id, uin, subscription, groups, nickname, flags) VALUES (?, ?, ?, ?, ?, ?)");
 
	m_updateBuddy = new Statement(&m_conn, "ssisis", "UPDATE " + m_prefix + "buddies SET groups=?, nickname=?, flags=?, subscription=? WHERE user_id=? AND uin=?");
 
	m_getBuddies = new Statement(&m_conn, "i|issssi", "SELECT id, uin, subscription, nickname, groups, flags FROM " + m_prefix + "buddies WHERE user_id=? ORDER BY id ASC");
 
	m_getBuddiesSettings = new Statement(&m_conn, "i|iiss", "SELECT buddy_id, type, var, value FROM " + m_prefix + "buddies_settings WHERE user_id=? ORDER BY buddy_id ASC");
 
	m_updateBuddySetting = new Statement(&m_conn, "iisiss", "INSERT INTO " + m_prefix + "buddies_settings (user_id, buddy_id, var, type, value) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE value=?");
 
	
 
	PREP_STMT(m_getUserSetting, "SELECT type, value FROM " + m_prefix + "users_settings WHERE user_id=? AND var=?");
 
	PREP_STMT(m_setUserSetting, "INSERT INTO " + m_prefix + "users_settings (user_id, var, type, value) VALUES (?,?,?,?)");
 
	PREP_STMT(m_updateUserSetting, "UPDATE " + m_prefix + "users_settings SET value=? WHERE user_id=? AND var=?");
 
	m_getUserSetting = new Statement(&m_conn, "is|is", "SELECT type, value FROM " + m_prefix + "users_settings WHERE user_id=? AND var=?");
 
	m_setUserSetting = new Statement(&m_conn, "isis", "INSERT INTO " + m_prefix + "users_settings (user_id, var, type, value) VALUES (?,?,?,?)");
 
	m_updateUserSetting = new Statement(&m_conn, "sis", "UPDATE " + m_prefix + "users_settings SET value=? WHERE user_id=? AND var=?");
 

	
 
	return true;
 
}
 

	
 
bool MySQLBackend::createDatabase() {
 
	int not_exist = exec("CREATE TABLE IF NOT EXISTS `" + m_prefix + "buddies` ("
 
@@ -494,20 +493,20 @@ bool MySQLBackend::getBuddies(long id, std::list<BuddyInfo> &roster) {
 
				buddy_id = -1;
 
			}
 
		}
 
	}
 

	
 
	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 (!m_removeUser->execute())
 
		return false;
 

	
 
	*m_removeUserSettings << (int) id;
 
	if (!m_removeUserSettings->execute())
 
@@ -522,17 +521,29 @@ bool MySQLBackend::removeUser(long id) {
 
		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;
 
	m_getUserSetting->execute();
 
	if (m_getUserSetting->fetch() != 0) {
 
// 		"INSERT INTO " + m_prefix + "users_settings (user_id, var, type, value) VALUES (?,?,?,?)"
 
		*m_setUserSetting << id << variable << type << value;
 
		m_setUserSetting->execute();
 
	}
 
	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;
 
	m_updateUserSetting->execute();
 
}
 

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

	
0 comments (0 inline, 0 general)