1 | // |
---|---|
2 | // MySQLException.cpp |
3 | // |
4 | // Library: SQL/MySQL |
5 | // Package: MySQL |
6 | // Module: Connector |
7 | // |
8 | // Copyright (c) 2008, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/SQL/MySQL/Connector.h" |
16 | #include "Poco/SQL/MySQL/SessionImpl.h" |
17 | #include "Poco/SQL/SessionFactory.h" |
18 | #include "Poco/Exception.h" |
19 | #include <mysql.h> |
20 | |
21 | |
22 | const MySQLConnectorRegistrator pocoMySQLConnectorRegistrator; |
23 | |
24 | |
25 | namespace Poco { |
26 | namespace SQL { |
27 | namespace MySQL { |
28 | |
29 | |
30 | std::string Connector::KEY(POCO_DATA_MYSQL_CONNECTOR_NAME); |
31 | Poco::FastMutex Connector::_mutex; |
32 | |
33 | |
34 | Connector::Connector() |
35 | { |
36 | } |
37 | |
38 | |
39 | Connector::~Connector() |
40 | { |
41 | } |
42 | |
43 | const std::string& Connector::name() const |
44 | { |
45 | static const std::string n(POCO_DATA_MYSQL_CONNECTOR_NAME); |
46 | return n; |
47 | } |
48 | |
49 | Poco::AutoPtr<Poco::SQL::SessionImpl> Connector::createSession(const std::string& connectionString, |
50 | std::size_t timeout) |
51 | { |
52 | static bool initDone = false; |
53 | { |
54 | Poco::FastMutex::ScopedLock l(_mutex); |
55 | if (!initDone) |
56 | { |
57 | if (mysql_library_init(0, 0, 0) != 0) |
58 | { |
59 | throw Exception("mysql_library_init error"); |
60 | } |
61 | initDone = true; |
62 | } |
63 | } |
64 | |
65 | return Poco::AutoPtr<Poco::SQL::SessionImpl>(new Poco::SQL::MySQL::SessionImpl(connectionString, timeout)); |
66 | } |
67 | |
68 | |
69 | void Connector::registerConnector() |
70 | { |
71 | Poco::SQL::SessionFactory::instance().add(new Connector()); |
72 | } |
73 | |
74 | |
75 | void Connector::unregisterConnector() |
76 | { |
77 | Poco::SQL::SessionFactory::instance().remove(POCO_DATA_MYSQL_CONNECTOR_NAME); |
78 | mysql_library_end(); |
79 | } |
80 | |
81 | |
82 | } } } // namespace Poco::SQL::MySQL |
83 | |
84 |