Skip to content

Commit 9f84901

Browse files
committed
feat(orm): update examples to new Database API and add HTTP ORM showcase
1 parent 12cbb6c commit 9f84901

21 files changed

Lines changed: 854 additions & 172 deletions

examples/.env.example

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# =========================================================
2+
# Vix HTTP + DB showcase
3+
# Copy this file to ".env"
4+
# =========================================================
5+
6+
# Database engine:
7+
# - sqlite
8+
# - mysql
9+
DATABASE_ENGINE=sqlite
10+
11+
# SQLite configuration
12+
DATABASE_SQLITE_PATH=vix.db
13+
14+
# MySQL configuration
15+
# Used only if DATABASE_ENGINE=mysql
16+
DATABASE_DEFAULT_HOST=127.0.0.1
17+
DATABASE_DEFAULT_PORT=3306
18+
DATABASE_DEFAULT_USER=root
19+
DATABASE_DEFAULT_PASSWORD=
20+
DATABASE_DEFAULT_NAME=vix
21+
22+
# Server configuration
23+
SERVER_PORT=8080
24+
SERVER_REQUEST_TIMEOUT=2000
25+
SERVER_IO_THREADS=0
26+
SERVER_SESSION_TIMEOUT_SEC=20
27+
SERVER_BENCH_MODE=false
28+
29+
# Logging configuration
30+
LOGGING_ASYNC=true
31+
LOGGING_QUEUE_MAX=20000
32+
LOGGING_DROP_ON_OVERFLOW=true
33+
34+
# WAF configuration
35+
WAF_MODE=basic
36+
WAF_MAX_TARGET_LEN=4096
37+
WAF_MAX_BODY_BYTES=1048576

