Changeset - 319a4d349e74
[Not reviewed]
Merge
0 4 0
HanzZ - 13 years ago 2013-01-11 23:26:54
hanzz.k@gmail.com
Merge remote-tracking branch 'jadestorm/master'
1 file changed with 4 insertions and 4 deletions:
0 comments (0 inline, 0 general)
src/pqxxbackend.cpp
Show inline comments
 
@@ -4,115 +4,115 @@
 
 * Copyright (C) 2011, Jan Kaluza <hanzz.k@gmail.com>
 
 *
 
 * This program is free software; you can redistribute it and/or modify
 
 * it under the terms of the GNU General Public License as published by
 
 * the Free Software Foundation; either version 2 of the License, or
 
 * (at your option) any later version.
 
 *
 
 * This program is distributed in the hope that it will be useful,
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 * GNU General Public License for more details.
 
 *
 
 * You should have received a copy of the GNU General Public License
 
 * along with this program; if not, write to the Free Software
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 
 */
 

	
 
#ifdef WITH_PQXX
 

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

	
 
using namespace log4cxx;
 
using namespace boost;
 

	
 
namespace Transport {
 

	
 
static LoggerPtr logger = Logger::getLogger("PQXXBackend");
 

	
 
PQXXBackend::PQXXBackend(Config *config) {
 
	m_config = config;
 
	m_prefix = CONFIG_STRING(m_config, "database.prefix");
 
}
 

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

	
 
void PQXXBackend::disconnect() {
 
	LOG4CXX_INFO(logger, "Disconnecting");
 

	
 
	delete m_conn;
 
}
 

	
 
bool PQXXBackend::connect() {
 
	std::string connection_str;
 
	connection_str = CONFIG_STRING(m_config, "database.connectionstring");
 
	connection_str = CONFIG_STRING_DEFAULTED(m_config, "database.connectionstring", "");
 
	if (connection_str.empty()) {
 
		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") <<
 
			CONFIG_STRING(m_config, "database.user") << ", dbname " << CONFIG_STRING(m_config, "database.database") <<
 
			", port " << CONFIG_INT(m_config, "database.port")
 
		);
 
		connection_str = "dbname=";
 
		connection_str += CONFIG_STRING(m_config, "database.database");
 
		if (!CONFIG_STRING(m_config, "database.server").empty()) {
 
			connection_str += " host=" + CONFIG_STRING(m_config, "database.server");
 
		}
 
		if (!CONFIG_STRING(m_config, "database.user").empty()) {
 
			connection_str += " user=" + CONFIG_STRING(m_config, "database.user");
 
		}
 
		if (!CONFIG_STRING(m_config, "database.password").empty()) {
 
			connection_str += " password=" + CONFIG_STRING(m_config, "database.password");
 
		}
 
		if (!CONFIG_STRING(m_config, "database.port").empty()) {
 
			connection_str += " port=" + CONFIG_STRING(m_config, "database.password");
 
		if (CONFIG_INT(m_config, "database.port") != 0) {
 
			connection_str += " port=" + boost:lexical_cast<std::string>(CONFIG_INT(m_config, "database.port"));
 
		}
 
	}
 
	else {
 
		LOG4CXX_INFO(logger, "Connecting PostgreSQL server via provided connection string.");
 
	}
 

	
 
	try {
 
		m_conn = new pqxx::connection(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 ("
 
				"user_id integer NOT NULL,"
 
				"buddy_id integer NOT NULL,"
 
				"var varchar(50) NOT NULL,"
 
				"type smallint NOT NULL,"
 
				"value varchar(255) NOT NULL,"
 
				"PRIMARY KEY (buddy_id,var)"
 
			");");
 
		
 
		exec("CREATE TYPE Subscription AS ENUM ('to','from','both','ask','none');");
 
		exec("CREATE TABLE " + m_prefix + "buddies ("
 
							"id SERIAL,"
 
							"user_id integer NOT NULL,"
 
							"uin varchar(255) NOT NULL,"
 
							"subscription Subscription NOT NULL,"
 
							"nickname varchar(255) NOT NULL,"
 
							"groups varchar(255) NOT NULL,"
 
							"flags smallint NOT NULL DEFAULT '0',"
 
							"PRIMARY KEY (id),"
 
							"UNIQUE (user_id,uin)"
 
						");");
 
 
 
		exec("CREATE TABLE " + m_prefix + "users ("
 
				"id SERIAL,"
0 comments (0 inline, 0 general)