-
Notifications
You must be signed in to change notification settings - Fork 0
/
cars.cpp
134 lines (105 loc) · 4.36 KB
/
cars.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <chrono>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <stdexcept>
#include <string>
#include <tuple>
#include <variant>
#include <vector>
#include "nldb/SQL3Implementation.hpp"
#include "nldb/backends/sqlite3/DB/DB.hpp"
// #include "Enums.hpp"
#include "nldb/LOG/log.hpp"
#include "nldb/Property/Property.hpp"
#include "nldb/Query/Query.hpp"
// #include "nlohmann/json.hpp"
#include "nldb/Utils/Enums.hpp"
#include "nldb/Utils/Variant.hpp"
#include "nldb/nldb_json.hpp"
#include "nldb/typedef.hpp"
using namespace nldb;
int main() {
nldb::LogManager::Initialize();
// optional, tweak the sqlite available memory.
DBSL3 db(DBConfig {
.page_size = 2048, .page_cache_size = 6144, .page_cache_N = 3});
// DBSL3 db;
remove("./cars.db");
if (!db.open("./cars.db" /*":memory:"*/)) {
std::cerr << "Could not open the database \n";
db.throwLastError();
}
// a query object can be used to execute multiple queries.
Query query = Query(&db, QueryConfiguration {
.PreferBuffer = false,
.SmallBufferSize = 1000, /*1 KB*/
.MediumBufferSize = 1000,
.LargeBufferSize = 1000,
});
// {name, founded, country}
json data_automaker = {{{"name", "ford"},
{"founded", "June 16, 1903"},
{"country", "United States"}},
// -------------
{{"name", "subaru"},
{"founded", "July 15, 1953"},
{"country", "Japan"}}};
// {maker, model, year}
json data_cars = {
{{"maker", "ford"}, {"model", "focus"}, {"year", 2011}},
{{"maker", "ford"}, {"model", "focus"}, {"year", 2015}},
{{"maker", "subaru"}, {"model", "impreza"}, {"year", 2003}}};
// insert automakers
query.from("automaker").insert(data_automaker);
db.logStatus();
// group all the automaker properties into the `automaker` variable.
Collection automakers = query.collection("automaker");
// we can obtain the properties from a collection before inserting any value
// in it
Collection cars = query.collection("cars");
auto [id, model, maker, year] = cars.get("_id", "model", "maker", "year");
// insert cars data
query.from("cars").insert(data_cars);
// select all the cars with manufacturer details
json all = query.from("cars")
.select(id, model, maker, year, automakers)
.where(year > 1990 && automakers["name"] == maker)
.page(1)
.limit(10)
.suppress(automakers["_id"])
.execute();
std::cout << "\n\nCars with automaker: " << all.dump(2) << std::endl
<< std::endl;
auto automaker = automakers.group("_id", "name", "founded");
// select id, model, maker and max year from each model
auto res1 = query.from("cars")
.select(id, model, maker, year.maxAs("year_newest_model"),
automaker)
.where(year > 1990 && automaker["name"] == maker)
.page(1)
.limit(10)
.groupBy(model, maker)
.execute();
std::cout << "\n\nNewest models with automaker details {name, founded}: "
<< res1.dump(2) << std::endl;
// update first car, set year to 2100 and add a new
// property called price
snowflake id0 = std::stoll(res1[0]["_id"].get<std::string>());
query.from("cars").update(id0, {{"year", 2100}, {"price", 50000}});
auto res2 = query.from("cars")
.select()
.page(1)
.limit(10)
.sortBy(year.desc())
.execute();
std::cout << "\n\nUpdated: " << res2.dump(2) << std::endl;
auto final = query.from("cars").select(model, maker, year).execute();
std::cout << "\n\nBefore remove 1: " << final.dump(2) << std::endl;
query.from("cars").remove(id0);
auto finalThen = query.from("cars").select(model, maker, year).execute();
std::cout << "\n\nAfter remove: " << finalThen.dump(2) << std::endl;
db.logStatus();
nldb::LogManager::Shutdown();
}