Commit e84ed083 authored by Vinogradov Aleksei's avatar Vinogradov Aleksei Committed by Pavel Vainerman

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

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