11#include < iostream>
2+ #include < string>
3+ #include < tuple>
24#include " sqlite_modern_cpp.h"
35using namespace sqlite ;
46using namespace std ;
57
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- */
888
899int main (){
9010 try {
@@ -101,7 +21,7 @@ int main(){
10121
10222 // inserts a new user and binds the values to ?
10323 // note that only types allowed for bindings are :
104- // int ,long, long long, float, double
24+ // int ,long, long long, float, double
10525 // string , wstring
10626 db << " insert into user (age,name,weight) values (?,?,?);"
10727 << 20
@@ -113,14 +33,14 @@ int main(){
11333 << L" jack"
11434 << 68.5 ;
11535
116- // slects from table user on a condition ( age > 18 ) and executes
36+ // slects from table user on a condition ( age > 18 ) and executes
11737 // the lambda for every row returned .
11838
11939 db << " select age,name,weight from user where age > ? ;"
12040 << 18
12141 >> [&](int age, string name, double weight) {
122- cout << age << ' ' << name << ' ' << weight << endl;
123- };
42+ cout << age << ' ' << name << ' ' << weight << endl;
43+ };
12444
12545 // selects the count(*) of table user
12646 // 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(){
13656 catch (exception& e){
13757 cout << e.what () << endl;
13858 }
139-
14059}
0 commit comments