-
Notifications
You must be signed in to change notification settings - Fork 1
/
CMysqlDb.cpp
149 lines (124 loc) · 3.59 KB
/
CMysqlDb.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
/*
* Copyright (c) 2012 Alexandros Nikolopoulos <[email protected]>
*
* 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.
*/
#include "CMysqlDb.h"
CMysqlDb::CMysqlDb(map<string, string> settings)
{
m_host = settings["host"];
m_user = settings["username"];
m_pass = settings["password"];
m_db = settings["db"];
m_table = settings["table"];
m_port = atoi(settings["port"].c_str());
mysql_init(&m_conn);
pthread_mutex_init(&queryMutex, NULL);
}
CMysqlDb::~CMysqlDb()
{
mysql_close(&m_conn);
}
int
CMysqlDb::Connect(void)
{
if(m_host.length() == 0 || m_user.length() == 0 || m_pass.length() == 0 || m_db.length() == 0)
{
Log.error("Mysql Configuration error\n");
return false;
}
if(!(m_port > 0 && m_port < 65536))
{
Log.error("Mysql Configuration error\n");
return false;
}
pthread_mutex_lock(&queryMutex);
mysql_close(&m_conn);
mysql_init(&m_conn);
mysql_options(&m_conn,MYSQL_OPT_COMPRESS,0);
unsigned int connectTimeout = 10;
mysql_options(&m_conn, MYSQL_OPT_CONNECT_TIMEOUT, &connectTimeout);
unsigned int timeout = 5;
mysql_options(&m_conn, MYSQL_OPT_READ_TIMEOUT, &timeout);
mysql_options(&m_conn, MYSQL_OPT_WRITE_TIMEOUT, &timeout);
my_bool MyTrue = 1;
mysql_options(&m_conn, MYSQL_OPT_RECONNECT, &MyTrue);
mysql_options(&m_conn, MYSQL_READ_DEFAULT_FILE, "./fpd.cnf");
mysql_options(&m_conn, MYSQL_READ_DEFAULT_GROUP, "fpd");
/* Connect to database */
if (!mysql_real_connect(&m_conn, m_host.c_str(), m_user.c_str(), m_pass.c_str(), m_db.c_str(), m_port, NULL, CLIENT_REMEMBER_OPTIONS))
{
Log.error("Error connecting to %s: %s", m_host.c_str(), mysql_error(&m_conn));
pthread_mutex_unlock(&queryMutex);
return false;
}
else
{
Log.log("Connected to database @[%s:%d] \n", m_host.c_str(), m_port);
pthread_mutex_unlock(&queryMutex);
return true;
}
}
int
CMysqlDb::Insert(DataContainer &Data)
{
DataContainerList DataList;
DataList.push_back(Data);
return Insert(DataList);
}
int
CMysqlDb::Insert(DataContainerList &DataList)
{
if(DataList.size() == 0)
return true;
while (!DataList.empty())
{
string ColumnsString = "(";
string ValuesString = "(";
DataContainer Data = DataList.front();
DataContainer::iterator LastElement = Data.end();
--LastElement;
for( DataContainer::iterator ii=Data.begin(); ii!=Data.end(); ++ii)
{
ColumnsString += (*ii).first;
ValuesString += (*ii).second;
if(ii == LastElement)
{
ColumnsString += ")";
ValuesString += ")";
}
else
{
ColumnsString += ",";
ValuesString += ",";
}
}
string query = "INSERT IGNORE INTO " + m_table + " " + ColumnsString + " VALUES " + ValuesString + ";";
pthread_mutex_lock(&queryMutex);
/* send SQL query */
if (mysql_query(&m_conn, query.c_str()))
{
int error = mysql_errno(&m_conn);
pthread_mutex_unlock(&queryMutex);
Log.log("MySQL Error (%d): %s", error, mysql_error(&m_conn));
// Server error (fatal). Most probably because of a bug or something like that
// Discard the entry so the program doesn't stop sending data because of it
if(error >= 1000 && error < 2000)
{
Log.log("[%s]\n", query.c_str());
DataList.pop_front();
continue;
}
// Client error (transient). Do not delete the results and retry sending them
else
return false;
}
else
pthread_mutex_unlock(&queryMutex);
DataList.pop_front();
}
return true;
}