This repository has been archived by the owner on Oct 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.h
275 lines (252 loc) · 11.3 KB
/
Database.h
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
#include <string>
#include <iostream>
#include <sstream>
#include "Team.h"
#include "Standing.h"
#include <msclr/marshal_cppstd.h>
#using <mscorlib.dll>
#using <System.Data.dll>
#using <System.dll>
using namespace System;
using namespace System::Data::OleDb;
ref class Database
{
private:
static String^ query;
static std::string* str;
static std::string* error;
static OleDbDataReader^ resultsReader;
static OleDbConnection^ database;
static OleDbCommand^ command;
public:
static std::string getNextMatch(std::vector<Team>& teams)
{
try {
database = gcnew OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=../scout.mdb");
database->Open();
query = "SELECT TOP 1 * FROM matches WHERE hasPlayed=false ORDER BY matchNumber ASC";
command = gcnew OleDbCommand(query, database);
resultsReader = command->ExecuteReader(System::Data::CommandBehavior::CloseConnection);
if (resultsReader->HasRows)
{
while (resultsReader->Read())
{
teams[0].setNumber(msclr::interop::marshal_as<std::string>(resultsReader["red1"]->ToString()));
teams[1].setNumber(msclr::interop::marshal_as<std::string>(resultsReader["red2"]->ToString()));
teams[2].setNumber(msclr::interop::marshal_as<std::string>(resultsReader["red3"]->ToString()));
teams[3].setNumber(msclr::interop::marshal_as<std::string>(resultsReader["blue1"]->ToString()));
teams[4].setNumber(msclr::interop::marshal_as<std::string>(resultsReader["blue2"]->ToString()));
teams[5].setNumber(msclr::interop::marshal_as<std::string>(resultsReader["blue3"]->ToString()));
return msclr::interop::marshal_as<std::string>(resultsReader["matchNumber"]->ToString());
}
}
else
return "Done";
}
catch (Exception^ ex)
{
*error = msclr::interop::marshal_as<std::string>(ex->ToString());
std::cout << error << std::endl;
}
resultsReader->Close();
if (database->State == System::Data::ConnectionState::Open)
database->Close();
}
static void getTeamMatches(std::vector<Team>& teams)
{
try {
for (int i = 0; i < 6; i++)
{
database = gcnew OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=../scout.mdb");
if (database->State == System::Data::ConnectionState::Closed)
database->Open();
query = "SELECT TOP 1 matchNumber FROM " + msclr::interop::marshal_as<String^>(teams[i].getNumber()) + " WHERE hasPlayed=false ORDER BY matchNumber ASC";
command = gcnew OleDbCommand(query, database);
resultsReader = command->ExecuteReader(System::Data::CommandBehavior::CloseConnection);
while (resultsReader->Read())
{
teams[i].setTeamMatch(msclr::interop::marshal_as<std::string>(resultsReader["matchNumber"]->ToString()));
}
resultsReader->Close();
}
}
catch (Exception^ ex)
{
*error = msclr::interop::marshal_as<std::string>(ex->ToString());
std::cout << error << std::endl;
}
resultsReader->Close();
if (database->State == System::Data::ConnectionState::Open)
database->Close();
}
static void updateTeamMatch(Team teamObj)
{
try {
database = gcnew OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=../scout.mdb");
if (database->State == System::Data::ConnectionState::Closed)
database->Open();
// SQL Query
// UPDATE [teamNumber] SET hasPlayed=true, autonGear='[Middle, Side, Miss, None, Unset]', autonBalls=[autonBallsValue],
// teleopGears=[teleopGearsValue], teleopBalls=[teleopBallsValue], climb=[true, false] WHERE match=[teamMatch]
query = "UPDATE " + msclr::interop::marshal_as<String^>(teamObj.getNumber()) + " SET hasPlayed = true, autonGear = '";
query += msclr::interop::marshal_as<String^>(teamObj.getAutonGearStr()) + "', autonBalls = " + msclr::interop::marshal_as<String^>(teamObj.getAutonBallsStr());
query += ", teleopGears = " + msclr::interop::marshal_as<String^>(teamObj.getTeleopGearsStr()) + ", teleopBalls = " + msclr::interop::marshal_as<String^>(teamObj.getTeleopBallsStr());
query += ", climb = ";
if (teamObj.getClimb() != climbEnum::UNDEF)
query += msclr::interop::marshal_as<String^>(teamObj.getClimbStr());
else
query += "false";
query += " WHERE matchNumber = " + msclr::interop::marshal_as<String^>(teamObj.getTeamMatch());
command = gcnew OleDbCommand(query, database);
command->ExecuteNonQuery();
}
catch (Exception^ ex)
{
*error = msclr::interop::marshal_as<std::string>(ex->ToString());
std::cout << error << std::endl;
}
if (database->State == System::Data::ConnectionState::Open)
database->Close();
}
static void updateTeamAverages(Team teamObj)
{
std::stringstream convert;
std::string tempStr;
double autonBalls = 0, teleopBalls = 0, teleopGears = 0, tempDouble = 0, climb = 0;
double gamesPlayedDouble = 0, autonGearScoredDouble = 0, autonGearAttemptedDouble = 0;
try {
database = gcnew OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=../scout.mdb");
if (database->State == System::Data::ConnectionState::Closed)
database->Open();
query = "SELECT hasPlayed FROM " + msclr::interop::marshal_as<String^>(teamObj.getNumber()) + " WHERE hasPlayed=true";
command = gcnew OleDbCommand(query, database);
resultsReader = command->ExecuteReader(System::Data::CommandBehavior::CloseConnection);
while (resultsReader->Read())
{
if (resultsReader["hasPlayed"]->ToString() == "True")
gamesPlayedDouble++;
}
resultsReader->Close();
if (database->State == System::Data::ConnectionState::Closed)
database->Open();
query = "SELECT autonBalls, teleopBalls, teleopGears, climb FROM " + msclr::interop::marshal_as<String^>(teamObj.getNumber()) + " WHERE hasPlayed=true";
command = gcnew OleDbCommand(query, database);
resultsReader = command->ExecuteReader(System::Data::CommandBehavior::CloseConnection);
while (resultsReader->Read())
{
convert.clear();
tempStr = msclr::interop::marshal_as<std::string>(resultsReader["autonBalls"]->ToString());
convert << tempStr;
convert >> tempDouble;
autonBalls += tempDouble;
convert.clear();
tempStr = msclr::interop::marshal_as<std::string>(resultsReader["teleopBalls"]->ToString());
convert << tempStr;
convert >> tempDouble;
teleopBalls += tempDouble;
convert.clear();
tempStr = msclr::interop::marshal_as<std::string>(resultsReader["teleopGears"]->ToString());
convert << tempStr;
convert >> tempDouble;
teleopGears += tempDouble;
if (resultsReader["climb"]->ToString() == "True")
climb++;
}
resultsReader->Close();
if (database->State == System::Data::ConnectionState::Closed)
database->Open();
query = "SELECT autonGear FROM " + msclr::interop::marshal_as<String^>(teamObj.getNumber()) + " WHERE hasPlayed=true AND (autonGear='Middle' OR autonGear='Side')";
command = gcnew OleDbCommand(query, database);
resultsReader = command->ExecuteReader(System::Data::CommandBehavior::CloseConnection);
while (resultsReader->Read())
{
if(resultsReader["autonGear"]->ToString() == "Middle" || resultsReader["autonGear"]->ToString() == "Side")
autonGearScoredDouble++;
}
resultsReader->Close();
if (database->State == System::Data::ConnectionState::Closed)
database->Open();
query = "SELECT autonGear FROM " + msclr::interop::marshal_as<String^>(teamObj.getNumber()) + " WHERE hasPlayed=true AND (autonGear='Miss' OR autonGear='Middle' OR autonGear='Side')";
command = gcnew OleDbCommand(query, database);
resultsReader = command->ExecuteReader(System::Data::CommandBehavior::CloseConnection);
while (resultsReader->Read())
{
if (resultsReader["autonGear"]->ToString() == "Miss" || resultsReader["autonGear"]->ToString() == "Middle" || resultsReader["autonGear"]->ToString() == "Side")
autonGearAttemptedDouble++;
}
resultsReader->Close();
if (database->State == System::Data::ConnectionState::Closed)
database->Open();
// UPDATE totals SET avgAutonBalls = [autonBallsAvg], avgTeleopBalls = [teleopBallsAvg],
// avgTeleopGears=[teleopGearsAvg], perClimb=[climbPercentage], perAutonGearTotal=[autonGearTotalPercentage],
// perAutonGearAttempted=[autonGearAttemptedPercentage] WHERE teamNumber=[teamNumber]
query = "UPDATE totals SET avgAutonBalls=" + autonBalls/gamesPlayedDouble + ", avgTeleopBalls=" + teleopBalls/gamesPlayedDouble;
query += ", avgTeleopGears=" + teleopGears / gamesPlayedDouble + ", perClimb=" + climb / gamesPlayedDouble + ", perAutonGearTotal=" + autonGearScoredDouble / gamesPlayedDouble;
query += ", perAutonGearAttempted=";
if (autonGearAttemptedDouble == 0)
query += "0";
else
query += autonGearScoredDouble / autonGearAttemptedDouble;
query += " WHERE teamNumber=" + msclr::interop::marshal_as<String^>(teamObj.getNumber());
command = gcnew OleDbCommand(query, database);
command->ExecuteNonQuery();
}
catch (Exception^ ex)
{
*error = msclr::interop::marshal_as<std::string>(ex->ToString());
std::cout << error << std::endl;
}
if (database->State == System::Data::ConnectionState::Open)
database->Close();
}
static void closeMatch(std::string match)
{
try {
database = gcnew OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=../scout.mdb");
database->Open();
// SQL Query
// UPDATE [teamNumber] SET hasPlayed=true, autonGear='[Middle, Side, Miss, None, Unset]', autonBalls=[autonBallsValue],
// teleopGears=[teleopGearsValue], teleopBalls=[teleopBallsValue], climb=[true, false] WHERE match=[teamMatch]
query = "UPDATE matches SET hasPlayed=true WHERE matchNumber=" + msclr::interop::marshal_as<String^>(match);
command = gcnew OleDbCommand(query, database);
command->ExecuteNonQuery();
}
catch (Exception^ ex)
{
*error = msclr::interop::marshal_as<std::string>(ex->ToString());
std::cout << error << std::endl;
}
if (database->State == System::Data::ConnectionState::Open)
database->Close();
}
static void getStandingTeams(std::vector<Standing>& standings, std::string totalsColumns[])
{
try {
int j = 0;
for (int i = 0; i < 6; i++)
{
database = gcnew OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=../scout.mdb");
if(database->State == System::Data::ConnectionState::Closed)
database->Open();
query = "SELECT TOP 5 teamNumber, " + msclr::interop::marshal_as<String^>(totalsColumns[i]) + " FROM totals ORDER BY " + msclr::interop::marshal_as<String^>(totalsColumns[i]) + " DESC";
command = gcnew OleDbCommand(query, database);
resultsReader = command->ExecuteReader(System::Data::CommandBehavior::CloseConnection);
j = 0;
while (resultsReader->Read())
{
standings[i].setTeamName(msclr::interop::marshal_as<std::string>(resultsReader["teamNumber"]->ToString()), j);
standings[i].setTeamValue(msclr::interop::marshal_as<std::string>(resultsReader[msclr::interop::marshal_as<String^>(totalsColumns[i])]->ToString()), j);
j++;
}
resultsReader->Close();
}
}
catch (Exception^ ex)
{
*error = msclr::interop::marshal_as<std::string>(ex->ToString());
std::cout << error << std::endl;
}
if (database->State == System::Data::ConnectionState::Open)
database->Close();
}
};