-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbHandler.php
630 lines (597 loc) · 20.7 KB
/
dbHandler.php
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
<?php
/*
MySQL database handler
*/
class dbHandler
{
private $dbIniFile = "../../db_config.ini";
private $charset = "utf8";
private $conn = null;
private $alreadyConnected = false;
private $configKeys = array(
"server", "db", "username", "password", "eventcode", "tbakey",
"fbapikey", "fbauthdomain", "fbdburl", "fbprojectid", "fbstoragebucket",
"fbsenderid", "fbappid", "fbmeasurementid",
"datatable", "tbatable", "pittable", "ranktable", "driveranktable",
"useP", "useQm", "useQf", "useSf", "useF"
);
function connectToDB()
{
if (!$this->alreadyConnected)
{
$dbConfig = $this->readDbConfig();
$dsn = "mysql:host=" . $dbConfig["server"] . ";dbname=" . $dbConfig["db"] . ";charset=" . $this->charset;
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false
];
$this->conn = new PDO($dsn, $dbConfig["username"], $dbConfig["password"], $opt);
$this->alreadyConnected = true;
}
return ($this->conn);
}
function connectToServer()
{
$dbConfig = $this->readDbConfig();
$dsn = "mysql:host=" . $dbConfig["server"] . ";charset=" . $this->charset;
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false
];
$this->alreadyConnected = true;
return (new PDO($dsn, $dbConfig["username"], $dbConfig["password"], $opt));
}
function writeRowToTable($data)
{
$dbConfig = $this->readDbConfig();
$sql = "INSERT INTO " . $dbConfig["datatable"] . "(entrykey,
teamnumber,
autonleave,
autonampnotes,
autonspeakernotes,
teleopampnotes,
teleopspeakernotes,
endgamestage,
endgameharmony,
endgamespotlit,
endgametrap,
died,
matchnumber,
eventcode,
scoutname,
comment)
VALUES(:entrykey,
:teamnumber,
:autonleave,
:autonampnotes,
:autonspeakernotes,
:teleopampnotes,
:teleopspeakernotes,
:endgamestage,
:endgameharmony,
:endgamespotlit,
:endgametrap,
:died,
:matchnumber,
:eventcode,
:scoutname,
:comment)";
$prepared_statement = $this->conn->prepare($sql);
$prepared_statement->execute($data);
}
function enforceInt($val)
{
return intval($val);
}
function enforceDataTyping($data)
{
$out = array();
foreach ($data as $row)
{
foreach ($row as $key => $value)
{
if ($key == "autonleave" || $key == "autonampnotes" || $key == "autonspeakernotes" || $key == "teleopampnotes" || $key == "teleopspeakernotes" || $key == "endgamestage" || $key == "endgameharmony" || $key == "endgamespotlit" ||$key == "endgametrap" || $key == "died")
{
$row[$key] = $this->enforceInt($value);
}
else
{
$row[$key] = $value;
}
}
array_push($out, $row);
}
return $out;
}
function readAllData($eventCode)
{
$dbConfig = $this->readDbConfig();
$sql = "SELECT teamnumber,
autonleave,
autonampnotes,
autonspeakernotes,
teleopampnotes,
teleopspeakernotes,
endgamestage,
endgameharmony,
endgamespotlit,
endgametrap,
died,
matchnumber,
eventcode,
scoutname,
comment from " . $dbConfig["datatable"] . " where eventcode='" . $eventCode . "'";
$prepared_statement = $this->conn->prepare($sql);
$prepared_statement->execute();
$result = $prepared_statement->fetchAll();
return $this->enforceDataTyping($result);
}
function readTeamData($teamNumber, $eventCode)
{
$dbConfig = $this->readDbConfig();
$sql = "SELECT teamnumber,
autonleave,
autonampnotes,
autonspeakernotes,
teleopampnotes,
teleopspeakernotes,
endgamestage,
endgameharmony,
endgamespotlit,
endgametrap,
died,
matchnumber,
eventcode,
scoutname,
comment from " . $dbConfig["datatable"] . " where
eventcode='" . $eventCode . "' AND
teamnumber='" . $teamNumber . "'";
$prepared_statement = $this->conn->prepare($sql);
$prepared_statement->execute();
$result = $prepared_statement->fetchAll();
return $this->enforceDataTyping($result);
}
function writeRowToPitTable($data)
{
$dbConfig = $this->readDbConfig();
$sql = "INSERT INTO " . $dbConfig["pittable"] . "(entrykey,
eventcode,
teamnumber,
numbatteries,
pitorg,
spareparts,
computervision,
swerve,
proglanguage,
drivemotors,
preparedness)
VALUES(:entrykey,
:eventcode,
:teamnumber,
:numbatteries,
:pitorg,
:spareparts,
:computervision,
:swerve,
:proglanguage,
:drivemotors,
:preparedness)";
$prepared_statement = $this->conn->prepare($sql);
$prepared_statement->execute($data);
}
function readPitData($eventCode)
{
$dbConfig = $this->readDbConfig();
$sql = "SELECT teamnumber,
numbatteries,
pitorg,
spareparts,
computervision,
swerve,
proglanguage,
drivemotors,
preparedness from " . $dbConfig["pittable"] . " where
eventcode='" . $eventCode . "'";
$prepared_statement = $this->conn->prepare($sql);
$prepared_statement->execute();
$result = $prepared_statement->fetchAll();
return $result;
}
function writeRowToDriveRankTable($data)
{
$dbConfig = $this->readDbConfig();
$sql = "INSERT INTO " . $dbConfig["driveranktable"] . "(entrykey,
eventcode,
teamnumber,
matchnumber,
scoutname,
driverability,
quickness,
fieldawareness,
gamepiecedrop)
VALUES(:entrykey,
:eventcode,
:teamnumber,
:matchnumber,
:scoutname,
:driverability,
:quickness,
:fieldawareness,
:gamepiecedrop)";
$prepared_statement = $this->conn->prepare($sql);
$prepared_statement->execute($data);
}
function readAllDriveRankData($eventCode)
{
$dbConfig = $this->readDbConfig();
$sql = "SELECT teamnumber,
matchnumber,
scoutname,
driverability,
quickness,
fieldawareness,
gamepiecedrop from " . $dbConfig["driveranktable"] . " where eventcode='" . $eventCode . "'";
$prepared_statement = $this->conn->prepare($sql);
$prepared_statement->execute();
$result = $prepared_statement->fetchAll();
return $result;
}
function readTeamDriveRankData($teamNumber,$eventCode)
{
$dbConfig = $this->readDbConfig();
$sql = "SELECT teamnumber,
matchnumber,
scoutname,
driverability,
quickness,
fieldawareness,
gamepiecedrop from " . $dbConfig["driveranktable"] . " where
eventcode='" . $eventCode . "' AND
teamnumber='" . $teamNumber . "'";
$prepared_statement = $this->conn->prepare($sql);
$prepared_statement->execute();
$result = $prepared_statement->fetchAll();
return $result;
}
function createDB()
{
$dbConfig = $this->readDbConfig();
$connection = $this->connectToServer();
$statement = $connection->prepare('CREATE DATABASE IF NOT EXISTS ' . $dbConfig["db"]);
if (!$statement->execute())
{
throw new Exception("createDB Error: CREATE DATABASE query failed.");
}
}
function createDataTable()
{
$conn = $this->connectToDB();
$dbConfig = $this->readDbConfig();
$query = "CREATE TABLE " . $dbConfig["db"] . "." . $dbConfig["datatable"] . " (
entrykey VARCHAR(60) NOT NULL PRIMARY KEY,
teamnumber VARCHAR(6) NOT NULL,
autonleave TINYINT UNSIGNED NOT NULL,
autonampnotes TINYINT UNSIGNED NOT NULL,
autonspeakernotes TINYINT UNSIGNED NOT NULL,
teleopampnotes TINYINT UNSIGNED NOT NULL,
teleopspeakernotes TINYINT UNSIGNED NOT NULL,
endgamestage TINYINT UNSIGNED NOT NULL,
endgameharmony TINYINT UNSIGNED NOT NULL,
endgamespotlit TINYINT UNSIGNED NOT NULL,
endgametrap TINYINT UNSIGNED NOT NULL,
died TINYINT UNSIGNED NOT NULL,
matchnumber VARCHAR(10) NOT NULL,
eventcode VARCHAR(10) NOT NULL,
scoutname VARCHAR(100) NOT NULL,
comment VARCHAR(500) NOT NULL
)";
$statement = $conn->prepare($query);
if (!$statement->execute())
{
throw new Exception("createTable Error: CREATE TABLE " . $dbConfig["datatable"] . " query failed.");
}
}
function createTBATable()
{
$conn = $this->connectToDB();
$dbConfig = $this->readDbConfig();
$query = "CREATE TABLE " . $dbConfig["db"] . "." . $dbConfig["tbatable"] . " (
requestURI VARCHAR(100) NOT NULL PRIMARY KEY,
expiryTime BIGINT NOT NULL,
response MEDIUMTEXT NOT NULL
)";
$statement = $conn->prepare($query);
if (!$statement->execute())
{
throw new Exception("createTBATable Error: CREATE TABLE " . $dbConfig["tbatable"] . " query failed.");
}
}
function createPitTable()
{
$conn = $this->connectToDB();
$dbConfig = $this->readDbConfig();
$query = "CREATE TABLE " . $dbConfig["db"] . "." . $dbConfig["pittable"] . " (
entrykey VARCHAR(60) NOT NULL PRIMARY KEY,
eventcode VARCHAR(10) NOT NULL,
teamnumber VARCHAR(6) NOT NULL,
numbatteries VARCHAR(8) NOT NULL,
pitorg TINYINT UNSIGNED NOT NULL,
spareparts TINYINT UNSIGNED NOT NULL,
computervision TINYINT UNSIGNED NOT NULL,
swerve TINYINT UNSIGNED NOT NULL,
proglanguage VARCHAR(20) NOT NULL,
drivemotors VARCHAR(20) NOT NULL,
preparedness TINYINT UNSIGNED NOT NULL
)";
$statement = $conn->prepare($query);
if (!$statement->execute())
{
throw new Exception("createPitTable Error: CREATE TABLE " . $dbConfig["pittable"] . " query failed.");
}
}
function createDriveRankTable()
{
$conn = $this->connectToDB();
$dbConfig = $this->readDbConfig();
$query = "CREATE TABLE " . $dbConfig["db"] . "." . $dbConfig["driveranktable"] . " (
entrykey VARCHAR(60) NOT NULL PRIMARY KEY,
eventcode VARCHAR(10) NOT NULL,
teamnumber VARCHAR(6) NOT NULL,
matchnumber VARCHAR(6) NOT NULL,
scoutname VARCHAR(15) NOT NULL,
driverability TINYINT UNSIGNED NOT NULL,
quickness TINYINT UNSIGNED NOT NULL,
fieldawareness TINYINT UNSIGNED NOT NULL,
gamepiecedrop TINYINT UNSIGNED NOT NULL
)";
$statement = $conn->prepare($query);
if (!$statement->execute())
{
throw new Exception("createdriveRankTable Error: CREATE TABLE " . $dbConfig["driveranktable"] . " query failed.");
}
}
function createRankTable()
{
$conn = $this->connectToDB();
$dbConfig = $this->readDbConfig();
$query = "CREATE TABLE " . $dbConfig["db"] . "." . $dbConfig["ranktable"] . " (
eventcode VARCHAR(10) NOT NULL,
matchkey VARCHAR(60) NOT NULL,
teamrank MEDIUMTEXT NOT NULL
)";
$statement = $conn->prepare($query);
if (!$statement->execute())
{
throw new Exception("createRankingTable Error: CREATE TABLE " . $dbConfig["ranktable"] . " query failed.");
}
}
function writeRowToRankTable($data)
{
$dbConfig = $this->readDbConfig();
$sql = "INSERT INTO " . $dbConfig["ranktable"] . "(eventcode, matchkey, teamrank)
VALUES(:eventcode, :matchkey, :teamrank)";
$prepared_statement = $this->conn->prepare($sql);
$prepared_statement->execute($data);
}
function readRawRankData($eventCode)
{
$dbConfig = $this->readDbConfig();
$sql = "SELECT matchkey,
teamrank from " . $dbConfig["ranktable"] . " where
eventcode='" . $eventCode . "'";
$prepared_statement = $this->conn->prepare($sql);
$prepared_statement->execute();
$result = $prepared_statement->fetchAll();
return $result;
}
function readRankData($eventCode)
{
$rawRankData = $this->readRawRankData($eventCode);
$rankData = array();
$dataSize = sizeof($rawRankData);
for ($i = 0; $i < $dataSize; $i++)
{
array_push($rankData, json_decode($rawRankData[$i]["teamrank"], True));
}
return $rankData;
}
function deleteRowFromRankTable($data)
{
$dbConfig = $this->readDbConfig();
$sql = "DELETE FROM " . $dbConfig["ranktable"] . " WHERE eventcode = :eventcode AND matchkey = :matchkey AND teamrank = :teamrank LIMIT 1";
$prepared_statement = $this->conn->prepare($sql);
$prepared_statement->execute($data);
}
function readDbConfig()
{
// Read dbIniFile
// If File doesn't exist, instantiate array as empty
try
{
$ini_arr = parse_ini_file($this->dbIniFile);
}
catch (Exception $e)
{
$ini_arr = array();
}
// If required keys don't exist, instantiate them to default empty string
foreach ($this->configKeys as $key)
{
if (!isset($ini_arr[$key]))
{
$ini_arr[$key] = "";
}
# Specific checking
if ($key == "useP" || $key == "useQm" || $key == "useQf" || $key == "useSf" || $key == "useF")
{
if ($ini_arr[$key] == "")
{
$ini_arr[$key] = true;
}
else if ($ini_arr[$key] == "1" || $ini_arr[$key] == "true")
{
$ini_arr[$key] = true;
}
else
{
$ini_arr[$key] = false;
}
}
}
return $ini_arr;
}
function writeDbConfig($dat)
{
// Get values to write
// If value is not in input, read from current DB config
$currDBConfig = $this->readDbConfig();
foreach ($dat as $key => $value)
{
$currDBConfig[$key] = $value;
}
// Build ini file string
$data = "";
foreach ($currDBConfig as $key => $value)
{
$data = $data . $key . "=" . $value . "\r\n";
}
// Write ini file string to actual file
if ($fp = fopen($this->dbIniFile, 'w'))
{
$startTime = microtime(True);
do
{
$writeLock = flock($fp, LOCK_EX);
if (!$writeLock)
{
usleep(round(21350));
}
} while ((!$writeLock) and ((microtime(True) - $startTime) < 5));
if ($writeLock)
{
fwrite($fp, $data);
flock($fp, LOCK_UN);
}
}
fclose($fp);
}
function getStatus()
{
$dbConfig = $this->readDbConfig();
$out = array();
//
$out["server"] = $dbConfig["server"];
$out["db"] = $dbConfig["db"];
$out["tbakey"] = substr($dbConfig["tbakey"], 0, 3) . "******";
$out["eventcode"] = $dbConfig["eventcode"];
$out["username"] = substr($dbConfig["username"], 0, 1) . "*****";
$out["fbapikey"] = substr($dbConfig["fbapikey"], 0, 1) . "*****";
$out["fbauthdomain"] = $dbConfig["fbauthdomain"];
$out["fbdburl"] = substr($dbConfig["fbdburl"], 0, 1) . "*****";
$out["fbprojectid"] = substr($dbConfig["fbprojectid"], 0, 1) . "*****";
$out["fbstoragebucket"] = substr($dbConfig["fbstoragebucket"], 0, 1) . "*****";
$out["fbsenderid"] = substr($dbConfig["fbsenderid"], 0, 1) . "*****";
$out["fbappid"] = substr($dbConfig["fbappid"], 0, 1) . "*****";
$out["fbmeasurementid"] = substr($dbConfig["fbmeasurementid"], 0, 1) . "*****";
$out["dbExists"] = false;
$out["serverExists"] = false;
$out["dataTableExists"] = false;
$out["tbaTableExists"] = false;
$out["pitTableExists"] = false;
$out["driveRankTableExists"] = false;
$out["rankTableExists"] = false;
$out["useP"] = $dbConfig["useP"];
$out["useQm"] = $dbConfig["useQm"];
$out["useQf"] = $dbConfig["useQf"];
$out["useSf"] = $dbConfig["useSf"];
$out["useF"] = $dbConfig["useF"];
//DB Connection
try
{
$dsn = "mysql:host=" . $dbConfig["server"] . ";dbname=" . $dbConfig["db"] . ";charset=" . $this->charset;
$conn = new PDO($dsn, $dbConfig["username"], $dbConfig["password"]);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$out["dbExists"] = true;
}
catch (PDOException $e)
{
$out["DBExists"] = false;
}
//Server Connection
try
{
$dsn = "mysql:host=" . $dbConfig["server"] . ";charset=" . $this->charset;
$conn = new PDO($dsn, $dbConfig["username"], $dbConfig["password"]);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$out["serverExists"] = true;
}
catch (PDOException $e)
{
$out["ServerExists"] = false;
}
// Table Connection
try
{
$dsn = "mysql:host=" . $dbConfig["server"] . ";dbname=" . $dbConfig["db"] . ";charset=" . $this->charset;
$conn = new PDO($dsn, $dbConfig["username"], $dbConfig["password"]);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$val = $conn->query('SELECT * from ' . $dbConfig["datatable"]);
$out["dataTableExists"] = true;
}
catch (PDOException $e)
{
$out["dataTableExists"] = false;
}
try
{
$dsn = "mysql:host=" . $dbConfig["server"] . ";dbname=" . $dbConfig["db"] . ";charset=" . $this->charset;
$conn = new PDO($dsn, $dbConfig["username"], $dbConfig["password"]);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$val = $conn->query('SELECT * from ' . $dbConfig["tbatable"]);
$out["tbaTableExists"] = true;
}
catch (PDOException $e)
{
$out["tbaTableExists"] = false;
}
try
{
$dsn = "mysql:host=" . $dbConfig["server"] . ";dbname=" . $dbConfig["db"] . ";charset=" . $this->charset;
$conn = new PDO($dsn, $dbConfig["username"], $dbConfig["password"]);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$val = $conn->query('SELECT * from ' . $dbConfig["pittable"]);
$out["pitTableExists"] = true;
}
catch (PDOException $e)
{
$out["pitTableExists"] = false;
}
try
{
$dsn = "mysql:host=" . $dbConfig["server"] . ";dbname=" . $dbConfig["db"] . ";charset=" . $this->charset;
$conn = new PDO($dsn, $dbConfig["username"], $dbConfig["password"]);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$val = $conn->query('SELECT * from ' . $dbConfig["driveranktable"]);
$out["driveRankTableExists"] = true;
}
catch (PDOException $e)
{
$out["driveRankTableExists"] = false;
}
try
{
$dsn = "mysql:host=" . $dbConfig["server"] . ";dbname=" . $dbConfig["db"] . ";charset=" . $this->charset;
$conn = new PDO($dsn, $dbConfig["username"], $dbConfig["password"]);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$val = $conn->query('SELECT * from ' . $dbConfig["ranktable"]);
$out["rankTableExists"] = true;
}
catch (PDOException $e)
{
$out["rankTableExists"] = false;
}
return $out;
}
}