mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-13 14:45:36 +02:00
auto-init for db back-ends
Auto initialization for all DB back-ends and some tidy-up for network windows auto initialization
This commit is contained in:
@@ -44,6 +44,12 @@
|
||||
#include "Poco/Data/Connector.h"
|
||||
|
||||
|
||||
// Note: to avoid static (de)initialization problems,
|
||||
// during connector automatic (un)registration, it is
|
||||
// best to have this as a macro.
|
||||
#define POCO_DATA_MYSQL_CONNECTOR_NAME "mysql"
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Data {
|
||||
namespace MySQL {
|
||||
@@ -80,4 +86,55 @@ public:
|
||||
} } } // namespace Poco::Data::MySQL
|
||||
|
||||
|
||||
//
|
||||
// Automatic Connector registration
|
||||
//
|
||||
|
||||
struct MySQL_API MySQLConnectorRegistrator
|
||||
/// Connector registering class.
|
||||
/// A global instance of this class is instantiated
|
||||
/// with sole purpose to automatically register the
|
||||
/// MySQL connector with central Poco Data registry.
|
||||
{
|
||||
MySQLConnectorRegistrator()
|
||||
/// Calls Poco::Data::MySQL::registerConnector();
|
||||
{
|
||||
Poco::Data::MySQL::Connector::registerConnector();
|
||||
}
|
||||
|
||||
~MySQLConnectorRegistrator()
|
||||
/// Calls Poco::Data::MySQL::unregisterConnector();
|
||||
{
|
||||
Poco::Data::MySQL::Connector::unregisterConnector();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#if !defined(POCO_NO_AUTOMATIC_LIB_INIT)
|
||||
#if defined(POCO_OS_FAMILY_WINDOWS)
|
||||
extern "C" const struct MySQL_API MySQLConnectorRegistrator pocoMySQLConnectorRegistrator;
|
||||
#if defined(MySQL_EXPORTS)
|
||||
#if defined(_WIN64)
|
||||
#define POCO_DATA_MYSQL_FORCE_SYMBOL(s) __pragma(comment (linker, "/export:"#s))
|
||||
#elif defined(_WIN32)
|
||||
#define POCO_DATA_MYSQL_FORCE_SYMBOL(s) __pragma(comment (linker, "/export:_"#s))
|
||||
#endif
|
||||
#else // !MySQL_EXPORTS
|
||||
#if defined(_WIN64)
|
||||
#define POCO_DATA_MYSQL_FORCE_SYMBOL(s) __pragma(comment (linker, "/include:"#s))
|
||||
#elif defined(_WIN32)
|
||||
#define POCO_DATA_MYSQL_FORCE_SYMBOL(s) __pragma(comment (linker, "/include:_"#s))
|
||||
#endif
|
||||
#endif // MySQL_EXPORTS
|
||||
#else // !POCO_OS_FAMILY_WINDOWS
|
||||
#define POCO_DATA_MYSQL_FORCE_SYMBOL(s) extern "C" const struct MySQLConnectorRegistrator s;
|
||||
#endif // POCO_OS_FAMILY_WINDOWS
|
||||
POCO_DATA_MYSQL_FORCE_SYMBOL(pocoMySQLConnectorRegistrator)
|
||||
#endif // POCO_NO_AUTOMATIC_LIB_INIT
|
||||
|
||||
//
|
||||
// End automatic Connector registration
|
||||
//
|
||||
|
||||
|
||||
#endif // Data_MySQL_Connector_INCLUDED
|
||||
|
@@ -38,16 +38,18 @@
|
||||
#include "Poco/Data/MySQL/SessionImpl.h"
|
||||
#include "Poco/Data/SessionFactory.h"
|
||||
#include "Poco/Exception.h"
|
||||
|
||||
#include <mysql.h>
|
||||
|
||||
|
||||
const MySQLConnectorRegistrator pocoMySQLConnectorRegistrator;
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Data {
|
||||
namespace MySQL {
|
||||
|
||||
|
||||
std::string Connector::KEY("mysql");
|
||||
std::string Connector::KEY(POCO_DATA_MYSQL_CONNECTOR_NAME);
|
||||
|
||||
|
||||
Connector::Connector()
|
||||
@@ -61,7 +63,8 @@ Connector::~Connector()
|
||||
|
||||
const std::string& Connector::name() const
|
||||
{
|
||||
return KEY;
|
||||
static const std::string n(POCO_DATA_MYSQL_CONNECTOR_NAME);
|
||||
return n;
|
||||
}
|
||||
|
||||
Poco::AutoPtr<Poco::Data::SessionImpl> Connector::createSession(const std::string& connectionString,
|
||||
@@ -84,7 +87,7 @@ void Connector::registerConnector()
|
||||
|
||||
void Connector::unregisterConnector()
|
||||
{
|
||||
Poco::Data::SessionFactory::instance().remove(KEY);
|
||||
Poco::Data::SessionFactory::instance().remove(POCO_DATA_MYSQL_CONNECTOR_NAME);
|
||||
mysql_library_end();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user