Skip to content

Commit 3fa0bd7

Browse files
committed
feat(db): add Database facade and simplify examples
2 parents 672dfd4 + 105077f commit 3fa0bd7

File tree

12 files changed

+588
-252
lines changed

12 files changed

+588
-252
lines changed

examples/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@ if (VIX_DB_BUILD_EXAMPLES)
1414
vix_db_example(prepared_query)
1515
vix_db_example(transaction)
1616
vix_db_example(migrations)
17+
vix_db_example(sqlite_basic)
18+
vix_db_example(pool_usage)
19+
20+
if (VIX_DB_HAS_MYSQL)
21+
vix_db_example(mysql_basic)
22+
endif()
1723
endif()

examples/basic_connect.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
1-
#include <vix/db/db.hpp>
21
#include <iostream>
3-
4-
using namespace vix::db;
2+
#include <vix/db/db.hpp>
53

64
int main()
75
{
8-
DbConfig cfg;
9-
cfg.engine = Engine::MySQL;
10-
cfg.mysql.host = "tcp://127.0.0.1:3306";
11-
cfg.mysql.user = "root";
12-
cfg.mysql.password = "";
13-
cfg.mysql.database = "vixdb";
6+
try
7+
{
8+
auto db = vix::db::Database::sqlite("vix.db");
149

15-
Database db(cfg);
10+
auto conn = db.pool().acquire();
11+
if (!conn->ping())
12+
{
13+
std::cerr << "DB ping failed\n";
14+
return 1;
15+
}
1616

17-
auto conn = db.pool().acquire();
18-
if (!conn->ping())
17+
std::cout << "DB connected successfully\n";
18+
return 0;
19+
}
20+
catch (const std::exception &e)
1921
{
20-
std::cerr << "DB ping failed\n";
22+
std::cerr << "DB error: " << e.what() << "\n";
2123
return 1;
2224
}
23-
24-
std::cout << "DB connected successfully\n";
25-
return 0;
2625
}

examples/migrations.cpp

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,60 @@
1-
#include <vix/db/db.hpp>
2-
3-
#include <iostream>
41
#include <filesystem>
2+
#include <iostream>
53
#include <string>
64

7-
using namespace vix::db;
8-
9-
static DbConfig make_mysql_cfg()
10-
{
11-
DbConfig cfg;
12-
cfg.engine = Engine::MySQL;
13-
cfg.mysql.host = "tcp://127.0.0.1:3306";
14-
cfg.mysql.user = "root";
15-
cfg.mysql.password = "";
16-
cfg.mysql.database = "vixdb";
17-
cfg.mysql.pool.min = 1;
18-
cfg.mysql.pool.max = 4;
19-
return cfg;
20-
}
5+
#include <vix/db/db.hpp>
216

22-
// ------------------------------
23-
// 1) Code-based migration example
24-
// ------------------------------
25-
class CreateUsersTable final : public Migration
7+
class CreateUsersTable final : public vix::db::Migration
268
{
279
public:
28-
std::string id() const override { return "2026-01-22-create-users"; }
10+
std::string id() const override
11+
{
12+
return "2026-01-22-create-users";
13+
}
2914

30-
void up(Connection &c) override
15+
void up(vix::db::Connection &c) override
3116
{
3217
auto st = c.prepare(
3318
"CREATE TABLE IF NOT EXISTS users ("
34-
" id BIGINT PRIMARY KEY AUTO_INCREMENT,"
35-
" name VARCHAR(255) NOT NULL,"
36-
" age INT NOT NULL"
19+
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
20+
"name TEXT NOT NULL, "
21+
"age INTEGER NOT NULL"
3722
");");
3823
st->exec();
3924
}
4025

41-
void down(Connection &c) override
26+
void down(vix::db::Connection &c) override
4227
{
4328
auto st = c.prepare("DROP TABLE IF EXISTS users;");
4429
st->exec();
4530
}
4631
};
4732

48-
static void run_code_migrations(Database &db)
33+
static void runCodeMigrations(vix::db::Database &db)
4934
{
5035
std::cout << "[migrations] running code migrations...\n";
5136

52-
Transaction tx(db.pool());
37+
vix::db::Transaction tx(db.pool());
5338

54-
CreateUsersTable m1;
55-
MigrationsRunner runner(tx.conn());
56-
runner.add(&m1);
39+
CreateUsersTable migration;
40+
vix::db::MigrationsRunner runner(tx.conn());
41+
runner.add(&migration);
5742
runner.runAll();
5843

5944
tx.commit();
6045
std::cout << "[migrations] done (code)\n";
6146
}
6247

