-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataoperation_mysql.cpp
292 lines (237 loc) · 8.3 KB
/
dataoperation_mysql.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
#include "dataoperation_mysql.h"
#include <iostream>
using namespace std;
DataHandlerLoadOperationMysql::DataHandlerLoadOperationMysql(MYSQL* connection, std::string id, std::string tableName)
: m_Connection(connection)
, m_ID(id)
, m_TableName(tableName)
{
m_Statement = mysql_stmt_init(m_Connection);
m_StatementCloser.statement = m_Statement;
stringstream ss;
ss << "SELECT data FROM " << m_TableName << " WHERE id=?";
string selectStatement(ss.str());
mysql_stmt_prepare(m_Statement, selectStatement.c_str(), selectStatement.length());
memset(m_BindParam, 0, sizeof(m_BindParam));
m_BindParam[0].buffer_type = MYSQL_TYPE_STRING;
m_BindParam[0].buffer = (void*)m_ID.c_str();
m_BindParam[0].buffer_length = m_ID.length();
mysql_stmt_bind_param(m_Statement, m_BindParam);
}
DataHandlerLoadOperationMysql::~DataHandlerLoadOperationMysql()
{
}
bool DataHandlerLoadOperationMysql::DoLoad(std::vector<char>& outData)
{
if(mysql_stmt_execute(m_Statement))
return false;
my_bool updateMaxLenFlag = true;
mysql_stmt_attr_set(m_Statement, STMT_ATTR_UPDATE_MAX_LENGTH, &updateMaxLenFlag);
mysql_stmt_store_result(m_Statement);
unsigned long dataLen = 0;
MYSQL_BIND bindResult[1];
memset(bindResult, 0, sizeof(bindResult));
bindResult[0].buffer_type = MYSQL_TYPE_LONG_BLOB;
bindResult[0].length = &dataLen;
if(mysql_stmt_bind_result(m_Statement, bindResult))
{
return false;
}
auto fetchRes = mysql_stmt_fetch(m_Statement);
if(fetchRes && fetchRes != MYSQL_DATA_TRUNCATED)
{
return false;
}
outData.resize(dataLen);
bindResult[0].buffer = outData.data();
bindResult[0].buffer_length = outData.size();
auto fetchColRes = mysql_stmt_fetch_column(m_Statement, bindResult, 0, 0);
return fetchColRes == 0;
}
MysqlDataOperationManager::MysqlDataOperationManager(const std::string& server, const std::string& username, const std::string& password, const std::string& dbname, std::string tablename, int port)
: m_Connection(nullptr)
, m_TableName(std::move(tablename))
, m_ownConnection(true)
{
m_Connection = mysql_init(NULL);
if(!mysql_real_connect(m_Connection, server.c_str(), username.c_str()
, password.c_str(), dbname.c_str(), port, NULL, 0))
{
mysql_close(m_Connection);
m_Connection = nullptr;
}
}
MysqlDataOperationManager::MysqlDataOperationManager(MYSQL* mysqlConnection, std::string tablename)
: m_Connection(mysqlConnection)
, m_TableName(std::move(tablename))
, m_ownConnection(false)
{
}
MysqlDataOperationManager::~MysqlDataOperationManager()
{
if(m_ownConnection && m_Connection)
{
mysql_close(m_Connection);
m_Connection = nullptr;
}
}
bool MysqlDataOperationManager::IsExists(const std::string& id) const
{
MYSQL_STMT* selectStatement = mysql_stmt_init(m_Connection);
StatementCloser closer;
closer.statement = selectStatement;
stringstream ss;
ss << "SELECT id FROM " << m_TableName << " WHERE id=?";
std::string statementStr(ss.str());
if(mysql_stmt_prepare(selectStatement, statementStr.c_str(), statementStr.length()))
{
return false;
}
MYSQL_BIND bindParam[1];
memset(bindParam, 0, sizeof(bindParam));
bindParam[0].buffer_type = MYSQL_TYPE_STRING;
bindParam[0].buffer = (void*)id.c_str();
bindParam[0].buffer_length = id.length();
if(mysql_stmt_bind_param(selectStatement, bindParam))
{
return false;
}
if(mysql_stmt_execute(selectStatement))
{
return false;
}
mysql_stmt_store_result(selectStatement);
auto rowCount = mysql_stmt_num_rows(selectStatement);
return rowCount > 0;
}
std::unique_ptr<DataHandler> MysqlDataOperationManager::GetDataHandler(std::string id) const
{
std::unique_ptr<DataHandler> result;
if(IsExists(id))
{
auto mysqlOperation = new DataHandlerLoadOperationMysql(m_Connection, id, m_TableName);
result = make_unique<DataHandler>(mysqlOperation);
}
return result;
}
std::vector<std::string> MysqlDataOperationManager::GetDataList(std::regex regex, bool checkNotation) const
{
std::vector<std::string> result;
MYSQL_STMT* selectStatement = mysql_stmt_init(m_Connection);
StatementCloser closer;
closer.statement = selectStatement;
stringstream ss;
ss << "SELECT id FROM " << m_TableName;
std::string statementStr(ss.str());
if(mysql_stmt_prepare(selectStatement, statementStr.c_str(), statementStr.length()))
{
return result;
}
MYSQL_BIND bindResult[1];
memset(bindResult, 0, sizeof(bindResult));
char idBuffer[256];
bindResult[0].buffer_type = MYSQL_TYPE_STRING;
bindResult[0].buffer = idBuffer;
bindResult[0].buffer_length = sizeof(idBuffer);
if (mysql_stmt_bind_result(selectStatement, bindResult))
{
return result;
}
if(mysql_stmt_execute(selectStatement))
{
return result;
}
mysql_stmt_store_result(selectStatement);
while (!mysql_stmt_fetch(selectStatement))
{
result.emplace_back(idBuffer);
}
return result;
}
std::unique_ptr<DataHandler> MysqlDataOperationManager::StoreData(std::string id, std::vector<char>&& data, bool forceOverwrite)
{
std::unique_ptr<DataHandler> result;
bool rowExisted = IsExists(id);
if(rowExisted && !forceOverwrite)
{
// TODO: warning file existed and not force overwrite
return result;
}
MYSQL_STMT* insertStatement = mysql_stmt_init(m_Connection);
StatementCloser closer;
closer.statement = insertStatement;
stringstream ss;
ss << "INSERT INTO " << m_TableName << "(id, data) VALUES (?, ?)";
std::string insertStatementStr(ss.str());
if(mysql_stmt_prepare(insertStatement, insertStatementStr.c_str(), insertStatementStr.length()))
{
return result;
}
MYSQL_BIND bindParam[2];
memset(bindParam, 0, sizeof(bindParam));
auto idLen = id.length();
bindParam[0].buffer_type = MYSQL_TYPE_STRING;
bindParam[0].buffer = (void*)id.c_str();
bindParam[0].buffer_length = idLen;
bindParam[0].length = &idLen;
auto dataSize = data.size();
bindParam[1].buffer_type = MYSQL_TYPE_LONG_BLOB;
bindParam[1].buffer = data.data();
bindParam[1].buffer_length = dataSize;
bindParam[1].length = &dataSize;
if (mysql_stmt_bind_param(insertStatement, bindParam))
{
return result;
}
// const unsigned long _maxChuckSize = 1000000;
// unsigned long dataLen = data.size();
// unsigned long offset = 0;
// while(offset < dataLen)
// {
// if (mysql_stmt_send_long_data(insertStatement, 1, data.data() + offset, std::min(dataLen - offset, _maxChuckSize)))
// {
// // fprintf(stderr, "\n send_long_data failed");
// // fprintf(stderr, "\n %s", mysql_stmt_error(insertStatement));
// return result;
// }
// offset += _maxChuckSize;
// }
if (mysql_stmt_execute(insertStatement))
{
std::cout << "execute err: " << mysql_stmt_error(insertStatement) << "\n";
return result;
}
auto mysqlOperation = new DataHandlerLoadOperationMysql(m_Connection, id, m_TableName);
result = make_unique<DataHandler>(mysqlOperation, std::move(data));
return result;
}
bool MysqlDataOperationManager::DeleteData(const std::string& id)
{
MYSQL_STMT* deleteStatement = mysql_stmt_init(m_Connection);
StatementCloser closer;
closer.statement = deleteStatement;
stringstream ss;
ss << "DELETE FROM " << m_TableName << " WHERE id=?";
std::string deleteStatementStr(ss.str());
if(mysql_stmt_prepare(deleteStatement, deleteStatementStr.c_str(), deleteStatementStr.length()))
{
return false;
}
MYSQL_BIND bindParam[1];
memset(bindParam, 0, sizeof(bindParam));
auto idLen = id.length();
bindParam[0].buffer_type = MYSQL_TYPE_STRING;
bindParam[0].buffer = (void*)id.c_str();
bindParam[0].buffer_length = idLen;
bindParam[0].length = &idLen;
if (mysql_stmt_bind_param(deleteStatement, bindParam))
{
return false;
}
if (mysql_stmt_execute(deleteStatement))
{
return false;
}
auto affectedRow = mysql_stmt_affected_rows(deleteStatement);
return affectedRow > 0;
}