-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdatabase.cpp
executable file
·223 lines (178 loc) · 5.23 KB
/
database.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
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
/**************
* File Name: database.cpp
* Author: G. J. Krafsig
* Date: June 28, 2007
* Purpose: implementation of the game database
**************/
#include "database.h"
/**************
* Purpose: default constructor, initialize class values
* Precondition: none
* Postcondition: none
**************/
database::database()
{
//initilize the mysql fields
sock = NULL;
row = NULL;
result = NULL;
field = NULL;
}
/**************
* Purpose: connect to the appropriate database
* Precondition: host, database, user name and password
* Postcondition: connected to that database
**************/
int database::openConnection(char *host, char *db, char *user, char *pass)
{
if (sock) //already connected to another database
disconnect(); //disconnect from that one
//initilize the socket
sock = mysql_init(0);
//something went wrong with the socket
if (!sock)
return ERR201;
//try connecting to the database
if (!mysql_real_connect(sock, host, user, pass, db, 0, NULL, 0))
return ERR202;
//successfully connected to the database
return SUCCESS;
}
/**************
* Purpose: disconnect from the database
* Precondition: none
* Postcondition: socket closed
**************/
bool database::disconnect()
{
if (sock) //they have a socket open
mysql_close(sock);
//release result data
free();
//database disconnected
return true;
}
/**************
* Purpose: free the results from the database query
* Precondition: none
* Postcondition: results no longer there
**************/
bool database::free()
{
if (result)
{
mysql_free_result(result);
return true;
}
return false;
}
/**************
* Purpose: return the approprite error message
* Precondition: error code
* Postcondition: string with the error returned
**************/
char *dberror(int errorcode)
{
//display the appropriate error message for this error
switch(errorcode)
{
case SUCCESS: return "SUCCESS";
break;
case ERR201: return "201 SOCKET ERROR: SOCKET FAILURE";
break;
case ERR202: return "202 CONNECTION ERROR: CANNOT ACCESS THE SERVER";
break;
case ERR203: return "203 DATABASE ERROR: QUERY FAILED";
break;
}
return NULL; //no error, return null char
}
/**************
* Purpose: return the result set of the query
* Precondition: the query string
* Postcondition: result set returned (or null)
**************/
MYSQL_RES *database::query(char *query)
{
//query the database
mysql_query(sock, query);
//store the results
result = mysql_store_result(sock);
return result;
}
/**************
* Purpose: update the database no result returned
* Precondition: the query string
* Postcondition: false if failed, true if suceess
**************/
bool database::updateQuery(char *query)
{
if (!mysql_query(sock, query))
return 0; //failed query
else
return 1; //successful query
}
/**************
* Purpose: return the result set of the query
* Precondition: the query string
* Postcondition: the FIRST result is returned (or null)
* will not return multiple rows, only the first
**************/
char *database::stringQuery(char *query)
{
//if old results exist, free them
//free();
//query the database
mysql_query(sock,query);
//store the results
result = mysql_store_result(sock);
if (!result)
return NULL; //no results
//fetch the row
row = mysql_fetch_row(result);
//store the result & convert it to a number
char *stringResult = row[0];
//free the results
free();
return stringResult;
}
/**************
* Purpose: return the result set of the query
* Precondition: the query string
* Postcondition: the FIRST result is returned (or null)
* will not return multiple rows, only the first
**************/
int database::intQuery(char *query)
{
//query the database
mysql_query(sock,query);
//store the results
result = mysql_store_result(sock);
if (!result)
return -1; //no results
//fetch the row
row = mysql_fetch_row(result);
//store the result & convert it to a number
int id = atoi(row[0]);
//free the results
free();
return id; //return the id number
}
/**************
* Purpose: return the result set of the query
* Precondition: the query string
* Postcondition: the FIRST result is returned (or null)
* will not return multiple rows, only the first
**************/
bool database::boolQuery(char *query)
{
//if old results exist, free them
//free();
//query the database
mysql_query(sock, query);
//store the results
result = mysql_store_result(sock);
//fetch the row
//row = mysql_fetch_row(result);
return (bool)row[0];
}