Skip to content

Commit 14bb2a3

Browse files
committed
add doxygen
1 parent 0f286d8 commit 14bb2a3

File tree

19 files changed

+1347
-42
lines changed

19 files changed

+1347
-42
lines changed

include/vix/db/Database.hpp

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
* @file Database.hpp
44
* @author Gaspard Kirira
55
*
6-
* Copyright 2025, Gaspard Kirira. All rights reserved.
6+
* Copyright 2025, Gaspard Kirira.
7+
* All rights reserved.
78
* https://github.com/vixcpp/vix
9+
*
810
* Use of this source code is governed by a MIT license
911
* that can be found in the License file.
1012
*
@@ -23,51 +25,136 @@ namespace vix::config
2325

2426
namespace vix::db
2527
{
28+
/**
29+
* @brief Supported database engines.
30+
*/
2631
enum class Engine
2732
{
33+
/// MySQL-compatible database engine
2834
MySQL,
35+
36+
/// SQLite embedded database engine
2937
SQLite
3038
};
3139

40+
/**
41+
* @brief Configuration parameters for a MySQL database.
42+
*/
3243
struct MySQLConfig
3344
{
45+
/// Database host
3446
std::string host;
47+
48+
/// Username
3549
std::string user;
50+
51+
/// Password
3652
std::string password;
53+
54+
/// Database name
3755
std::string database;
56+
57+
/// Connection pool configuration
3858
PoolConfig pool{};
3959
};
4060

61+
/**
62+
* @brief Configuration parameters for a SQLite database.
63+
*/
4164
struct SQLiteConfig
4265
{
66+
/// Path to the SQLite database file
4367
std::string path;
68+
69+
/// Connection pool configuration
4470
PoolConfig pool{};
4571
};
4672

73+
/**
74+
* @brief Unified database configuration.
75+
*
76+
* Holds engine-specific configuration while exposing a single
77+
* entry point for database initialization.
78+
*/
4779
struct DbConfig
4880
{
81+
/// Selected database engine
4982
Engine engine{Engine::MySQL};
83+
84+
/// MySQL-specific configuration
5085
MySQLConfig mysql{};
86+
87+
/// SQLite-specific configuration
5188
SQLiteConfig sqlite{};
5289
};
5390

91+
/**
92+
* @brief Build a database configuration from a Vix configuration.
93+
*
94+
* Extracts database-related settings from a vix::config::Config
95+
* instance and maps them to a DbConfig structure.
96+
*
97+
* @param cfg Vix configuration object.
98+
* @return Parsed database configuration.
99+
*/
54100
DbConfig make_db_config_from_vix_config(const vix::config::Config &cfg);
55101

102+
/**
103+
* @brief High-level database facade.
104+
*
105+
* Database owns the connection pool and exposes access to it,
106+
* providing a unified entry point for database access within
107+
* the application.
108+
*
109+
* Engine selection and driver wiring are performed at construction
110+
* time based on the provided DbConfig.
111+
*/
56112
class Database
57113
{
58114
public:
115+
/**
116+
* @brief Construct a database instance.
117+
*
118+
* Initializes the underlying connection pool according to
119+
* the selected engine and configuration.
120+
*
121+
* @param cfg Database configuration.
122+
*/
59123
explicit Database(const DbConfig &cfg);
60124

125+
/**
126+
* @brief Return the selected database engine.
127+
*
128+
* @return Database engine.
129+
*/
61130
Engine engine() const noexcept { return cfg_.engine; }
131+
132+
/**
133+
* @brief Access the database configuration.
134+
*
135+
* @return Database configuration.
136+
*/
62137
const DbConfig &config() const noexcept { return cfg_; }
63138

139+
/**
140+
* @brief Access the connection pool.
141+
*
142+
* @return Reference to the connection pool.
143+
*/
64144
ConnectionPool &pool() noexcept { return pool_; }
145+
146+
/**
147+
* @brief Access the connection pool (const).
148+
*
149+
* @return Const reference to the connection pool.
150+
*/
65151
const ConnectionPool &pool() const noexcept { return pool_; }
66152

67153
private:
68154
DbConfig cfg_;
69155
ConnectionPool pool_;
70156
};
157+
71158
} // namespace vix::db
72159

73-
#endif
160+
#endif // VIX_DB_DATABASE_HPP

include/vix/db/Sha256.hpp

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
* @file Sha256.hpp
44
* @author Gaspard Kirira
55
*
6-
* Copyright 2025, Gaspard Kirira. All rights reserved.
6+
* Copyright 2025, Gaspard Kirira.
7+
* All rights reserved.
78
* https://github.com/vixcpp/vix
9+
*
810
* Use of this source code is governed by a MIT license
911
* that can be found in the License file.
1012
*
@@ -20,33 +22,103 @@
2022

