-
Notifications
You must be signed in to change notification settings - Fork 4
/
db_mysql.cpp
140 lines (103 loc) · 2.55 KB
/
db_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
#include "db_mysql.h"
struct ConnDetails MySql::conn_details;
ConnDetails::ConnDetails() {
server = allocate_str_mem(BUFFER_SIZE);
user = allocate_str_mem(BUFFER_SIZE);
password = allocate_str_mem(BUFFER_SIZE);
db = allocate_str_mem(BUFFER_SIZE);
}
ConnDetails::ConnDetails(const ConnDetails &src_obj) {
server = allocate_str_mem(BUFFER_SIZE);
user = allocate_str_mem(BUFFER_SIZE);
password = allocate_str_mem(BUFFER_SIZE);
db = allocate_str_mem(BUFFER_SIZE);
strcpy(server, src_obj.server);
strcpy(user, src_obj.user);
strcpy(password, src_obj.password);
strcpy(db, src_obj.db);
}
void swap(ConnDetails &src_obj, ConnDetails &dst_obj) {
using std::swap;
swap(src_obj.server, dst_obj.server);
swap(src_obj.user, dst_obj.user);
swap(src_obj.password, dst_obj.password);
swap(src_obj.db, dst_obj.db);
}
ConnDetails& ConnDetails::operator=(ConnDetails src_obj) {
swap(*this, src_obj);
return *this;
}
ConnDetails::ConnDetails(ConnDetails &&src_obj)
:ConnDetails() {
swap(*this, src_obj);
}
void ConnDetails::set_details() {
strcpy(server, "localhost");
strcpy(user, "root");
strcpy(password, "mysql");
strcpy(db, "NFV");
}
ConnDetails::~ConnDetails() {
free(server);
free(user);
free(password);
free(db);
}
MySql::MySql() {
conn = NULL;
result = NULL;
}
MySql::MySql(const MySql &src_obj) {
conn = src_obj.conn;
result = src_obj.result;
}
void swap(MySql &src_obj, MySql &dst_obj) {
using std::swap;
swap(src_obj.conn, dst_obj.conn);
swap(src_obj.result, dst_obj.result);
}
MySql& MySql::operator=(MySql src_obj) {
swap(*this, src_obj);
return *this;
}
MySql::MySql(MySql &&src_obj)
:MySql() {
swap(*this, src_obj);
}
void MySql::set_conn_details() {
conn_details.set_details();
}
void MySql::setup_conn() {
conn = mysql_init(NULL);
if (!mysql_real_connect(conn, conn_details.server, conn_details.user, conn_details.password, conn_details.db, 0, NULL, 0))
report_error(conn);
print_message("Connected to database");
}
void MySql::perform_query(const char *query) {
if (mysql_query(conn, query))
report_error(conn);
result = mysql_store_result(conn);
}
void MySql::fetch_result() {
MYSQL_ROW row;
const char *inp;
int num_fields;
int i;
num_fields = mysql_num_fields(result);
print_message("QUERY RESULT");
while (row = mysql_fetch_row(result)) {
for (i = 0; i < num_fields; i++) {
inp = row[i];
cout << inp << " ";
}
cout << endl;
}
}
void MySql::report_error(MYSQL *conn) {
cout << mysql_error(conn) << endl;
exit(EXIT_FAILURE);
}
MySql::~MySql() {
mysql_free_result(result);
mysql_close(conn);
}