This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlHelpers.cc
347 lines (326 loc) · 11.1 KB
/
sqlHelpers.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
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
#include <iostream>
#include <mysql.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include "paper.h"
#include "room.h"
#include "author.h"
#include "session.h"
#include "config.h"
using namespace std;
// SQL Helper Functions
//Pre: *con contains an error message
//Post: The error message has been sent to the console
void dispSqlError(MYSQL *con)
{
cerr << "About to display an error" << endl;
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
cerr << "Closing the connection" << endl;
exit(1);
}
//Pre: myCon is a MYSQL objec
//Post: myCon is a valid connection to the database or an appropriate
// error has been displayed
MYSQL * connect (MYSQL *myCon) {
bool connectedStatus=true;
myCon = mysql_init(NULL);
if (myCon == NULL) {
connectedStatus=false;
// cerr << "myCon init = NULL" << endl;
}
const char* serverLocation = serverAddress;
const char* serverUser = username;
const char* serverPass = password;
const char* databaseName = dbName;
if (mysql_real_connect(myCon, serverLocation, serverUser, serverPass, databaseName,
0, NULL, 0) == NULL){
dispSqlError(myCon);
connectedStatus=false;
// cerr << "myCon real connect = NULL" << endl;
}
// cerr << "Connected Status = " << connectedStatus << endl;
return myCon;//(connectedStatus);
}
//Pre:
//Post: returns the number of rows in the specified table
int getRows (const char* tableName) {
int RV;
const char* query = "SELECT * FROM ";
char* queryName = new char[strlen(tableName)+strlen(query)+1];
strcpy (queryName, query);
strcat (queryName, tableName);
// cerr << "about to connect (getRows)" << endl;
// cerr << tableName << endl;
MYSQL *myCon = connect(myCon);
if (myCon != NULL){
// cerr << "getRows: got a connection to " << tableName << endl;
// cerr << "Query = *" << queryName << "*" << endl;
// cerr << mysql_query(myCon, queryName);
if (mysql_query(myCon, queryName)) dispSqlError(myCon);
else {
// cerr << "getRows: Ran the query, storing result" << endl;
MYSQL_RES *result = mysql_store_result(myCon);
if (result == NULL) dispSqlError(myCon);
else RV = mysql_num_rows(result);
}
}
// else cerr << "Failed to connect to SQL database" << endl;
return (RV);
}
//PRE
//POST: RV is the number of distinct paper IDs in the specified table
int getPapers (const char* tableName) {
int RV = 0;
const char * query = "SELECT DISTINCT paper_id FROM ";
char* queryName = new char[strlen(tableName)+strlen(query)+1];
strcpy (queryName, query);
strcat (queryName, tableName);
MYSQL *myCon = connect(myCon);
if (myCon!=NULL) {
if (mysql_query(myCon, queryName)) dispSqlError(myCon);
else {
MYSQL_RES *result = mysql_store_result(myCon);
if (result == NULL) dispSqlError(myCon);
else RV = mysql_num_rows(result);
}
}
return (RV);
}
//PRE
//POST: RV is the ID of the specified paper
int getPaperID (int paperNum) {
int RV = 0;
const char * query = "SELECT DISTINCT paper_id FROM papers_authors";
MYSQL *myCon=connect(myCon);
if (myCon!=NULL){
if (mysql_query(myCon, query)) dispSqlError(myCon);
else {
MYSQL_RES *result = mysql_store_result(myCon);
if (result == NULL) dispSqlError(myCon);
else if (mysql_num_rows(result)>paperNum) {
MYSQL_ROW row;
for (int i=0; i<=paperNum; i++) row = mysql_fetch_row(result);
RV = atoi(row[0]);
}
else RV = -1;
}
}
return (RV);
}
//PRE
//POST RV is the ID for the room number in the 'rooms' table
int getRoomID (int roomNum) {
int RV = 0;
MYSQL *myCon=connect(myCon);
if (myCon!=NULL) {
const char * query = "SELECT * FROM rooms";
// cerr << "After char * query before if" << endl;
if (mysql_query(myCon, query)) {
// cerr <<"in if" << endl;
dispSqlError(myCon);
}
else {
// cerr << "start else" << endl;
MYSQL_RES *result = mysql_store_result(myCon);
// cerr << "before nested if" << endl;
if (result == NULL) {
dispSqlError(myCon);
// cerr << "After nested if" << endl;
}
else if (mysql_num_rows(result)>roomNum) {
// cerr << "start nested else" << endl;
MYSQL_ROW row;
for (int i=0; i<=roomNum; i++) row = mysql_fetch_row(result);
RV = atoi(row[0]);
}
else RV = -1;
}
return (RV);
}
}
/*
//PRE
//POST: RV is the paper ID for the provided paper number
int getPaperSession (int paperNum, MYSQL *myCon) {
int RV = 0;
const char * query = "SELECT * from sessionpaper WHERE paper_id = ";
char* pNumStr;
sprintf (pNumStr, "%d", paperNum);
char* fullQuery = new char[strlen(query)+strlen(pNumStr)+1];
strcpy (fullQuery, query);
strcat (fullQuery, pNumStr);
if (mysql_query(myCon, fullQuery)) dispSqlError(myCon);
else {
MYSQL_RES *result = mysql_store_result(myCon);
if (result == NULL) dispSqlError(myCon);
else {
MYSQL_ROW row;
RV = atoi(row[1]);
}
}
return (RV);
}*/
//PRE authorNum is the number of the author to retrieve
//POST RV is the ID of the specified author
int getAuthorID (int authorNum) {
int RV = 0;
const char * query = "SELECT DISTINCT id FROM authors";
// cerr <<"getAuthorID: about to connect. authorNum = " << authorNum << endl;
MYSQL *myCon=connect(myCon);
if (myCon!=NULL){
// cerr << "getAuthorID: connected, myCon != NULL" << endl;
if (mysql_query(myCon, query)) dispSqlError(myCon);
else {
// cerr << "getAuthorID: ran the query" << endl;
MYSQL_RES *result = mysql_store_result(myCon);
if (result == NULL) dispSqlError(myCon);
else if (mysql_num_rows(result)>authorNum) {
// cerr << "getAuthorID: stored query result" << endl;
MYSQL_ROW row;
for (int i=0; i<=authorNum; i++) row = mysql_fetch_row(result);
RV = atoi(row[0]);
// cerr << "getAuthorID: RV = " << RV << endl;
}
else RV = -1;
}
}
// cerr << "getAuthorID: Done, returning " << RV << " to calling function for authorNum " << authorNum << endl;
return (RV);
}
int getAuthorPaper (int authorNum, int paperNum) {
int RV = 0;
const char * query = "SELECT author_id from papers_authors WHERE paper_id = ";
char pNumStr[1000] = {0};
sprintf (pNumStr, "%d", paperNum);
char* fullQuery = new char[strlen(query)+strlen(pNumStr)+1];
strcpy (fullQuery, query);
strcat (fullQuery, pNumStr);
MYSQL *myCon=connect(myCon);
if (myCon!=NULL) {
if (mysql_query(myCon, fullQuery)) dispSqlError(myCon);
else {
MYSQL_RES *result = mysql_store_result(myCon);
if (result == NULL) dispSqlError(myCon);
else if (mysql_num_rows(result)>authorNum) {
MYSQL_ROW row;
for (int i=0; i<=authorNum; i++) row = mysql_fetch_row(result);
RV = atoi(row[0]);
}
}
}
return (RV);
}
int getSessionID (int sessionNum) {
int RV = 0;
const char * query = "SELECT * from sessions";
MYSQL *myCon=connect(myCon);
if (myCon!=NULL){
if (mysql_query(myCon, query)) dispSqlError(myCon);
else {
MYSQL_RES *result = mysql_store_result(myCon);
if (result == NULL) dispSqlError(myCon);
else {
MYSQL_ROW row;
for (int i=0; i<=sessionNum; i++) row = mysql_fetch_row(result);
RV = atoi(row[0]);
}
}
}
return (RV);
}
int getAuthorConflict (int conflictNum, int authorNum) {
int RV = 0;
const char * query = "SELECT * from authors_sessions_constraints where author_id = ";
char* NumStr = new char[10];
sprintf (NumStr, "%d", authorNum);
char* fullQuery = new char[strlen(query)+strlen(NumStr)+1];
strcpy (fullQuery, query);
strcat (fullQuery, NumStr);
MYSQL *myCon=connect(myCon);
if (myCon!=NULL) {
if (mysql_query(myCon, fullQuery)) dispSqlError(myCon);
else {
MYSQL_RES *result = mysql_store_result(myCon);
if (result == NULL) dispSqlError(myCon);
else if (mysql_num_rows(result)>conflictNum) {
MYSQL_ROW row;
for (int i=0; i<=conflictNum; i++) row = mysql_fetch_row(result);
RV = atoi(row[2]);
}
}
}
//cerr << "getAuthorConflict: author " << authorNum;
//cerr << " has a conflict with session " << RV << endl;
return (RV);
}
//PRE
//POST: RV is the number of rows in the permutations table of confsched
int getPermutationCount () {
int RV = -1;
const char * query = "SELECT distinct permutation_id from permutations";
MYSQL *myCon = connect(myCon);
if (myCon!=NULL) {
if (mysql_query(myCon, query)) dispSqlError(myCon);
else {
MYSQL_RES *result = mysql_store_result(myCon);
if (result == NULL) dispSqlError(myCon);
else RV = mysql_num_rows(result);
}
}
return (RV);
}
//PRE: permID, sessionID, authorID are positive integers
//POST: A row has been added to permutations containing the input data
void storePermutation (int permID, int sessionID, int authorID) {
const char * query = "INSERT INTO permutations (permutation_id, session, author) VALUES (";
char permIDStr[1000] = {0};
char sessionIDStr[1000] = {0};
char authorIDStr[1000] = {0};
sprintf (permIDStr, "%d, ", permID);
sprintf (sessionIDStr, "%d, ", sessionID);
sprintf (authorIDStr, "%d)", authorID);
char* fullQuery = new char[strlen(query)+strlen(permIDStr)+strlen(sessionIDStr)+strlen(authorIDStr)+1];
strcpy (fullQuery, query);
strcat (fullQuery, permIDStr);
strcat (fullQuery, sessionIDStr);
strcat (fullQuery, authorIDStr);
// cerr << "storePermutation: Query = *" << fullQuery << "*" << endl;
//cerr << "getting conflict " << conflictNum << " for author " << authorNum;
//cerr << " using query : " << fullQuery << endl;
MYSQL *myCon=connect(myCon);
if (myCon!=NULL) mysql_query(myCon, fullQuery);
}
//PRE: permID and conflictCount are positive integers
//POST: a row has been added to conflicts indicating the number of conflicts for the provided permutation id
void storeConflicts (int permID, int conflictCount) {
const char * query = "INSERT INTO conflicts (permutation_id, num_conflicts) VALUES (";
char permIDStr[1000] = {0};
char conflictStr[1000] = {0};
sprintf (permIDStr, "%d, ", permID);
sprintf (conflictStr, "%d)", conflictCount);
char* fullQuery = new char[strlen(query)+strlen(permIDStr)+strlen(conflictStr)+1];
strcpy (fullQuery, query);
strcat (fullQuery, permIDStr);
strcat (fullQuery, conflictStr);
// cerr << "storeConflicts: Query = *" << fullQuery << "*" << endl;
MYSQL *myCon=connect(myCon);
if (myCon!=NULL) mysql_query(myCon, fullQuery);
}
//PRE:
//POST: permutations and conflicts have been dropped and recreated in the database
void clearTables() {
MYSQL *myCon=connect(myCon);
if (myCon!=NULL) {
const char * query = "DROP table conflicts";
mysql_query (myCon, query);
query = "DROP table permutations";
mysql_query (myCon, query);
// Don't drop the tables! Just truncate.
query = "CREATE TABLE conflicts (permutation_id INT UNSIGNED NOT NULL PRIMARY KEY, num_conflicts int(10) UNSIGNED)";
mysql_query (myCon, query);
query = "CREATE TABLE permutations (permutation_id INT UNSIGNED NOT NULL, session INT UNSIGNED NOT NULL, author INT UNSIGNED NOT NULL, PRIMARY KEY (permutation_id,session,author))";
mysql_query (myCon, query);
}}