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