-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseReader.c
141 lines (106 loc) · 2.95 KB
/
DatabaseReader.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
#include "DatabaseReader.h"
#include "DatabaseLogin.h"
#include <stdio.h>
void DatabaseReaderCreate(DatabaseReader* self) {
self->isConnected = 1;
self->connection = mysql_init(NULL);
if (!self->connection) {
printf("failed to init mysql !\n");
self->isConnected = 0;
return;
}
if (!mysql_real_connect(self->connection, "localhost", DATABASE_USER, DATABASE_PASSWORD, "Cproject2i", 0, NULL, 0)) {
printf("failed to connect to the database !\n");
self->isConnected = 0;
return;
}
}
void DatabaseReaderDestroy(DatabaseReader* self)
{
mysql_close(self->connection);
}
int DatabaseReaderAddGame(DatabaseReader* self, unsigned score)
{
if (!self->isConnected) return 0;
char buf[100];
sprintf_s(buf, 100, "INSERT INTO game(score, datetime) VALUES(%u, CURRENT_TIMESTAMP)", score);
return !mysql_query(self->connection, buf);
}
const GameModel* DatabaseReaderGetGames(DatabaseReader* self, unsigned* count)
{
if (!self->isConnected) return NULL;
if (mysql_query(self->connection, "SELECT * FROM game LIMIT 1000")) {
printf("failed to read database !\n");
return NULL;
}
MYSQL_RES* result = mysql_store_result(self->connection);
if (!result) {
printf("failed to store result from mysql\n");
return NULL;
}
GameModel* buf = malloc(sizeof(GameModel) * 1000);
if (!buf) return NULL;
const int num_fields = mysql_num_fields(result);
if (num_fields < 3) return NULL;
*count = 0;
MYSQL_ROW row;
while ((row = mysql_fetch_row(result)))
{
if (!row[0]) return 0;
if (!row[1]) return 0;
buf[*count].id = atoi(row[0]);
buf[*count].score = atoi(row[1]);
strcpy_s(buf[*count].datetime, 50, row[2]);
(*count)++;
}
mysql_free_result(result);
return buf;
}
unsigned DatabaseReaderGetBestScore(DatabaseReader* self)
{
if (!self->isConnected) return 0;
if (mysql_query(self->connection, "SELECT MAX(score) FROM game WHERE score = (SELECT MAX(score) FROM game)")) {
printf("failed to read database !\n");
return 0;
}
MYSQL_RES* result = mysql_store_result(self->connection);
if (!result) {
printf("failed to store result from mysql\n");
return 0;
}
const int num_fields = mysql_num_fields(result);
if (num_fields < 1) return 0;
unsigned ret = 0;
MYSQL_ROW row;
while ((row = mysql_fetch_row(result)))
{
if (!row[0]) return 0;
ret = atoi(row[0]);
}
mysql_free_result(result);
return ret;
}
double DatabaseReaderGetMeanScore(DatabaseReader* self)
{
if (!self->isConnected) return 0.;
if (mysql_query(self->connection, "SELECT SUM(score) / COUNT(score) FROM game")) {
printf("failed to read database !\n");
return 0.;
}
MYSQL_RES* result = mysql_store_result(self->connection);
if (!result) {
printf("failed to store result from mysql\n");
return 0.;
}
const int num_fields = mysql_num_fields(result);
if (num_fields < 1) return 0.;
double ret = 0.;
MYSQL_ROW row;
while ((row = mysql_fetch_row(result)))
{
if (!row[0]) return 0.;
sscanf_s(row[0], "%lf", &ret);
}
mysql_free_result(result);
return ret;
}