forked from SqliteModernCpp/sqlite_modern_cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctors.cc
More file actions
39 lines (30 loc) · 985 Bytes
/
functors.cc
File metadata and controls
39 lines (30 loc) · 985 Bytes
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sqlite_modern_cpp.h>
#include <catch2/catch.hpp>
using namespace sqlite;
using namespace std;
struct tbl_functor {
explicit tbl_functor(vector<pair<int, string> > &vec_) : vec(vec_) { }
void operator() ( int id, string name) {
vec.push_back(make_pair(id, move(name)));
}
vector<pair<int,string> > &vec;
};
TEST_CASE("functors work", "[functors]") {
database db(":memory:");
db << "CREATE TABLE tbl (id integer, name string);";
db << "INSERT INTO tbl VALUES (?, ?);" << 1 << "hello";
db << "INSERT INTO tbl VALUES (?, ?);" << 2 << "world";
vector<pair<int,string> > vec;
db << "select id,name from tbl;" >> tbl_functor(vec);
REQUIRE(vec.size() == 2);
vec.clear();
tbl_functor functor(vec);
db << "select id,name from tbl;" >> functor;
REQUIRE(vec.size() == 2);
REQUIRE(vec[0].first == 1);
REQUIRE(vec[0].second == "hello");
}