Skip to content

Commit 7328832

Browse files
committed
ConnectionProvider and ConnectionPool
1 parent 5befa02 commit 7328832

File tree

9 files changed

+237
-37
lines changed

9 files changed

+237
-37
lines changed

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ add_library(${OATPP_THIS_MODULE_NAME}
1515
oatpp-postgresql/ql_template/TemplateValueProvider.hpp
1616
oatpp-postgresql/Connection.cpp
1717
oatpp-postgresql/Connection.hpp
18+
oatpp-postgresql/ConnectionProvider.cpp
19+
oatpp-postgresql/ConnectionProvider.hpp
1820
oatpp-postgresql/Executor.cpp
1921
oatpp-postgresql/Executor.hpp
2022
oatpp-postgresql/QueryResult.cpp

src/oatpp-postgresql/Connection.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,25 @@
2626

2727
namespace oatpp { namespace postgresql {
2828

29-
Connection::Connection(PGconn* connection)
29+
ConnectionImpl::ConnectionImpl(PGconn* connection)
3030
: m_connection(connection)
3131
{}
3232

33-
Connection::~Connection() {
33+
ConnectionImpl::~ConnectionImpl() {
3434
if(m_connection != nullptr) {
3535
PQfinish(m_connection);
3636
}
3737
}
3838

39-
PGconn* Connection::getHandle() {
39+
PGconn* ConnectionImpl::getHandle() {
4040
return m_connection;
4141
}
4242

43-
void Connection::setPrepared(const oatpp::String& statementName) {
43+
void ConnectionImpl::setPrepared(const oatpp::String& statementName) {
4444
m_prepared.insert(statementName);
4545
}
4646

47-
bool Connection::isPrepared(const oatpp::String& statementName) {
47+
bool ConnectionImpl::isPrepared(const oatpp::String& statementName) {
4848
auto it = m_prepared.find(statementName);
4949
return it != m_prepared.end();
5050
}

src/oatpp-postgresql/Connection.hpp

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,57 @@
2626
#define oatpp_postgresql_Connection_hpp
2727

2828
#include "oatpp/orm/Connection.hpp"
29+
#include "oatpp/core/provider/Pool.hpp"
2930
#include "oatpp/core/Types.hpp"
3031

3132
#include <libpq-fe.h>
3233

3334
namespace oatpp { namespace postgresql {
3435

3536
class Connection : public orm::Connection {
37+
public:
38+
39+
virtual PGconn* getHandle() = 0;
40+
41+
virtual void setPrepared(const oatpp::String& statementName) = 0;
42+
virtual bool isPrepared(const oatpp::String& statementName) = 0;
43+
44+
};
45+
46+
class ConnectionImpl : public Connection {
3647
private:
3748
PGconn* m_connection;
3849
std::unordered_set<oatpp::String> m_prepared;
3950
public:
4051

41-
Connection(PGconn* connection);
42-
~Connection();
52+
ConnectionImpl(PGconn* connection);
53+
~ConnectionImpl();
54+
55+
PGconn* getHandle() override;
56+
57+
void setPrepared(const oatpp::String& statementName) override;
58+
bool isPrepared(const oatpp::String& statementName) override;
59+
60+
};
61+
62+
struct ConnectionAcquisitionProxy : public provider::AcquisitionProxy<Connection, ConnectionAcquisitionProxy> {
63+
64+
ConnectionAcquisitionProxy(const std::shared_ptr<Connection> &resource,
65+
const std::shared_ptr<PoolInstance> &pool)
66+
: provider::AcquisitionProxy<Connection, ConnectionAcquisitionProxy>(resource, pool)
67+
{}
68+
69+
PGconn* getHandle() override {
70+
return _obj->getHandle();
71+
}
4372

44-
PGconn* getHandle();
73+
void setPrepared(const oatpp::String& statementName) override {
74+
_obj->setPrepared(statementName);
75+
}
4576

46-
void setPrepared(const oatpp::String& statementName);
47-
bool isPrepared(const oatpp::String& statementName);
77+
bool isPrepared(const oatpp::String& statementName) override {
78+
return _obj->isPrepared(statementName);
79+
}
4880

4981
};
5082

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/***************************************************************************
2+
*
3+
* Project _____ __ ____ _ _
4+
* ( _ ) /__\ (_ _)_| |_ _| |_
5+
* )(_)( /(__)\ )( (_ _)(_ _)
6+
* (_____)(__)(__)(__) |_| |_|
7+
*
8+
*
9+
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
10+
*
11+
* Licensed under the Apache License, Version 2.0 (the "License");
12+
* you may not use this file except in compliance with the License.
13+
* You may obtain a copy of the License at
14+
*
15+
* http://www.apache.org/licenses/LICENSE-2.0
16+
*
17+
* Unless required by applicable law or agreed to in writing, software
18+
* distributed under the License is distributed on an "AS IS" BASIS,
19+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20+
* See the License for the specific language governing permissions and
21+
* limitations under the License.
22+
*
23+
***************************************************************************/
24+
25+
#include "ConnectionProvider.hpp"
26+
27+
namespace oatpp { namespace postgresql {
28+
29+
ConnectionProvider::ConnectionProvider(const oatpp::String& connectionString)
30+
: m_connectionString(connectionString)
31+
{}
32+
33+
std::shared_ptr<Connection> ConnectionProvider::get() {
34+
35+
auto handle = PQconnectdb(m_connectionString->c_str());
36+
37+
if(PQstatus(handle) == CONNECTION_BAD) {
38+
std::string errMsg = PQerrorMessage(handle);
39+
PQfinish(handle);
40+
throw std::runtime_error("[oatpp::postgresql::ConnectionProvider::get()]: "
41+
"Error. Can't connect. " + errMsg);
42+
}
43+
44+
OATPP_LOGI("ConnectionProvider", "get()");
45+
return std::make_shared<ConnectionImpl>(handle);
46+
47+
}
48+
49+
async::CoroutineStarterForResult<const std::shared_ptr<Connection>&> ConnectionProvider::getAsync() {
50+
throw std::runtime_error("[oatpp::postgresql::ConnectionProvider::getAsync()]: Error. Not implemented!");
51+
}
52+
53+
void ConnectionProvider::invalidate(const std::shared_ptr<Connection>& resource) {
54+
// DO nothing
55+
}
56+
57+
void ConnectionProvider::stop() {
58+
// DO nothing
59+
}
60+
61+
}}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/***************************************************************************
2+
*
3+
* Project _____ __ ____ _ _
4+
* ( _ ) /__\ (_ _)_| |_ _| |_
5+
* )(_)( /(__)\ )( (_ _)(_ _)
6+
* (_____)(__)(__)(__) |_| |_|
7+
*
8+
*
9+
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
10+
*
11+
* Licensed under the Apache License, Version 2.0 (the "License");
12+
* you may not use this file except in compliance with the License.
13+
* You may obtain a copy of the License at
14+
*
15+
* http://www.apache.org/licenses/LICENSE-2.0
16+
*
17+
* Unless required by applicable law or agreed to in writing, software
18+
* distributed under the License is distributed on an "AS IS" BASIS,
19+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20+
* See the License for the specific language governing permissions and
21+
* limitations under the License.
22+
*
23+
***************************************************************************/
24+
25+
#ifndef oatpp_postgresql_ConnectionProvider_hpp
26+
#define oatpp_postgresql_ConnectionProvider_hpp
27+
28+
#include "Connection.hpp"
29+
30+
#include "oatpp/core/provider/Pool.hpp"
31+
#include "oatpp/core/Types.hpp"
32+
33+
namespace oatpp { namespace postgresql {
34+
35+
/**
36+
* Connection provider.
37+
*/
38+
class ConnectionProvider : public provider::Provider<Connection> {
39+
private:
40+
oatpp::String m_connectionString;
41+
public:
42+
43+
/**
44+
* Constructor.
45+
* @param connectionString
46+
*/
47+
ConnectionProvider(const oatpp::String& connectionString);
48+
49+
/**
50+
* Get Connection.
51+
* @return - resource.
52+
*/
53+
std::shared_ptr<Connection> get() override;
54+
55+
/**
56+
* Get Connection in Async manner.
57+
* @return - &id:oatpp::async::CoroutineStarterForResult; of `Connection`.
58+
*/
59+
async::CoroutineStarterForResult<const std::shared_ptr<Connection>&> getAsync() override;
60+
61+
/**
62+
* Invalidate Connection that was previously created by this provider. <br>
63+
* @param resource
64+
*/
65+
void invalidate(const std::shared_ptr<Connection>& resource) override;
66+
67+
/**
68+
* Stop provider and free associated resources.
69+
*/
70+
void stop() override;
71+
72+
};
73+
74+
/**
75+
* Connection pool. <br>
76+
* - &id:oatpp::postgresql::Connection;.
77+
* - &id:oatpp::postgresql::ConnectionAcquisitionProxy;.
78+
*/
79+
typedef oatpp::provider::Pool<
80+
provider::Provider<Connection>,
81+
Connection,
82+
ConnectionAcquisitionProxy
83+
> ConnectionPool;
84+
85+
}}
86+
87+
#endif // oatpp_postgresql_ConnectionProvider_hpp

src/oatpp-postgresql/Executor.cpp

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@
3535

3636
namespace oatpp { namespace postgresql {
3737

38+
Executor::Executor(const std::shared_ptr<provider::Provider<Connection>>& connectionProvider)
39+
: m_connectionProvider(connectionProvider)
40+
, m_resultMapper(std::make_shared<mapping::ResultMapper>())
41+
{}
42+
43+
3844
std::unique_ptr<Oid[]> Executor::getParamTypes(const StringTemplate& queryTemplate, const ParamsTypeMap& paramsTypeMap) {
3945

4046
std::unique_ptr<Oid[]> result(new Oid[queryTemplate.getTemplateVariables().size()]);
@@ -135,24 +141,24 @@ data::share::StringTemplate Executor::parseQueryTemplate(const oatpp::String& na
135141

136142
std::shared_ptr<orm::Connection> Executor::getConnection() {
137143

138-
oatpp::String dbHost = "localhost";
139-
oatpp::String dbUser = "postgres";
140-
oatpp::String dbPassword = "db-pass";
141-
oatpp::String dbName = "postgres";
142-
143-
oatpp::data::stream::ChunkedBuffer stream;
144-
stream << "host=" << dbHost << " user=" << dbUser << " password=" << dbPassword << " dbname=" << dbName;
145-
auto connStr = stream.toString();
146-
147-
auto handle = PQconnectdb(connStr->c_str());
148-
149-
if(PQstatus(handle) == CONNECTION_BAD) {
150-
OATPP_LOGD("Database", "Connection to database failed: %s\n", PQerrorMessage(handle));
151-
PQfinish(handle);
152-
return nullptr;
153-
}
154-
155-
return std::make_shared<Connection>(handle);
144+
// oatpp::String dbHost = "localhost";
145+
// oatpp::String dbUser = "postgres";
146+
// oatpp::String dbPassword = "db-pass";
147+
// oatpp::String dbName = "postgres";
148+
//
149+
// oatpp::data::stream::ChunkedBuffer stream;
150+
// stream << "host=" << dbHost << " user=" << dbUser << " password=" << dbPassword << " dbname=" << dbName;
151+
// auto connStr = stream.toString();
152+
//
153+
// auto handle = PQconnectdb(connStr->c_str());
154+
//
155+
// if(PQstatus(handle) == CONNECTION_BAD) {
156+
// OATPP_LOGD("Database", "Connection to database failed: %s\n", PQerrorMessage(handle));
157+
// PQfinish(handle);
158+
// return nullptr;
159+
// }
160+
161+
return m_connectionProvider->get();
156162

157163
}
158164

src/oatpp-postgresql/Executor.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#ifndef oatpp_postgresql_Executor_hpp
2626
#define oatpp_postgresql_Executor_hpp
2727

28-
#include "Connection.hpp"
28+
#include "ConnectionProvider.hpp"
2929
#include "QueryResult.hpp"
3030

3131
#include "mapping/Serializer.hpp"
@@ -48,11 +48,14 @@ class Executor : public orm::Executor {
4848
const std::unordered_map<oatpp::String, oatpp::Void>& params,
4949
const std::shared_ptr<postgresql::Connection>& connection);
5050
private:
51+
std::shared_ptr<provider::Provider<Connection>> m_connectionProvider;
52+
std::shared_ptr<mapping::ResultMapper> m_resultMapper;
5153
mapping::TypeMapper m_typeMapper;
5254
mapping::Serializer m_serializer;
53-
std::shared_ptr<mapping::ResultMapper> m_resultMapper = std::make_shared<mapping::ResultMapper>();
5455
public:
5556

57+
Executor(const std::shared_ptr<provider::Provider<Connection>>& connectionProvider);
58+
5659
StringTemplate parseQueryTemplate(const oatpp::String& name,
5760
const oatpp::String& text,
5861
const ParamsTypeMap& paramsTypeMap) override;

src/oatpp-postgresql/QueryResult.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#ifndef oatpp_postgresql_QueryResult_hpp
2626
#define oatpp_postgresql_QueryResult_hpp
2727

28-
#include "Connection.hpp"
28+
#include "ConnectionProvider.hpp"
2929
#include "mapping/Deserializer.hpp"
3030
#include "mapping/ResultMapper.hpp"
3131
#include "oatpp/orm/QueryResult.hpp"

test/oatpp-postgresql/tests.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,16 @@ class Test : public oatpp::test::UnitTest {
9999

100100
void onRun() override {
101101

102-
auto executor = std::make_shared<oatpp::postgresql::Executor>();
102+
oatpp::String connStr = "postgresql://postgres:db-pass@localhost:5432/postgres";
103+
auto connectionProvider = std::make_shared<oatpp::postgresql::ConnectionProvider>(connStr);
104+
auto connectionPool = oatpp::postgresql::ConnectionPool::createShared(
105+
connectionProvider,
106+
10,
107+
std::chrono::seconds(1)
108+
);
109+
110+
auto executor = std::make_shared<oatpp::postgresql::Executor>(connectionPool);
103111
auto client = MyClient(executor);
104-
auto connection = executor->getConnection();
105112

106113
//client.createUser("my-login1", "pass1", "email@email.com1", connection);
107114
//client.createUser("my-login2", "pass2", "email@email.com2", connection);
@@ -112,13 +119,13 @@ class Test : public oatpp::test::UnitTest {
112119
//client.insertFloats(0.32, 0.64, connection);
113120
//client.insertFloats(-0.32, -0.64, connection);
114121

115-
//client.insertStrs("Hello", "World", "Oat++", connection);
116-
//client.insertStrs("Hello", "World", "oatpp", connection);
117-
//client.insertStrs("Yeah", "Ops", "!!!", connection);
122+
//client.insertStrs("Hello", "World", "Oat++");
123+
//client.insertStrs("Hello", "World", "oatpp");
124+
//client.insertStrs("Yeah", "Ops", "!!!");
118125

119126
{
120127

121-
auto res = client.selectStrs(connection);
128+
auto res = client.selectStrs();
122129
OATPP_LOGD(TAG, "OK=%d, count=%d", res->isSuccess(), res->count());
123130

124131
auto dataset = res->fetch<oatpp::Vector<oatpp::Fields<oatpp::Any>>>();
@@ -132,6 +139,8 @@ class Test : public oatpp::test::UnitTest {
132139

133140
}
134141

142+
connectionPool->stop();
143+
135144
}
136145

137146
};

0 commit comments

Comments
 (0)