-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.c
677 lines (627 loc) · 21.7 KB
/
db.c
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql/mysql.h>
#define HOST "dursley.socs.uoguelph.ca"
#define USER "ryoung08"
#define PWD "0925398"
#define DB "ryoung08"
void checkArgs(int argc);
void error(char * msg, MYSQL * con, int type);
void performAction(char * flag, MYSQL * con, char const *argv[], int argc);
void checkArgs(int argc)
{
if (argc < 2)
{
printf("error: please make sure you have at least one argument: ./db -clear\n");
exit(1);
}
}
/* standard error message from dr calvert's example */
void error(char * msg, MYSQL * con, int type)
{
if (type == 0)
printf("%s\n%s\n", msg, mysql_error(con));
else
printf("%s<br>%s<br>", msg, mysql_error(con));
exit(1);
}
/* main function that chooses the action and performs it */
void performAction(char * flag, MYSQL * con, char const *argv[], int argc)
{
MYSQL_RES * result;
MYSQL_ROW row;
int i;
char * tables[] = {"users", "posts", "streams"};
/* clears the tables */
if (strcmp(flag, "-clear") == 0)
{
for (i = 0; i < 3; i++)
{
char q[80];
sprintf(q, "TRUNCATE TABLE %s", tables[i]);
if(mysql_query(con,q))
{
char err[80];
sprintf(err, "Could not empty table %s", tables[i]);
error(err, con, 0);
}
}
printf("Tables have been cleared\n");
} /* deletes the tables */
else if (strcmp(flag, "-reset") == 0)
{
for (i = 0; i < 3; i++)
{
char q[80];
sprintf(q, "DROP TABLE %s", tables[i]);
if(mysql_query(con,q))
{
char err[80];
sprintf(err, "Could not drop table %s", tables[i]);
error(err, con, 0);
}
}
printf("Tables have been dropped\n");
} /* prints out posts */
else if (strcmp(flag, "-posts") == 0)
{
char q[80];
sprintf(q, "SELECT * FROM %s", tables[1]);
if(mysql_query(con,q))
{
char err[80];
sprintf(err, "Could not fetch users from %s", tables[0]);
error(err, con, 0);
}
result = mysql_store_result(con);
if (!result)
error("Failed to store result", con, 0);
int rows = mysql_num_rows(result);
if (rows == 0)
printf("There are no posts to display\n");
else
{
while((row = mysql_fetch_row(result)))
{
printf("Stream: %s\n", row[3]);
printf("Sender: %s\n", row[0]);
printf("Date: %s\n", row[1]);
printf("Message: %s\n\n", row[2]);
}
}
mysql_free_result(result);
} /* prints out users */
else if (strcmp(flag, "-users") == 0)
{
char q[80];
sprintf(q, "SELECT DISTINCT name FROM %s", tables[0]);
if(mysql_query(con,q))
{
char err[80];
sprintf(err, "Could not fetch users from %s", tables[0]);
error(err, con, 0);
}
result = mysql_store_result(con);
if (!result)
error("Failed to store result", con, 0);
int rows = mysql_num_rows(result);
if (rows == 0)
printf("There are no users to display\n");
else
while((row = mysql_fetch_row(result))) printf("%s\n", row[0]);
mysql_free_result(result);
} /*prints out streams */
else if (strcmp(flag, "-streams") == 0)
{
char q[80];
sprintf(q, "SELECT * FROM %s", tables[2]);
if(mysql_query(con,q))
{
char err[80];
sprintf(err, "Could not fetch users from %s", tables[0]);
error(err, con, 0);
}
result = mysql_store_result(con);
if (!result)
error("Failed to store result", con, 0);
int rows = mysql_num_rows(result);
if (rows == 0)
printf("There are no streams to display\n");
else
while((row = mysql_fetch_row(result))) printf("%s\n", row[0]);
mysql_free_result(result);
}
else if (strcmp(flag, "-updateR") == 0)
{
if (argc != 5)
{
printf("Sorry incorrect number of arguments\n");
}
else
{
char username[80], stream[80], value[40], q[200];
strcpy(username, argv[2]);
strcpy(stream, argv[3]);
strcpy(value, argv[4]);
sprintf(q, "UPDATE users SET msg_read=%d WHERE name='%s' and stream='%s' and msg_read < %d", atoi(value), username, stream, atoi(value));
if(mysql_query(con,q))
{
char err[80];
sprintf(err, "Failed to update users");
error(err, con, 1);
}
}
}
else if (strcmp(flag, "-fetchP") == 0)
{
if (argc != 6)
{
printf("Sorry incorrect number of arguments\n");
}
else
{
char q[200], username[80], stream[80], count[40], limit[40];
strcpy(username, argv[2]);
strcpy(stream, argv[3]);
strcpy(count, argv[4]);
strcpy(limit, argv[5]);
if (strcmp(stream, "all") == 0)
{
char a[200];
sprintf(a, "SELECT * FROM users WHERE name='%s'", username);
if(mysql_query(con,a))
{
char err[80];
sprintf(err, "Could not fetch users from %s", tables[0]);
error(err, con, 1);
}
result = mysql_store_result(con);
if (!result) error("Failed to store result", con, 1);
int rows = mysql_num_rows(result);
i = 0;
sprintf(q, "SELECT * FROM posts WHERE ");
while((row = mysql_fetch_row(result)))
{
char temp[40];
sprintf(temp, " stream='%s'", row[1]);
strcat(q, temp);
if (i + 1 < rows)
strcat(q, " or ");
i++;
}
mysql_free_result(result);
char lim[40];
sprintf(lim, " LIMIT %d,%d", atoi(count), atoi(limit));
strcat(q, lim);
}
else
sprintf(q, "SELECT * FROM posts WHERE stream='%s' LIMIT %d,%d", stream, atoi(count) , atoi(limit));
if(mysql_query(con,q))
{
char err[80];
sprintf(err, "Could not fetch users from %s", tables[0]);
error(err, con, 1);
}
result = mysql_store_result(con);
if (!result) error("Failed to store result", con, 1);
int rows = mysql_num_rows(result);
if (rows > 0)
{
while((row = mysql_fetch_row(result)))
{
printf("Sender: %s\n", row[0]);
printf("Date: %s\n", row[1]);
printf("Message: %s\n", row[2]);
printf("%s\n", row[3]);
}
}
mysql_free_result(result);
}
}
else if (strcmp(flag, "-getRead") == 0)
{
if (argc != 4)
{
printf("Sorry incorrect number of arguments\n");
}
else
{
char username[80], stream[80], q[200];
strcpy(username, argv[2]);
strcpy(stream, argv[3]);
if (strcmp(stream, "all") == 0)
sprintf(q, "SELECT * FROM users WHERE name='%s'", username);
else
sprintf(q, "SELECT * FROM users WHERE name='%s' and stream='%s'", username, stream);
if(mysql_query(con,q))
{
char err[80];
sprintf(err, "Could not fetch users from %s", tables[0]);
error(err, con, 1);
}
result = mysql_store_result(con);
if (!result) error("Failed to store result", con, 1);
int rows = mysql_num_rows(result);
if (rows == 0)
{
printf("0");
}
else
{
int count = 0;
while((row = mysql_fetch_row(result)))
{
count += atoi(row[2]);
}
printf("%d", count);
}
mysql_free_result(result);
}
}
else if (strcmp(flag, "-lPosts") == 0)
{
if (argc != 5)
{
printf("Sorry incorrect number of arguments\n");
}
else
{
char username[80], stream[80], limit[30], q[200], a[200];
strcpy(username, argv[2]);
strcpy(stream, argv[3]);
strcpy(limit, argv[4]);
int l = atoi(limit);
if (strcmp(stream, "all") == 0)
{
sprintf(a, "SELECT * FROM users WHERE name='%s'", username);
if(mysql_query(con,a))
{
char err[80];
sprintf(err, "Could not fetch users from %s", tables[0]);
error(err, con, 1);
}
result = mysql_store_result(con);
if (!result) error("Failed to store result", con, 1);
int rows = mysql_num_rows(result);
i = 0;
sprintf(q, "SELECT * FROM posts WHERE ");
while((row = mysql_fetch_row(result)))
{
char temp[40];
sprintf(temp, " stream='%s'", row[1]);
strcat(q, temp);
if (i + 1 < rows)
strcat(q, " or ");
i++;
}
char lim[40];
sprintf(lim, " LIMIT %d", l);
strcat(q, lim);
mysql_free_result(result);
}
else
{
sprintf(q, "SELECT * FROM posts WHERE stream='%s' LIMIT %d", stream, l);
}
if(mysql_query(con,q))
{
char err[80];
sprintf(err, "Could not fetch users from %s", tables[0]);
error(err, con, 1);
}
result = mysql_store_result(con);
if (!result) error("Failed to store result", con, 1);
while((row = mysql_fetch_row(result)))
{
printf("Stream: %s\n", row[3]);
printf("Sender: %s\n", row[0]);
printf("Date: %s\n", row[1]);
printf("Message: %s\n", row[2]);
}
mysql_free_result(result);
}
}
else if (strcmp(flag, "-pCount") == 0)
{
if (argc != 4)
{
printf("Sorry incorrect number of arguments\n");
}
else
{
char username[80], stream[80], q[80];
int count = 0;
strcpy(username, argv[2]);
strcpy(stream, argv[3]);
if (strcmp(stream, "all") == 0)
{
sprintf(q, "SELECT * FROM users WHERE name='%s'", username);
if(mysql_query(con,q))
{
char err[80];
sprintf(err, "Could not fetch users from %s", tables[0]);
error(err, con, 1);
}
result = mysql_store_result(con);
if (!result) error("Failed to store result", con, 1);
while((row = mysql_fetch_row(result)))
{
sprintf(q, "SELECT * FROM posts WHERE stream='%s'", row[1]);
if(mysql_query(con,q))
{
char err[80];
sprintf(err, "Could not fetch posts from posts");
error(err, con, 1);
}
MYSQL_RES * r = mysql_store_result(con);
if (!r) error("Failed to store result", con, 1);
int rows = mysql_num_rows(r);
count += rows;
mysql_free_result(r);
}
mysql_free_result(result);
}
else
{
sprintf(q, "SELECT * FROM posts WHERE stream='%s'", stream);
if(mysql_query(con,q))
{
char err[80];
sprintf(err, "Could not fetch posts from posts");
error(err, con, 1);
}
result = mysql_store_result(con);
if (!result) error("Failed to store result", con, 1);
int rows = mysql_num_rows(result);
count += rows;
mysql_free_result(result);
}
printf("%d", count);
}
}
else if (strcmp(flag, "-lStreams") == 0)
{
if (argc != 3)
{
printf("Sorry incorrect number of arguments\n");
}
else
{
char username[80], q[80];
strcpy(username, argv[2]);
sprintf(q, "SELECT * FROM users WHERE name='%s'", username);
if(mysql_query(con,q))
{
char err[80];
sprintf(err, "Sorry unable to find any groups for %s. Please add some using addauthor!", username);
error(err, con, 1);
}
result = mysql_store_result(con);
if (!result) error("Failed to store result", con, 1);
int rows = mysql_num_rows(result);
if (rows == 0)
{
printf("Sorry unable to find any groups for %s. Please add some using addauthor!<br>", username);
}
else
{
printf("<form action=\"viewer.php\" method=\"post\">\n");
int first = 0;
while((row = mysql_fetch_row(result)))
{
if (first == 0)
{
printf("%s: <input type=\"radio\" checked=\"checked\" name=\"stream\" value=\"%s\"/><br>\n", row[1], row[1]);
first = 1;
}
else
{
printf("%s: <input type=\"radio\" name=\"stream\" value=\"%s\"/><br>\n", row[1], row[1]);
}
}
printf("all: <input type=\"radio\" name=\"stream\" value=\"all\"/><br>\n");
printf("<input type=\"hidden\" name=\"username\" value=\"%s\"/>\n", username);
printf("<input type=\"hidden\" name=\"counter\" value=\"0\"/>\n");
printf("<input type=\"hidden\" name=\"sorted\" value=\"0\"/>\n");
printf("<input type=\"hidden\" name=\"name\" value=\"choose\"/>\n");
printf("<input type=\"submit\" value=\"choose\"/>\n");
printf("</form>\n");
}
mysql_free_result(result);
}
}
else if (strcmp(flag, "-post") == 0)
{
if (argc < 6)
{
printf("Sorry incorrect number of arguments\n");
}
else
{
char q[2000];
char username[80];
char stream[80];
char date[80];
char message[1000];
strcpy(username, argv[2]);
strcpy(stream, argv[3]);
strcpy(date, argv[4]);
strcpy(message, argv[5]);
sprintf(q, "SELECT * from users WHERE name='%s' and stream='%s'", username, stream);
if(mysql_query(con,q))
{
char err[80];
sprintf(err, "Error: stream does not exist could not complete your operation");
error(err, con, 1);
}
result = mysql_store_result(con);
if (!result) error("Failed to store result", con, 1);
int rows = mysql_num_rows(result);
if (rows == 0)
{
printf("Error you do not have permission to post to that stream\n");
}
else
{
sprintf(q, "insert into posts values('%s', '%s', '%s', '%s')", username, date, message , stream);
if(mysql_query(con,q))
{
char err[80];
sprintf(err, "Failed to insert post");
error(err, con, 1);
}
printf("The post has been submitted!\n");
}
mysql_free_result(result);
}
}
else if (strcmp(flag, "-addauthor") == 0)
{
if (argc != 4)
{
printf("Sorry incorrect number of arguments\n");
}
else
{
char q[80];
char username[80];
char stream[80];
strcpy(username, argv[2]);
strcpy(stream, argv[3]);
char * create[] = {
"create table if not exists users (name varchar(40), stream varchar(200), msg_read int)",
"create table if not exists posts (name varchar(40), date varchar(40), message varchar(255), stream varchar(40))",
"create table if not exists streams (name varchar(40))" };
for (i = 0; i < 3; i++)
{
if(mysql_query(con,create[i]))
{
char err[80];
sprintf(err, "Could not create table");
error(err, con, 1);
}
}
sprintf(q, "SELECT * FROM streams WHERE name='%s'", stream);
if(mysql_query(con,q))
{
char err[80];
sprintf(err, "Could not fetch streams");
error(err, con, 1);
}
result = mysql_store_result(con);
if (!result)
error("Failed to store result", con, 1);
int rows = mysql_num_rows(result);
if (rows == 0)
{
sprintf(q, "insert into streams values('%s')", stream);
if(mysql_query(con,q))
{
char err[80];
sprintf(err, "Could not fetch streams");
error(err, con, 1);
}
}
mysql_free_result(result);
/* make username a actual var */
sprintf(q, "SELECT * FROM users WHERE name='%s' and stream='%s'", username, stream);
if(mysql_query(con,q))
{
char err[80];
sprintf(err, "Could not fetch User");
error(err, con, 1);
}
result = mysql_store_result(con);
if (!result) error("Failed to store result", con, 1);
rows = mysql_num_rows(result);
if (rows == 0)
{
sprintf(q, "insert into users values ('%s', '%s', 0)", username, stream);
if(mysql_query(con,q))
{
char err[80];
sprintf(err, "Could not give user permission");
error(err, con, 1);
}
printf("%s was succesfuly added to %s<br>", username, stream);
}
else
{
printf("error: %s already in %s<br>", username, stream);
}
mysql_free_result(result);
}
}
else if (strcmp(flag, "-removeA") == 0)
{
if (argc != 4)
{
printf("Sorry incorrect number of arguments\n");
}
else
{
char username[80];
char stream[80];
char q[200];
strcpy(username, argv[2]);
strcpy(stream, argv[3]);
sprintf(q, "SELECT * FROM users WHERE name='%s' and stream='%s'", username, stream);
if(mysql_query(con,q))
{
char err[80];
sprintf(err, "Could not fetch User");
error(err, con, 1);
}
result = mysql_store_result(con);
if (!result) error("Failed to store result", con, 1);
int rows = mysql_num_rows(result);
if (rows == 0)
printf("Error: could not remove %s from %s because no record exists<br>", username, stream);
else
{
sprintf(q, "DELETE FROM users WHERE name='%s' and stream='%s'", username, stream);
if(mysql_query(con,q))
{
char err[80];
sprintf(err, "Could not delete User");
error(err, con, 1);
}
printf("%s was removed succesfuly from %s<br>", username, stream);
}
mysql_free_result(result);
}
}
else if (strcmp(flag, "-help") == 0)
{
printf("Help:\n");
printf("------\n");
printf("-clear: empties the tables\n");
printf("-reset: deletes the tables\n");
printf("-posts: prints out all the posts\n");
printf("-users: prints out all the users\n");
printf("-streams: prints out all the streams\n");
}
else
{
printf("Sorry, that isn't a valid command. If you need help with the commands please type -help as your agument\n");
}
}
int main(int argc, char const *argv[]) {
checkArgs(argc);
MYSQL connection;
mysql_init(&connection);
mysql_options(&connection, MYSQL_READ_DEFAULT_GROUP, "ryoung08");
if (!mysql_real_connect(&connection, HOST, USER, PWD, DB, 0, NULL, 0))
{
char err[250];
sprintf(err, "Error: unable to connect to %s", HOST);
error(err, &connection, 0);
}
char action[50];
strcpy(action, argv[1]);
performAction(action, &connection, argv, argc);
mysql_close(&connection);
mysql_library_end();
return 0;
}