33using  namespace   sqlite ; 
44using  namespace  std ; 
55
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+ 
689int  main (){
790	try  {
891		//  creates a database file 'dbfile.db' if not exists
992		database db (" dbfile.db" 
1093
1194		//  executes the query and creates a 'user' table
1295		db <<
13- 			" create table user (" 
96+ 			" create table if not exists  user (" 
1497			" 	age int," 
1598			" 	name text," 
1699			" 	weight real" 
17100			" );" 
18101
19102		//  inserts a new user and binds the values to ?
20- 		//  note that only types allowed for bindings are 
21- 		// 		1 - numeric types(  int ,long , long long, float, double , ... ) 
22- 		// 		2 -  string , wstring
103+ 		//  note that only types allowed for bindings are : 
104+ 		// 		int ,long,  long long, float, double 
105+ 		// 		string , wstring
23106		db << " insert into user (age,name,weight) values (?,?,?);" 
24107			<< 20 
25108			<< " bob" 
26109			<< 83.0 ;
27110
28111		db << " insert into user (age,name,weight) values (?,?,?);" 
29112			<< 21 
30- 			<< L" jak " 
113+ 			<< L" jack " 
31114			<< 68.5 ;
32115
33116		//  slects from table user on a condition ( age > 18 ) and executes 
34- 		//  the body of magid_mapper for every row returned .
35- 		//  node : magic_mapper is just a simple macro , the next sample is
36- 		//  equivalent to this one without the use of magic_mapper macro
117+ 		//  the lambda for every row returned .
118+ 
37119		db << " select age,name,weight from user where age > ? ;" 
38120			<< 18 
39- 			>> magic_mapper (int  age, string name, double  weight) {
121+ 			>> [&] (int  age, string name, double  weight) {
40122				cout << age << '  ' '  ' 
41123			};
42124
43- 		db << " select age,name,weight from user where age > ? ;" 
44- 			<< 18 
45- 			>> function<void (int ,string,double )>([&](int  age, string name, double  weight) {
46- 				cout << age << '  ' '  ' 
47- 			});
48- 
49- 		//  i am currently working on a solution to avoid magic mapper
50- 		//  i future i want to this syntax also work 
51- 		/* 
52- 			db << "select age,name,weight from user where age > ? ;" 
53- 				<< 18 
54- 				>> [&](int age, string name, double weight) { 
55- 					cout << age << ' ' << name << ' ' << weight << endl; 
56- 				}; 
57- 		*/  
125+ 		//  selects the count(*) of table user
126+ 		//  note that you can extract a single culumn single row answer only to : int,long,long,float,double,string,wstring
127+ 		int  count = 0 ;
128+ 		db << " select count(*) from user" 
129+ 		cout << " cout : " 
58130
131+ 		//  this also works and the returned value will automatically converted to string
132+ 		string scount;
133+ 		db << " select count(*) from user" 
134+ 		cout << " scount : " 
59135	}
60136	catch  (exception& e){
61137		cout << e.what () << endl;
62138	}
139+ 
63140}
0 commit comments