Changeset - 83604ed64063
[Not reviewed]
0 1 0
Daniel Henninger - 13 years ago 2012-01-27 22:05:18
daniel@vorpalcloud.org
Finished last postgres function.
1 file changed with 21 insertions and 16 deletions:
0 comments (0 inline, 0 general)
src/pqxxbackend.cpp
Show inline comments
 
@@ -97,49 +97,49 @@ bool PQXXBackend::createDatabase() {
 
				"uin varchar(4095) NOT NULL,"
 
				"password varchar(255) NOT NULL,"
 
				"language varchar(25) NOT NULL,"
 
				"encoding varchar(50) NOT NULL default 'utf8',"
 
				"last_login timestamp,"
 
				"vip boolean NOT NULL  default '0',"
 
				"online boolean NOT NULL  default '0',"
 
				"PRIMARY KEY (id),"
 
				"UNIQUE (jid)"
 
			");");
 

	
 
		exec("CREATE TABLE " + m_prefix + "users_settings ("
 
				"user_id integer NOT NULL,"
 
				"var varchar(50) NOT NULL,"
 
				"type smallint NOT NULL,"
 
				"value varchar(255) NOT NULL,"
 
				"PRIMARY KEY (user_id,var)"
 
			");");
 

	
 
		exec("CREATE TABLE " + m_prefix + "db_version ("
 
				"ver integer NOT NULL default '1',"
 
				"UNIQUE (ver)"
 
			");");
 

	
 
// 		exec("INSERT INTO db_version (ver) VALUES ('2');");
 
 		exec("INSERT INTO db_version (ver) VALUES ('1');");
 
	}
 

	
 
	return true;
 
}
 

	
 
template<typename T>
 
std::string PQXXBackend::quote(pqxx::work &txn, const T &t) {
 
	return "'" + txn.esc(pqxx::to_string(t)) + "'";
 
}
 

	
 
bool PQXXBackend::exec(const std::string &query, bool show_error) {
 
	pqxx::work txn(*m_conn);
 
	return exec(txn, query, show_error);
 
}
 

	
 
bool PQXXBackend::exec(pqxx::work &txn, const std::string &query, bool show_error) {
 
	try {
 
		txn.exec(query);
 
		txn.commit();
 
	}
 
	catch (std::exception& e) {
 
		if (show_error)
 
			LOG4CXX_ERROR(logger, e.what());
 
		return false;
 
@@ -197,71 +197,76 @@ void PQXXBackend::setUserOnline(long id, bool online) {
 
	}
 
	catch (std::exception& e) {
 
		LOG4CXX_ERROR(logger, e.what());
 
	}
 
}
 

	
 
bool PQXXBackend::getOnlineUsers(std::vector<std::string> &users) {
 
	try {
 
		pqxx::work txn(*m_conn);
 
		pqxx::result r = txn.exec("SELECT jid FROM " + m_prefix + "users WHERE online=1");
 

	
 
		for (pqxx::result::const_iterator it = r.begin(); it != r.end(); it++)  {
 
			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) {
 
// 	"INSERT INTO " + m_prefix + "buddies (user_id, uin, subscription, groups, nickname, flags) VALUES (?, ?, ?, ?, ?, ?)"
 
//	std::string groups = Util::serializeGroups(buddyInfo.groups);
 
//	*m_addBuddy << userId << buddyInfo.legacyName << buddyInfo.subscription;
 
//	*m_addBuddy << groups;
 
//	*m_addBuddy << buddyInfo.alias << buddyInfo.flags;
 

	
 
//	EXEC(m_addBuddy, addBuddy(userId, buddyInfo));
 
	pqxx::work 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 = (long) mysql_insert_id(&m_conn);
 
	long id = r[0][0].as<long>();
 

	
 
// 	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, addBuddy(userId, buddyInfo));
 
//	}
 
	r = txn.exec("UPDATE " + m_prefix + "buddies_settings SET var = " + quote(txn, buddyInfo.settings.find("icon_hash")->first) + ", type = " + pqxx::to_string(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) {
 
		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(TYPE_STRING) + ","
 
			+ quote(txn,  buddyInfo.settings.find("icon_hash")->second.s) + ")");
 
	}
 

	
 
	return 0;
 
	return id;
 
}
 

	
 
void PQXXBackend::updateBuddy(long userId, const BuddyInfo &buddyInfo) {
 
	try {
 
		pqxx::work txn(*m_conn);
 
		exec(txn, "UPDATE " + m_prefix + "buddies SET groups=" + quote(txn, Util::serializeGroups(buddyInfo.groups)) + ", nickname=" + quote(txn, buddyInfo.alias) + ", flags=" + quote(txn, buddyInfo.flags) + ", subscription=" + quote(txn, buddyInfo.subscription) + " WHERE user_id=" + pqxx::to_string(userId) + " AND uin=" + quote(txn, buddyInfo.legacyName));
 
		exec(txn, "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) {
 
	try {
 
		pqxx::work 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>();
 

	
 
			if (!group.empty()) {
 
				b.groups = Util::deserializeGroups(group);
0 comments (0 inline, 0 general)