1 | // |
2 | // SessionFactory.cpp |
3 | // |
4 | // Library: Data |
5 | // Package: DataCore |
6 | // Module: SessionFactory |
7 | // |
8 | // Copyright (c) 2006, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/Data/SessionFactory.h" |
16 | #include "Poco/URI.h" |
17 | #include "Poco/String.h" |
18 | |
19 | |
20 | namespace Poco { |
21 | namespace Data { |
22 | |
23 | |
24 | SessionFactory::SessionFactory() |
25 | { |
26 | } |
27 | |
28 | |
29 | SessionFactory::~SessionFactory() |
30 | { |
31 | } |
32 | |
33 | |
34 | SessionFactory& SessionFactory::instance() |
35 | { |
36 | static SessionFactory sf; |
37 | return sf; |
38 | } |
39 | |
40 | |
41 | void SessionFactory::add(Connector* pIn) |
42 | { |
43 | Poco::FastMutex::ScopedLock lock(_mutex); |
44 | SessionInfo info(pIn); |
45 | std::pair<Connectors::iterator, bool> res = |
46 | _connectors.insert(std::make_pair(pIn->name(), info)); |
47 | if (!res.second) res.first->second.cnt++; |
48 | } |
49 | |
50 | |
51 | void SessionFactory::remove(const std::string& key) |
52 | { |
53 | Poco::FastMutex::ScopedLock lock(_mutex); |
54 | Connectors::iterator it = _connectors.find(key); |
55 | poco_assert (_connectors.end() != it); |
56 | |
57 | --(it->second.cnt); |
58 | if (it->second.cnt == 0) _connectors.erase(it); |
59 | } |
60 | |
61 | |
62 | Session SessionFactory::create(const std::string& key, |
63 | const std::string& connectionString, |
64 | std::size_t timeout) |
65 | { |
66 | Poco::SharedPtr<Connector> ptrSI; |
67 | { |
68 | Poco::FastMutex::ScopedLock lock(_mutex); |
69 | Connectors::iterator it = _connectors.find(key); |
70 | if (_connectors.end() == it) throw Poco::NotFoundException(key); |
71 | ptrSI = it->second.ptrSI; |
72 | } |
73 | return Session(ptrSI->createSession(connectionString, timeout)); |
74 | } |
75 | |
76 | |
77 | Session SessionFactory::create(const std::string& uri, |
78 | std::size_t timeout) |
79 | { |
80 | URI u(uri); |
81 | poco_assert (!u.getPath().empty()); |
82 | return create(u.getScheme(), u.getPath().substr(1), timeout); |
83 | } |
84 | |
85 | |
86 | SessionFactory::SessionInfo::SessionInfo(Connector* pSI): |
87 | cnt(1), |
88 | ptrSI(pSI) |
89 | { |
90 | } |
91 | |
92 | |
93 | } } // namespace Poco::Data |
94 | |