Joedb 9.1.4
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
Connection_Parser.cpp
Go to the documentation of this file.
8
9#ifdef JOEDB_HAS_NETWORKING
11#endif
12
13#ifdef JOEDB_HAS_SSH
15#endif
16
17#include <cstring>
18#include <sstream>
19
20namespace joedb
21{
22 //////////////////////////////////////////////////////////////////////////
24 //////////////////////////////////////////////////////////////////////////
25 {
26 if (local)
27 builders.emplace_back(new Dummy_Connection_Builder());
28
29 builders.emplace_back
30 (
32 );
33
34 builders.emplace_back
35 (
37 );
38
39 builders.emplace_back(new File_Connection_Builder());
40
41#ifdef JOEDB_HAS_NETWORKING
42 builders.emplace_back(new Network_Connection_Builder());
43#endif
44
45#ifdef JOEDB_HAS_SSH
46 builders.emplace_back(new SSH_Connection_Builder());
47#endif
48 }
49
50 //////////////////////////////////////////////////////////////////////////
51 void Connection_Parser::print_help(std::ostream &out) const
52 //////////////////////////////////////////////////////////////////////////
53 {
54 out << "\n<connection> is one of:\n";
55 for (size_t i = 0; i < builders.size(); i++)
56 {
57 out << ' ';
58
59 if (i == 0)
60 out << '[';
61
62 out<< builders[i]->get_name();
63
64 if (i == 0)
65 out << "] (default)";
66
67 out << ' ' << builders[i]->get_parameters_description();
68 out << '\n';
69 }
70 }
71
72 //////////////////////////////////////////////////////////////////////////
73 Connection_Builder &Connection_Parser::get_builder(const char *name) const
74 //////////////////////////////////////////////////////////////////////////
75 {
76 for (const auto &b: builders)
77 {
78 if (std::strcmp(b->get_name(), name) == 0)
79 return *b;
80 }
81
82 std::ostringstream message;
83 message << "Unknown connection type: " << name << '\n';
84 print_help(message);
85 throw Exception(message.str());
86 }
87
88 //////////////////////////////////////////////////////////////////////////
89 Connection &Connection_Parser::build
90 //////////////////////////////////////////////////////////////////////////
91 (
92 Connection_Builder &builder,
93 int argc,
94 char **argv,
95 Buffered_File *file
96 )
97 {
98 if
99 (
100 argc < builder.get_min_parameters() ||
101 argc > builder.get_max_parameters()
102 )
103 {
104 const char * description = builder.get_parameters_description();
105 if (!*description)
106 description = "no parameters";
107 throw Exception
108 (
109 std::string("Wrong number of connection arguments. Expected: ") +
110 std::string(description)
111 );
112 }
113
114 return builder.build(argc, argv, file);
115 }
116
117 //////////////////////////////////////////////////////////////////////////
118 Connection &Connection_Parser::build
119 //////////////////////////////////////////////////////////////////////////
120 (
121 int argc,
122 char **argv,
123 Buffered_File *file
124 ) const
125 {
126 const char * connection_name;
127 if (argc <= 0)
128 {
129 argc = 1;
130 connection_name = builders[0]->get_name();
131 }
132 else
133 connection_name = argv[0];
134
135 std::cerr << "Creating connection (" << connection_name << ") ... ";
136
137 Connection &result = build
138 (
139 get_builder(connection_name),
140 argc - 1,
141 argv + 1,
142 file
143 );
144
145 std::cerr << "OK\n";
146
147 return result;
148 }
149}
void print_help(std::ostream &out) const
Definition Blob.h:7