Commit a5a16445 authored by Pavel Vainerman's avatar Pavel Vainerman

(DBServer_PGSQL): работа над оптимизацией интерфейса

parent f628ffb0
...@@ -50,9 +50,9 @@ MySQLInterface::~MySQLInterface() ...@@ -50,9 +50,9 @@ MySQLInterface::~MySQLInterface()
} }
// ----------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------
bool MySQLInterface::nconnect( const string& host, const string& user, const string& pswd, const string& dbname) bool MySQLInterface::nconnect(const string& host, const string& user, const string& pswd, const string& dbname, unsigned int port )
{ {
if( !mysql_real_connect(mysql, host.c_str(), user.c_str(), pswd.c_str(), dbname.c_str(), 0, NULL, 0) ) if( !mysql_real_connect(mysql, host.c_str(), user.c_str(), pswd.c_str(), dbname.c_str(), port, NULL, 0) )
{ {
cout << error() << endl; cout << error() << endl;
mysql_close(mysql); mysql_close(mysql);
......
...@@ -41,7 +41,7 @@ class MySQLInterface: ...@@ -41,7 +41,7 @@ class MySQLInterface:
// DBResult listFields( const std::string& table, const std::string& wild ); // DBResult listFields( const std::string& table, const std::string& wild );
virtual bool nconnect( const std::string& host, const std::string& user, const std::string& pswd, virtual bool nconnect( const std::string& host, const std::string& user, const std::string& pswd,
const std::string& dbname) override; const std::string& dbname, unsigned int port=0 ) override;
virtual bool close() override; virtual bool close() override;
bool query_ok( const std::string& q ); bool query_ok( const std::string& q );
......
...@@ -120,7 +120,7 @@ void DBServer_PostgreSQL::confirmInfo( const UniSetTypes::ConfirmMessage* cem ) ...@@ -120,7 +120,7 @@ void DBServer_PostgreSQL::confirmInfo( const UniSetTypes::ConfirmMessage* cem )
dbinfo << myname << "(update_confirm): " << data.str() << endl; dbinfo << myname << "(update_confirm): " << data.str() << endl;
if( !writeToBase(data.str()) ) if( !writeToBase( std::move(data.str())) )
{ {
dbcrit << myname << "(update_confirm): db error: " << db->error() << endl; dbcrit << myname << "(update_confirm): db error: " << db->error() << endl;
} }
...@@ -165,7 +165,7 @@ bool DBServer_PostgreSQL::writeToBase( const string& query ) ...@@ -165,7 +165,7 @@ bool DBServer_PostgreSQL::writeToBase( const string& query )
flushBuffer(); flushBuffer();
// А теперь собственно запрос.. // А теперь собственно запрос..
if( db->insertAndSaveRowid(query) ) if( db->insert(query) )
return true; return true;
return false; return false;
...@@ -178,7 +178,7 @@ void DBServer_PostgreSQL::flushBuffer() ...@@ -178,7 +178,7 @@ void DBServer_PostgreSQL::flushBuffer()
// Сперва пробуем очистить всё что накопилось в очереди до этого... // Сперва пробуем очистить всё что накопилось в очереди до этого...
while( !qbuf.empty() ) while( !qbuf.empty() )
{ {
if(!db->insertAndSaveRowid( qbuf.front() )) if(!db->insert( qbuf.front() ))
{ {
dbcrit << myname << "(writeToBase): error: " << db->error() << " lost query: " << qbuf.front() << endl; dbcrit << myname << "(writeToBase): error: " << db->error() << " lost query: " << qbuf.front() << endl;
} }
...@@ -216,7 +216,7 @@ void DBServer_PostgreSQL::sensorInfo( const UniSetTypes::SensorMessage* si ) ...@@ -216,7 +216,7 @@ void DBServer_PostgreSQL::sensorInfo( const UniSetTypes::SensorMessage* si )
dbinfo << myname << "(insert_main_history): " << data.str() << endl; dbinfo << myname << "(insert_main_history): " << data.str() << endl;
if( !writeToBase(data.str()) ) if( !writeToBase(std::move(data.str())) )
{ {
dbcrit << myname << "(insert) sensor msg error: " << db->error() << endl; dbcrit << myname << "(insert) sensor msg error: " << db->error() << endl;
} }
...@@ -266,6 +266,7 @@ void DBServer_PostgreSQL::initDBServer() ...@@ -266,6 +266,7 @@ void DBServer_PostgreSQL::initDBServer()
string dbnode(conf->getProp(node, "dbnode")); string dbnode(conf->getProp(node, "dbnode"));
string user(conf->getProp(node, "dbuser")); string user(conf->getProp(node, "dbuser"));
string password(conf->getProp(node, "dbpass")); string password(conf->getProp(node, "dbpass"));
unsigned int dbport = conf->getPIntProp(node, "dbport",5432);
tblMap[UniSetTypes::Message::SensorInfo] = "main_history"; tblMap[UniSetTypes::Message::SensorInfo] = "main_history";
tblMap[UniSetTypes::Message::Confirm] = "main_history"; tblMap[UniSetTypes::Message::Confirm] = "main_history";
...@@ -289,7 +290,7 @@ void DBServer_PostgreSQL::initDBServer() ...@@ -289,7 +290,7 @@ void DBServer_PostgreSQL::initDBServer()
<< " pingTime=" << PingTime << " pingTime=" << PingTime
<< " ReconnectTime=" << ReconnectTime << endl; << " ReconnectTime=" << ReconnectTime << endl;
if( !db->nconnect(dbnode, user, password, dbname) ) if( !db->nconnect(dbnode, user, password, dbname, dbport) )
{ {
dbwarn << myname << "(init): DB connection error: " << db->error() << endl; dbwarn << myname << "(init): DB connection error: " << db->error() << endl;
askTimer(DBServer_PostgreSQL::ReconnectTimer, ReconnectTime); askTimer(DBServer_PostgreSQL::ReconnectTimer, ReconnectTime);
......
...@@ -23,6 +23,11 @@ ...@@ -23,6 +23,11 @@
#include "PostgreSQLInterface.h" #include "PostgreSQLInterface.h"
#include "DBServer.h" #include "DBServer.h"
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
/*!
* \brief The DBServer_PostgreSQL class
* Реализация работы с PostgreSQL.
*
*/
class DBServer_PostgreSQL: class DBServer_PostgreSQL:
public DBServer public DBServer
{ {
......
...@@ -50,18 +50,22 @@ bool PostgreSQLInterface::ping() ...@@ -50,18 +50,22 @@ bool PostgreSQLInterface::ping()
return db && db->is_open(); return db && db->is_open();
} }
// ----------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------
bool PostgreSQLInterface::nconnect( const string& host, const string& user, const string& pswd, const string& dbname) bool PostgreSQLInterface::nconnect(const string& host, const string& user, const string& pswd, const string& dbname, unsigned int port )
{ {
if( db ) if( db )
return true; return true;
std::string conninfo = "dbname=" + dbname + " host=" + host + " user=" + user + " password=" + pswd; ostringstream conninfo;
conninfo << "dbname=" << dbname
<< " host=" << host
<< " user=" << user
<< " password=" << pswd
<< " port=" << port;
try try
{ {
db = make_shared<pqxx::connection>(conninfo); db = make_shared<pqxx::connection>( std::move(conninfo.str()) );
return db->is_open(); return db->is_open();
} }
catch( const std::exception& e ) catch( const std::exception& e )
{ {
...@@ -140,7 +144,7 @@ DBResult PostgreSQLInterface::query( const string& q ) ...@@ -140,7 +144,7 @@ DBResult PostgreSQLInterface::query( const string& q )
result res( n.exec(q) ); result res( n.exec(q) );
DBResult dbres; DBResult dbres;
makeResult(dbres, res); makeResult(dbres, res);
return dbres; return std::move(dbres);
} }
catch( const std::exception& e ) catch( const std::exception& e )
{ {
...@@ -182,10 +186,10 @@ void PostgreSQLInterface::makeResult(DBResult& dbres, const pqxx::result& res ) ...@@ -182,10 +186,10 @@ void PostgreSQLInterface::makeResult(DBResult& dbres, const pqxx::result& res )
{ {
DBResult::COL col; DBResult::COL col;
for( pqxx::result::tuple::size_type i = 0; i < c.size(); i++ ) for( pqxx::result::tuple::const_iterator i = c.begin(); i != c.end(); i++ )
col.push_back( c[i].as<string>() ); col.push_back( (i.is_null() ? "" : i.as<string>()) );
dbres.row().push_back(col); dbres.row().push_back( std::move(col) );
} }
} }
// ----------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------
......
...@@ -33,7 +33,9 @@ class PostgreSQLInterface: ...@@ -33,7 +33,9 @@ class PostgreSQLInterface:
PostgreSQLInterface(); PostgreSQLInterface();
~PostgreSQLInterface(); ~PostgreSQLInterface();
virtual bool nconnect( const std::string& host, const std::string& user, const std::string& pswd, const std::string& dbname ) override; virtual bool nconnect( const std::string& host, const std::string& user,
const std::string& pswd, const std::string& dbname,
unsigned int port=5432) override;
virtual bool close() override; virtual bool close() override;
virtual bool isConnection() override; virtual bool isConnection() override;
virtual bool ping() override; // проверка доступности БД virtual bool ping() override; // проверка доступности БД
......
...@@ -16,13 +16,16 @@ int main(int argc, char** argv) ...@@ -16,13 +16,16 @@ int main(int argc, char** argv)
try try
{ {
PostgreSQLInterface db; PostgreSQLInterface db;
cout << "connect to '" << dbname << "'..." << endl;
if( !db.nconnect("localhost", "dbadmin", "dbadmin", dbname) ) if( !db.nconnect("localhost", "dbadmin", "dbadmin", dbname) )
{ {
cerr << "db connect error: " << db.error() << endl; cerr << "[FAILED] connect error: " << db.error() << endl;
return 1; return 1;
} }
cout << "connect to '" << dbname << "' [OK]" << endl;
stringstream q; stringstream q;
q << "SELECT * from main_history"; q << "SELECT * from main_history";
...@@ -31,7 +34,7 @@ int main(int argc, char** argv) ...@@ -31,7 +34,7 @@ int main(int argc, char** argv)
if( !r ) if( !r )
{ {
cerr << "db connect error: " << db.error() << endl; cerr << "query error: " << db.error() << endl;
return 1; return 1;
} }
......
...@@ -37,9 +37,10 @@ class DBNetInterface : public DBInterface ...@@ -37,9 +37,10 @@ class DBNetInterface : public DBInterface
DBNetInterface() {}; DBNetInterface() {};
virtual ~DBNetInterface() {}; virtual ~DBNetInterface() {};
// Для сетевых БД параметры должны быть в формате user@host:pswd:dbname // Для сетевых БД параметры должны быть в формате user@host:pswd:dbname:port
virtual bool connect( const std::string& param ); virtual bool connect( const std::string& param );
virtual bool nconnect( const std::string& host, const std::string& user, const std::string& pswd, const std::string& dbname ) = 0; virtual bool nconnect( const std::string& host, const std::string& user, const std::string& pswd,
const std::string& dbname, unsigned int port ) = 0;
}; };
// ---------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------
class DBResult class DBResult
......
...@@ -6,6 +6,7 @@ bool DBNetInterface::connect( const std::string& param ) ...@@ -6,6 +6,7 @@ bool DBNetInterface::connect( const std::string& param )
std::string user = ""; std::string user = "";
std::string pswd = ""; std::string pswd = "";
std::string dbname = ""; std::string dbname = "";
unsigned int port = 0;
for(;;) for(;;)
{ {
...@@ -32,10 +33,17 @@ bool DBNetInterface::connect( const std::string& param ) ...@@ -32,10 +33,17 @@ bool DBNetInterface::connect( const std::string& param )
prev = pos + 1; prev = pos + 1;
pos = param.find_first_of(":", prev); pos = param.find_first_of(":", prev);
dbname = param.substr(prev, pos - prev); dbname = param.substr(prev, pos - prev);
if( pos == std::string::npos )
break;
prev = pos + 1;
pos = param.find_first_of(":", prev);
port = atoi( param.substr(prev, pos - prev).c_str() );
break; break;
} }
return nconnect( host, user, pswd, dbname ); return nconnect( host, user, pswd, dbname, port );
} }
//-------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------
DBResult::ROW& DBResult::row() DBResult::ROW& DBResult::row()
...@@ -108,4 +116,4 @@ DBResult::COL DBResult::get_col( DBResult::iterator& it ) ...@@ -108,4 +116,4 @@ DBResult::COL DBResult::get_col( DBResult::iterator& it )
{ {
return (*it); return (*it);
} }
#endif #endif
\ No newline at end of file
...@@ -268,6 +268,7 @@ include/ComPort485F.h ...@@ -268,6 +268,7 @@ include/ComPort485F.h
include/Configuration.1.h include/Configuration.1.h
include/Configuration.h include/Configuration.h
include/DBServer.h include/DBServer.h
include/DBInterface.h
include/Debug.h include/Debug.h
include/DebugStream.h include/DebugStream.h
include/DelayTimer.h include/DelayTimer.h
...@@ -402,6 +403,7 @@ src/Processes/NCRestorer_XML.cc ...@@ -402,6 +403,7 @@ src/Processes/NCRestorer_XML.cc
src/Processes/CommonEventLoop.cc src/Processes/CommonEventLoop.cc
src/Processes/EventLoopServer.cc src/Processes/EventLoopServer.cc
src/Services/DBServer.cc src/Services/DBServer.cc
src/Services/DBInterface.cc
src/Services/Makefile.am src/Services/Makefile.am
src/Timers/Makefile.am src/Timers/Makefile.am
src/Timers/PassiveCondTimer.cc src/Timers/PassiveCondTimer.cc
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment