forked from six-ddc/sql-builder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.hpp
164 lines (139 loc) · 3.77 KB
/
model.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#pragma once
#include "adapter.hpp"
#include "col.hpp"
#include <memory>
#include <vector>
#include <functional>
namespace boosql
{
class model
{
public:
const std::string & table_name()
{
return _table_name;
}
model()
{
_adapter = new sqlite_adapter();
_auto_delete_adapter = true;
}
model(const model & m)
{
_adapter = m._adapter->clone();
_auto_delete_adapter = true;
_table_name = m._table_name;
_where_condition = m._where_condition;
}
model(adapter * adapter) : _adapter(adapter) {}
model(adapter * adapter, const std::string & table_name)
: _adapter(adapter), _table_name(table_name)
{
}
model(const std::string & table_name)
{
_adapter = new sqlite_adapter();
_auto_delete_adapter = true;
_table_name = table_name;
}
virtual std::string where_str()
{
std::string ret;
for(auto i = _where_condition.begin(); i != _where_condition.end(); ++i) {
ret.append((*i).str(_adapter, table_name()));
}
return ret;
}
virtual std::string where_str(std::vector<std::string> & params)
{
std::string ret;
for(auto i = _where_condition.begin(); i != _where_condition.end(); ++i) {
ret.append((*i).str(_adapter, table_name(), params));
}
return ret;
}
virtual ~model()
{
if (_auto_delete_adapter) {
delete _adapter;
}
}
virtual const std::string& str() = 0;
virtual const std::string& str(std::vector<std::string> &) = 0;
protected:
void append_where()
{
std::string w = where_str();
if (w.length() > 0) {
_sql.append( " WHERE " );
_sql.append(w);
}
}
void append_where(std::vector<std::string> & params)
{
std::string w = where_str(params);
if (w.length() > 0) {
_sql.append( " WHERE " );
_sql.append(w);
}
}
void and_where(const std::string & condition)
{
_where_condition.push_back(col().o_and());
_where_condition.push_back(col()(condition));
}
void and_where(const col & condition)
{
_where_condition.push_back(col().o_and());
_where_condition.push_back(condition);
}
void or_where(const std::string & condition)
{
_where_condition.push_back(col().o_or());
_where_condition.push_back(col()(condition));
}
void or_where(const col & condition)
{
_where_condition.push_back(col().o_or());
_where_condition.push_back(condition);
}
template <class T>
void quote(std::function<void(T& model)> callback, T& model)
{
if (_where_condition.size() > 0) {
_where_condition.push_back(col().o_and());
}
_where_condition.push_back(col().quote_begin());
callback(model);
_where_condition.push_back(col().quote_end());
}
void where(const std::string& condition) {
if (_where_condition.size() > 0) {
_where_condition.push_back(col().o_and());
}
_where_condition.push_back(col()(condition));
}
void where(const col& condition) {
int s = _where_condition.size();
if (s > 0) {
col last = _where_condition.back();
if (!last.empty() && last.last().val != "(") {
_where_condition.push_back(col().o_and());
}
}
_where_condition.push_back(condition);
}
void reset()
{
_table_name.clear();
_where_condition.clear();
}
protected:
std::string _sql;
adapter * _adapter;
bool _auto_delete_adapter = false;
std::string _table_name;
private:
std::vector<col> _where_condition;
};
}