Commit d5ff6808 authored by Pavel Vainerman's avatar Pavel Vainerman

(SQL): починил запуск для MySQL и SQLite dbserver-ов.

parent c21ba9d0
...@@ -44,6 +44,9 @@ Version 2.0 ...@@ -44,6 +44,9 @@ Version 2.0
uniset-codegen: добавить ключ --gen-sensor-name для генерирования name_Item.. (чтобы можно было в логах использовать текстовые названия) uniset-codegen: добавить ключ --gen-sensor-name для генерирования name_Item.. (чтобы можно было в логах использовать текстовые названия)
SQL:
====
- добавить работу с History (при передаче указателя на SM в конструкторе).
Version 2.1 Version 2.1
============ ============
......
...@@ -264,7 +264,7 @@ SharedMemoryPlus extension ('all in one') for libuniset ...@@ -264,7 +264,7 @@ SharedMemoryPlus extension ('all in one') for libuniset
%build %build
%autoreconf %autoreconf
%configure %{subst_enable doc} %{subst_enable mysql} %{subst_enable sqlite} %{subst_enable pgsql} %{subst_enable python} %{subst_enable rrd} %{subst_enable io} %{subst_enable logicproc} %{subst_enable tests} %configure %{subst_enable docs} %{subst_enable mysql} %{subst_enable sqlite} %{subst_enable pgsql} %{subst_enable python} %{subst_enable rrd} %{subst_enable io} %{subst_enable logicproc} %{subst_enable tests}
%make %make
%install %install
...@@ -444,6 +444,9 @@ mv -f %buildroot%python_sitelibdir_noarch/* %buildroot%python_sitelibdir/%oname ...@@ -444,6 +444,9 @@ mv -f %buildroot%python_sitelibdir_noarch/* %buildroot%python_sitelibdir/%oname
# .. # ..
%changelog %changelog
* Sat Apr 04 2015 Pavel Vainerman <pv@altlinux.ru> 2.0-alt24.1
- test pgsql extension build
* Thu Apr 02 2015 Pavel Vainerman <pv@altlinux.ru> 2.0-alt24 * Thu Apr 02 2015 Pavel Vainerman <pv@altlinux.ru> 2.0-alt24
- codegen: modify interface for messages (setMsg()) - codegen: modify interface for messages (setMsg())
- remove alarm() function (deprecated) - remove alarm() function (deprecated)
......
...@@ -38,9 +38,8 @@ using namespace std; ...@@ -38,9 +38,8 @@ using namespace std;
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
#define dblog if( ulog()->debugging(DBLogInfoLevel) ) ulog()->debug(DBLogInfoLevel) #define dblog if( ulog()->debugging(DBLogInfoLevel) ) ulog()->debug(DBLogInfoLevel)
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
DBServer_MySQL::DBServer_MySQL(ObjectId id): DBServer_MySQL::DBServer_MySQL(ObjectId id, const std::string& prefix ):
DBServer(id), DBServer(id),
db(new MySQLInterface()),
PingTime(300000), PingTime(300000),
ReconnectTime(180000), ReconnectTime(180000),
connect_ok(false), connect_ok(false),
...@@ -48,6 +47,8 @@ DBServer_MySQL::DBServer_MySQL(ObjectId id): ...@@ -48,6 +47,8 @@ DBServer_MySQL::DBServer_MySQL(ObjectId id):
qbufSize(200), qbufSize(200),
lastRemove(false) lastRemove(false)
{ {
db = make_shared<MySQLInterface>();
if( getId() == DefaultObjectId ) if( getId() == DefaultObjectId )
{ {
ostringstream msg; ostringstream msg;
...@@ -55,12 +56,12 @@ DBServer_MySQL::DBServer_MySQL(ObjectId id): ...@@ -55,12 +56,12 @@ DBServer_MySQL::DBServer_MySQL(ObjectId id):
throw Exception(msg.str()); throw Exception(msg.str());
} }
mqbuf.setName(myname + "_qbufMutex"); mqbuf.setName(myname + "_qbufMutex");
} }
DBServer_MySQL::DBServer_MySQL(): DBServer_MySQL::DBServer_MySQL():
DBServer(uniset_conf()->getDBServer()), DBServer(uniset_conf()->getDBServer()),
db(new MySQLInterface()),
PingTime(300000), PingTime(300000),
ReconnectTime(180000), ReconnectTime(180000),
connect_ok(false), connect_ok(false),
...@@ -68,6 +69,7 @@ DBServer_MySQL::DBServer_MySQL(): ...@@ -68,6 +69,7 @@ DBServer_MySQL::DBServer_MySQL():
qbufSize(200), qbufSize(200),
lastRemove(false) lastRemove(false)
{ {
db = make_shared<MySQLInterface>();
// init(); // init();
if( getId() == DefaultObjectId ) if( getId() == DefaultObjectId )
{ {
...@@ -81,11 +83,8 @@ DBServer_MySQL::DBServer_MySQL(): ...@@ -81,11 +83,8 @@ DBServer_MySQL::DBServer_MySQL():
//-------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------
DBServer_MySQL::~DBServer_MySQL() DBServer_MySQL::~DBServer_MySQL()
{ {
if( db != NULL ) if( db )
{
db->close(); db->close();
delete db;
}
} }
//-------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------
void DBServer_MySQL::sysCommand( const UniSetTypes::SystemMessage *sm ) void DBServer_MySQL::sysCommand( const UniSetTypes::SystemMessage *sm )
...@@ -329,7 +328,7 @@ void DBServer_MySQL::init_dbserver() ...@@ -329,7 +328,7 @@ void DBServer_MySQL::init_dbserver()
} }
} }
//-------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------
void DBServer_MySQL::createTables( MySQLInterface *db ) void DBServer_MySQL::createTables( std::shared_ptr<MySQLInterface>& db )
{ {
auto conf = uniset_conf(); auto conf = uniset_conf();
...@@ -399,3 +398,34 @@ void DBServer_MySQL::timerInfo( const UniSetTypes::TimerMessage* tm ) ...@@ -399,3 +398,34 @@ void DBServer_MySQL::timerInfo( const UniSetTypes::TimerMessage* tm )
} }
} }
//-------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------
std::shared_ptr<DBServer_MySQL> DBServer_MySQL::init_dbserver( int argc, const char* const* argv,
const std::string& prefix )
{
auto conf = uniset_conf();
ObjectId ID = conf->getDBServer();
string name = conf->getArgParam("--" + prefix + "-name","");
if( !name.empty() )
{
ObjectId ID = conf->getObjectID(name);
if( ID == UniSetTypes::DefaultObjectId )
{
ucrit << "(DBServer_SQLite): Unknown ObjectID for '" << name << endl;
return 0;
}
}
uinfo << "(DBServer_MySQL): name = " << name << "(" << ID << ")" << endl;
return make_shared<DBServer_MySQL>(ID,prefix);
}
// -----------------------------------------------------------------------------
void DBServer_MySQL::help_print( int argc, const char* const* argv )
{
auto conf = uniset_conf();
cout << "Default: prefix='mysql'" << endl;
cout << "--prefix-name objectID - ObjectID. Default: 'conf->getDBServer()'" << endl;
}
// -----------------------------------------------------------------------------
...@@ -135,16 +135,22 @@ class DBServer_MySQL: ...@@ -135,16 +135,22 @@ class DBServer_MySQL:
public DBServer public DBServer
{ {
public: public:
DBServer_MySQL( UniSetTypes::ObjectId id ); DBServer_MySQL( UniSetTypes::ObjectId id, const std::string& prefix );
DBServer_MySQL(); DBServer_MySQL();
~DBServer_MySQL(); ~DBServer_MySQL();
static const Debug::type DBLogInfoLevel = Debug::LEVEL9; static const Debug::type DBLogInfoLevel = Debug::LEVEL9;
/*! глобальная функция для инициализации объекта */
static std::shared_ptr<DBServer_MySQL> init_dbserver( int argc, const char* const* argv, const std::string& prefix="mysql" );
/*! глобальная функция для вывода help-а */
static void help_print( int argc, const char* const* argv );
protected: protected:
typedef std::map<int, std::string> DBTableMap; typedef std::map<int, std::string> DBTableMap;
virtual void initDB(MySQLInterface *db){}; virtual void initDB(std::shared_ptr<MySQLInterface>& db){};
virtual void initDBTableMap(DBTableMap& tblMap){}; virtual void initDBTableMap(DBTableMap& tblMap){};
virtual void timerInfo( const UniSetTypes::TimerMessage* tm ) override; virtual void timerInfo( const UniSetTypes::TimerMessage* tm ) override;
...@@ -154,7 +160,7 @@ class DBServer_MySQL: ...@@ -154,7 +160,7 @@ class DBServer_MySQL:
bool writeToBase( const string& query ); bool writeToBase( const string& query );
virtual void init_dbserver(); virtual void init_dbserver();
void createTables( MySQLInterface* db ); void createTables( std::shared_ptr<MySQLInterface>& db );
inline const char* tblName(int key) inline const char* tblName(int key)
{ {
...@@ -169,7 +175,7 @@ class DBServer_MySQL: ...@@ -169,7 +175,7 @@ class DBServer_MySQL:
}; };
MySQLInterface *db; std::shared_ptr<MySQLInterface> db;
int PingTime; int PingTime;
int ReconnectTime; int ReconnectTime;
bool connect_ok; /*! признак наличия соеднинения с сервером БД */ bool connect_ok; /*! признак наличия соеднинения с сервером БД */
......
...@@ -6,11 +6,6 @@ ...@@ -6,11 +6,6 @@
using namespace UniSetTypes; using namespace UniSetTypes;
using namespace std; using namespace std;
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
static void short_usage()
{
cout << "Usage: uniset-mysql-dbserver [--name ObjectId] [--confile configure.xml]\n";
}
// --------------------------------------------------------------------------
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
std::ios::sync_with_stdio(false); std::ios::sync_with_stdio(false);
...@@ -18,53 +13,27 @@ int main(int argc, char** argv) ...@@ -18,53 +13,27 @@ int main(int argc, char** argv)
{ {
if( argc > 1 && !strcmp(argv[1],"--help") ) if( argc > 1 && !strcmp(argv[1],"--help") )
{ {
short_usage(); DBServer_MySQL::help_print(argc,argv);
return 0; return 0;
} }
auto conf = uniset_init(argc,argv,"configure.xml"); auto conf = uniset_init(argc,argv,"configure.xml");
ObjectId ID = conf->getDBServer(); auto dbs = DBServer_MySQL::init_dbserver(argc,argv);
// определяем ID объекта
string name = conf->getArgParam("--name");
if( !name.empty())
{
if( ID != UniSetTypes::DefaultObjectId )
{
uwarn << "(DBServer::main): переопределяем ID заданный в "
<< conf->getConfFileName() << endl;
}
ID = conf->oind->getIdByName(conf->getServicesSection()+"/"+name);
if( ID == UniSetTypes::DefaultObjectId )
{
cerr << "(DBServer::main): идентификатор '" << name
<< "' не найден в конф. файле!"
<< " в секции " << conf->getServicesSection() << endl;
return 1;
}
}
else if( ID == UniSetTypes::DefaultObjectId )
{
cerr << "(DBServer::main): Не удалось определить ИДЕНТИФИКАТОР сервера" << endl;
short_usage();
return 1;
}
DBServer_MySQL dbs(ID);
auto act = UniSetActivator::Instance(); auto act = UniSetActivator::Instance();
act->add(dbs.get_ptr());
act->add(dbs);
act->run(false); act->run(false);
return 0;
} }
catch( const std::exception& ex ) catch( const std::exception& ex )
{ {
cerr << "(DBServer::main): " << ex.what() << endl; cerr << "(DBServer_MySQL::main): " << ex.what() << endl;
} }
catch(...) catch(...)
{ {
cerr << "(DBServer::main): catch ..." << endl; cerr << "(DBServer_MySQL::main): catch ..." << endl;
} }
return 0; return 1;
} }
...@@ -33,15 +33,13 @@ ...@@ -33,15 +33,13 @@
#include "Debug.h" #include "Debug.h"
#include "UniXML.h" #include "UniXML.h"
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
#define dblog if( ulog()->debugging(DBLogInfoLevel) ) (*(ulog().get()))[DBLogInfoLevel]
using namespace UniSetTypes; using namespace UniSetTypes;
using namespace std; using namespace std;
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
const Debug::type DBLEVEL = Debug::LEVEL1; #define dblog if( ulog()->debugging(DBLogInfoLevel) ) ulog()->debug(DBLogInfoLevel)
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
DBServer_SQLite::DBServer_SQLite( ObjectId id ): DBServer_SQLite::DBServer_SQLite( ObjectId id, const std::string& prefix ):
DBServer(id), DBServer(id),
db(new SQLiteInterface()),
PingTime(300000), PingTime(300000),
ReconnectTime(180000), ReconnectTime(180000),
connect_ok(false), connect_ok(false),
...@@ -49,6 +47,8 @@ DBServer_SQLite::DBServer_SQLite( ObjectId id ): ...@@ -49,6 +47,8 @@ DBServer_SQLite::DBServer_SQLite( ObjectId id ):
qbufSize(200), qbufSize(200),
lastRemove(false) lastRemove(false)
{ {
db = make_shared<SQLiteInterface>();
if( getId() == DefaultObjectId ) if( getId() == DefaultObjectId )
{ {
ostringstream msg; ostringstream msg;
...@@ -59,7 +59,6 @@ DBServer_SQLite::DBServer_SQLite( ObjectId id ): ...@@ -59,7 +59,6 @@ DBServer_SQLite::DBServer_SQLite( ObjectId id ):
DBServer_SQLite::DBServer_SQLite(): DBServer_SQLite::DBServer_SQLite():
DBServer(uniset_conf()->getDBServer()), DBServer(uniset_conf()->getDBServer()),
db(new SQLiteInterface()),
PingTime(300000), PingTime(300000),
ReconnectTime(180000), ReconnectTime(180000),
connect_ok(false), connect_ok(false),
...@@ -67,6 +66,7 @@ DBServer_SQLite::DBServer_SQLite(): ...@@ -67,6 +66,7 @@ DBServer_SQLite::DBServer_SQLite():
qbufSize(200), qbufSize(200),
lastRemove(false) lastRemove(false)
{ {
db = make_shared<SQLiteInterface>();
// init(); // init();
if( getId() == DefaultObjectId ) if( getId() == DefaultObjectId )
{ {
...@@ -78,12 +78,8 @@ DBServer_SQLite::DBServer_SQLite(): ...@@ -78,12 +78,8 @@ DBServer_SQLite::DBServer_SQLite():
//-------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------
DBServer_SQLite::~DBServer_SQLite() DBServer_SQLite::~DBServer_SQLite()
{ {
if( db != NULL ) if(db)
{
db->close(); db->close();
delete db;
db = 0;
}
} }
//-------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------
void DBServer_SQLite::sysCommand( const UniSetTypes::SystemMessage *sm ) void DBServer_SQLite::sysCommand( const UniSetTypes::SystemMessage *sm )
...@@ -308,7 +304,7 @@ void DBServer_SQLite::init_dbserver() ...@@ -308,7 +304,7 @@ void DBServer_SQLite::init_dbserver()
} }
} }
//-------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------
void DBServer_SQLite::createTables( SQLiteInterface *db ) void DBServer_SQLite::createTables( std::shared_ptr<SQLiteInterface>& db )
{ {
auto conf = uniset_conf(); auto conf = uniset_conf();
UniXML::iterator it( conf->getNode("Tables") ); UniXML::iterator it( conf->getNode("Tables") );
...@@ -377,3 +373,34 @@ void DBServer_SQLite::timerInfo( const UniSetTypes::TimerMessage* tm ) ...@@ -377,3 +373,34 @@ void DBServer_SQLite::timerInfo( const UniSetTypes::TimerMessage* tm )
} }
} }
//-------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------
std::shared_ptr<DBServer_SQLite> DBServer_SQLite::init_dbserver( int argc, const char* const* argv,
const std::string& prefix )
{
auto conf = uniset_conf();
ObjectId ID = conf->getDBServer();
string name = conf->getArgParam("--" + prefix + "-name","");
if( !name.empty() )
{
ObjectId ID = conf->getObjectID(name);
if( ID == UniSetTypes::DefaultObjectId )
{
ucrit << "(DBServer_SQLite): Unknown ObjectID for '" << name << endl;
return 0;
}
}
uinfo << "(DBServer_SQLite): name = " << name << "(" << ID << ")" << endl;
return make_shared<DBServer_SQLite>(ID,prefix);
}
// -----------------------------------------------------------------------------
void DBServer_SQLite::help_print( int argc, const char* const* argv )
{
auto conf = uniset_conf();
cout << "Default: prefix='sqlite'" << endl;
cout << "--prefix-name objectID - ObjectID. Default: 'conf->getDBServer()'" << endl;
}
// -----------------------------------------------------------------------------
...@@ -135,16 +135,22 @@ class DBServer_SQLite: ...@@ -135,16 +135,22 @@ class DBServer_SQLite:
public DBServer public DBServer
{ {
public: public:
DBServer_SQLite( UniSetTypes::ObjectId id ); DBServer_SQLite( UniSetTypes::ObjectId id, const std::string& prefix );
DBServer_SQLite(); DBServer_SQLite();
~DBServer_SQLite(); ~DBServer_SQLite();
static const Debug::type DBLogInfoLevel = Debug::LEVEL9; static const Debug::type DBLogInfoLevel = Debug::LEVEL9;
/*! глобальная функция для инициализации объекта */
static std::shared_ptr<DBServer_SQLite> init_dbserver( int argc, const char* const* argv, const std::string& prefix="sqlite" );
/*! глобальная функция для вывода help-а */
static void help_print( int argc, const char* const* argv );
protected: protected:
typedef std::map<int, std::string> DBTableMap; typedef std::map<int, std::string> DBTableMap;
virtual void initDB( SQLiteInterface *db ){}; virtual void initDB( std::shared_ptr<SQLiteInterface>& db ){};
virtual void initDBTableMap(DBTableMap& tblMap){}; virtual void initDBTableMap(DBTableMap& tblMap){};
virtual void timerInfo( const UniSetTypes::TimerMessage* tm ) override; virtual void timerInfo( const UniSetTypes::TimerMessage* tm ) override;
...@@ -154,7 +160,7 @@ class DBServer_SQLite: ...@@ -154,7 +160,7 @@ class DBServer_SQLite:
bool writeToBase( const string& query ); bool writeToBase( const string& query );
virtual void init_dbserver(); virtual void init_dbserver();
void createTables( SQLiteInterface* db ); void createTables( std::shared_ptr<SQLiteInterface>& db );
inline const char* tblName(int key) inline const char* tblName(int key)
{ {
...@@ -169,7 +175,7 @@ class DBServer_SQLite: ...@@ -169,7 +175,7 @@ class DBServer_SQLite:
}; };
SQLiteInterface *db; std::shared_ptr<SQLiteInterface> db;
int PingTime; int PingTime;
int ReconnectTime; int ReconnectTime;
bool connect_ok; /*! признак наличия соеднинения с сервером БД */ bool connect_ok; /*! признак наличия соеднинения с сервером БД */
......
...@@ -6,11 +6,6 @@ ...@@ -6,11 +6,6 @@
using namespace UniSetTypes; using namespace UniSetTypes;
using namespace std; using namespace std;
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
static void short_usage()
{
cout << "Usage: uniset-mysql-dbserver [--name ObjectId] [--confile configure.xml]\n";
}
// --------------------------------------------------------------------------
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
std::ios::sync_with_stdio(false); std::ios::sync_with_stdio(false);
...@@ -18,54 +13,27 @@ int main(int argc, char** argv) ...@@ -18,54 +13,27 @@ int main(int argc, char** argv)
{ {
if( argc > 1 && !strcmp(argv[1],"--help") ) if( argc > 1 && !strcmp(argv[1],"--help") )
{ {
short_usage(); DBServer_SQLite::help_print(argc,argv);
return 0; return 0;
} }
auto conf = uniset_init(argc,argv,"configure.xml"); auto conf = uniset_init(argc,argv,"configure.xml");
ObjectId ID = conf->getDBServer(); auto dbs = DBServer_SQLite::init_dbserver(argc,argv);
// определяем ID объекта
string name = conf->getArgParam("--name");
if( !name.empty())
{
if( ID != UniSetTypes::DefaultObjectId )
{
uwarn << "(DBServer::main): переопределяем ID заданнй в "
<< conf->getConfFileName() << endl;
}
ID = conf->oind->getIdByName(conf->getServicesSection()+"/"+name);
if( ID == UniSetTypes::DefaultObjectId )
{
cerr << "(DBServer::main): идентификатор '" << name
<< "' не найден в конф. файле!"
<< " в секции " << conf->getServicesSection() << endl;
return 1;
}
}
else if( ID == UniSetTypes::DefaultObjectId )
{
cerr << "(DBServer::main): Не удалось определить ИДЕНТИФИКАТОР сервера" << endl;
short_usage();
return 1;
}
DBServer_SQLite dbs(ID);
auto act = UniSetActivator::Instance(); auto act = UniSetActivator::Instance();
act->add(dbs.get_ptr());
act->add(dbs);
act->run(false); act->run(false);
return 0;
} }
catch( const std::exception& ex ) catch( const std::exception& ex )
{ {
cerr << "(DBServer::main): " << ex.what() << endl; cerr << "(DBServer_SQLite::main): " << ex.what() << endl;
} }
catch(...) catch(...)
{ {
cerr << "(DBServer::main): catch ..." << endl; cerr << "(DBServer_SQLite::main): catch ..." << endl;
} }
return 0; return 1;
} }
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