forked from ZoneMinder/zoneminder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzm_db.cpp
296 lines (264 loc) · 8.66 KB
/
zm_db.cpp
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//
// ZoneMinder MySQL Implementation, $Date$, $Revision$
// Copyright (C) 2001-2008 Philip Coombes
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#include "zm_db.h"
#include "zm_logger.h"
#include "zm_signal.h"
#include <cstdlib>
MYSQL dbconn;
std::mutex db_mutex;
zmDbQueue dbQueue;
bool zmDbConnected = false;
bool zmDbConnect() {
// For some reason having these lines causes memory corruption and crashing on newer debian/ubuntu
// But they really need to be here in order to prevent a double open of mysql
if ( zmDbConnected ) {
//Warning("Calling zmDbConnect when already connected");
return true;
}
if ( !mysql_init(&dbconn) ) {
Error("Can't initialise database connection: %s", mysql_error(&dbconn));
return false;
}
bool reconnect = 1;
if ( mysql_options(&dbconn, MYSQL_OPT_RECONNECT, &reconnect) )
Error("Can't set database auto reconnect option: %s", mysql_error(&dbconn));
if ( !staticConfig.DB_SSL_CA_CERT.empty() ) {
mysql_ssl_set(&dbconn,
staticConfig.DB_SSL_CLIENT_KEY.c_str(),
staticConfig.DB_SSL_CLIENT_CERT.c_str(),
staticConfig.DB_SSL_CA_CERT.c_str(),
nullptr, nullptr);
}
std::string::size_type colonIndex = staticConfig.DB_HOST.find(":");
if ( colonIndex == std::string::npos ) {
if ( !mysql_real_connect(
&dbconn,
staticConfig.DB_HOST.c_str(),
staticConfig.DB_USER.c_str(),
staticConfig.DB_PASS.c_str(),
nullptr, 0, nullptr, 0) ) {
Error("Can't connect to server: %s", mysql_error(&dbconn));
mysql_close(&dbconn);
return false;
}
} else {
std::string dbHost = staticConfig.DB_HOST.substr(0, colonIndex);
std::string dbPortOrSocket = staticConfig.DB_HOST.substr(colonIndex+1);
if ( dbPortOrSocket[0] == '/' ) {
if ( !mysql_real_connect(
&dbconn,
nullptr,
staticConfig.DB_USER.c_str(),
staticConfig.DB_PASS.c_str(),
nullptr, 0, dbPortOrSocket.c_str(), 0) ) {
Error("Can't connect to server: %s", mysql_error(&dbconn));
mysql_close(&dbconn);
return false;
}
} else {
if ( !mysql_real_connect(
&dbconn,
dbHost.c_str(),
staticConfig.DB_USER.c_str(),
staticConfig.DB_PASS.c_str(),
nullptr,
atoi(dbPortOrSocket.c_str()),
nullptr, 0) ) {
Error("Can't connect to server: %s", mysql_error(&dbconn));
mysql_close(&dbconn);
return false;
}
}
}
if ( mysql_select_db(&dbconn, staticConfig.DB_NAME.c_str()) ) {
Error("Can't select database: %s", mysql_error(&dbconn));
mysql_close(&dbconn);
return false;
}
if ( mysql_query(&dbconn, "SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED") ) {
Error("Can't set isolation level: %s", mysql_error(&dbconn));
}
mysql_set_character_set(&dbconn, "utf8");
zmDbConnected = true;
return zmDbConnected;
}
void zmDbClose() {
std::lock_guard<std::mutex> lck(db_mutex);
if (zmDbConnected) {
mysql_close(&dbconn);
// mysql_init() call implicitly mysql_library_init() but
// mysql_close() does not call mysql_library_end()
mysql_library_end();
zmDbConnected = false;
}
}
MYSQL_RES *zmDbFetch(const std::string &query) {
std::lock_guard<std::mutex> lck(db_mutex);
if (!zmDbConnected) {
Error("Not connected.");
return nullptr;
}
if (mysql_query(&dbconn, query.c_str())) {
Error("Can't run query: %s", mysql_error(&dbconn));
return nullptr;
}
MYSQL_RES *result = mysql_store_result(&dbconn);
if (!result) {
Error("Can't use query result: %s for query %s", mysql_error(&dbconn), query.c_str());
}
return result;
}
zmDbRow *zmDbFetchOne(const std::string &query) {
zmDbRow *row = new zmDbRow();
if (row->fetch(query)) {
return row;
}
delete row;
return nullptr;
}
MYSQL_RES *zmDbRow::fetch(const std::string &query) {
result_set = zmDbFetch(query);
if (!result_set) return result_set;
int n_rows = mysql_num_rows(result_set);
if (n_rows != 1) {
Error("Bogus number of lines return from query, %d returned for query %s.", n_rows, query.c_str());
mysql_free_result(result_set);
result_set = nullptr;
return result_set;
}
row = mysql_fetch_row(result_set);
if (!row) {
mysql_free_result(result_set);
result_set = nullptr;
Error("Error getting row from query %s. Error is %s", query.c_str(), mysql_error(&dbconn));
} else {
Debug(5, "Success");
}
return result_set;
}
int zmDbDo(const std::string &query) {
std::lock_guard<std::mutex> lck(db_mutex);
if (!zmDbConnected)
return 0;
int rc;
while ((rc = mysql_query(&dbconn, query.c_str())) and !zm_terminate) {
Logger *logger = Logger::fetch();
Logger::Level oldLevel = logger->databaseLevel();
logger->databaseLevel(Logger::NOLOG);
Error("Can't run query %s: %s", query.c_str(), mysql_error(&dbconn));
logger->databaseLevel(oldLevel);
if ( (mysql_errno(&dbconn) != ER_LOCK_WAIT_TIMEOUT) ) {
return rc;
}
}
Logger *logger = Logger::fetch();
Logger::Level oldLevel = logger->databaseLevel();
logger->databaseLevel(Logger::NOLOG);
Debug(1, "Success running sql query %s", query.c_str());
logger->databaseLevel(oldLevel);
return 1;
}
int zmDbDoInsert(const std::string &query) {
std::lock_guard<std::mutex> lck(db_mutex);
if (!zmDbConnected) return 0;
int rc;
while ( (rc = mysql_query(&dbconn, query.c_str())) and !zm_terminate) {
Error("Can't run query %s: %s", query.c_str(), mysql_error(&dbconn));
if ( (mysql_errno(&dbconn) != ER_LOCK_WAIT_TIMEOUT) )
return 0;
}
int id = mysql_insert_id(&dbconn);
Debug(2, "Success running sql insert %s. Resulting id is %d", query.c_str(), id);
return id;
}
int zmDbDoUpdate(const std::string &query) {
std::lock_guard<std::mutex> lck(db_mutex);
if (!zmDbConnected) return 0;
int rc;
while ( (rc = mysql_query(&dbconn, query.c_str())) and !zm_terminate) {
Error("Can't run query %s: %s", query.c_str(), mysql_error(&dbconn));
if ( (mysql_errno(&dbconn) != ER_LOCK_WAIT_TIMEOUT) )
return -rc;
}
int affected = mysql_affected_rows(&dbconn);
Debug(2, "Success running sql update %s. Rows modified %d", query.c_str(), affected);
return affected;
}
zmDbRow::~zmDbRow() {
if (result_set) {
mysql_free_result(result_set);
result_set = nullptr;
}
row = nullptr;
}
zmDbQueue::zmDbQueue() :
mThread(&zmDbQueue::process, this),
mTerminate(false)
{ }
zmDbQueue::~zmDbQueue() {
stop();
}
void zmDbQueue::stop() {
{
std::unique_lock<std::mutex> lock(mMutex);
mTerminate = true;
}
mCondition.notify_all();
if (mThread.joinable()) mThread.join();
}
void zmDbQueue::process() {
std::unique_lock<std::mutex> lock(mMutex);
while (!mTerminate and !zm_terminate) {
if (mQueue.empty()) {
mCondition.wait(lock);
}
while (!mQueue.empty()) {
if (mQueue.size() > 30) {
Logger *log = Logger::fetch();
Logger::Level db_level = log->databaseLevel();
log->databaseLevel(Logger::NOLOG);
Warning("db queue size has grown larger %zu than 20 entries", mQueue.size());
log->databaseLevel(db_level);
}
std::string sql = mQueue.front();
mQueue.pop();
// My idea for leaving the locking around each sql statement is to allow
// other db writers to get a chance
lock.unlock();
zmDbDo(sql);
lock.lock();
}
}
} // end void zmDbQueue::process()
void zmDbQueue::push(std::string &&sql) {
{
std::unique_lock<std::mutex> lock(mMutex);
if (mTerminate) return;
mQueue.push(std::move(sql));
}
mCondition.notify_all();
}
std::string zmDbEscapeString(const std::string& to_escape) {
// According to docs, size of safer_whatever must be 2 * length + 1
// due to unicode conversions + null terminator.
std::string escaped((to_escape.length() * 2) + 1, '\0');
size_t escaped_len = mysql_real_escape_string(&dbconn, &escaped[0], to_escape.c_str(), to_escape.length());
escaped.resize(escaped_len);
return escaped;
}