Skip to content

Commit 5aab360

Browse files
committed
add doxygen
1 parent cc90727 commit 5aab360

File tree

6 files changed

+276
-14
lines changed

6 files changed

+276
-14
lines changed

include/vix/orm/Entity.hpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
* @file Entity.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
*
@@ -18,10 +20,21 @@
1820

1921
namespace vix::orm
2022
{
23+
/**
24+
* @brief Base class for ORM entities.
25+
*
26+
* Entity is the common base type for all ORM-managed objects.
27+
* It provides a polymorphic interface and a virtual destructor,
28+
* allowing entities to be handled via base pointers or references.
29+
*
30+
* This type intentionally contains no data or behavior and serves
31+
* purely as a semantic marker and extension point for the ORM.
32+
*/
2133
struct Entity
2234
{
2335
virtual ~Entity() = default;
2436
};
25-
} // namespace Vix::orm
37+
38+
} // namespace vix::orm
2639

2740
#endif // VIX_ENTITY_HPP

include/vix/orm/Mapper.hpp

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
* @file Mapper.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,23 +25,72 @@
2325
namespace vix::orm
2426
{
2527
/**
26-
* User-specialized mapper.
28+
* @brief User-specialized mapper for ORM entities.
29+
*
30+
* Mapper<T> defines how an ORM entity of type @p T is:
31+
* - constructed from a database result row
32+
* - converted into parameters for INSERT statements
33+
* - converted into parameters for UPDATE statements
34+
*
35+
* This template is intended to be fully specialized by the user
36+
* for each entity type.
2737
*
2838
* Example:
39+
* @code
2940
* template<>
30-
* struct Mapper<User> { ... };
41+
* struct Mapper<User>
42+
* {
43+
* static User fromRow(const vix::db::ResultRow &row);
44+
*
45+
* static std::vector<std::pair<std::string, std::any>>
46+
* toInsertParams(const User &u);
47+
*
48+
* static std::vector<std::pair<std::string, std::any>>
49+
* toUpdateParams(const User &u);
50+
* };
51+
* @endcode
3152
*/
3253
template <class T>
3354
struct Mapper
3455
{
56+
/**
57+
* @brief Construct an entity instance from a database row.
58+
*
59+
* This method is called when materializing query results
60+
* into ORM entities.
61+
*
62+
* @param row Database result row.
63+
* @return Constructed entity instance.
64+
*/
3565
static T fromRow(const vix::db::ResultRow &row);
3666

67+
/**
68+
* @brief Produce parameters for an INSERT statement.
69+
*
70+
* Returns a list of column/value pairs representing the
71+
* fields to be inserted. Values are stored as std::any
72+
* and later converted to DbValue by the ORM.
73+
*
74+
* @param v Entity instance.
75+
* @return List of column/value pairs.
76+
*/
3777
static std::vector<std::pair<std::string, std::any>>
3878
toInsertParams(const T &v);
3979

80+
/**
81+
* @brief Produce parameters for an UPDATE statement.
82+
*
83+
* Returns a list of column/value pairs representing the
84+
* fields to be updated. Typically excludes immutable
85+
* fields such as primary keys.
86+
*
87+
* @param v Entity instance.
88+
* @return List of column/value pairs.
89+
*/
4090
static std::vector<std::pair<std::string, std::any>>
4191
toUpdateParams(const T &v);
4292
};
93+
4394
} // namespace vix::orm
4495

4596
#endif // VIX_MAPPER_HPP

include/vix/orm/QueryBuilder.hpp

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
* @file QueryBuilder.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
*
1113
* Vix.cpp
1214
*/
13-
1415
#ifndef VIX_QUERY_BUILDER
1516
#define VIX_QUERY_BUILDER
1617

@@ -22,36 +23,77 @@
2223

2324
namespace vix::orm
2425
{
26+
/**
27+
* @brief Lightweight SQL query builder with bound parameters.
28+
*
29+
* QueryBuilder helps construct SQL statements incrementally while
30+
* keeping parameters separated from the SQL string. Parameters are
31+
* collected as DbValue instances and are intended to be bound to a
32+
* prepared statement by the ORM or DB layer.
33+
*
34+
* This class does not attempt to validate SQL syntax and intentionally
35+
* avoids abstraction leakage. It is designed to be minimal, explicit,
36+
* and composable.
37+
*/
2538
class QueryBuilder
2639
{
2740
std::string sql_;
2841
std::vector<vix::db::DbValue> params_;
2942

3043
public:
44+
/**
45+
* @brief Append raw SQL text.
46+
*
47+
* @param s SQL fragment.
48+
* @return Reference to this builder.
49+
*/
3150
QueryBuilder &raw(std::string_view s)
3251
{
3352
sql_.append(s);
3453
return *this;
3554
}
3655

56+
/**
57+
* @brief Append a single space character.
58+
*
59+
* Convenience helper for readable chaining.
60+
*
61+
* @return Reference to this builder.
62+
*/
3763
QueryBuilder &space()
3864
{
3965
sql_.push_back(' ');
4066
return *this;
4167
}
4268

69+
/**
70+
* @brief Append a parameter value.
71+
*
72+
* The parameter is stored and expected to be bound to the
73+
* next positional placeholder by the caller.
74+
*
75+
* @param v Database value.
76+
* @return Reference to this builder.
77+
*/
4378
QueryBuilder &param(const vix::db::DbValue &v)
4479
{
4580
params_.push_back(v);
4681
return *this;
4782
}
4883

84+
/**
85+
* @brief Append a parameter value (move).
86+
*
87+
* @param v Database value.
88+
* @return Reference to this builder.
89+
*/
4990
QueryBuilder &param(vix::db::DbValue &&v)
5091
{
5192
params_.push_back(std::move(v));
5293
return *this;
5394
}
5495

96+
/// Convenience overloads for common C++ types
5597
QueryBuilder &param(std::int64_t v) { return param(vix::db::i64(v)); }
5698
QueryBuilder &param(int v) { return param(vix::db::i64(static_cast<std::int64_t>(v))); }
5799
QueryBuilder &param(std::uint64_t v) { return param(vix::db::i64(static_cast<std::int64_t>(v))); }
@@ -61,11 +103,28 @@ namespace vix::orm
61103
QueryBuilder &param(std::string v) { return param(vix::db::str(std::move(v))); }
62104
QueryBuilder &param(const char *v) { return param(vix::db::str(std::string(v ? v : ""))); }
63105

106+
/**
107+
* @brief Append a NULL parameter.
108+
*
109+
* @return Reference to this builder.
110+
*/
64111
QueryBuilder &paramNull() { return param(vix::db::null()); }
65112

113+
/**
114+
* @brief Access the constructed SQL string.
115+
*
116+
* @return SQL string.
117+
*/
66118
const std::string &sql() const { return sql_; }
119+
120+
/**
121+
* @brief Access the collected parameters.
122+
*
123+
* @return Vector of bound parameters.
124+
*/
67125
const std::vector<vix::db::DbValue> &params() const { return params_; }
68126
};
127+
69128
} // namespace vix::orm
70129

