|
1 | | -#define BOOST_TEST_MODULE sqltest |
2 | | - |
3 | 1 | #include <iostream> |
4 | 2 | #include <sstream> |
5 | 3 |
|
6 | | -#include <boost/test/unit_test.hpp> |
7 | 4 | #include "sql.h" |
8 | 5 |
|
9 | | -BOOST_AUTO_TEST_SUITE(sqltest) |
| 6 | +/* |
10 | 7 |
|
11 | | -BOOST_AUTO_TEST_CASE(insert) |
12 | | -{ |
13 | | - std::vector<int> a = {1, 2, 3}; |
| 8 | +create table if not exists user ( |
| 9 | + `id` int(10) unsigned not null auto_increment, |
| 10 | + `age` tinyint(8) unsigned, |
| 11 | + `score` int(10) unsigned not null default 0, |
| 12 | + `name` varchar(128) not null default '', |
| 13 | + `address` varchar(256), |
| 14 | + `create_time` datetime not null, |
| 15 | + primary key(`id`) |
| 16 | +) |
| 17 | +
|
| 18 | +*/ |
14 | 19 |
|
| 20 | +int main() |
| 21 | +{ |
15 | 22 | InsertModel i; |
16 | | - i.insert("id", 32312) |
| 23 | + i.insert("score", 100) |
17 | 24 | .insert("name", std::string("six")) |
| 25 | + .insert("age", (unsigned char)20) |
18 | 26 | .insert("address", "beijing") |
19 | | - .insert("time", time(NULL)) |
20 | | - .insert("age", (int64_t)15323892489203488) |
21 | | - .into("info"); |
| 27 | + .insert("create_time", time(NULL)) |
| 28 | + .into("user"); |
22 | 29 | std::cout<<i.str()<<std::endl; |
| 30 | + // insert into user(score, name, age, address, create_time) values(100, 'six', 20, 'beijing', '2016-03-25 10:15:59') |
23 | 31 |
|
24 | 32 | SelectModel s; |
25 | | - s.select("aaa", "bbb", "ccc") |
26 | | - .from("table") |
27 | | - .where(column("aaa") >= 0 and column("bbb").is_null() or column("ddd") != 1 and column("eee").in(a)); |
| 33 | + s.select("id", "age", "name") |
| 34 | + .from("user") |
| 35 | + .where(column("score") > 60 and (column("age") >= 20 or column("address").is_not_null())); |
28 | 36 | std::cout<<s<<std::endl; |
| 37 | + // select id, age, name from user where (score > 60) and ((age >= 20) or (address is not null)) |
29 | 38 |
|
| 39 | + std::vector<int> a = {1, 2, 3}; |
30 | 40 | UpdateModel u; |
31 | | - u.update("table") |
32 | | - .set("id", 2) |
33 | | - .set("name", "six") |
34 | | - .where(column("aaa") >= 0 and column("bbb").is_null() or column("ddd") != 1 and column("eee").in(a)); |
| 41 | + u.update("user") |
| 42 | + .set("name", "ddc") |
| 43 | + .set("age", 18) |
| 44 | + .where(column("id").in(a)); |
35 | 45 | std::cout<<u<<std::endl; |
| 46 | + // update user set name = 'ddc', age = 18 where id in (1, 2, 3) |
36 | 47 |
|
37 | 48 | DeleteModel d; |
38 | 49 | d._delete() |
39 | | - .from("table") |
40 | | - .where(column("aaa") >= 0 and column("bbb").is_null() or column("ddd") != 1 and column("eee").in(a)); |
| 50 | + .from("user") |
| 51 | + .where(column("id") > 1); |
41 | 52 | std::cout<<d<<std::endl; |
| 53 | + // delete from user where id > 1 |
42 | 54 |
|
43 | 55 | SqlModel m; |
44 | | - m["id"] = 1; |
45 | | - m["name"] = "six-ddc"; |
| 56 | + m["address"] = "chengdu"; |
| 57 | + m["score"] = 80; |
| 58 | + m["create_time"] = time(NULL); |
46 | 59 |
|
47 | 60 | u.reset(); |
48 | | - u.update("table") |
| 61 | + u.update("user") |
49 | 62 | .set(m); |
50 | 63 | std::cout<<u<<std::endl; |
| 64 | + // update user set address = 'chengdu', create_time = '2016-03-25 10:33:32', score = 80 |
51 | 65 |
|
52 | 66 | i.reset(); |
53 | 67 | i.insert(m) |
54 | | - .into("table"); |
| 68 | + .into("user"); |
55 | 69 | std::cout<<i<<std::endl; |
56 | | -} |
| 70 | + // insert into user(address, create_time, score) values('chengdu', '2016-03-25 10:33:32', 80) |
57 | 71 |
|
58 | | -BOOST_AUTO_TEST_SUITE_END() |
| 72 | + return 0; |
| 73 | +} |
0 commit comments