1
1
#include < iostream>
2
+ #include < string>
3
+ #include < tuple>
2
4
#include " sqlite_modern_cpp.h"
3
5
using namespace sqlite ;
4
6
using namespace std ;
5
7
6
- /*
7
- template <typename T>
8
- struct function_traits
9
- : public function_traits<decltype(&T::operator())>
10
- {};
11
-
12
- template <typename ClassType, typename ReturnType, typename... Args>
13
- struct function_traits<ReturnType(ClassType::*)(Args...) const>
14
- // we specialize for pointers to member function
15
- {
16
- enum { arity = sizeof...(Args) };
17
- // arity is the number of arguments.
18
-
19
- typedef ReturnType result_type;
20
-
21
- template <size_t i>
22
- struct arg
23
- {
24
- typedef typename std::tuple_element<i, std::tuple<Args...>>::type type;
25
- // the i-th argument is equivalent to the i-th tuple element of a tuple
26
- // composed of those arguments.
27
- };
28
- };
29
-
30
- class database_bind {};
31
-
32
- template<int N>
33
- class A {
34
- template<typename F>
35
- static void run(F l);
36
- };
37
-
38
- template<>
39
- struct A<1> {
40
- template<typename F>
41
- static void run(F l) {
42
- typedef function_traits<decltype(l)> traits;
43
- typedef typename traits::arg<0>::type type_1;
44
-
45
- type_1 col_1;
46
- get_from_db(0,col_1);
47
-
48
- l(col_1);
49
- }
50
- };
51
- template<>
52
- struct A<2> {
53
- template<typename F>
54
- static void run(F l) {
55
- typedef function_traits<decltype(l)> traits;
56
- typedef typename traits::arg<0>::type type_1;
57
- typedef typename traits::arg<1>::type type_2;
58
-
59
- type_1 col_1;
60
- type_2 col_2;
61
- get_from_db(0,col_1);
62
- get_from_db(1,col_2);
63
-
64
- l(col_1, col_2);
65
- }
66
- };
67
-
68
-
69
- void get_from_db(int col_inx, string& str){
70
- // code to get a column from with col_inx from the database
71
- // for simplicity
72
- str = "str_col";
73
- }
74
- void get_from_db(int col_inx, int& i){
75
- // just for simplicity
76
- i = 12;
77
- }
78
-
79
-
80
- template<typename F>
81
- void operator>>(database_bind dbb, F l)
82
- {
83
- typedef function_traits<decltype(l)> traits;
84
- A<traits::arity>::run(l);
85
- }
86
-
87
- */
88
8
89
9
int main (){
90
10
try {
@@ -101,7 +21,7 @@ int main(){
101
21
102
22
// inserts a new user and binds the values to ?
103
23
// note that only types allowed for bindings are :
104
- // int ,long, long long, float, double
24
+ // int ,long, long long, float, double
105
25
// string , wstring
106
26
db << " insert into user (age,name,weight) values (?,?,?);"
107
27
<< 20
@@ -113,14 +33,14 @@ int main(){
113
33
<< L" jack"
114
34
<< 68.5 ;
115
35
116
- // slects from table user on a condition ( age > 18 ) and executes
36
+ // slects from table user on a condition ( age > 18 ) and executes
117
37
// the lambda for every row returned .
118
38
119
39
db << " select age,name,weight from user where age > ? ;"
120
40
<< 18
121
41
>> [&](int age, string name, double weight) {
122
- cout << age << ' ' << name << ' ' << weight << endl;
123
- };
42
+ cout << age << ' ' << name << ' ' << weight << endl;
43
+ };
124
44
125
45
// selects the count(*) of table user
126
46
// note that you can extract a single culumn single row answer only to : int,long,long,float,double,string,wstring
@@ -136,5 +56,4 @@ int main(){
136
56
catch (exception & e){
137
57
cout << e.what () << endl;
138
58
}
139
-
140
59
}
0 commit comments