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
2223struct 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 " ;
0 commit comments