-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysqlconnection.cpp
98 lines (86 loc) · 2.56 KB
/
mysqlconnection.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
#include "mysqlconnection.h"
#include "stdio.h"
#include <QDebug>
MySQLConnection::MySQLConnection(QString server_, int port_, QString user_,
QString password_, QString database_, QObject *parent) :
QObject(parent),
server(server_),
port(port_),
user(user_),
password(password_),
database(database_)
{
#ifdef USE_LIB_MYSQL_CLIENT
MYSQL_RES *res;
MYSQL_ROW row;
/* Connect to database */
if (!mysql_real_connect(conn,
server.toLocal8Bit().data(),
user.toLocal8Bit().data(),
password.toLocal8Bit().data(),
database.toLocal8Bit().data(),
port,
NULL,
0)
) {
fprintf(stderr, "%s, %s, %s, %s, %d",
server.toLocal8Bit().data(),
user.toLocal8Bit().data(),
password.toLocal8Bit().data(),
database.toLocal8Bit().data(),
port,
NULL
);
qDebug() << "MySQL connection error:"<< mysql_error(conn);
exit(1);
}
/* send SQL query */
if (mysql_query(conn, "select @@datadir")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
while ((row = mysql_fetch_row(res)) != NULL)
datadir = row[0];
mysql_free_result(res);
/* send SQL query */
if (mysql_query(conn, "select @@version")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
while ((row = mysql_fetch_row(res)) != NULL)
version = row[0];
mysql_free_result(res);
qDebug() << "Got mysql parameters from connection:" << datadir << version;
#else
datadir = "/var/lib/mysql";
version = "5.5.32";
#endif
}
MySQLConnection::~MySQLConnection() {
/* close connection */
#ifdef USE_LIB_MYSQL_CLIENT
mysql_close(conn);
#endif
}
void MySQLConnection::lock_all_tables() {
qDebug() << "Locking all tables";
#ifdef USE_LIB_MYSQL_CLIENT
if (mysql_query(conn, "FLUSH TABLES WITH READ LOCK")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
#endif
emit all_tables_locked();
}
void MySQLConnection::unlock_all_tables() {
qDebug() << "Unlocking all tables";
#ifdef USE_LIB_MYSQL_CLIENT
if (mysql_query(conn, "UNLOCK TABLES")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
#endif
emit all_tables_unlocked();
}