-
Notifications
You must be signed in to change notification settings - Fork 96
/
dao.hpp
173 lines (144 loc) · 3.67 KB
/
dao.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
165
166
167
168
169
170
171
172
173
//
// Created by root on 4/24/18.
//
#ifndef FEATHER_DAO_HPP
#define FEATHER_DAO_HPP
#include "NanoLog.hpp"
namespace feather {
template <typename DB>
class dao_t final {
public:
dao_t() : conn_(ormpp::connection_pool<DB>::instance().get()), guard_(conn_) {
if (!conn_)
LOG_WARN << "db get connection failed";
}
bool is_open() const { return conn_ != nullptr; }
template <typename T>
bool create_table(const ormpp_auto_key& key) {
if (!conn_)
return false;
return conn_->template create_datatable<T>(key);
}
template <typename T, typename... Args>
bool create_table(Args&&... args) {
if (!conn_)
return false;
return conn_->template create_datatable<T>(std::forward<Args>(args)...);
}
template <typename T>
bool drop_table() {
if (!conn_)
return false;
constexpr auto name = iguana::get_name<T>();
return conn_->execute("drop table if exists " +
std::string(name.data(), name.length()));
}
template <typename T>
int add_object(T& t) {
if (!conn_)
return -1;
conn_->begin();
int n = conn_->insert(t);
if (n < 0) {
conn_->rollback();
LOG_WARN << "insert role failed";
return -1;
}
auto v = conn_->template query<std::tuple<int>>("SELECT LAST_INSERT_ID();");
if (v.empty()) {
conn_->rollback();
LOG_WARN << "db failed";
return -1;
}
conn_->commit();
int seq = std::get<0>(v[0]);
return seq;
}
template <typename T>
int add_objects(const T& t) {
if (!conn_)
return -1;
int n = conn_->insert(t);
if (n < 0) {
LOG_WARN << "insert failed";
return -1;
}
return n;
}
template <typename T>
bool remove_object(const std::string& condition) {
if (!conn_)
return false;
bool r = conn_->template delete_records<T>(condition);
if (!r) {
LOG_WARN << "insert role failed";
return false;
}
return true;
}
template <typename T, typename... Args>
bool get_object(std::vector<T>& v, std::string where_condition = "",
Args&&... args) {
if (!conn_)
return false;
v = conn_->template query<T>(std::move(where_condition),
std::forward<Args>(args)...);
if (conn_->has_error())
return false;
return true;
}
template <typename T>
int update_object(const T& t) {
if (!conn_)
return -1;
int n = conn_->update(t);
if (n < 0) {
LOG_WARN << "insert role failed";
return -1;
}
return n;
}
template <typename T>
auto query(const std::string& sql) {
return conn_->template query<T>(sql);
}
// non query sql, such as update, delete, insert
bool execute(const std::string& sql) {
if (!conn_)
return false;
bool r = conn_->execute(sql);
if (!r) {
LOG_WARN << "insert role failed";
return false;
}
return r;
}
bool begin() {
if (!conn_)
return false;
return conn_->begin();
}
bool rollback() {
if (!conn_)
return false;
return conn_->rollback();
}
bool commit() {
if (!conn_)
return false;
return conn_->commit();
}
static void init(int max_conns, const char* ip, const char* usr,
const char* pwd, const char* db_name, int timeout = 1,
int port = 3306) {
ormpp::connection_pool<DB>::instance().init(max_conns, ip, usr, pwd,
db_name, timeout, port);
}
private:
dao_t(const dao_t&) = delete;
dao_t& operator=(const dao_t&) = delete;
std::shared_ptr<DB> conn_;
ormpp::conn_guard<DB> guard_;
};
} // namespace feather
#endif // FEATHER_DAO_HPP