|
| 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 | +}}}} |
0 commit comments