63-
// ------------------------------
64-
// 2) File-based migration example
65-
// ------------------------------
66-
static void run_file_migrations(Database &db, std::filesystem::path dir)
48+
static void runFileMigrations(vix::db::Database &db,
49+
std::filesystem::path dir)
6750
{
68-
std::cout << "[migrations] running file migrations from: " << dir.string() << "\n";
51+
std::cout << "[migrations] running file migrations from: "
52+
<< dir.string() << "\n";
6953

70-
Transaction tx(db.pool());
54+
vix::db::Transaction tx(db.pool());
7155

72-
FileMigrationsRunner runner(tx.conn(), std::move(dir));
73-
runner.setTable("schema_migrations"); // optional (default already)
56+
vix::db::FileMigrationsRunner runner(tx.conn(), std::move(dir));
57+
runner.setTable("schema_migrations");
7458
runner.applyAll();
7559

7660
tx.commit();
@@ -81,13 +65,10 @@ int main()
8165
{
8266
try
8367
{
84-
Database db(make_mysql_cfg());
85-
86-
// 1) Code migrations
87-
run_code_migrations(db);
68+
auto db = vix::db::Database::sqlite("vix.db");
8869

89-
// 2) File migrations (expects ./migrations/*.up.sql and *.down.sql)
90-
run_file_migrations(db, std::filesystem::path{"migrations"});
70+
runCodeMigrations(db);
71+
runFileMigrations(db, std::filesystem::path{"migrations"});
9172

9273
std::cout << "OK\n";
9374
return 0;

examples/mysql_basic.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <iostream>
2+
#include <vix/db/Database.hpp>
3+
4+
int main()
5+
{
6+
try
7+
{
8+
auto db = vix::db::Database::mysql(
9+
"tcp://127.0.0.1:3306",
10+
"root",
11+
"",
12+
"vixdb");
13+
14+
auto &pool = db.pool();
15+
16+
{
17+
vix::db::PooledConn conn(pool);
18+
19+
conn->prepare(
20+
"CREATE TABLE IF NOT EXISTS users ("
21+
"id BIGINT AUTO_INCREMENT PRIMARY KEY, "
22+
"name TEXT)")
23+
->exec();
24+
25+
conn->prepare(
26+
"INSERT INTO users (name) VALUES (?)")
27+
->bind(1, std::string("Gaspard"));
28+
29+
std::cout << "[OK] inserted user\n";
30+
}
31+
32+
return 0;
33+
}
34+
catch (const std::exception &e)
35+
{
36+
std::cerr << "[ERR] " << e.what() << "\n";
37+
return 1;
38+
}
39+
}

examples/pool_usage.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <iostream>
2+
#include <thread>
3+
#include <vector>
4+
5+
#include <vix/db/Database.hpp>
6+
7+
void worker(vix::db::ConnectionPool &pool, int id)
8+
{
9+
vix::db::PooledConn conn(pool);
10+
11+
conn->prepare(
12+
"INSERT INTO users (name) VALUES (?)")
13+
->bind(1, std::string("worker_" + std::to_string(id)));
14+
15+
std::cout << "[thread] inserted " << id << "\n";
16+
}
17+
18+
int main()
19+
{
20+
try
21+
{
22+
auto db = vix::db::Database::sqlite("vix.db");
23+
24+
auto &pool = db.pool();
25+
26+
{
27+
vix::db::PooledConn conn(pool);
28+
conn->prepare(
29+
"CREATE TABLE IF NOT EXISTS users ("
30+
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
31+
"name TEXT)")
32+
->exec();
33+
}
34+
35+
std::vector<std::thread> threads;
36+
37+
for (int i = 0; i < 4; ++i)
38+
{
39+
threads.emplace_back(worker, std::ref(pool), i);
40+
}
41+
42+
for (auto &t : threads)
43+
{
44+
t.join();
45+
}
46+
47+
std::cout << "[OK] done\n";
48+
49+
return 0;
50+
}
51+
catch (const std::exception &e)
52+
{
53+
std::cerr << "[ERR] " << e.what() << "\n";
54+
return 1;
55+
}
56+
}

examples/prepared_query.cpp

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,50 @@
1-
#include <vix/db/db.hpp>
1+
#include <cstdint>
22
#include <iostream>
3-
4-
using namespace vix::db;
3+
#include <vix/db/db.hpp>
54

65
int main()
76
{
8-
DbConfig cfg;
9-
cfg.engine = Engine::MySQL;
10-
cfg.mysql.host = "tcp://127.0.0.1:3306";
11-
cfg.mysql.user = "root";
12-
cfg.mysql.password = "";
13-
cfg.mysql.database = "vixdb";
14-
cfg.mysql.pool.min = 1;
15-
cfg.mysql.pool.max = 8;
16-
17-
Database db(cfg);
18-
19-
auto conn = db.pool().acquire();
20-
auto st = conn->prepare("SELECT id, name FROM users WHERE age > ?");
21-
22-
st->bind(1, 18);
23-
// or: st->bind(1, std::int64_t{18});
24-
// or: st->bind(1, i64(18));
25-
26-
auto rs = st->query();
27-
while (rs->next())
7+
try
8+
{
9+
auto db = vix::db::Database::sqlite("vix.db");
10+
11+
{
12+
auto conn = db.pool().acquire();
13+
14+
conn->prepare(
15+
"CREATE TABLE IF NOT EXISTS users ("
16+
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
17+
"name TEXT NOT NULL, "
18+
"age INTEGER NOT NULL)")
19+
->exec();
20+
21+
auto insert = conn->prepare(
22+
"INSERT INTO users (name, age) VALUES (?, ?)");
23+
24+
insert->bind(1, std::string("Alice"));
25+
insert->bind(2, static_cast<std::int64_t>(20));
26+
insert->exec();
27+
}
28+
29+
{
30+
auto conn = db.pool().acquire();
31+
auto st = conn->prepare("SELECT id, name FROM users WHERE age > ?");
32+
33+
st->bind(1, static_cast<std::int64_t>(18));
34+
35+
auto rs = st->query();
36+
while (rs->next())
37+
{
38+
const auto &row = rs->row();
39+
std::cout << row.getInt64(0) << " " << row.getString(1) << "\n";
40+
}
41+
}
42+
43+
return 0;
44+
}
45+
catch (const std::exception &e)
2846
{
29-
const auto &row = rs->row();
30-
std::cout << row.getInt64(0) << " " << row.getString(1) << "\n";
47+
std::cerr << "DB error: " << e.what() << "\n";
48+
return 1;
3149
}
3250
}

examples/sqlite_basic.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <iostream>
2+
#include <vix/db/Database.hpp>
3+
4+
int main()
5+
{
6+
try
7+
{
8+
// Ultra simple
9+
auto db = vix::db::Database::sqlite("vix.db");
10+
11+
auto &pool = db.pool();
12+
13+
{
14+
vix::db::PooledConn conn(pool);
15+
16+
conn->prepare(
17+
"CREATE TABLE IF NOT EXISTS users ("
18+
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
19+
"name TEXT)")
20+
->exec();
21+
22+
conn->prepare(
23+
"INSERT INTO users (name) VALUES (?)")
24+
->bind(1, std::string("Gaspard"));
25+
26+
conn->prepare(
27+
"INSERT INTO users (name) VALUES (?)")
28+
->bind(1, std::string("Adastra"));
29+
30+
std::cout << "[OK] inserted users\n";
31+
}
32+
33+
{
34+
vix::db::PooledConn conn(pool);
35+
36+
auto stmt = conn->prepare("SELECT id, name FROM users");
37+
auto rs = stmt->query();
38+
39+
while (rs->next())
40+
{
41+
const auto &row = rs->row();
42+
std::cout << "User: "
43+
<< row.getInt64(0) << " "
44+
<< row.getString(1) << "\n";
45+
}
46+
}
47+
48+
return 0;
49+
}
50+
catch (const std::exception &e)
51+
{
52+
std::cerr << "[ERR] " << e.what() << "\n";
53+
return 1;
54+
}
55+
}

0 commit comments

Comments
 (0)