Commit dc2419df authored by Vinogradov Aleksei's avatar Vinogradov Aleksei

Сделал создание DBInterface через std::shared_ptr

parent 5243d3d8
......@@ -40,7 +40,14 @@ MySQLInterface::MySQLInterface():
MySQLInterface::~MySQLInterface()
{
close();
try
{
close();
}
catch( ... ) // пропускаем все необработанные исключения, если требуется обработать нужно вызывать close() до деструктора
{
cerr << "MySQLInterface::~MySQLInterface(): an error occured while closing connection!" << endl;
}
delete mysql;
}
......@@ -194,13 +201,8 @@ void MySQLInterface::makeResult(DBResult& dbres, MYSQL_RES* myres, bool finalize
mysql_free_result(myres);
}
// -----------------------------------------------------------------------------------------
extern "C" DBInterface* create_mysqlinterface()
{
return new MySQLInterface();
}
// -----------------------------------------------------------------------------------------
extern "C" void destroy_mysqlinterface(DBInterface* p)
extern "C" std::shared_ptr<DBInterface> create_mysqlinterface()
{
delete p;
return std::shared_ptr<DBInterface>(new MySQLInterface(), DBInterfaceDeleter());
}
// -----------------------------------------------------------------------------------------
......@@ -19,7 +19,14 @@ PostgreSQLInterface::PostgreSQLInterface():
PostgreSQLInterface::~PostgreSQLInterface()
{
close();
try
{
close();
}
catch( ... ) // пропускаем все необработанные исключения, если требуется обработать нужно вызывать close() до деструктора
{
cerr << "MySQLInterface::~MySQLInterface(): an error occured while closing connection!" << endl;
}
}
// -----------------------------------------------------------------------------------------
......@@ -167,13 +174,8 @@ void PostgreSQLInterface::makeResult(DBResult& dbres, const pqxx::result& res )
}
}
// -----------------------------------------------------------------------------------------
extern "C" DBInterface* create_postgresqlinterface()
{
return new PostgreSQLInterface();
}
// -----------------------------------------------------------------------------------------
extern "C" void destroy_postgresqlinterface(DBInterface* p)
extern "C" std::shared_ptr<DBInterface> create_postgresqlinterface()
{
delete p;
return std::shared_ptr<DBInterface>(new PostgreSQLInterface(), DBInterfaceDeleter());
}
// -----------------------------------------------------------------------------------------
......@@ -43,7 +43,14 @@ SQLiteInterface::SQLiteInterface():
SQLiteInterface::~SQLiteInterface()
{
close();
try
{
close();
}
catch( ... ) // пропускаем все необработанные исключения, если требуется обработать нужно вызывать close() до деструктора
{
cerr << "MySQLInterface::~MySQLInterface(): an error occured while closing connection!" << endl;
}
}
// -----------------------------------------------------------------------------------------
......@@ -254,13 +261,8 @@ void SQLiteInterface::makeResult(DBResult& dbres, sqlite3_stmt* s, bool finalize
sqlite3_finalize(s);
}
// -----------------------------------------------------------------------------------------
extern "C" DBInterface* create_sqliteinterface()
{
return new SQLiteInterface();
}
// -----------------------------------------------------------------------------------------
extern "C" void destroy_sqliteinterface(DBInterface* p)
extern "C" std::shared_ptr<DBInterface> create_sqliteinterface()
{
delete p;
return std::shared_ptr<DBInterface>(new SQLiteInterface(), DBInterfaceDeleter());
}
// -----------------------------------------------------------------------------------------
......@@ -77,9 +77,17 @@ class DBResult
ROW row_;
};
// ----------------------------------------------------------------------------------
struct DBInterfaceDeleter
{
void operator()(DBInterface* p) const
{
try{ delete p; } catch(...) {}
}
};
// ----------------------------------------------------------------------------------
// the types of the class factories
typedef DBInterface* create_dbinterface_t();
typedef void destroy_dbinterface_t(DBInterface*);
typedef std::shared_ptr<DBInterface> create_dbinterface_t();
// --------------------------------------------------------------------------
#endif // DBInterface_H_
// --------------------------------------------------------------------------
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