-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathlvalue_functor.cc
More file actions
52 lines (40 loc) · 1.32 KB
/
lvalue_functor.cc
File metadata and controls
52 lines (40 loc) · 1.32 KB
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
#include<iostream>
#include<sqlite_modern_cpp.h>
#include<string>
#include<vector>
#include<catch2/catch.hpp>
using namespace sqlite;
using namespace std;
template<typename Target, typename... AttrTypes>
struct builder {
vector<Target> results;
void operator()(AttrTypes... args) {
results.emplace_back(std::forward<AttrTypes&&>(args)...);
};
};
struct user {
int age;
string name;
double weight;
user(int age, string name, double weight) : age(age), name(name), weight(weight) { }
static std::vector<user> all(sqlite::database& db) {
builder<user, int, std::string, double> person_builder;
db << "SELECT * FROM user;"
>> person_builder;
return std::move(person_builder.results); // move to avoid copying data ;-)
};
};
TEST_CASE("lvalue functors work", "[lvalue_functor]") {
database db(":memory:");
db <<
"create table if not exists user ("
" age int,"
" name text,"
" weight real"
");";
db << "insert into user (age,name,weight) values (?,?,?);" << 20 << u"chandler" << 83.25;
db << "insert into user (age,name,weight) values (?,?,?);" << 21 << u"monika" << 86.25;
db << "insert into user (age,name,weight) values (?,?,?);" << 22 << u"ross" << 88.25;
auto users = user::all(db);
REQUIRE(users.size() == 3);
}