-
Notifications
You must be signed in to change notification settings - Fork 0
/
persona.cpp
135 lines (109 loc) · 4.67 KB
/
persona.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
135
#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"
using namespace nldb;
int main() {
nldb::LogManager::Initialize();
DBSL3 db;
remove("./tests.db");
if (!db.open("./tests.db" /*":memory:"*/)) {
std::cerr << "Could not open the database \n";
db.throwLastError();
}
auto query = Query(&db);
json data = json::array({{// obj 1
{"name", "enzo"},
{"aliases", {"x", "p", "r"}},
{"contact",
{{"phone", "12344"},
{"address", "fake st 99"},
{"email", "c.enzo@at.com"}}}
}, // end obj1
{
// obj 2
{"name", "pepe"},
{"aliases", {"pepe", "mr pepe"}},
{
"contact",
{// contact
{"phone", "999"},
{"location",
{{"city", "big city"},
{"address", "not a fake st 89"},
{"country", "argentina"}}},
{"email", "f@f.f"}} // contact
}
//
}});
auto persona = query.collection("persona");
auto now = std::chrono::high_resolution_clock::now();
query.from("persona").insert(data);
std::cout << "insert took "
<< (std::chrono::high_resolution_clock::now() - now) /
std::chrono::milliseconds(1)
<< " ms" << std::endl;
now = std::chrono::high_resolution_clock::now();
auto [_id, name, aliases, contact] = query.collection("persona").get(
"_id", "name", "aliases", "contact{email, location{_id, country}}"_obj);
auto result = query.from(persona)
.select(_id, aliases, contact)
.where(_id != 9 && name != "foo")
.sortBy(contact["email"].desc())
.execute();
std::cout << "select took "
<< (std::chrono::high_resolution_clock::now() - now) /
std::chrono::milliseconds(1)
<< " ms" << std::endl;
std::cout << result.dump(4);
/* ------------------ filter out fields ----------------- */
// you can suppress filters by passing them in the suppress function
auto persona_c = query.collection("persona").group();
// Select all from persona but persona._id and persona.name
result = query.from("persona")
.select()
// you can use all the person members in
// where/sort/groupBy, even if we will suppress them
.where(_id > 0)
// .suppress(_id) // equal to:
.suppress(persona_c["_id"], name)
.execute();
std::cout << "all but _id, name: " << result.dump(2) << std::endl;
/* ------- suppress fields in embedded documents ------ */
// to use a property from a collection by:
// collection["sub collection name"]["property name"]
// or
// collection["sub collection name.property name"]
// with as many sub-collections between the main and the property as
// necessary
result =
query.from("persona")
.select()
.where(persona["contact"]["phone"] != "12344" && name != "hola")
.sortBy(persona["contact"]["location"]["_id"].asc())
.suppress(_id, persona["contact"]["phone"], persona["aliases"])
.execute();
std::cout << "all but _id and contact phone " << result.dump(2)
<< std::endl;
assert(result.size() == 1 && result[0]["name"] == "pepe");
// Note: we try to use as few tables as possible, so in case you select all
// but suppressed fields A, B and C, those fields won't be joined in the sql
// query unless you explicitly use them in a where/sort/group by.
nldb::LogManager::Shutdown();
}