@ -51,28 +51,7 @@ void PQXXBackend::disconnect() {
@@ -51,28 +51,7 @@ void PQXXBackend::disconnect() {
bool PQXXBackend : : connect ( ) {
std : : string connection_str ;
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 " ) < < " , 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_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. " ) ;
}
LOG4CXX_INFO ( logger , " Connecting PostgreSQL server: " < < connection_str ) ;
try {
m_conn = new pqxx : : connection ( connection_str ) ;
@ -146,12 +125,13 @@ bool PQXXBackend::createDatabase() {
@@ -146,12 +125,13 @@ bool PQXXBackend::createDatabase() {
exec ( " INSERT INTO " + m_prefix + " db_version (ver) VALUES ('1'); " ) ;
}
exec ( " CREATE TABLE IF NOT EXISTS " + m_prefix + " muc_names ( "
" user_id INTEGER NOT NULL, "
" muc_id VARCHAR NOT NULL, "
" name VARCHAR NOT NULL, "
" UNIQUE(user_id, muc_id) "
" ); " ) ;
exec ( " CREATE TABLE IF NOT EXISTS " + m_prefix +
" muc_names ( "
" user_id INTEGER NOT NULL, "
" muc_id VARCHAR NOT NULL, "
" name VARCHAR NOT NULL, "
" UNIQUE(user_id, muc_id) "
" ); " ) ;
return true ;
}
@ -172,6 +152,7 @@ bool PQXXBackend::exec(pqxx::nontransaction &txn, const std::string &query, bool
@@ -172,6 +152,7 @@ bool PQXXBackend::exec(pqxx::nontransaction &txn, const std::string &query, bool
} catch ( std : : exception & e ) {
if ( show_error )
LOG4CXX_ERROR ( logger , e . what ( ) ) ;
connect ( ) ;
return false ;
}
return true ;
@ -192,6 +173,7 @@ void PQXXBackend::setUser(const UserInfo &user) {
@@ -192,6 +173,7 @@ void PQXXBackend::setUser(const UserInfo &user) {
( user . vip ? " 'true' " : " 'false' " ) + " ) " ) ;
} catch ( std : : exception & e ) {
LOG4CXX_ERROR ( logger , e . what ( ) ) ;
connect ( ) ;
}
}
@ -215,6 +197,7 @@ bool PQXXBackend::getUser(const std::string &barejid, UserInfo &user) {
@@ -215,6 +197,7 @@ bool PQXXBackend::getUser(const std::string &barejid, UserInfo &user) {
user . vip = r [ 0 ] [ 6 ] . as < bool > ( ) ;
} catch ( std : : exception & e ) {
LOG4CXX_ERROR ( logger , e . what ( ) ) ;
connect ( ) ;
return false ;
}
@ -228,6 +211,7 @@ void PQXXBackend::setUserOnline(long id, bool online) {
@@ -228,6 +211,7 @@ void PQXXBackend::setUserOnline(long id, bool online) {
" , last_login=NOW() WHERE id= " + pqxx : : to_string ( id ) ) ;
} catch ( std : : exception & e ) {
LOG4CXX_ERROR ( logger , e . what ( ) ) ;
connect ( ) ;
}
}
@ -241,6 +225,7 @@ bool PQXXBackend::getOnlineUsers(std::vector<std::string> &users) {
@@ -241,6 +225,7 @@ bool PQXXBackend::getOnlineUsers(std::vector<std::string> &users) {
}
} catch ( std : : exception & e ) {
LOG4CXX_ERROR ( logger , e . what ( ) ) ;
connect ( ) ;
return false ;
}
@ -257,6 +242,7 @@ bool PQXXBackend::getUsers(std::vector<std::string> &users) {
@@ -257,6 +242,7 @@ bool PQXXBackend::getUsers(std::vector<std::string> &users) {
}
} catch ( std : : exception & e ) {
LOG4CXX_ERROR ( logger , e . what ( ) ) ;
connect ( ) ;
return false ;
}
@ -291,6 +277,7 @@ long PQXXBackend::addBuddy(long userId, const BuddyInfo &buddyInfo) {
@@ -291,6 +277,7 @@ long PQXXBackend::addBuddy(long userId, const BuddyInfo &buddyInfo) {
return id ;
} catch ( std : : exception & e ) {
LOG4CXX_ERROR ( logger , e . what ( ) ) ;
connect ( ) ;
return - 1 ;
}
}
@ -305,6 +292,7 @@ void PQXXBackend::updateBuddy(long userId, const BuddyInfo &buddyInfo) {
@@ -305,6 +292,7 @@ void PQXXBackend::updateBuddy(long userId, const BuddyInfo &buddyInfo) {
" AND uin= " + quote ( txn , buddyInfo . legacyName ) ) ;
} catch ( std : : exception & e ) {
LOG4CXX_ERROR ( logger , e . what ( ) ) ;
connect ( ) ;
}
}
@ -367,6 +355,7 @@ bool PQXXBackend::getBuddies(long id, std::list<BuddyInfo> &roster) {
@@ -367,6 +355,7 @@ bool PQXXBackend::getBuddies(long id, std::list<BuddyInfo> &roster) {
return true ;
} catch ( std : : exception & e ) {
LOG4CXX_ERROR ( logger , e . what ( ) ) ;
connect ( ) ;
}
return false ;
@ -383,6 +372,7 @@ bool PQXXBackend::removeUser(long id) {
@@ -383,6 +372,7 @@ bool PQXXBackend::removeUser(long id) {
return true ;
} catch ( std : : exception & e ) {
LOG4CXX_ERROR ( logger , e . what ( ) ) ;
connect ( ) ;
}
return false ;
}
@ -404,6 +394,7 @@ void PQXXBackend::getUserSetting(long id, const std::string &variable, int &type
@@ -404,6 +394,7 @@ void PQXXBackend::getUserSetting(long id, const std::string &variable, int &type
}
} catch ( std : : exception & e ) {
LOG4CXX_ERROR ( logger , e . what ( ) ) ;
connect ( ) ;
}
}
@ -414,34 +405,39 @@ void PQXXBackend::updateUserSetting(long id, const std::string &variable, const
@@ -414,34 +405,39 @@ void PQXXBackend::updateUserSetting(long id, const std::string &variable, const
" WHERE user_id= " + pqxx : : to_string ( id ) + " AND var= " + quote ( txn , variable ) ) ;
} catch ( std : : exception & e ) {
LOG4CXX_ERROR ( logger , e . what ( ) ) ;
connect ( ) ;
}
}
void PQXXBackend : : addMucName ( long userId , const std : : string & mucId , const std : : string & mucName ) {
try {
pqxx : : nontransaction txn ( * m_conn ) ;
txn . exec_params ( " INSERT INTO " + m_prefix + " muc_names (user_id, muc_id, name) VALUES ($1, $2, $3) ON CONFLICT (user_id, muc_id) DO UPDATE SET name = EXCLUDED.name " , userId , mucId , mucName ) ;
} catch ( std : : exception & e ) {
LOG4CXX_ERROR ( logger , e . what ( ) ) ;
}
}
std : : string PQXXBackend : : getMucName ( long userId , const std : : string & mucId ) {
try {
pqxx : : nontransaction txn ( * m_conn ) ;
pqxx : : result r =
txn . exec_params ( " SELECT name FROM " + m_prefix + " muc_names WHERE user_id = $1 AND muc_id = $2 " , userId , mucId ) ;
if ( r . size ( ) = = 0 ) {
return " " ;
}
else {
return r [ 0 ] [ 0 ] . as < std : : string > ( ) ;
}
} catch ( std : : exception & e ) {
LOG4CXX_ERROR ( logger , e . what ( ) ) ;
return " " ;
}
}
void PQXXBackend : : addMucName ( long userId , const std : : string & mucId , const std : : string & mucName ) {
try {
pqxx : : nontransaction txn ( * m_conn ) ;
txn . exec_params ( " INSERT INTO " + m_prefix +
" muc_names (user_id, muc_id, name) VALUES ($1, $2, $3) ON CONFLICT (user_id, muc_id) DO "
" UPDATE SET name = EXCLUDED.name " ,
userId , mucId , mucName ) ;
} catch ( std : : exception & e ) {
LOG4CXX_ERROR ( logger , e . what ( ) ) ;
connect ( ) ;
}
}
std : : string PQXXBackend : : getMucName ( long userId , const std : : string & mucId ) {
try {
pqxx : : nontransaction txn ( * m_conn ) ;
pqxx : : result r = txn . exec_params (
" SELECT name FROM " + m_prefix + " muc_names WHERE user_id = $1 AND muc_id = $2 " , userId , mucId ) ;
if ( r . size ( ) = = 0 ) {
return " " ;
} else {
return r [ 0 ] [ 0 ] . as < std : : string > ( ) ;
}
} catch ( std : : exception & e ) {
LOG4CXX_ERROR ( logger , e . what ( ) ) ;
connect ( ) ;
return " " ;
}
}
void PQXXBackend : : beginTransaction ( ) {
exec ( " BEGIN; " ) ;