examples/01_basic_repository.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,12 @@ int main()
4242
{
4343
auto db = vix::db::Database::sqlite("orm_basic.db");
4444

45-
{
46-
auto conn = db.pool().acquire();
47-
conn->prepare(
48-
"CREATE TABLE IF NOT EXISTS users ("
49-
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
50-
"name TEXT NOT NULL)")
51-
->exec();
52-
}
45+
db.exec(
46+
"CREATE TABLE IF NOT EXISTS users ("
47+
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
48+
"name TEXT NOT NULL)");
5349

54-
auto repo = vix::orm::repository<User>(db, "users");
50+
vix::orm::BaseRepository<User> repo(db.pool(), "users");
5551
const auto id = repo.create(User{0, "Alice"});
5652

5753
std::cout << "[OK] created id=" << id << "\n";

examples/02_repository_crud.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,18 @@ int main()
5050
{
5151
auto db = vix::db::Database::sqlite("orm_crud.db");
5252

53-
{
54-
auto conn = db.pool().acquire();
55-
conn->prepare(
56-
"CREATE TABLE IF NOT EXISTS users ("
57-
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
58-
"name TEXT NOT NULL, "
59-
"email TEXT NOT NULL, "
60-
"age INTEGER NOT NULL)")
61-
->exec();
62-
}
53+
db.exec(
54+
"CREATE TABLE IF NOT EXISTS users ("
55+
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
56+
"name TEXT NOT NULL, "
57+
"email TEXT NOT NULL, "
58+
"age INTEGER NOT NULL)");
6359

64-
auto repo = vix::orm::repository<User>(db, "users");
60+
vix::orm::BaseRepository<User> repo(db.pool(), "users");
6561

6662
const auto id = static_cast<std::int64_t>(
6763
repo.create(User{0, "Alice", "alice@example.com", 25}));
64+
6865
std::cout << "[OK] create id=" << id << "\n";
6966

7067
if (auto u = repo.findById(id))

examples/03_find_all_and_count.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,14 @@ int main()
3838
{
3939
auto db = vix::db::Database::sqlite("orm_find_all.db");
4040

41-
{
42-
auto conn = db.pool().acquire();
43-
conn->prepare(
44-
"CREATE TABLE IF NOT EXISTS users ("
45-
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
46-
"name TEXT NOT NULL)")
47-
->exec();
48-
conn->prepare("DELETE FROM users")->exec();
49-
}
41+
db.exec(
42+
"CREATE TABLE IF NOT EXISTS users ("
43+
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
44+
"name TEXT NOT NULL)");
45+
46+
db.exec("DELETE FROM users");
5047

51-
auto repo = vix::orm::repository<User>(db, "users");
48+
vix::orm::BaseRepository<User> repo(db.pool(), "users");
5249
repo.create(User{0, "Alice"});
5350
repo.create(User{0, "Bob"});
5451
repo.create(User{0, "Charlie"});

examples/04_exists_and_delete.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,29 @@ int main()
3838
{
3939
auto db = vix::db::Database::sqlite("orm_exists.db");
4040

41-
{
42-
auto conn = db.pool().acquire();
43-
conn->prepare(
44-
"CREATE TABLE IF NOT EXISTS users ("
45-
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
46-
"name TEXT NOT NULL)")
47-
->exec();
48-
conn->prepare("DELETE FROM users")->exec();
49-
}
50-
51-
auto repo = vix::orm::repository<User>(db, "users");
41+
db.exec(
42+
"CREATE TABLE IF NOT EXISTS users ("
43+
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
44+
"name TEXT NOT NULL)");
45+
46+
db.exec("DELETE FROM users");
47+
48+
vix::orm::BaseRepository<User> repo(db.pool(), "users");
5249
const auto id = static_cast<std::int64_t>(repo.create(User{0, "Alice"}));
5350

5451
std::cout << "[OK] exists before delete=" << (repo.existsById(id) ? "yes" : "no") << "\n";
52+
5553
repo.removeById(id);
54+
5655
std::cout << "[OK] exists after delete=" << (repo.existsById(id) ? "yes" : "no") << "\n";
5756

5857
repo.create(User{0, "Bob"});
5958
repo.create(User{0, "Charlie"});
59+
6060
std::cout << "[OK] count before removeAll=" << repo.count() << "\n";
6161

6262
repo.removeAll();
63+
6364
std::cout << "[OK] count after removeAll=" << repo.count() << "\n";
6465

6566
return 0;

examples/05_unit_of_work.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,11 @@ int main()
1010
{
1111
auto db = vix::db::Database::sqlite("orm_uow.db");
1212

13-
{
14-
auto conn = db.pool().acquire();
15-
conn->prepare(
16-
"CREATE TABLE IF NOT EXISTS users ("
17-
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
18-
"name TEXT NOT NULL, "
19-
"age INTEGER NOT NULL)")
20-
->exec();
21-
}
13+
db.exec(
14+
"CREATE TABLE IF NOT EXISTS users ("
15+
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
16+
"name TEXT NOT NULL, "
17+
"age INTEGER NOT NULL)");
2218

2319
auto uow = vix::orm::unit_of_work(db);
2420

examples/06_batch_insert_tx.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,11 @@ int main()
1111
{
1212
auto db = vix::db::Database::sqlite("orm_batch.db");
1313

14-
{
15-
auto conn = db.pool().acquire();
16-
conn->prepare(
17-
"CREATE TABLE IF NOT EXISTS users ("
18-
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
19-
"name TEXT NOT NULL, "
20-
"age INTEGER NOT NULL)")
21-
->exec();
22-
}
14+
db.exec(
15+
"CREATE TABLE IF NOT EXISTS users ("
16+
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
17+
"name TEXT NOT NULL, "
18+
"age INTEGER NOT NULL)");
2319

2420
const std::vector<std::pair<std::string, std::int64_t>> users = {
2521
{"Alice", 20},
@@ -28,7 +24,7 @@ int main()
2824
{"Diane", 35},
2925
};
3026

31-
auto uow = vix::orm::unit_of_work(db);
27+
auto uow = vix::orm::UnitOfWork(db);
3228

3329
for (const auto &[name, age] : users)
3430
{

examples/07_query_builder_select.cpp

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,23 @@ int main()
1010
{
1111
auto db = vix::db::Database::sqlite("orm_qb_select.db");
1212

13-
{
14-
auto conn = db.pool().acquire();
15-
conn->prepare(
16-
"CREATE TABLE IF NOT EXISTS users ("
17-
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
18-
"name TEXT NOT NULL, "
19-
"age INTEGER NOT NULL)")
20-
->exec();
21-
conn->prepare("DELETE FROM users")->exec();
22-
23-
auto st1 = conn->prepare("INSERT INTO users (name, age) VALUES (?, ?)");
24-
st1->bind(1, std::string("Alice"));
25-
st1->bind(2, static_cast<std::int64_t>(20));
26-
st1->exec();
27-
28-
auto st2 = conn->prepare("INSERT INTO users (name, age) VALUES (?, ?)");
29-
st2->bind(1, std::string("Bob"));
30-
st2->bind(2, static_cast<std::int64_t>(16));
31-
st2->exec();
32-
}
13+
db.exec(
14+
"CREATE TABLE IF NOT EXISTS users ("
15+
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
16+
"name TEXT NOT NULL, "
17+
"age INTEGER NOT NULL)");
18+
19+
db.exec("DELETE FROM users");
20+
21+
db.exec("INSERT INTO users (name, age) VALUES (?, ?)",
22+
std::string("Alice"),
23+
static_cast<std::int64_t>(20));
24+
25+
db.exec("INSERT INTO users (name, age) VALUES (?, ?)",
26+
std::string("Bob"),
27+
static_cast<std::int64_t>(16));
3328

34-
auto conn = db.pool().acquire();
29+
vix::db::PooledConn conn(db.pool());
3530

3631
vix::orm::QueryBuilder qb;
3732
qb.raw("SELECT id, name, age FROM users WHERE age >= ?")
@@ -41,7 +36,7 @@ int main()
4136
qb.bind(*st);
4237

4338
auto rs = st->query();
44-
while (rs->next())
39+
while (rs && rs->next())
4540
{
4641
const auto &row = rs->row();
4742
std::cout << row.getInt64(0) << " "

examples/08_query_builder_update.cpp

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,20 @@ int main()
1010
{
1111
auto db = vix::db::Database::sqlite("orm_qb_update.db");
1212

13-
{
14-
auto conn = db.pool().acquire();
15-
conn->prepare(
16-
"CREATE TABLE IF NOT EXISTS users ("
17-
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
18-
"name TEXT NOT NULL, "
19-
"age INTEGER NOT NULL)")
20-
->exec();
21-
conn->prepare("DELETE FROM users")->exec();
22-
23-
auto insert = conn->prepare("INSERT INTO users (name, age) VALUES (?, ?)");
24-
insert->bind(1, std::string("Alice"));
25-
insert->bind(2, static_cast<std::int64_t>(20));
26-
insert->exec();
27-
}
13+
db.exec(
14+
"CREATE TABLE IF NOT EXISTS users ("
15+
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
16+
"name TEXT NOT NULL, "
17+
"age INTEGER NOT NULL)");
18+
19+
db.exec("DELETE FROM users");
20+
21+
db.exec("INSERT INTO users (name, age) VALUES (?, ?)",
22+
std::string("Alice"),
23+
static_cast<std::int64_t>(20));
2824

2925
{
30-
auto conn = db.pool().acquire();
26+
vix::db::PooledConn conn(db.pool());
3127

3228
vix::orm::QueryBuilder qb;
3329
qb.raw("UPDATE users SET age = ? WHERE name = ?")
@@ -40,11 +36,9 @@ int main()
4036
}
4137

4238
{
43-
auto conn = db.pool().acquire();
44-
auto st = conn->prepare("SELECT name, age FROM users");
45-
auto rs = st->query();
39+
auto rs = db.query("SELECT name, age FROM users");
4640

47-
while (rs->next())
41+
while (rs && rs->next())
4842
{
4943
const auto &row = rs->row();
5044
std::cout << row.getString(0) << " " << row.getInt64(1) << "\n";

examples/09_error_handling.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ int main()
77
try
88
{
99
auto db = vix::db::Database::sqlite("orm_errors.db");
10-
auto conn = db.pool().acquire();
1110

12-
auto st = conn->prepare("SELECT * FROM table_that_does_not_exist");
13-
auto rs = st->query();
11+
auto rs = db.query("SELECT * FROM table_that_does_not_exist");
1412

15-
while (rs->next())
13+
while (rs && rs->next())
1614
{
1715
(void)rs->row();
1816
}

0 commit comments

Comments
 (0)