Changeset - 76dda381db24
[Not reviewed]
0 1 0
HanzZ - 13 years ago 2013-02-12 18:22:57
hanzz.k@gmail.com
Use iterator in pqxx storage backend
1 file changed with 10 insertions and 10 deletions:
0 comments (0 inline, 0 general)
src/pqxxbackend.cpp
Show inline comments
 
@@ -274,74 +274,74 @@ long PQXXBackend::addBuddy(long userId, const BuddyInfo &buddyInfo) {
 
		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, StorageBackend::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) {
 
	try {
 
		pqxx::nontransaction txn(*m_conn);
 

	
 
		pqxx::result r = txn.exec("SELECT id, uin, subscription, nickname, groups, flags FROM " + m_prefix + "buddies WHERE user_id=" + pqxx::to_string(id) + " ORDER BY id ASC");
 
		for (pqxx::result::const_iterator it = r.begin(); it != r.end(); it++)  {
 
			BuddyInfo b;
 
			std::string group;
 

	
 
			b.id = r[0][0].as<long>();
 
			b.legacyName = r[0][1].as<std::string>();
 
			b.subscription = r[0][2].as<std::string>();
 
			b.alias = r[0][3].as<std::string>();
 
			group = r[0][4].as<std::string>();
 
			b.flags = r[0][5].as<long>();
 
			b.id = (*it)[0].as<long>();
 
			b.legacyName = (*it)[1].as<std::string>();
 
			b.subscription = (*it)[2].as<std::string>();
 
			b.alias = (*it)[3].as<std::string>();
 
			group = (*it)[4].as<std::string>();
 
			b.flags = (*it)[5].as<long>();
 

	
 
			if (!group.empty()) {
 
				b.groups = StorageBackend::deserializeGroups(group);
 
			}
 

	
 
			roster.push_back(b);
 
		}
 

	
 

	
 
		r = txn.exec("SELECT buddy_id, type, var, value FROM " + m_prefix + "buddies_settings WHERE user_id=" + pqxx::to_string(id) + " ORDER BY buddy_id ASC");
 
		for (pqxx::result::const_iterator it = r.begin(); it != r.end(); it++)  {
 
			SettingVariableInfo var;
 
			long buddy_id = -1;
 
			std::string key;
 
			std::string val;
 

	
 
			buddy_id = r[0][0].as<long>();
 
			var.type = r[0][1].as<long>();
 
			key = r[0][2].as<std::string>();
 
			val = r[0][3].as<std::string>();
 
			buddy_id = (*it)[0].as<long>();
 
			var.type = (*it)[1].as<long>();
 
			key = (*it)[2].as<std::string>();
 
			val = (*it)[3].as<std::string>();
 
			switch (var.type) {
 
				case TYPE_BOOLEAN:
 
					var.b = atoi(val.c_str());
 
					break;
 
				case TYPE_STRING:
 
					var.s = val;
 
					break;
 
				default:
 
					continue;
 
					break;
 
			}
 

	
 
			BOOST_FOREACH(BuddyInfo &b, roster) {
 
				if (buddy_id == b.id) {
 
					b.settings[key] = var;
 
					break;
 
				}
 
			}
 
		}
 

	
 
		return true;
 
	}
 
	catch (std::exception& e) {
 
		LOG4CXX_ERROR(logger, e.what());
0 comments (0 inline, 0 general)