Skip to content

Commit c175890

Browse files
authored
Merge pull request #20 from DmitriiBurnaev/Add-support-for-char-type
Add support for char type
2 parents e131a87 + fc5d30c commit c175890

File tree

6 files changed

+292
-1
lines changed

6 files changed

+292
-1
lines changed

src/oatpp-postgresql/mapping/Deserializer.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ oatpp::Void Deserializer::deserializeString(const Deserializer* _this, const InD
156156

157157
switch(data.oid) {
158158
case TEXTOID:
159+
case CHAROID:
160+
case BPCHAROID:
159161
case VARCHAROID: return oatpp::String(data.data, data.size);
160162
}
161163

test/CMakeLists.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ add_executable(module-tests
2222
oatpp-postgresql/types/InterpretationTest.hpp
2323
oatpp-postgresql/types/IntTest.cpp
2424
oatpp-postgresql/types/IntTest.hpp
25+
oatpp-postgresql/types/CharacterTest.cpp
26+
oatpp-postgresql/types/CharacterTest.hpp
2527
oatpp-postgresql/tests.cpp
2628
)
2729

@@ -49,4 +51,4 @@ target_link_libraries(module-tests
4951

5052
## TODO link dependencies here (if some)
5153

52-
add_test(module-tests module-tests)
54+
add_test(module-tests module-tests)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
DROP TABLE IF EXISTS test_characters;
2+
3+
CREATE TABLE test_characters (
4+
f_char "char",
5+
f_bpchar char,
6+
f_bpchar4 char(4),
7+
f_varchar varchar(4),
8+
f_text text
9+
);
10+
11+
INSERT INTO test_characters
12+
(f_char, f_bpchar, f_bpchar4, f_varchar, f_text) VALUES (null, null, null, null, null);
13+
14+
INSERT INTO test_characters
15+
(f_char, f_bpchar, f_bpchar4, f_varchar, f_text) VALUES ('#', '$', '%', '^', '&');
16+
17+
INSERT INTO test_characters
18+
(f_char, f_bpchar, f_bpchar4, f_varchar, f_text) VALUES ('a', 'b', 'cccc', 'dddd', 'eeeee');

test/oatpp-postgresql/tests.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "types/IntTest.hpp"
66
#include "types/FloatTest.hpp"
77
#include "types/InterpretationTest.hpp"
8+
#include "types/CharacterTest.hpp"
89

910

1011
#include "oatpp-postgresql/orm.hpp"
@@ -40,6 +41,7 @@ void runTests() {
4041
OATPP_RUN_TEST(oatpp::test::postgresql::types::FloatTest);
4142
OATPP_RUN_TEST(oatpp::test::postgresql::types::ArrayTest);
4243
OATPP_RUN_TEST(oatpp::test::postgresql::types::InterpretationTest);
44+
OATPP_RUN_TEST(oatpp::test::postgresql::types::CharacterTest);
4345

4446
}
4547

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
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 "CharacterTest.hpp"
26+
27+
#include "oatpp-postgresql/orm.hpp"
28+
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
29+
30+
#include <limits>
31+
#include <cstdio>
32+
33+
namespace oatpp { namespace test { namespace postgresql { namespace types {
34+
35+
namespace {
36+
37+
#include OATPP_CODEGEN_BEGIN(DTO)
38+
39+
class Row : public oatpp::DTO {
40+
41+
DTO_INIT(Row, DTO);
42+
43+
DTO_FIELD(String, f_char);
44+
DTO_FIELD(String, f_bpchar);
45+
DTO_FIELD(String, f_bpchar4);
46+
DTO_FIELD(String, f_varchar);
47+
DTO_FIELD(String, f_text);
48+
49+
};
50+
51+
#include OATPP_CODEGEN_END(DTO)
52+
53+
#include OATPP_CODEGEN_BEGIN(DbClient)
54+
55+
class MyClient : public oatpp::orm::DbClient {
56+
public:
57+
58+
MyClient(const std::shared_ptr<oatpp::orm::Executor>& executor)
59+
: oatpp::orm::DbClient(executor)
60+
{
61+
executeQuery("DROP TABLE IF EXISTS oatpp_schema_version_CharacterTest;", {});
62+
oatpp::orm::SchemaMigration migration(executor, "CharacterTest");
63+
migration.addFile(1, TEST_DB_MIGRATION "CharacterTest.sql");
64+
migration.migrate();
65+
66+
auto version = executor->getSchemaVersion("CharacterTest");
67+
OATPP_LOGD("DbClient", "Migration - OK. Version=%d.", version);
68+
69+
}
70+
71+
QUERY(insertValues,
72+
"INSERT INTO test_characters "
73+
"(f_char, f_bpchar, f_bpchar4, f_varchar, f_text) "
74+
"VALUES "
75+
"(:row.f_char, :row.f_bpchar, :row.f_bpchar4, :row.f_varchar, :row.f_text);",
76+
PARAM(oatpp::Object<Row>, row), PREPARE(true))
77+
78+
QUERY(deleteValues,
79+
"DELETE FROM test_characters;")
80+
81+
QUERY(selectValues, "SELECT * FROM test_characters;")
82+
83+
};
84+
85+
#include OATPP_CODEGEN_END(DbClient)
86+
87+
}
88+
89+
void CharacterTest::onRun() {
90+
91+
OATPP_LOGI(TAG, "DB-URL='%s'", TEST_DB_URL);
92+
93+
auto connectionProvider = std::make_shared<oatpp::postgresql::ConnectionProvider>(TEST_DB_URL);
94+
auto executor = std::make_shared<oatpp::postgresql::Executor>(connectionProvider);
95+
96+
auto client = MyClient(executor);
97+
98+
{
99+
auto res = client.selectValues();
100+
if(res->isSuccess()) {
101+
OATPP_LOGD(TAG, "OK, knownCount=%d, hasMore=%d", res->getKnownCount(), res->hasMoreToFetch());
102+
} else {
103+
auto message = res->getErrorMessage();
104+
OATPP_LOGD(TAG, "Error, message=%s", message->c_str());
105+
}
106+
107+
auto dataset = res->fetch<oatpp::Vector<oatpp::Object<Row>>>();
108+
109+
oatpp::parser::json::mapping::ObjectMapper om;
110+
om.getSerializer()->getConfig()->useBeautifier = true;
111+
om.getSerializer()->getConfig()->enabledInterpretations = {"postgresql"};
112+
113+
auto str = om.writeToString(dataset);
114+
115+
OATPP_LOGD(TAG, "res=%s", str->c_str());
116+
117+
OATPP_ASSERT(dataset->size() == 3);
118+
119+
{
120+
auto row = dataset[0];
121+
OATPP_ASSERT(row->f_char == nullptr);
122+
OATPP_ASSERT(row->f_bpchar == nullptr);
123+
OATPP_ASSERT(row->f_bpchar4 == nullptr);
124+
OATPP_ASSERT(row->f_varchar == nullptr);
125+
OATPP_ASSERT(row->f_text == nullptr);
126+
}
127+
128+
{
129+
auto row = dataset[1];
130+
OATPP_ASSERT(row->f_char == "#");
131+
OATPP_ASSERT(row->f_bpchar == "$");
132+
OATPP_ASSERT(row->f_bpchar4 == "% ");
133+
OATPP_ASSERT(row->f_varchar == "^");
134+
OATPP_ASSERT(row->f_text == "&");
135+
}
136+
137+
{
138+
auto row = dataset[2];
139+
OATPP_ASSERT(row->f_char == "a");
140+
OATPP_ASSERT(row->f_bpchar == "b");
141+
OATPP_ASSERT(row->f_bpchar4 == "cccc");
142+
OATPP_ASSERT(row->f_varchar == "dddd");
143+
OATPP_ASSERT(row->f_text == "eeeee");
144+
}
145+
146+
147+
}
148+
149+
{
150+
auto res = client.deleteValues();
151+
if (res->isSuccess()) {
152+
OATPP_LOGD(TAG, "OK, knownCount=%d, hasMore=%d", res->getKnownCount(), res->hasMoreToFetch());
153+
} else {
154+
auto message = res->getErrorMessage();
155+
OATPP_LOGD(TAG, "Error, message=%s", message->c_str());
156+
}
157+
158+
OATPP_ASSERT(res->isSuccess());
159+
}
160+
161+
{
162+
auto connection = client.getConnection();
163+
{
164+
auto row = Row::createShared();
165+
row->f_char = nullptr;
166+
row->f_bpchar = nullptr;
167+
row->f_bpchar4= nullptr;
168+
row->f_varchar = nullptr;
169+
row->f_text = nullptr;
170+
client.insertValues(row, connection);
171+
}
172+
173+
{
174+
auto row = Row::createShared();
175+
row->f_char = "a";
176+
row->f_bpchar = "b";
177+
row->f_bpchar4= "ccc";
178+
row->f_varchar = "dddd";
179+
row->f_text = "eeeee";
180+
client.insertValues(row, connection);
181+
}
182+
}
183+
184+
{
185+
auto res = client.selectValues();
186+
if(res->isSuccess()) {
187+
OATPP_LOGD(TAG, "OK, knownCount=%d, hasMore=%d", res->getKnownCount(), res->hasMoreToFetch());
188+
} else {
189+
auto message = res->getErrorMessage();
190+
OATPP_LOGD(TAG, "Error, message=%s", message->c_str());
191+
}
192+
193+
auto dataset = res->fetch<oatpp::Vector<oatpp::Object<Row>>>();
194+
195+
oatpp::parser::json::mapping::ObjectMapper om;
196+
om.getSerializer()->getConfig()->useBeautifier = true;
197+
om.getSerializer()->getConfig()->enabledInterpretations = {"postgresql"};
198+
199+
auto str = om.writeToString(dataset);
200+
201+
OATPP_LOGD(TAG, "res=%s", str->c_str());
202+
203+
OATPP_ASSERT(dataset->size() == 2);
204+
205+
{
206+
auto row = dataset[0];
207+
OATPP_ASSERT(row->f_char == nullptr);
208+
OATPP_ASSERT(row->f_bpchar == nullptr);
209+
OATPP_ASSERT(row->f_bpchar4 == nullptr);
210+
OATPP_ASSERT(row->f_varchar == nullptr);
211+
OATPP_ASSERT(row->f_text == nullptr);
212+
}
213+
214+
{
215+
auto row = dataset[1];
216+
OATPP_ASSERT(row->f_char == "a");
217+
OATPP_ASSERT(row->f_bpchar == "b");
218+
OATPP_ASSERT(row->f_bpchar4 == "ccc ");
219+
OATPP_ASSERT(row->f_varchar == "dddd");
220+
OATPP_ASSERT(row->f_text == "eeeee");
221+
}
222+
223+
}
224+
225+
}
226+
227+
}}}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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_test_postgresql_types_CharacterTest_hpp
26+
#define oatpp_test_postgresql_types_CharacterTest_hpp
27+
28+
#include "oatpp-test/UnitTest.hpp"
29+
30+
namespace oatpp { namespace test { namespace postgresql { namespace types {
31+
32+
class CharacterTest : public UnitTest {
33+
public:
34+
CharacterTest() : UnitTest("TEST[postgresql::types::CharacterTest]") {}
35+
void onRun() override;
36+
};
37+
38+
}}}}
39+
40+
#endif // oatpp_test_postgresql_types_CharacterTest_hpp

0 commit comments

Comments
 (0)