Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3d4f842

Browse files
issue #24 - insert DTO type ENUM as string
1 parent 812572b commit 3d4f842

File tree

5 files changed

+307
-8
lines changed

5 files changed

+307
-8
lines changed

‎src/oatpp-postgresql/mapping/Serializer.cpp‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,23 @@ void Serializer::serializeEnum(const Serializer* _this, OutputData& outData, con
399399
const auto& enumInterpretation = polymorphicDispatcher->toInterpretation(polymorph, false, e);
400400

401401
if(e == data::type::EnumInterpreterError::OK) {
402-
_this->serialize(outData, enumInterpretation);
403-
return;
402+
if (enumInterpretation &&
403+
enumInterpretation.getValueType()->classId == data::type::__class::String::CLASS_ID)
404+
{
405+
std::string* buff = static_cast<std::string*>(enumInterpretation.get());
406+
outData.dataBuffer.reset(new char[buff->size()]);
407+
outData.data = outData.dataBuffer.get();
408+
outData.dataSize = buff->size();
409+
outData.dataFormat = 1;
410+
outData.oid = TEXTOID;
411+
412+
std::memcpy(outData.data, buff->data(), outData.dataSize);
413+
}
414+
else
415+
{
416+
_this->serialize(outData, enumInterpretation);
417+
}
418+
return;
404419
}
405420

406421
switch(e) {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
DROP TABLE IF EXISTS test_enumasstring;
2+
3+
CREATE TABLE test_enumasstring (
4+
f_enumint int,
5+
f_enumstring varchar(256)
6+
);
7+
8+
INSERT INTO test_enumasstring
9+
(f_enumint, f_enumstring) VALUES (null, null);
10+
11+
INSERT INTO test_enumasstring
12+
(f_enumint, f_enumstring) VALUES (0, 'dog');
13+
14+
INSERT INTO test_enumasstring
15+
(f_enumint, f_enumstring) VALUES (1, 'cat');

‎test/oatpp-postgresql/tests.cpp‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "types/FloatTest.hpp"
77
#include "types/InterpretationTest.hpp"
88
#include "types/CharacterTest.hpp"
9+
#include "types/EnumAsStringTest.hpp"
910

1011

1112
#include "oatpp-postgresql/orm.hpp"
@@ -35,14 +36,15 @@ void runTests() {
3536
std::this_thread::sleep_for(std::chrono::seconds(10));
3637
}
3738

38-
OATPP_RUN_TEST(oatpp::test::postgresql::ql_template::ParserTest);
39+
//OATPP_RUN_TEST(oatpp::test::postgresql::ql_template::ParserTest);
3940

40-
OATPP_RUN_TEST(oatpp::test::postgresql::types::IntTest);
41-
OATPP_RUN_TEST(oatpp::test::postgresql::types::FloatTest);
42-
OATPP_RUN_TEST(oatpp::test::postgresql::types::ArrayTest);
43-
OATPP_RUN_TEST(oatpp::test::postgresql::types::InterpretationTest);
44-
OATPP_RUN_TEST(oatpp::test::postgresql::types::CharacterTest);
41+
//OATPP_RUN_TEST(oatpp::test::postgresql::types::IntTest);
42+
//OATPP_RUN_TEST(oatpp::test::postgresql::types::FloatTest);
43+
//OATPP_RUN_TEST(oatpp::test::postgresql::types::ArrayTest);
44+
//OATPP_RUN_TEST(oatpp::test::postgresql::types::InterpretationTest);
45+
//OATPP_RUN_TEST(oatpp::test::postgresql::types::CharacterTest);
4546

47+
OATPP_RUN_TEST(oatpp::test::postgresql::types::EnumAsStringTest);
4648
}
4749

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /