Skip to content

Commit cab2555

Browse files
committed
orm: stabilize public API, fix examples, and inline templates
- Re-export DB primitives (DBError, make_mysql_factory) via db_compat - Fix all ORM examples to rely only on orm.hpp - Align QueryBuilder params binding with DbValue - Inline template-only components (Mapper, Repository, UnitOfWork) - Remove obsolete .cpp files for header-only ORM parts - Clean CMake and example build consistency
1 parent 8b24512 commit cab2555

17 files changed

Lines changed: 360 additions & 168 deletions

CMakeLists.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,7 @@ set(VIX_ORM_PUBLIC_HEADERS
4848
)
4949

5050
set(VIX_ORM_SOURCES
51-
src/Mapper.cpp
5251
src/QueryBuilder.cpp
53-
src/Repository.cpp
54-
src/UnitOfWork.cpp
5552
)
5653

5754
# ------------------------------------------------------------------------------

examples/batch_insert_tx.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,24 @@
1111
* Vix.cpp
1212
*/
1313
#include <vix/orm/orm.hpp>
14-
#include <vix/orm/ConnectionPool.hpp>
15-
#include <vix/orm/MySQLDriver.hpp>
1614

15+
#include <cstdint>
1716
#include <iostream>
18-
#include <vector>
1917
#include <string>
18+
#include <vector>
2019

2120
using namespace vix::orm;
2221

2322
int main(int argc, char **argv)
2423
{
25-
std::string host = (argc > 1 ? argv[1] : "tcp://127.0.0.1:3306");
26-
std::string user = (argc > 2 ? argv[2] : "root");
27-
std::string pass = (argc > 3 ? argv[3] : "");
28-
std::string db = (argc > 4 ? argv[4] : "vixdb");
24+
const std::string host = (argc > 1 ? argv[1] : "tcp://127.0.0.1:3306");
25+
const std::string user = (argc > 2 ? argv[2] : "root");
26+
const std::string pass = (argc > 3 ? argv[3] : "");
27+
const std::string db = (argc > 4 ? argv[4] : "vixdb");
2928

3029
try
3130
{
31+
// DB factory (MySQL driver)
3232
auto factory = make_mysql_factory(host, user, pass, db);
3333

3434
PoolConfig cfg;
@@ -38,6 +38,7 @@ int main(int argc, char **argv)
3838
ConnectionPool pool{factory, cfg};
3939
pool.warmup();
4040

41+
// Transaction (RAII rollback if not committed)
4142
Transaction tx(pool);
4243
auto &c = tx.conn();
4344

@@ -50,7 +51,7 @@ int main(int argc, char **argv)
5051
int age;
5152
};
5253

53-
std::vector<Row> rows = {
54+
const std::vector<Row> rows = {
5455
{"Zoe", "zoe@example.com", 23},
5556
{"Mina", "mina@example.com", 31},
5657
{"Omar", "omar@example.com", 35},
@@ -69,6 +70,11 @@ int main(int argc, char **argv)
6970
std::cout << "[OK] inserted rows = " << total << "\n";
7071
return 0;
7172
}
73+
catch (const DBError &e)
74+
{
75+
std::cerr << "[DBError] " << e.what() << "\n";
76+
return 1;
77+
}
7278
catch (const std::exception &e)
7379
{
7480
std::cerr << "[ERR] " << e.what() << "\n";

examples/error_handling.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,54 @@
1111
* Vix.cpp
1212
*/
1313
#include <vix/orm/orm.hpp>
14+
1415
#include <iostream>
16+
#include <string>
1517

1618
using namespace vix::orm;
1719

1820
int main(int argc, char **argv)
1921
{
2022
(void)argc;
2123
(void)argv;
24+
2225
try
2326
{
24-
// Intentionally wrong DB name to show error
25-
auto factory = make_mysql_factory("tcp://127.0.0.1:3306", "root", "", "db_does_not_exist");
27+
// Intentionally wrong DB name to show error handling
28+
const std::string host = "tcp://127.0.0.1:3306";
29+
const std::string user = "root";
30+
const std::string pass = "";
31+
const std::string db = "db_does_not_exist";
32+
33+
auto factory = make_mysql_factory(host, user, pass, db);
34+
2635
PoolConfig cfg;
2736
cfg.min = 1;
2837
cfg.max = 8;
2938

3039
ConnectionPool pool{factory, cfg};
40+
41+
// will throw if factory returns invalid connection (recommended after our warmup fix),
42+
// or later when first query fails.
3143
pool.warmup();
3244

3345
UnitOfWork uow{pool};
3446
auto &con = uow.conn();
3547

3648
auto st = con.prepare("SELECT 1");
37-
st->exec();
49+
(void)st->exec();
50+
3851
std::cout << "[INFO] This message may not be reached if connection fails.\n";
52+
return 0;
3953
}
4054
catch (const DBError &e)
4155
{
4256
std::cerr << "[DBError] " << e.what() << "\n";
57+
return 1;
4358
}
4459
catch (const std::exception &e)
4560
{
4661
std::cerr << "[std::exception] " << e.what() << "\n";
62+
return 1;
4763
}
48-
return 0;
4964
}

examples/querybuilder_update.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,19 @@
1111
* Vix.cpp
1212
*/
1313
#include <vix/orm/orm.hpp>
14-
#include <vix/orm/ConnectionPool.hpp>
15-
#include <vix/orm/MySQLDriver.hpp>
1614

15+
#include <cstddef>
1716
#include <iostream>
1817
#include <string>
1918

2019
using namespace vix::orm;
2120

2221
int main(int argc, char **argv)
2322
{
24-
std::string host = (argc > 1 ? argv[1] : "tcp://127.0.0.1:3306");
25-
std::string user = (argc > 2 ? argv[2] : "root");
26-
std::string pass = (argc > 3 ? argv[3] : "");
27-
std::string db = (argc > 4 ? argv[4] : "vixdb");
23+
const std::string host = (argc > 1 ? argv[1] : "tcp://127.0.0.1:3306");
24+
const std::string user = (argc > 2 ? argv[2] : "root");
25+
const std::string pass = (argc > 3 ? argv[3] : "");
26+
const std::string db = (argc > 4 ? argv[4] : "vixdb");
2827

2928
try
3029
{
@@ -47,12 +46,17 @@ int main(int argc, char **argv)
4746

4847
const auto &ps = qb.params();
4948
for (std::size_t i = 0; i < ps.size(); ++i)
50-
st->bind(i + 1, ps[i]);
49+
st->bind(i + 1, any_to_dbvalue_or_throw(ps[i]));
5150

52-
auto affected = st->exec();
51+
const auto affected = st->exec();
5352
std::cout << "[OK] affected rows = " << affected << "\n";
5453
return 0;
5554
}
55+
catch (const DBError &e)
56+
{
57+
std::cerr << "[DBError] " << e.what() << "\n";
58+
return 1;
59+
}
5660
catch (const std::exception &e)
5761
{
5862
std::cerr << "[ERR] " << e.what() << "\n";

examples/repository_crud_full.cpp

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
*/
1313

1414
#include <vix/orm/orm.hpp>
15-
#include <vix/orm/ConnectionPool.hpp>
16-
#include <vix/orm/MySQLDriver.hpp>
1715

18-
#include <iostream>
16+
#include <any>
1917
#include <cstdint>
18+
#include <iostream>
2019
#include <string>
20+
#include <utility>
21+
#include <vector>
2122

2223
struct User
2324
{
@@ -32,7 +33,15 @@ namespace vix::orm
3233
template <>
3334
struct Mapper<User>
3435
{
35-
static User fromRow(const ResultRow &) { return {}; } // pending
36+
static User fromRow(const ResultRow &row)
37+
{
38+
User u{};
39+
u.id = row.getInt64Or(0, 0);
40+
u.name = row.getStringOr(1, "");
41+
u.email = row.getStringOr(2, "");
42+
u.age = static_cast<int>(row.getInt64Or(3, 0));
43+
return u;
44+
}
3645

3746
static std::vector<std::pair<std::string, std::any>>
3847
toInsertParams(const User &u)
@@ -60,10 +69,10 @@ int main(int argc, char **argv)
6069
{
6170
using namespace vix::orm;
6271

63-
std::string host = (argc > 1 ? argv[1] : "tcp://127.0.0.1:3306");
64-
std::string user = (argc > 2 ? argv[2] : "root");
65-
std::string pass = (argc > 3 ? argv[3] : "");
66-
std::string db = (argc > 4 ? argv[4] : "vixdb");
72+
const std::string host = (argc > 1 ? argv[1] : "tcp://127.0.0.1:3306");
73+
const std::string user = (argc > 2 ? argv[2] : "root");
74+
const std::string pass = (argc > 3 ? argv[3] : "");
75+
const std::string db = (argc > 4 ? argv[4] : "vixdb");
6776

6877
try
6978
{
@@ -79,20 +88,31 @@ int main(int argc, char **argv)
7988
BaseRepository<User> repo{pool, "users"};
8089

8190
// Create
82-
std::int64_t id = static_cast<std::int64_t>(
91+
const std::int64_t id = static_cast<std::int64_t>(
8392
repo.create(User{0, "Bob", "gaspardkirira@example.com", 30}));
84-
std::cout << "[OK] create id=" << id << "\n";
93+
std::cout << "[OK] create -> id=" << id << "\n";
8594

8695
// Update
87-
repo.updateById(id, User{id, "Adastra", "adastra@example.com", 31});
88-
std::cout << "[OK] update → id=" << id << "\n";
96+
(void)repo.updateById(id, User{id, "Adastra", "adastra@example.com", 31});
97+
std::cout << "[OK] update -> id=" << id << "\n";
98+
99+
// (Optional) Read back
100+
if (auto u = repo.findById(id))
101+
{
102+
std::cout << "[OK] findById -> name=" << u->name << " email=" << u->email << " age=" << u->age << "\n";
103+
}
89104

90105
// Delete
91-
repo.removeById(id);
92-
std::cout << "[OK] delete id=" << id << "\n";
106+
(void)repo.removeById(id);
107+
std::cout << "[OK] delete -> id=" << id << "\n";
93108

94109
return 0;
95110
}
111+
catch (const DBError &e)
112+
{
113+
std::cerr << "[DBError] " << e.what() << "\n";
114+
return 1;
115+
}
96116
catch (const std::exception &e)
97117
{
98118
std::cerr << "[ERR] " << e.what() << "\n";

examples/tx_unit_of_work.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,19 @@
1212
*/
1313

1414
#include <vix/orm/orm.hpp>
15-
#include <vix/orm/ConnectionPool.hpp>
16-
#include <vix/orm/MySQLDriver.hpp>
1715

16+
#include <cstdint>
1817
#include <iostream>
1918
#include <string>
2019

2120
using namespace vix::orm;
2221

2322
int main(int argc, char **argv)
2423
{
25-
std::string host = (argc > 1 ? argv[1] : "tcp://127.0.0.1:3306");
26-
std::string user = (argc > 2 ? argv[2] : "root");
27-
std::string pass = (argc > 3 ? argv[3] : "");
28-
std::string db = (argc > 4 ? argv[4] : "vixdb");
24+
const std::string host = (argc > 1 ? argv[1] : "tcp://127.0.0.1:3306");
25+
const std::string user = (argc > 2 ? argv[2] : "root");
26+
const std::string pass = (argc > 3 ? argv[3] : "");
27+
const std::string db = (argc > 4 ? argv[4] : "vixdb");
2928

3029
try
3130
{
@@ -43,17 +42,17 @@ int main(int argc, char **argv)
4342

4443
{
4544
auto st = c.prepare("INSERT INTO users(name,email,age) VALUES(?,?,?)");
46-
st->bind(1, std::string("Alice"));
47-
st->bind(2, std::string("alice@example.com"));
45+
st->bind(1, "Alice");
46+
st->bind(2, "alice@example.com");
4847
st->bind(3, 27);
4948
st->exec();
5049
}
5150

52-
const auto userId = c.lastInsertId();
51+
const std::uint64_t userId = c.lastInsertId();
5352

5453
{
5554
auto st = c.prepare("INSERT INTO orders(user_id,total) VALUES(?,?)");
56-
st->bind(1, static_cast<std::int64_t>(userId));
55+
st->bind(1, userId);
5756
st->bind(2, 199.99);
5857
st->exec();
5958
}

examples/users_crud.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@
1212
*/
1313

1414
#include <vix/orm/orm.hpp>
15-
#include <vix/orm/ConnectionPool.hpp>
16-
#include <vix/orm/MySQLDriver.hpp>
15+
1716
#include <iostream>
17+
#include <string>
1818
#include <vector>
1919

2020
using namespace vix::orm;
2121

2222
int main(int argc, char **argv)
2323
{
24-
std::string host = (argc > 1 ? argv[1] : "tcp://127.0.0.1:3306");
25-
std::string user = (argc > 2 ? argv[2] : "root");
26-
std::string pass = (argc > 3 ? argv[3] : "");
27-
std::string db = (argc > 4 ? argv[4] : "vixdb");
24+
const std::string host = (argc > 1 ? argv[1] : "tcp://127.0.0.1:3306");
25+
const std::string user = (argc > 2 ? argv[2] : "root");
26+
const std::string pass = (argc > 3 ? argv[3] : "");
27+
const std::string db = (argc > 4 ? argv[4] : "vixdb");
2828

2929
try
3030
{
@@ -48,7 +48,8 @@ int main(int argc, char **argv)
4848
const char *email;
4949
int age;
5050
};
51-
std::vector<Row> rows = {
51+
52+
const std::vector<Row> rows = {
5253
{"Gaspard", "gaspardkirira@outlook.com", 23},
5354
{"Mina", "mina@example.com", 31},
5455
{"Omar", "omar@example.com", 35},

include/vix/orm/Mapper.hpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,33 @@
1313
#ifndef VIX_MAPPER_HPP
1414
#define VIX_MAPPER_HPP
1515

16-
#include <vix/db/Drivers.hpp>
16+
#include <any>
1717
#include <string>
18+
#include <utility>
1819
#include <vector>
19-
#include <any>
20+
21+
#include <vix/db/core/Result.hpp>
2022

2123
namespace vix::orm
2224
{
25+
/**
26+
* User-specialized mapper.
27+
*
28+
* Example:
29+
* template<>
30+
* struct Mapper<User> { ... };
31+
*/
2332
template <class T>
2433
struct Mapper
2534
{
2635
static T fromRow(const vix::db::ResultRow &row);
27-
static std::vector<std::pair<std::string, std::any>> toInsertParams(const T &v);
28-
static std::vector<std::pair<std::string, std::any>> toUpdateParams(const T &v);
36+
37+
static std::vector<std::pair<std::string, std::any>>
38+
toInsertParams(const T &v);
39+
40+
static std::vector<std::pair<std::string, std::any>>
41+
toUpdateParams(const T &v);
2942
};
30-
} // namespace Vix::orm
43+
} // namespace vix::orm
3144

3245
#endif // VIX_MAPPER_HPP

0 commit comments

Comments
 (0)