Skip to content

Commit 80c6f88

Browse files
committed
add is_not_null
1 parent b0e08f1 commit 80c6f88

4 files changed

Lines changed: 54 additions & 93 deletions

File tree

makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
all: sql.cpp
22
g++ -std=c++11 -o libsql_builder.so -shared -fPIC -D_DEBUG sql.cpp
33
test: test.cpp all
4-
g++ -std=c++11 -g -ggdb -lboost_system -lboost_test_exec_monitor -lsql_builder -L. -D_DEBUG -o test test.cpp
4+
g++ -std=c++11 -g -ggdb -lsql_builder -L. -D_DEBUG -o test test.cpp
55
run: test
66
./test

sql.cpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,17 @@
33
#include <sstream>
44

55
template <>
6-
std::string SqlHelper::to_string<std::string>(const std::string& data, const std::string& op/* = ""*/) {
7-
if(op == "is") {
8-
if(data == "null" || data == "NULL") {
9-
return data;
10-
}
11-
}
6+
std::string SqlHelper::to_string<std::string>(const std::string& data) {
127
return "'" + data + "'";
138
}
149

1510
template <>
16-
std::string SqlHelper::to_string<const char*>(const char* const& data, const std::string& op/* = ""*/) {
17-
if(op == "is") {
18-
std::string a(data);
19-
if(a == "null" || a == "NULL") {
20-
return a;
21-
}
22-
}
11+
std::string SqlHelper::to_string<const char*>(const char* const& data) {
2312
return "'" + std::string(data) + "'";
2413
}
2514

2615
template <>
27-
std::string SqlHelper::to_string<time_t>(const time_t& data, const std::string& op/* = ""*/) {
16+
std::string SqlHelper::to_string<time_t>(const time_t& data) {
2817
char buff[128] = {0};
2918
struct tm* ttime = localtime(&data);
3019
strftime(buff, sizeof(buff), "%Y-%m-%d %H:%M:%S", ttime);

sql.h

Lines changed: 9 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,24 @@ class SqlHelper
1313
{
1414
public:
1515
template <typename T>
16-
static std::string to_string(const T& data, const std::string& op = "") {
16+
static std::string to_string(const T& data) {
1717
return std::to_string(data);
1818
}
1919

2020
template <size_t N>
21-
static std::string to_string(char const(&data)[N], const std::string& op = "") {
22-
if(op == "is") {
23-
std::string a(data);
24-
if(a == "null" || a == "NULL") {
25-
return a;
26-
}
27-
}
21+
static std::string to_string(char const(&data)[N]) {
2822
return "'" + std::string(data) + "'";
2923
}
3024
};
3125

3226
template <>
33-
std::string SqlHelper::to_string<std::string>(const std::string& data, const std::string& op/* = ""*/);
27+
std::string SqlHelper::to_string<std::string>(const std::string& data);
3428

3529
template <>
36-
std::string SqlHelper::to_string<const char*>(const char* const& data, const std::string& op/* = ""*/);
30+
std::string SqlHelper::to_string<const char*>(const char* const& data);
3731

3832
template <>
39-
std::string SqlHelper::to_string<time_t>(const time_t& data, const std::string& op/* = ""*/);
33+
std::string SqlHelper::to_string<time_t>(const time_t& data);
4034

4135
class SqlModel
4236
{
@@ -274,47 +268,6 @@ class DeleteModel
274268
std::string _sql;
275269
};
276270

277-
/*
278-
template <typename... Args>
279-
std::string AND(Args&&... args) {
280-
std::string a[] = {args...};
281-
int size = sizeof...(args);
282-
std::stringstream ss;
283-
ss<<"(";
284-
for(int i = 0; i < size; ++i) {
285-
if(i < size - 1) {
286-
ss<<a[i]<<" and ";
287-
} else {
288-
ss<<a[i];
289-
}
290-
}
291-
ss<<")";
292-
return ss.str();
293-
}
294-
295-
template <typename... Args>
296-
std::string OR(Args&&... args) {
297-
std::string a[] = {args...};
298-
int size = sizeof...(args);
299-
std::stringstream ss;
300-
ss<<"(";
301-
for(int i = 0; i < size; ++i) {
302-
if(i < size - 1) {
303-
ss<<a[i]<<" or ";
304-
} else {
305-
ss<<a[i];
306-
}
307-
}
308-
ss<<")";
309-
return ss.str();
310-
}
311-
312-
template <typename T>
313-
static std::string MAKE_WHERE(const std::string& c, const std::string& op, const T& data) {
314-
return c + " " + op + " " + SqlHelper::to_string(data, op);
315-
}
316-
*/
317-
318271
class column
319272
{
320273
public:
@@ -327,6 +280,10 @@ class column
327280
return *this;
328281
}
329282

283+
column& is_not_null() {
284+
_cond += " is not null";
285+
return *this;
286+
}
330287

331288
template <typename T>
332289
column& in(const std::vector<T>& args) {

test.cpp

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,73 @@
1-
#define BOOST_TEST_MODULE sqltest
2-
31
#include <iostream>
42
#include <sstream>
53

6-
#include <boost/test/unit_test.hpp>
74
#include "sql.h"
85

9-
BOOST_AUTO_TEST_SUITE(sqltest)
6+
/*
107
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+
*/
1419

20+
int main()
21+
{
1522
InsertModel i;
16-
i.insert("id", 32312)
23+
i.insert("score", 100)
1724
.insert("name", std::string("six"))
25+
.insert("age", (unsigned char)20)
1826
.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");
2229
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')
2331

2432
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()));
2836
std::cout<<s<<std::endl;
37+
// select id, age, name from user where (score > 60) and ((age >= 20) or (address is not null))
2938

39+
std::vector<int> a = {1, 2, 3};
3040
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));
3545
std::cout<<u<<std::endl;
46+
// update user set name = 'ddc', age = 18 where id in (1, 2, 3)
3647

3748
DeleteModel d;
3849
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);
4152
std::cout<<d<<std::endl;
53+
// delete from user where id > 1
4254

4355
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);
4659

4760
u.reset();
48-
u.update("table")
61+
u.update("user")
4962
.set(m);
5063
std::cout<<u<<std::endl;
64+
// update user set address = 'chengdu', create_time = '2016-03-25 10:33:32', score = 80
5165

5266
i.reset();
5367
i.insert(m)
54-
.into("table");
68+
.into("user");
5569
std::cout<<i<<std::endl;
56-
}
70+
// insert into user(address, create_time, score) values('chengdu', '2016-03-25 10:33:32', 80)
5771

58-
BOOST_AUTO_TEST_SUITE_END()
72+
return 0;
73+
}

0 commit comments

Comments
 (0)