-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mysql.cc
311 lines (265 loc) · 7.44 KB
/
Mysql.cc
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "Mysql.h"
int Mysql::select_user_store(char *sql,char *user_info)
{
int reta = mysql_real_query1(sql);
if ( 0 != reta){
return (-1);
}
//process result
result = mysql_store_result(conn);
if (result == NULL){
if (mysql_error(conn))
fprintf(stderr, "%s\n", mysql_error(conn));
else
fprintf(stderr, "%s\n", "unknown error\n");
return -1;
}
row = mysql_fetch_row(result);
if (!row)
{
cout << "the user you selected not exist!" << endl;
return -2;
}
if (user_info == NULL)
{
return 0;
}
sprintf(user_info, "%s:%s:%s:%s:%s:%s:%s:%s", row[0], row[1],row[2],row[3],row[4],row[5],row[6],row[7]);
return (0);
}
Mysql::Mysql():result(NULL),conn(NULL),row(NULL)
{
//create connection to MySQL
conn = mysql_init(NULL);
if (mysql_real_connect(conn, "localhost", "root", "qiaominghe", "zhong_soft", 0, NULL, 0) == NULL){
fprintf(stderr, "Sorry, no database connection ...\n");
mysql_query(conn, "SET NAMES 'utf8'");
printf("daole\n");
}
}
Mysql::~Mysql()
{
//release memory, server connection
if (result)
{
mysql_free_result(result);
}
mysql_close(conn);
}
int Mysql::add_user(char user_info[8][512])
{
char sql[MAX_SQL_LEN];
bzero(sql,MAX_SQL_LEN);
sprintf(sql,"insert into usrs(name,tel,mail,work,creater,create_time,privilege,passwd) values('%s','%s','%s','%s','%s','%s','%s','%s')",user_info[0],user_info[1],user_info[2],user_info[3],user_info[4],user_info[5],user_info[6],user_info[7]);
return mysql_real_query1(sql);
}
int Mysql::delete_user(char *name)
{
char sql[MAX_SQL_LEN];
bzero(sql,MAX_SQL_LEN);
sprintf(sql,"delete from usrs where name = '%s'",name);
return mysql_real_query1(sql);
}
int Mysql::select_users_name(char **p)
{
char sql[MAX_SQL_LEN];
bzero(sql,MAX_SQL_LEN);
sprintf(sql,"select name from usrs");
int ret = mysql_real_query1(sql);
if (ret != 0)
return -1;
result = mysql_store_result(conn);
unsigned int num = mysql_field_count(conn);
unsigned int count = mysql_num_rows(result);
cout << "users name count:" << count << endl;
char *buffer = new char[count * 8];
bzero(buffer, count * 8);
while (row = mysql_fetch_row(result))
{
for (unsigned int i = 0; i < num; i++)
{
strcat(buffer, row[i]);
strcat(buffer, ":");
}
}
*p = buffer;
return 0;
}
int Mysql::select_user(char* acnt,char *user_info)
{
if (NULL == acnt){ //如果输入acnt为NULL,则返回为-1
return (-1);
}
char sql[MAX_SQL_LEN];
bzero(sql,MAX_SQL_LEN);
sprintf(sql,"select * from usrs where name = '%s'",acnt);
cout << sql << endl;
return select_user_store(sql, user_info);
}
int Mysql::update_user(char user_info[8][512])
{
char sql[MAX_SQL_LEN];
bzero(sql,MAX_SQL_LEN);
sprintf(sql,"update usrs set name = '%s',tel = '%s',mail='%s',work = '%s',privilege = '%s',passwd = '%s'where name = '%s'",user_info[0],user_info[1],user_info[2],user_info[3],user_info[4],user_info[5], user_info[0]);
printf("update usrs set name = '%s',tel = '%s',mail='%s',work = '%s',creater = '%s',create_time = '%s',role = '%s',passwd = '%s'\n",user_info[0],user_info[1],user_info[2],user_info[3],user_info[4],user_info[5],user_info[6],user_info[7]);
return mysql_real_query1(sql);
}
int Mysql::update_privilege(char *str)
{
char sql[MAX_SQL_LEN];
bzero(sql,MAX_SQL_LEN);
sprintf(sql,"update privilege set id1 = '%c',id2='%c',id3 = '%c',id4 = '%c',id5 = '%c',id6 = '%c',id7 = '%c', id8 = '%c', id9 = '%c', id10 = '%c' where name = 'system'",str[0],str[1],str[2],str[3],str[4],str[5],str[6],str[7], str[8], str[9]);
if (mysql_real_query1(sql))
{
return -1;
}
for (int i = 1; i <= 10; i++)
{
if (str[i-1] == '0')
{
bzero(sql,MAX_SQL_LEN);
sprintf(sql, "update privilege set id%d = 0", i);
cout << sql << endl;
if (mysql_real_query1(sql))
{
return -1;
}
}
}
return 0;
}
int Mysql::select_privilege(char *str)
{
char sql[MAX_SQL_LEN];
bzero(sql,MAX_SQL_LEN);
sprintf(sql,"select * from privilege where name = 'system'");
if (mysql_real_query1(sql))
{
return -1;
}
result = mysql_store_result(conn);
row = mysql_fetch_row(result);
if (row == NULL)
{
return -1;
}
unsigned int num = mysql_field_count(conn);
for (unsigned int i = 0; i < num-1; i++)
{
strcat(str, row[i]);
}
return 0;
}
int Mysql::mysql_real_query1(char *sql)
{
if (mysql_query(conn,sql)){
fprintf(stderr,"%s\n\n",mysql_error(conn));
fprintf(stderr,"%s\n",sql);
return (-1);
}
return (0);
}
int Mysql::add_role(char *role,char *role_info)
{
char sql[MAX_SQL_LEN];
bzero(sql,MAX_SQL_LEN);
sprintf(sql,"insert into privilege(id1,id2,id3,id4,id5,id6,id7,id8,id9,id10,name) values('%c','%c','%c','%c','%c','%c','%c','%c','%c','%c','%s')",role_info[0],role_info[1],role_info[2],role_info[3],role_info[4],role_info[5],role_info[6],role_info[7],role_info[8],role_info[9], role);
return mysql_real_query1(sql);
}
int Mysql::delete_role(char *role)
{
char sql[MAX_SQL_LEN];
bzero(sql,MAX_SQL_LEN);
sprintf(sql,"delete from privilege where name= '%s'",role);
return mysql_real_query1(sql);
}
int Mysql::update_role(char *role,char *role_info)
{
char sql[MAX_SQL_LEN];
bzero(sql,MAX_SQL_LEN);
sprintf(sql,"update privilege set id1 = '%c',id2 = '%c',id3 = '%c',id4 = '%c',id5 = '%c',id6 = '%c',id7 ='%c',id8 = '%c',id9 = '%c',id10 = '%c' where name = '%s'",role_info[0],role_info[1],role_info[2],role_info[3],role_info[4],role_info[5],role_info[6],role_info[7],role_info[8],role_info[9], role);
return mysql_real_query1(sql);
}
int Mysql::select_role(char *role,char *role_info)
{
char sql[MAX_SQL_LEN];
bzero(sql,MAX_SQL_LEN);
sprintf(sql,"select * from privilege where name = '%s'",role);
return select_role_store(sql,role_info);
}
int Mysql::select_role_store(char *sql,char *role_info)
{
if ( -1 == mysql_real_query1(sql))
{
return (-1);
}
//process result
result = mysql_store_result(conn);
if (result == NULL)
{
if (mysql_error(conn))
fprintf(stderr, "%s\n", mysql_error(conn));
fprintf(stderr, "%s\n", "unknown error\n");
return -1;
}
if (row = mysql_fetch_row(result))
{
if (NULL == role_info)
{
return (0);
}else {
sprintf(role_info,"%s%s%s%s%s%s%s%s%s%s%s:%s",row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8],row[9],row[10]);
return 0;
}
}
else
return -2;
return (0);
}
int Mysql::select_roles_name(char **p)
{
char sql[MAX_SQL_LEN];
bzero(sql,MAX_SQL_LEN);
sprintf(sql,"select name from privilege");
int ret = mysql_real_query1(sql);
if (ret != 0)
return -1;
result = mysql_store_result(conn);
unsigned int num = mysql_field_count(conn);
unsigned int count = mysql_num_rows(result);
cout << "roles name count:" << count << endl;
char *buffer = new char[count * 8];
bzero(buffer, count * 8);
while (row = mysql_fetch_row(result))
{
for (unsigned int i = 0; i < num; i++)
{
strcat(buffer, row[i]);
strcat(buffer, ":");
}
}
*p = buffer;
return 0;
}
int Mysql::selectUserPrivilege(const char *name, char *body)
{
char sql[MAX_SQL_LEN];
bzero(sql,MAX_SQL_LEN);
sprintf(sql,"select privilege.id1, privilege.id2, privilege.id3, privilege.id4, privilege.id5, privilege.id6, privilege.id7, privilege.id8, privilege.id9, privilege.id10, usrs.name from usrs, privilege where usrs.name = '%s' and usrs.privilege = privilege.name", name);
cout << sql << endl;
int ret = mysql_real_query1(sql);
if (ret != 0)
return -1;
result = mysql_store_result(conn);
if (row = mysql_fetch_row(result))
{
for (int i = 0; i < 11; i++)
{
strcat(body, row[i]);
}
cout << body << endl;
return 0;
}
else
return -2;
}