Changeset - 7f5a07be383f
[Not reviewed]
0 1 0
Daniel Henninger - 14 years ago 2012-02-17 19:49:57
daniel@vorpalcloud.org
Cleaned up code a bit, primarily adding more exception catching in proper locations.
1 file changed with 18 insertions and 2 deletions:
0 comments (0 inline, 0 general)
src/pqxxbackend.cpp
Show inline comments
 
@@ -48,25 +48,32 @@ void PQXXBackend::disconnect() {
 
}
 

	
 
bool PQXXBackend::connect() {
 
	LOG4CXX_INFO(logger, "Connecting PostgreSQL server " << CONFIG_STRING(m_config, "database.server") << ", user " <<
 
		CONFIG_STRING(m_config, "database.user") << ", database " << CONFIG_STRING(m_config, "database.database") <<
 
		", port " << CONFIG_INT(m_config, "database.port")
 
	);
 
	
 
	std::string str = "dbname=";
 
	str += CONFIG_STRING(m_config, "database.database") + " ";
 

	
 
	str += "user=" + CONFIG_STRING(m_config, "database.user") + " ";
 

	
 
	try {
 
		m_conn = new pqxx::connection(str);
 
	}
 
	catch (std::exception& e) {
 
		LOG4CXX_ERROR(logger, e.what());
 
		return false;
 
	}
 

	
 
	createDatabase();
 

	
 
	return true;
 
}
 

	
 
bool PQXXBackend::createDatabase() {
 
	
 
	int exist = exec("SELECT * FROM " + m_prefix + "buddies_settings LIMIT 1;", false);
 

	
 
	if (!exist) {
 
		exec("CREATE TABLE " + m_prefix + "buddies_settings ("
 
@@ -143,34 +150,39 @@ bool PQXXBackend::exec(pqxx::nontransaction &txn, const std::string &query, bool
 
		if (show_error)
 
			LOG4CXX_ERROR(logger, e.what());
 
		return false;
 
	}
 
	return true;
 
}
 

	
 
void PQXXBackend::setUser(const UserInfo &user) {
 
	std::string encrypted = user.password;
 
	if (!CONFIG_STRING(m_config, "database.encryption_key").empty()) {
 
		encrypted = Util::encryptPassword(encrypted, CONFIG_STRING(m_config, "database.encryption_key"));
 
	}
 
	try {
 
		pqxx::nontransaction txn(*m_conn);
 
		txn.exec("INSERT INTO " + m_prefix + "users (jid, uin, password, language, encoding, last_login, vip) VALUES "
 
			+ "(" + quote(txn, user.jid) + ","
 
			+ quote(txn, user.uin) + ","
 
			+ quote(txn, encrypted) + ","
 
			+ quote(txn, user.language) + ","
 
			+ quote(txn, user.encoding) + ","
 
			+ "NOW(),"
 
			+ (user.vip ? "'true'" : "'false'") +")");
 
	}
 
	catch (std::exception& e) {
 
		LOG4CXX_ERROR(logger, e.what());
 
	}
 
}
 

	
 
bool PQXXBackend::getUser(const std::string &barejid, UserInfo &user) {
 
	try {
 
		pqxx::nontransaction txn(*m_conn);
 

	
 
		pqxx::result r = txn.exec("SELECT id, jid, uin, password, encoding, language, vip FROM " + m_prefix + "users WHERE jid="
 
			+ quote(txn, barejid));
 

	
 
		if (r.size() == 0) {
 
			return false;
 
		}
 

	
 
@@ -209,49 +221,53 @@ bool PQXXBackend::getOnlineUsers(std::vector<std::string> &users) {
 
			users.push_back((*it)[0].as<std::string>());
 
		}
 
	}
 
	catch (std::exception& e) {
 
		LOG4CXX_ERROR(logger, e.what());
 
		return false;
 
	}
 

	
 
	return true;
 
}
 

	
 
long PQXXBackend::addBuddy(long userId, const BuddyInfo &buddyInfo) {
 
	try {
 
		pqxx::nontransaction txn(*m_conn);
 
		pqxx::result r = txn.exec("INSERT INTO " + m_prefix + "buddies (user_id, uin, subscription, groups, nickname, flags) VALUES "
 
			+ "(" + pqxx::to_string(userId) + ","
 
			+ quote(txn, buddyInfo.legacyName) + ","
 
			+ quote(txn, buddyInfo.subscription) + ","
 
			+ quote(txn, Util::serializeGroups(buddyInfo.groups)) + ","
 
			+ quote(txn, buddyInfo.alias) + ","
 
			+ pqxx::to_string(buddyInfo.flags) + ") RETURNING id");
 

	
 
		long id = r[0][0].as<long>();
 

	
 
		r = txn.exec("UPDATE " + m_prefix + "buddies_settings SET var = " + quote(txn, buddyInfo.settings.find("icon_hash")->first) + ", type = " + pqxx::to_string((int)TYPE_STRING) + ", value = " + quote(txn, buddyInfo.settings.find("icon_hash")->second.s) + " WHERE user_id = " + pqxx::to_string(userId) + " AND buddy_id = " + pqxx::to_string(id));
 
		if (r.affected_rows() == 0) {
 
			txn.exec("INSERT INTO " + m_prefix + "buddies_settings (user_id, buddy_id, var, type, value) VALUES "
 
				+ "(" + pqxx::to_string(userId) + ","
 
				+ pqxx::to_string(id) + ","
 
				+ quote(txn, buddyInfo.settings.find("icon_hash")->first) + ","
 
				+ pqxx::to_string((int)TYPE_STRING) + ","
 
				+ quote(txn,  buddyInfo.settings.find("icon_hash")->second.s) + ")");
 
		}
 

	
 
	txn.commit();
 

	
 
		return id;
 
	}
 
	catch (std::exception& e) {
 
		LOG4CXX_ERROR(logger, e.what());
 
		return -1;
 
	}
 
}
 

	
 
void PQXXBackend::updateBuddy(long userId, const BuddyInfo &buddyInfo) {
 
	try {
 
		pqxx::nontransaction txn(*m_conn);
 
		txn.exec("UPDATE " + m_prefix + "buddies SET groups=" + quote(txn, Util::serializeGroups(buddyInfo.groups)) + ", nickname=" + quote(txn, buddyInfo.alias) + ", flags=" + pqxx::to_string(buddyInfo.flags) + ", subscription=" + quote(txn, buddyInfo.subscription) + " WHERE user_id=" + pqxx::to_string(userId) + " AND uin=" + quote(txn, buddyInfo.legacyName));
 
	}
 
	catch (std::exception& e) {
 
		LOG4CXX_ERROR(logger, e.what());
 
	}
 
}
 

	
 
bool PQXXBackend::getBuddies(long id, std::list<BuddyInfo> &roster) {
0 comments (0 inline, 0 general)