forked from btccom/btcpool-ABANDONED
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQLConnection.h
More file actions
165 lines (141 loc) · 3.94 KB
/
Copy pathMySQLConnection.h
File metadata and controls
165 lines (141 loc) · 3.94 KB
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
/*
The MIT License (MIT)
Copyright (c) [2016] [BTC.COM]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#pragma once
#include <string>
#include <vector>
#include <set>
#include <queue>
#include <mutex>
#include <atomic>
#include <thread>
#include <condition_variable>
using std::string;
using std::vector;
using std::set;
extern "C" struct st_mysql;
typedef struct st_mysql MYSQL;
extern "C" struct st_mysql_res;
typedef struct st_mysql_res MYSQL_RES;
/**
* Simple wrapper for MYSQL_RES
* auto free
*/
struct MySQLResult {
struct st_mysql_res *result;
MySQLResult();
MySQLResult(MYSQL_RES *result);
void reset(MYSQL_RES *result);
~MySQLResult();
uint64_t numRows();
uint32_t fields();
char **nextRow();
};
class MysqlConnectInfo {
public:
string host_;
int32_t port_;
string username_;
string password_;
string dbName_;
MysqlConnectInfo(
const string &host,
int32_t port,
const string &userName,
const string &password,
const string &dbName)
: host_(host)
, port_(port)
, username_(userName)
, password_(password)
, dbName_(dbName) {}
MysqlConnectInfo(const MysqlConnectInfo &r) {
host_ = r.host_;
port_ = r.port_;
username_ = r.username_;
password_ = r.password_;
dbName_ = r.dbName_;
}
MysqlConnectInfo &operator=(const MysqlConnectInfo &r) {
host_ = r.host_;
port_ = r.port_;
username_ = r.username_;
password_ = r.password_;
dbName_ = r.dbName_;
return *this;
}
};
/**
* Simple wrapper for MYSQL connection
* open/close auto managed by class
* with support for auto-reconnect
* @see test/TestMySQLConnection.cc for demo usage
*/
class MySQLConnection {
protected:
string host_;
int32_t port_;
string username_;
string password_;
string dbName_;
struct st_mysql *conn;
public:
MySQLConnection(const MysqlConnectInfo &connectInfo);
~MySQLConnection();
bool open();
void close();
bool ping();
bool reconnect();
bool execute(const char *sql);
bool execute(const string &sql) { return execute(sql.c_str()); }
bool query(const char *sql, MySQLResult &result);
bool query(const string &sql, MySQLResult &result) {
return query(sql.c_str(), result);
}
// return -1 on failure
int64_t update(const char *sql);
int64_t update(const string &sql) { return update(sql.c_str()); }
uint64_t affectedRows();
uint64_t getInsertId();
string getVariable(const char *name);
};
bool multiInsert(
MySQLConnection &db,
const string &table,
const string &fields,
const vector<string> &values);
/**
* Execute SQL statements in order
*/
class MySQLExecQueue {
protected:
MysqlConnectInfo dbInfo_;
std::queue<std::string> sqlQueue_;
std::mutex sqlQueueLock_;
std::condition_variable notify_;
std::atomic<bool> running_;
std::thread thread_;
void run();
void stop();
void execSQL(const string &sql);
public:
MySQLExecQueue(const MysqlConnectInfo &dbInfo);
~MySQLExecQueue();
void addSQL(const string &sql);
};