2123
namespace vix::db
2224
{
25+
/**
26+
* @brief Incremental SHA-256 hash calculator.
27+
*
28+
* Sha256 provides a minimal, self-contained implementation of the
29+
* SHA-256 hashing algorithm. It supports incremental updates and
30+
* produces a 256-bit digest.
31+
*
32+
* This utility is primarily intended for internal database-related
33+
* features such as migration checksums.
34+
*/
2335
class Sha256
2436
{
2537
public:
38+
/**
39+
* @brief Construct a new SHA-256 hasher.
40+
*
41+
* The hasher is initialized in a reset state.
42+
*/
2643
Sha256() { reset(); }
2744

45+
/**
46+
* @brief Reset the internal state.
47+
*
48+
* After calling reset(), the hasher can be reused to compute
49+
* a new digest.
50+
*/
2851
void reset();
52+
53+
/**
54+
* @brief Update the hash with raw binary data.
55+
*
56+
* @param data Pointer to the input buffer.
57+
* @param len Number of bytes to process.
58+
*/
2959
void update(const void *data, std::size_t len);
60+
61+
/**
62+
* @brief Update the hash with string data.
63+
*
64+
* @param s Input string view.
65+
*/
3066
void update(std::string_view s) { update(s.data(), s.size()); }
3167

68+
/**
69+
* @brief Finalize the hash and return the digest.
70+
*
71+
* Calling this function finalizes the computation and returns
72+
* the 32-byte SHA-256 digest.
73+
*
74+
* @return SHA-256 digest.
75+
*/
3276
std::array<std::uint8_t, 32> digest();
77+
78+
/**
79+
* @brief Convert a SHA-256 digest to a hexadecimal string.
80+
*
81+
* @param d Digest bytes.
82+
* @return Lowercase hexadecimal representation.
83+
*/
3384
static std::string hex(std::array<std::uint8_t, 32> d);
3485

3586
private:
87+
/**
88+
* @brief Process a single 512-bit message block.
89+
*
90+
* @param block Input block.
91+
*/
3692
void transform(const std::uint8_t block[64]);
3793

94+
/// Total number of processed bits
3895
std::uint64_t bitlen_ = 0;
96+
97+
/// Internal hash state
3998
std::array<std::uint32_t, 8> state_{};
99+
100+
/// Partial message buffer
40101
std::array<std::uint8_t, 64> buffer_{};
102+
103+
/// Number of bytes currently stored in the buffer
41104
std::size_t buffer_len_ = 0;
42105
};
43106

107+
/**
108+
* @brief Compute a SHA-256 hash and return it as hexadecimal.
109+
*
110+
* Convenience helper for hashing a single string.
111+
*
112+
* @param s Input string.
113+
* @return SHA-256 digest encoded as hexadecimal.
114+
*/
44115
inline std::string sha256_hex(std::string_view s)
45116
{
46117
Sha256 h;
47118
h.update(s);
48119
return Sha256::hex(h.digest());
49120
}
50-
}
51121

52-
#endif
122+
} // namespace vix::db
123+
124+
#endif // VIX_DB_SHA256_HPP

include/vix/db/Transaction.hpp

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
* @file Transaction.hpp
44
* @author Gaspard Kirira
55
*
6-
* Copyright 2025, Gaspard Kirira. All rights reserved.
6+
* Copyright 2025, Gaspard Kirira.
7+
* All rights reserved.
78
* https://github.com/vixcpp/vix
9+
*
810
* Use of this source code is governed by a MIT license
911
* that can be found in the License file.
1012
*
@@ -17,18 +19,40 @@
1719

1820
namespace vix::db
1921
{
22+
/**
23+
* @brief RAII wrapper for database transactions.
24+
*
25+
* Transaction acquires a connection from a ConnectionPool and
26+
* starts a database transaction on construction. If the transaction
27+
* is still active when the object is destroyed, it is automatically
28+
* rolled back.
29+
*
30+
* This ensures strong exception safety and prevents leaked
31+
* transactions.
32+
*/
2033
class Transaction
2134
{
2235
PooledConn pooled_;
2336
bool active_ = true;
2437

2538
public:
39+
/**
40+
* @brief Begin a new transaction using a pooled connection.
41+
*
42+
* @param pool Connection pool from which to acquire a connection.
43+
*/
2644
explicit Transaction(ConnectionPool &pool)
2745
: pooled_(pool)
2846
{
2947
pooled_.get().begin();
3048
}
3149

50+
/**
51+
* @brief Roll back the transaction if still active.
52+
*
53+
* The destructor never throws. Any exception raised during rollback
54+
* is swallowed to preserve stack unwinding.
55+
*/
3256
~Transaction() noexcept
3357
{
3458
if (!active_)
@@ -45,6 +69,14 @@ namespace vix::db
4569
Transaction(const Transaction &) = delete;
4670
Transaction &operator=(const Transaction &) = delete;
4771

72+
/**
73+
* @brief Move-construct a transaction.
74+
*
75+
* Ownership of the active transaction is transferred to the
76+
* new instance. The source transaction becomes inactive.
77+
*
78+
* @param other Transaction to move from.
79+
*/
4880
Transaction(Transaction &&other) noexcept
4981
: pooled_(std::move(other.pooled_)), active_(other.active_)
5082
{
@@ -53,18 +85,34 @@ namespace vix::db
5385

5486
Transaction &operator=(Transaction &&) = delete;
5587

88+
/**
89+
* @brief Commit the transaction.
90+
*
91+
* After commit(), the transaction becomes inactive and will
92+
* not be rolled back on destruction.
93+
*/
5694
void commit()
5795
{
5896
pooled_.get().commit();
5997
active_ = false;
6098
}
6199

100+
/**
101+
* @brief Roll back the transaction explicitly.
102+
*
103+
* After rollback(), the transaction becomes inactive.
104+
*/
62105
void rollback()
63106
{
64107
pooled_.get().rollback();
65108
active_ = false;
66109
}
67110

111+
/**
112+
* @brief Access the underlying database connection.
113+
*
114+
* @return Reference to the active connection.
115+
*/
68116
Connection &conn() { return pooled_.get(); }
69117
};
70118

0 commit comments

Comments
 (0)