-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDatabase.cpp
More file actions
231 lines (205 loc) · 5.88 KB
/
Copy pathDatabase.cpp
File metadata and controls
231 lines (205 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
*
* @file Database.cpp
* @author Gaspard Kirira
*
* Copyright 2025, Gaspard Kirira.
* All rights reserved.
* https://github.com/vixcpp/vix
*
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*/
#include <vix/db/Database.hpp>
#if VIX_DB_HAS_MYSQL
#include <vix/db/drivers/mysql/MySQLDriver.hpp>
#endif
#if VIX_DB_HAS_SQLITE
#include <vix/db/drivers/sqlite/SQLiteDriver.hpp>
#endif
#include <vix/config/Config.hpp>
#include <algorithm>
#include <cctype>
#include <memory>
#include <stdexcept>
#include <string>
#include <utility>
namespace vix::db
{
namespace
{
/**
* @brief Convert a string to lowercase using ASCII rules.
*
* @param value Input string.
* @return Lowercased string.
*/
[[nodiscard]] std::string to_lower_ascii(std::string value)
{
std::transform(
value.begin(),
value.end(),
value.begin(),
[](unsigned char c)
{ return static_cast<char>(std::tolower(c)); });
return value;
}
/**
* @brief Build a MySQL TCP host string.
*
* @param host Host name or IP address.
* @param port TCP port.
* @return MySQL Connector/C++ host string.
*/
[[nodiscard]] std::string build_mysql_host_string(
const std::string &host,
int port)
{
return "tcp://" + host + ":" + std::to_string(port);
}
} // namespace
namespace
{
/**
* @brief Create a connection factory for the selected engine.
*
* @param cfg Database configuration.
* @return Connection factory.
*
* @throws std::runtime_error if the requested backend is not enabled.
*/
ConnectionFactory makeFactoryFor(const DbConfig &cfg)
{
switch (cfg.engine)
{
case Engine::MySQL:
{
#if VIX_DB_HAS_MYSQL
return make_mysql_factory(
cfg.mysql.host,
cfg.mysql.user,
cfg.mysql.password,
cfg.mysql.database);
#else
throw std::runtime_error(
"MySQL requested but VIX_DB_HAS_MYSQL=0");
#endif
}
case Engine::SQLite:
{
#if VIX_DB_HAS_SQLITE
return make_sqlite_factory(cfg.sqlite.path);
#else
throw std::runtime_error(
"SQLite requested but VIX_DB_HAS_SQLITE=0");
#endif
}
default:
throw std::runtime_error("Unsupported database engine");
}
}
/**
* @brief Resolve the pool configuration for the selected engine.
*
* @param cfg Database configuration.
* @return Pool configuration for the active backend.
*/
PoolConfig poolConfigFor(const DbConfig &cfg)
{
switch (cfg.engine)
{
case Engine::MySQL:
return cfg.mysql.pool;
case Engine::SQLite:
return cfg.sqlite.pool;
default:
return {};
}
}
/**
* @brief Create and warm up a connection pool.
*
* @param cfg Database configuration.
* @return Shared pointer to the initialized connection pool.
*/
std::shared_ptr<ConnectionPool> makePoolFor(const DbConfig &cfg)
{
auto pool = std::make_shared<ConnectionPool>(
makeFactoryFor(cfg),
poolConfigFor(cfg));
pool->warmup();
return pool;
}
} // namespace
Database::Database(const DbConfig &cfg)
: cfg_(cfg), pool_(makePoolFor(cfg))
{
}
Database::Database(const vix::config::Config &cfg)
: Database([&cfg]()
{
DbConfig out;
const std::string engine_str =
to_lower_ascii(cfg.getString("database.engine", "sqlite"));
if (engine_str == "mysql")
{
out.engine = Engine::MySQL;
}
else if (engine_str == "sqlite")
{
out.engine = Engine::SQLite;
}
else
{
throw std::runtime_error(
"Invalid database.engine: '" + engine_str +
"'. Supported values: mysql, sqlite");
}
const std::string mysql_host =
cfg.getString("database.default.host", "127.0.0.1");
const int mysql_port =
cfg.getInt("database.default.port", 3306);
out.mysql.host = build_mysql_host_string(mysql_host, mysql_port);
out.mysql.user = cfg.getString("database.default.user", "root");
out.mysql.password = cfg.getDbPasswordFromEnv();
out.mysql.database = cfg.getString("database.default.name", "");
out.mysql.pool.min = static_cast<std::size_t>(
std::max(1, cfg.getInt("database.pool.min", 1)));
out.mysql.pool.max = static_cast<std::size_t>(
std::max(static_cast<int>(out.mysql.pool.min),
cfg.getInt("database.pool.max", 8)));
out.sqlite.path = cfg.getString("database.sqlite.path", "vix.db");
out.sqlite.pool.min = out.mysql.pool.min;
out.sqlite.pool.max = out.mysql.pool.max;
return out; }())
{
}
Database Database::mysql(
std::string host,
std::string user,
std::string password,
std::string database,
PoolConfig pool)
{
DbConfig cfg;
cfg.engine = Engine::MySQL;
cfg.mysql.host = std::move(host);
cfg.mysql.user = std::move(user);
cfg.mysql.password = std::move(password);
cfg.mysql.database = std::move(database);
cfg.mysql.pool = pool;
return Database(cfg);
}
Database Database::sqlite(
std::string path,
PoolConfig pool)
{
DbConfig cfg;
cfg.engine = Engine::SQLite;
cfg.sqlite.path = std::move(path);
cfg.sqlite.pool = pool;
return Database(cfg);
}
} // namespace vix::db