Line data Source code
1 : /**
2 : * Copyright Soramitsu Co., Ltd. All Rights Reserved.
3 : * SPDX-License-Identifier: Apache-2.0
4 : */
5 :
6 : #include "ametsuchi/impl/postgres_options.hpp"
7 :
8 : #include <cctype>
9 : #include <regex>
10 :
11 : #include <boost/algorithm/string.hpp>
12 :
13 : namespace iroha {
14 : namespace ametsuchi {
15 :
16 : // regex to fetch dbname from pg_opt string
17 : const static std::regex e("\\bdbname=([^ ]*)");
18 :
19 : PostgresOptions::PostgresOptions(const std::string &pg_opt)
20 288 : : pg_opt_(pg_opt) {
21 288 : std::smatch m;
22 :
23 288 : if (std::regex_search(pg_opt_, m, e)) {
24 287 : dbname_ = *(m.end() - 1);
25 :
26 : // get pg_opt_without_db_name_
27 287 : pg_opt_without_db_name_ = m.prefix().str() + m.suffix().str();
28 :
29 : // remove consecutive spaces
30 : auto end =
31 287 : std::unique(pg_opt_without_db_name_.begin(),
32 287 : pg_opt_without_db_name_.end(),
33 : [](char l, char r) {
34 18309 : return std::isspace(l) && std::isspace(r) && l == r;
35 : });
36 287 : pg_opt_without_db_name_ =
37 287 : std::string(pg_opt_without_db_name_.begin(), end);
38 287 : } else {
39 1 : dbname_ = boost::none;
40 1 : pg_opt_without_db_name_ = pg_opt_;
41 : }
42 288 : }
43 :
44 : std::string PostgresOptions::optionsString() const {
45 2 : return pg_opt_;
46 : }
47 :
48 : std::string PostgresOptions::optionsStringWithoutDbName() const {
49 571 : return pg_opt_without_db_name_;
50 : }
51 :
52 : boost::optional<std::string> PostgresOptions::dbname() const {
53 856 : return dbname_;
54 : }
55 :
56 : } // namespace ametsuchi
57 : } // namespace iroha
|