Skip to content

Commit a1ffdc5

Browse files
committed
second version
1 parent db58f61 commit a1ffdc5

File tree

3 files changed

+476
-343
lines changed

3 files changed

+476
-343
lines changed

README.md

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,62 +9,57 @@ This library is lightweight wrapper around sqlite C api .
99
using namespace sqlite;
1010
using namespace std;
1111

12+
1213
int main(){
1314
try {
1415
// creates a database file 'dbfile.db' if not exists
1516
database db("dbfile.db");
1617

17-
// executes the query and creates a 'user' table
18+
// executes the query and creates a 'user' table if not exists
1819
db <<
19-
"create table user ("
20+
"create table if not exists user ("
2021
" age int,"
2122
" name text,"
2223
" weight real"
2324
");";
2425

2526
// inserts a new user and binds the values to ?
26-
// note that only types allowed for bindings are
27-
// 1 - numeric types( int ,long ,long long, float, double , ... )
28-
// 2 - string , wstring
27+
// note that only types allowed for bindings are :
28+
// int ,long, long long, float, double
29+
// string , wstring
2930
db << "insert into user (age,name,weight) values (?,?,?);"
3031
<< 20
3132
<< "bob"
3233
<< 83.0;
3334

3435
db << "insert into user (age,name,weight) values (?,?,?);"
3536
<< 21
36-
<< L"jak"
37+
<< L"jack"
3738
<< 68.5;
3839

3940
// slects from table user on a condition ( age > 18 ) and executes
40-
// the body of magid_mapper for every row returned .
41-
// node : magic_mapper is just a simple macro , the next sample is
42-
// equivalent to this one without the use of magic_mapper macro
43-
db << "select age,name,weight from user where age > ? ;"
44-
<< 18
45-
>> magic_mapper(int age, string name, double weight) {
46-
cout << age << ' ' << name << ' ' << weight << endl;
47-
};
41+
// the lambda for every row returned .
4842

4943
db << "select age,name,weight from user where age > ? ;"
5044
<< 18
51-
>> function<void(int,string,double)>([&](int age, string name, double weight) {
45+
>> [&](int age, string name, double weight) {
5246
cout << age << ' ' << name << ' ' << weight << endl;
53-
});
47+
};
5448

55-
// i am currently working on a solution to avoid magic mapper
56-
// i future i want to this syntax also work
57-
/*
58-
db << "select age,name,weight from user where age > ? ;"
59-
<< 18
60-
>> [&](int age, string name, double weight) {
61-
cout << age << ' ' << name << ' ' << weight << endl;
62-
};
63-
*/
49+
// selects the count(*) of table user
50+
// note that you can extract a single culumn single row answer only to : int,long,long,float,double,string,wstring
51+
int count = 0;
52+
db << "select count(*) from user" >> count;
53+
cout << "cout : " << count << endl;
6454

55+
// this also works and the returned value will automatically converted to string
56+
string scount;
57+
db << "select count(*) from user" >> scount;
58+
cout << "scount : " << scount << endl;
6559
}
6660
catch (exception& e){
6761
cout << e.what() << endl;
6862
}
63+
6964
}
7065
```

example.cpp

Lines changed: 101 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,138 @@
33
using namespace sqlite;
44
using 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+
689
int 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 << ' ' << name << ' ' << weight << endl;
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 << ' ' << name << ' ' << weight << endl;
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" >> count;
129+
cout << "cout : " << count << endl;
58130

131+
// this also works and the returned value will automatically converted to string
132+
string scount;
133+
db << "select count(*) from user" >> scount;
134+
cout << "scount : " << scount << endl;
59135
}
60136
catch (exception& e){
61137
cout << e.what() << endl;
62138
}
139+
63140
}

0 commit comments

Comments
 (0)