71130
#endif // VIX_QUERY_BUILDER

include/vix/orm/Repository.hpp

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
* @file Repository.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
*
@@ -25,13 +27,28 @@
2527

2628
namespace vix::orm
2729
{
30+
/**
31+
* @brief Generic repository for ORM entities.
32+
*
33+
* BaseRepository<T> provides a minimal CRUD interface for an
34+
* ORM entity mapped to a single database table.
35+
*
36+
* Assumptions:
37+
* - The underlying table uses a primary key column named "id"
38+
* - The Mapper<T> specialization defines how the entity is
39+
* serialized/deserialized
40+
*
41+
* This class intentionally avoids complex query abstraction
42+
* and focuses on predictable, explicit SQL.
43+
*/
2844
template <class T>
2945
class BaseRepository
3046
{
3147
vix::db::ConnectionPool &pool_;
3248
std::string table_;
3349

34-
static std::string build_insert_cols(const std::vector<std::pair<std::string, std::any>> &params)
50+
static std::string
51+
build_insert_cols(const std::vector<std::pair<std::string, std::any>> &params)
3552
{
3653
std::string cols;
3754
cols.reserve(64);
@@ -59,7 +76,8 @@ namespace vix::orm
5976
return qs;
6077
}
6178

62-
static std::string build_update_set(const std::vector<std::pair<std::string, std::any>> &params)
79+
static std::string
80+
build_update_set(const std::vector<std::pair<std::string, std::any>> &params)
6381
{
6482
std::string set;
6583
set.reserve(128);
@@ -75,9 +93,23 @@ namespace vix::orm
7593
}
7694

7795
public:
96+
/**
97+
* @brief Construct a repository bound to a table.
98+
*
99+
* @param pool Connection pool used for queries.
100+
* @param table Database table name.
101+
*/
78102
BaseRepository(vix::db::ConnectionPool &pool, std::string table)
79103
: pool_(pool), table_(std::move(table)) {}
80104

105+
/**
106+
* @brief Insert a new entity.
107+
*
108+
* Uses Mapper<T>::toInsertParams to build the INSERT statement.
109+
*
110+
* @param v Entity instance.
111+
* @return Generated primary key (last insert id).
112+
*/
81113
std::uint64_t create(const T &v)
82114
{
83115
const auto params = Mapper<T>::toInsertParams(v);
@@ -98,6 +130,12 @@ namespace vix::orm
98130
return pc.get().lastInsertId();
99131
}
100132

133+
/**
134+
* @brief Find an entity by its primary key.
135+
*
136+
* @param id Primary key value.
137+
* @return Entity instance if found, otherwise std::nullopt.
138+
*/
101139
std::optional<T> findById(std::int64_t id)
102140
{
103141
const std::string sql =
@@ -111,10 +149,18 @@ namespace vix::orm
111149
if (!rs || !rs->next())
112150
return std::nullopt;
113151

114-
// Mapper<T>::fromRow expects a ResultRow
115152
return Mapper<T>::fromRow(rs->row());
116153
}
117154

155+
/**
156+
* @brief Update an entity by its primary key.
157+
*
158+
* Uses Mapper<T>::toUpdateParams to generate column assignments.
159+
*
160+
* @param id Primary key value.
161+
* @param v Entity instance.
162+
* @return Number of affected rows.
163+
*/
118164
std::uint64_t updateById(std::int64_t id, const T &v)
119165
{
120166
const auto params = Mapper<T>::toUpdateParams(v);
@@ -134,6 +180,12 @@ namespace vix::orm
134180
return st->exec();
135181
}
136182

183+
/**
184+
* @brief Delete an entity by its primary key.
185+
*
186+
* @param id Primary key value.
187+
* @return Number of affected rows.
188+
*/
137189
std::uint64_t removeById(std::int64_t id)
138190
{
139191
vix::db::PooledConn pc(pool_);

0 commit comments

Comments
 (0)